From 48811bb91523f792e6b1345a4429a854fe67ed57 Mon Sep 17 00:00:00 2001 From: Fangyu Gai Date: Fri, 13 Dec 2024 18:58:48 +0800 Subject: [PATCH 1/3] add keys cmd --- covenant-signer/cmd/helper.go | 42 +++++++++++++++++++++++++++++++++++ covenant-signer/cmd/keys.go | 9 ++++++++ covenant-signer/cmd/root.go | 20 ++++++++++++----- 3 files changed, 66 insertions(+), 5 deletions(-) create mode 100644 covenant-signer/cmd/helper.go create mode 100644 covenant-signer/cmd/keys.go diff --git a/covenant-signer/cmd/helper.go b/covenant-signer/cmd/helper.go new file mode 100644 index 0000000..b185464 --- /dev/null +++ b/covenant-signer/cmd/helper.go @@ -0,0 +1,42 @@ +package cmd + +import ( + "os" + + "github.com/babylonlabs-io/babylon/app/params" + bstypes "github.com/babylonlabs-io/babylon/x/btcstaking/types" + "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/std" + "github.com/spf13/cobra" +) + +// PersistClientCtx persist some vars from the cmd or config to the client context. +// It gives preferences to flags over the values in the config. If the flag is not set +// and exists a value in the config that could be used, it will be set in the ctx. +func PersistClientCtx(ctx client.Context) func(cmd *cobra.Command, _ []string) error { + return func(cmd *cobra.Command, _ []string) error { + encCfg := params.DefaultEncodingConfig() + std.RegisterInterfaces(encCfg.InterfaceRegistry) + bstypes.RegisterInterfaces(encCfg.InterfaceRegistry) + + ctx = ctx. + WithCodec(encCfg.Codec). + WithInterfaceRegistry(encCfg.InterfaceRegistry). + WithTxConfig(encCfg.TxConfig). + WithLegacyAmino(encCfg.Amino). + WithInput(os.Stdin) + + // set the default command outputs + cmd.SetOut(cmd.OutOrStdout()) + cmd.SetErr(cmd.ErrOrStderr()) + + if err := client.SetCmdClientContextHandler(ctx, cmd); err != nil { + return err + } + + ctx = client.GetClientContextFromCmd(cmd) + + // updates the ctx in the cmd in case something was modified bt the config + return client.SetCmdClientContext(cmd, ctx) + } +} diff --git a/covenant-signer/cmd/keys.go b/covenant-signer/cmd/keys.go new file mode 100644 index 0000000..c341259 --- /dev/null +++ b/covenant-signer/cmd/keys.go @@ -0,0 +1,9 @@ +package cmd + +import ( + "github.com/cosmos/cosmos-sdk/client/keys" +) + +func init() { + rootCmd.AddCommand(keys.Commands()) +} diff --git a/covenant-signer/cmd/root.go b/covenant-signer/cmd/root.go index 3762f64..ed24b0c 100644 --- a/covenant-signer/cmd/root.go +++ b/covenant-signer/cmd/root.go @@ -4,6 +4,7 @@ import ( "path/filepath" "github.com/btcsuite/btcd/btcutil" + "github.com/cosmos/cosmos-sdk/client" "github.com/spf13/cobra" ) @@ -12,18 +13,27 @@ var ( configPath string configPathKey = "config" - rootCmd = &cobra.Command{ - Use: "covenant-signer", - Short: "remote signing serivce to perform covenant duties", - } - // C:\Users\\AppData\Local\signer on Windows // ~/.signer on Linux // ~/Library/Application Support/signer on MacOS dafaultConfigDir = btcutil.AppDataDir("signer", false) dafaultConfigPath = filepath.Join(dafaultConfigDir, "config.toml") + + rootCmd = NewRootCmd() ) +// NewRootCmd creates a new root command for fpd. It is called once in the main function. +func NewRootCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "covenant-signer", + Short: "remote signing service to perform covenant duties", + SilenceErrors: false, + PersistentPreRunE: PersistClientCtx(client.Context{}), + } + + return cmd +} + // Execute executes the root command. func Execute() error { return rootCmd.Execute() From 1191343b56f9b2a008d80719e0a3a97e428a1f1b Mon Sep 17 00:00:00 2001 From: Fangyu Gai Date: Fri, 13 Dec 2024 19:00:14 +0800 Subject: [PATCH 2/3] changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1a60ab0..06e48bd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -41,6 +41,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) * [#63](https://github.com/babylonlabs-io/covenant-emulator/pull/63) Add babylon address to keys command output and fix casing in dockerfile +* [#67](https://github.com/babylonlabs-io/covenant-emulator/pull/67) Add keys cmd to covenant-signer ## v0.10.0 From 98321ab16b57c5ef0cd2cff500790a96e2458ad5 Mon Sep 17 00:00:00 2001 From: Fangyu Gai Date: Fri, 13 Dec 2024 19:21:09 +0800 Subject: [PATCH 3/3] minor --- covenant-signer/cmd/helper.go | 3 --- covenant-signer/cmd/root.go | 8 +++++--- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/covenant-signer/cmd/helper.go b/covenant-signer/cmd/helper.go index b185464..7ed1ace 100644 --- a/covenant-signer/cmd/helper.go +++ b/covenant-signer/cmd/helper.go @@ -10,9 +10,6 @@ import ( "github.com/spf13/cobra" ) -// PersistClientCtx persist some vars from the cmd or config to the client context. -// It gives preferences to flags over the values in the config. If the flag is not set -// and exists a value in the config that could be used, it will be set in the ctx. func PersistClientCtx(ctx client.Context) func(cmd *cobra.Command, _ []string) error { return func(cmd *cobra.Command, _ []string) error { encCfg := params.DefaultEncodingConfig() diff --git a/covenant-signer/cmd/root.go b/covenant-signer/cmd/root.go index ed24b0c..468018d 100644 --- a/covenant-signer/cmd/root.go +++ b/covenant-signer/cmd/root.go @@ -5,6 +5,7 @@ import ( "github.com/btcsuite/btcd/btcutil" "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/client/flags" "github.com/spf13/cobra" ) @@ -16,8 +17,8 @@ var ( // C:\Users\\AppData\Local\signer on Windows // ~/.signer on Linux // ~/Library/Application Support/signer on MacOS - dafaultConfigDir = btcutil.AppDataDir("signer", false) - dafaultConfigPath = filepath.Join(dafaultConfigDir, "config.toml") + defaultConfigDir = btcutil.AppDataDir("signer", false) + defaultConfigPath = filepath.Join(defaultConfigDir, "config.toml") rootCmd = NewRootCmd() ) @@ -31,6 +32,7 @@ func NewRootCmd() *cobra.Command { PersistentPreRunE: PersistClientCtx(client.Context{}), } + cmd.PersistentFlags().String(flags.FlagHome, defaultConfigDir, "The application home directory") return cmd } @@ -43,7 +45,7 @@ func init() { rootCmd.PersistentFlags().StringVar( &configPath, configPathKey, - dafaultConfigPath, + defaultConfigPath, "path to the configuration file", ) }