From a51c5b17bc407f6a4d20692fd926e2c5dbd0d6cf Mon Sep 17 00:00:00 2001 From: RafilxTenfen Date: Mon, 16 Dec 2024 12:31:41 -0300 Subject: [PATCH 1/3] feat: add save key on mapping after import of keys --- eotsmanager/cmd/eotsd/daemon/keys.go | 51 ++++++++++++++++++++++------ 1 file changed, 41 insertions(+), 10 deletions(-) diff --git a/eotsmanager/cmd/eotsd/daemon/keys.go b/eotsmanager/cmd/eotsd/daemon/keys.go index b7248a46..b68c8831 100644 --- a/eotsmanager/cmd/eotsd/daemon/keys.go +++ b/eotsmanager/cmd/eotsd/daemon/keys.go @@ -50,53 +50,84 @@ func NewKeysCmd() *cobra.Command { } cmd.SetOut(oldOut) - return saveKeyNameMapping(cmd, args) + keyName := args[0] + eotsPk, err := saveKeyNameMapping(cmd, keyName) + if err != nil { + return err + } + + return printFromKey(cmd, keyName, eotsPk) } + saveKeyOnPostRun(keysCmd, "import") + saveKeyOnPostRun(keysCmd, "import-hex") + return keysCmd } -func saveKeyNameMapping(cmd *cobra.Command, args []string) error { +func saveKeyOnPostRun(cmd *cobra.Command, commandName string) { + subCmd := util.GetSubCommand(cmd, commandName) + if subCmd == nil { + panic(fmt.Sprintf("failed to find keys %s command", commandName)) + } + + subCmd.PostRunE = func(cmd *cobra.Command, args []string) error { + keyName := args[0] + _, err := saveKeyNameMapping(cmd, keyName) + return err + } +} + +func saveKeyNameMapping(cmd *cobra.Command, keyName string) (*types.BIP340PubKey, error) { clientCtx, err := client.GetClientQueryContext(cmd) if err != nil { - return err + return nil, err } - keyName := args[0] // Load configuration cfg, err := config.LoadConfig(clientCtx.HomeDir) if err != nil { - return fmt.Errorf("failed to load config: %w", err) + return nil, fmt.Errorf("failed to load config: %w", err) } // Setup logger logger, err := log.NewRootLoggerWithFile(config.LogFile(clientCtx.HomeDir), cfg.LogLevel) if err != nil { - return fmt.Errorf("failed to load the logger: %w", err) + return nil, fmt.Errorf("failed to load the logger: %w", err) } // Get database backend dbBackend, err := cfg.DatabaseConfig.GetDBBackend() if err != nil { - return fmt.Errorf("failed to create db backend: %w", err) + return nil, fmt.Errorf("failed to create db backend: %w", err) } defer dbBackend.Close() // Create EOTS manager eotsManager, err := eotsmanager.NewLocalEOTSManager(clientCtx.HomeDir, clientCtx.Keyring.Backend(), dbBackend, logger) if err != nil { - return fmt.Errorf("failed to create EOTS manager: %w", err) + return nil, fmt.Errorf("failed to create EOTS manager: %w", err) } // Get the public key for the newly added key eotsPk, err := eotsManager.LoadBIP340PubKeyFromKeyName(keyName) if err != nil { - return fmt.Errorf("failed to get public key for key %s: %w", keyName, err) + return nil, fmt.Errorf("failed to get public key for key %s: %w", keyName, err) } // Save the public key to key name mapping if err := eotsManager.SaveEOTSKeyName(eotsPk.MustToBTCPK(), keyName); err != nil { - return fmt.Errorf("failed to save key name mapping: %w", err) + return nil, fmt.Errorf("failed to save key name mapping: %w", err) + } + + cmd.Printf("Sucessfully wrote key name %s to mapping", keyName) + return eotsPk, nil +} + +func printFromKey(cmd *cobra.Command, keyName string, eotsPk *types.BIP340PubKey) error { + clientCtx, err := client.GetClientQueryContext(cmd) + if err != nil { + return err } k, err := clientCtx.Keyring.Key(keyName) From 92e17c577c95c17dcb179e2c5ee9c635fc2c1e79 Mon Sep 17 00:00:00 2001 From: RafilxTenfen Date: Mon, 16 Dec 2024 12:34:01 -0300 Subject: [PATCH 2/3] chore: add #228 to changelog --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1080566d..fca2e8bb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -46,6 +46,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) * [#216](https://github.com/babylonlabs-io/finality-provider/pull/216) Add multiple fpd connecting to one eotsd in e2e tests * [#218](https://github.com/babylonlabs-io/finality-provider/pull/218) Prune used merkle proof * [#221](https://github.com/babylonlabs-io/finality-provider/pull/221) Cleanup TODOs +* [#228](https://github.com/babylonlabs-io/finality-provider/pull/228) Save key name mapping in eotsd import commands ## v0.13.1 @@ -118,7 +119,7 @@ finality vote submission * [#117](https://github.com/babylonlabs-io/finality-provider/pull/117) Spec of commit public randomness -* [#130](https://github.com/babylonlabs-io/finality-provider/pull/130) Finality +* [#130](https://github.com/babylonlabs-io/finality-provider/pull/130) Finality Provider operation documentation ### Bug Fixes From 7dbb509fa5501f2486c5df4d3ca2a7a65ed52b6a Mon Sep 17 00:00:00 2001 From: RafilxTenfen Date: Mon, 16 Dec 2024 12:34:43 -0300 Subject: [PATCH 3/3] fix: lint misspell --- eotsmanager/cmd/eotsd/daemon/keys.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eotsmanager/cmd/eotsd/daemon/keys.go b/eotsmanager/cmd/eotsd/daemon/keys.go index b68c8831..ceead6f6 100644 --- a/eotsmanager/cmd/eotsd/daemon/keys.go +++ b/eotsmanager/cmd/eotsd/daemon/keys.go @@ -120,7 +120,7 @@ func saveKeyNameMapping(cmd *cobra.Command, keyName string) (*types.BIP340PubKey return nil, fmt.Errorf("failed to save key name mapping: %w", err) } - cmd.Printf("Sucessfully wrote key name %s to mapping", keyName) + cmd.Printf("Successfully wrote key name %s to mapping", keyName) return eotsPk, nil }