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 diff --git a/covenant-signer/cmd/helper.go b/covenant-signer/cmd/helper.go new file mode 100644 index 0000000..7ed1ace --- /dev/null +++ b/covenant-signer/cmd/helper.go @@ -0,0 +1,39 @@ +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" +) + +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..468018d 100644 --- a/covenant-signer/cmd/root.go +++ b/covenant-signer/cmd/root.go @@ -4,6 +4,8 @@ import ( "path/filepath" "github.com/btcsuite/btcd/btcutil" + "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/client/flags" "github.com/spf13/cobra" ) @@ -12,18 +14,28 @@ 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") + defaultConfigDir = btcutil.AppDataDir("signer", false) + defaultConfigPath = filepath.Join(defaultConfigDir, "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{}), + } + + cmd.PersistentFlags().String(flags.FlagHome, defaultConfigDir, "The application home directory") + return cmd +} + // Execute executes the root command. func Execute() error { return rootCmd.Execute() @@ -33,7 +45,7 @@ func init() { rootCmd.PersistentFlags().StringVar( &configPath, configPathKey, - dafaultConfigPath, + defaultConfigPath, "path to the configuration file", ) }