From f3733aaecb83531da81531ce2f29e50c8cced97d Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Thu, 8 Oct 2020 13:41:35 -0400 Subject: [PATCH] Merge PR #7485: keyring-dir flag --- CHANGELOG.md | 2 ++ client/cmd.go | 12 +++++++ client/cmd_test.go | 2 +- client/context.go | 11 +++++-- client/flags/flags.go | 2 ++ client/keys/add.go | 10 +++--- client/keys/add_ledger_test.go | 15 ++++++--- client/keys/add_test.go | 32 +++++++++++-------- client/keys/delete.go | 15 +++------ client/keys/delete_test.go | 8 ++++- client/keys/export.go | 14 ++------ client/keys/export_test.go | 7 +++- client/keys/import.go | 14 ++------ client/keys/import_test.go | 8 +++-- ...2001f91b9aaf7758fb727bea3932bb236e.address | 1 + ...e0336e82b269d1c26e379b11b54474f57a.address | 1 + ...8e891fd1ef46a28d67c9be33ffecd8a6f8.address | 1 + ...a63587e58ce2487836d90bda016c6fff7c.address | 1 + ...0b8932e443b60fd6273cc3026aa25116c8.address | 1 + ...de78d0e55e7650f11dcfdbb6bd131038d0.address | 1 + client/keys/keyring-test/keyname1.info | 1 + client/keys/keyring-test/keyname2.info | 1 + client/keys/keyring-test/keyname4.info | 1 + client/keys/keyring-test/keyname5.info | 1 + client/keys/keyring-test/keyname6.info | 1 + client/keys/keyring-test/keyname7.info | 1 + client/keys/list.go | 13 ++------ client/keys/list_test.go | 9 ++++-- client/keys/migrate_test.go | 10 ++++-- client/keys/root.go | 1 + client/keys/show.go | 14 +++----- client/keys/show_test.go | 32 +++++++++++-------- 32 files changed, 143 insertions(+), 100 deletions(-) create mode 100644 client/keys/keyring-test/00ddf62001f91b9aaf7758fb727bea3932bb236e.address create mode 100644 client/keys/keyring-test/252b5fe0336e82b269d1c26e379b11b54474f57a.address create mode 100644 client/keys/keyring-test/43ca6f8e891fd1ef46a28d67c9be33ffecd8a6f8.address create mode 100644 client/keys/keyring-test/454b29a63587e58ce2487836d90bda016c6fff7c.address create mode 100644 client/keys/keyring-test/900b3a0b8932e443b60fd6273cc3026aa25116c8.address create mode 100644 client/keys/keyring-test/9a85abde78d0e55e7650f11dcfdbb6bd131038d0.address create mode 100644 client/keys/keyring-test/keyname1.info create mode 100644 client/keys/keyring-test/keyname2.info create mode 100644 client/keys/keyring-test/keyname4.info create mode 100644 client/keys/keyring-test/keyname5.info create mode 100644 client/keys/keyring-test/keyname6.info create mode 100644 client/keys/keyring-test/keyname7.info diff --git a/CHANGELOG.md b/CHANGELOG.md index abc6f483130..094ffc042e8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -38,6 +38,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ## [Unreleased] ### Client Breaking + * (modules) [\#7243](https://github.com/cosmos/cosmos-sdk/pull/7243) Rename `RegisterCodec` to `RegisterLegacyAminoCodec` and `codec.New()` is now renamed to `codec.NewLegacyAmino()` * (cli) [\#6651](https://github.com/cosmos/cosmos-sdk/pull/6651) The `gentx` command has been improved. No longer are `--from` and `--name` flags required. Instead, a single argument, `name`, is required which refers to the key pair in the Keyring. In addition, an optional `--moniker` flag can be provided to override the moniker found in `config.toml`. @@ -163,6 +164,7 @@ be used to retrieve the actual proposal `Content`. Also the `NewMsgSubmitProposa ### Features +* [\#7485](https://github.com/cosmos/cosmos-sdk/pull/7485) Introduce a new optional `--keyring-dir` flag that allows clients to specify a Keyring directory if it does not reside in the directory specified by `--home`. * [\#6755](https://github.com/cosmos/cosmos-sdk/pull/6755) Add custom regex validation for `Coin` denom by overwriting `CoinDenomRegex` when using `/types/coin.go`. * [\#7265](https://github.com/cosmos/cosmos-sdk/pull/7265) Support Tendermint block pruning through a new `min-retain-blocks` configuration that can be set in either `app.toml` or via the CLI. This parameter is used in conjunction with other criteria to determine the height at which Tendermint should prune blocks. * (vesting) [\#7209](https://github.com/cosmos/cosmos-sdk/pull/7209) Create new `MsgCreateVestingAccount` message type along with CLI handler that allows for the creation of delayed and continuous vesting types. diff --git a/client/cmd.go b/client/cmd.go index 1502cfab33a..712a7927405 100644 --- a/client/cmd.go +++ b/client/cmd.go @@ -97,6 +97,18 @@ func ReadPersistentCommandFlags(clientCtx Context, flagSet *pflag.FlagSet) (Cont clientCtx = clientCtx.WithHomeDir(homeDir) } + if clientCtx.KeyringDir == "" || flagSet.Changed(flags.FlagKeyringDir) { + keyringDir, _ := flagSet.GetString(flags.FlagKeyringDir) + + // The keyring directory is optional and falls back to the home directory + // if omitted. + if keyringDir == "" { + keyringDir = clientCtx.HomeDir + } + + clientCtx = clientCtx.WithKeyringDir(keyringDir) + } + if clientCtx.ChainID == "" || flagSet.Changed(flags.FlagChainID) { chainID, _ := flagSet.GetString(flags.FlagChainID) clientCtx = clientCtx.WithChainID(chainID) diff --git a/client/cmd_test.go b/client/cmd_test.go index 039b239062a..c4c8fd4da3b 100644 --- a/client/cmd_test.go +++ b/client/cmd_test.go @@ -56,7 +56,7 @@ func TestValidateCmd(t *testing.T) { } func TestSetCmdClientContextHandler(t *testing.T) { - initClientCtx := client.Context{}.WithHomeDir("/foo/bar").WithChainID("test-chain") + initClientCtx := client.Context{}.WithHomeDir("/foo/bar").WithChainID("test-chain").WithKeyringDir("/foo/bar") newCmd := func() *cobra.Command { c := &cobra.Command{ diff --git a/client/context.go b/client/context.go index 71ee6891acc..96c909478b0 100644 --- a/client/context.go +++ b/client/context.go @@ -32,6 +32,7 @@ type Context struct { OutputFormat string Height int64 HomeDir string + KeyringDir string From string BroadcastMode string FromName string @@ -134,6 +135,12 @@ func (ctx Context) WithHomeDir(dir string) Context { return ctx } +// WithKeyringDir returns a copy of the Context with KeyringDir set. +func (ctx Context) WithKeyringDir(dir string) Context { + ctx.KeyringDir = dir + return ctx +} + // WithGenerateOnly returns a copy of the context with updated GenerateOnly value func (ctx Context) WithGenerateOnly(generateOnly bool) Context { ctx.GenerateOnly = generateOnly @@ -302,8 +309,8 @@ func GetFromFields(kr keyring.Keyring, from string, genOnly bool) (sdk.AccAddres func newKeyringFromFlags(ctx Context, backend string) (keyring.Keyring, error) { if ctx.GenerateOnly { - return keyring.New(sdk.KeyringServiceName(), keyring.BackendMemory, ctx.HomeDir, ctx.Input) + return keyring.New(sdk.KeyringServiceName(), keyring.BackendMemory, ctx.KeyringDir, ctx.Input) } - return keyring.New(sdk.KeyringServiceName(), backend, ctx.HomeDir, ctx.Input) + return keyring.New(sdk.KeyringServiceName(), backend, ctx.KeyringDir, ctx.Input) } diff --git a/client/flags/flags.go b/client/flags/flags.go index fdb776f82ec..2131c376397 100644 --- a/client/flags/flags.go +++ b/client/flags/flags.go @@ -35,6 +35,7 @@ const ( // List of CLI flags const ( FlagHome = tmcli.HomeFlag + FlagKeyringDir = "keyring-dir" FlagUseLedger = "ledger" FlagChainID = "chain-id" FlagNode = "node" @@ -84,6 +85,7 @@ func AddQueryFlagsToCmd(cmd *cobra.Command) { // AddTxFlagsToCmd adds common flags to a module tx command. func AddTxFlagsToCmd(cmd *cobra.Command) { + cmd.Flags().String(FlagKeyringDir, "", "The client Keyring directory; if omitted, the default 'home' directory will be used") cmd.Flags().String(FlagFrom, "", "Name or address of private key with which to sign") cmd.Flags().Uint64P(FlagAccountNumber, "a", 0, "The account number of the signing account (offline mode only)") cmd.Flags().Uint64P(FlagSequence, "s", 0, "The sequence number of the signing account (offline mode only)") diff --git a/client/keys/add.go b/client/keys/add.go index 0be9b4970f6..57b37a50013 100644 --- a/client/keys/add.go +++ b/client/keys/add.go @@ -12,6 +12,7 @@ import ( "github.com/tendermint/tendermint/crypto" "github.com/tendermint/tendermint/libs/cli" + "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/flags" "github.com/cosmos/cosmos-sdk/client/input" "github.com/cosmos/cosmos-sdk/crypto/hd" @@ -84,20 +85,19 @@ the flag --nosort is set. func runAddCmd(cmd *cobra.Command, args []string) error { buf := bufio.NewReader(cmd.InOrStdin()) - - homeDir, _ := cmd.Flags().GetString(flags.FlagHome) - dryRun, _ := cmd.Flags().GetBool(flags.FlagHome) + clientCtx := client.GetClientContextFromCmd(cmd) var ( kr keyring.Keyring err error ) + dryRun, _ := cmd.Flags().GetBool(flags.FlagDryRun) if dryRun { - kr, err = keyring.New(sdk.KeyringServiceName(), keyring.BackendMemory, homeDir, buf) + kr, err = keyring.New(sdk.KeyringServiceName(), keyring.BackendMemory, clientCtx.KeyringDir, buf) } else { backend, _ := cmd.Flags().GetString(flags.FlagKeyringBackend) - kr, err = keyring.New(sdk.KeyringServiceName(), backend, homeDir, buf) + kr, err = keyring.New(sdk.KeyringServiceName(), backend, clientCtx.KeyringDir, buf) } if err != nil { diff --git a/client/keys/add_ledger_test.go b/client/keys/add_ledger_test.go index e3ecd91bb50..b8ba1ec7f59 100644 --- a/client/keys/add_ledger_test.go +++ b/client/keys/add_ledger_test.go @@ -3,6 +3,7 @@ package keys import ( + "context" "fmt" "testing" @@ -10,6 +11,7 @@ import ( "github.com/tendermint/tendermint/libs/cli" + "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/flags" "github.com/cosmos/cosmos-sdk/crypto/hd" "github.com/cosmos/cosmos-sdk/crypto/keyring" @@ -39,9 +41,11 @@ func Test_runAddCmdLedgerWithCustomCoinType(t *testing.T) { // Prepare a keybase kbHome := t.TempDir() + clientCtx := client.Context{}.WithKeyringDir(kbHome) + ctx := context.WithValue(context.Background(), client.ClientContextKey, &clientCtx) + cmd.SetArgs([]string{ "keyname1", - fmt.Sprintf("--%s=%s", flags.FlagHome, kbHome), fmt.Sprintf("--%s=true", flags.FlagUseLedger), fmt.Sprintf("--%s=0", flagAccount), fmt.Sprintf("--%s=0", flagIndex), @@ -53,7 +57,7 @@ func Test_runAddCmdLedgerWithCustomCoinType(t *testing.T) { mockIn := testutil.ApplyMockIODiscardOutErr(cmd) mockIn.Reset("test1234\ntest1234\n") - require.NoError(t, cmd.Execute()) + require.NoError(t, cmd.ExecuteContext(ctx)) // Now check that it has been stored properly kb, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendTest, kbHome, mockIn) @@ -83,12 +87,15 @@ func Test_runAddCmdLedgerWithCustomCoinType(t *testing.T) { func Test_runAddCmdLedger(t *testing.T) { cmd := AddKeyCommand() cmd.Flags().AddFlagSet(Commands("home").PersistentFlags()) + mockIn := testutil.ApplyMockIODiscardOutErr(cmd) kbHome := t.TempDir() + clientCtx := client.Context{}.WithKeyringDir(kbHome) + ctx := context.WithValue(context.Background(), client.ClientContextKey, &clientCtx) + cmd.SetArgs([]string{ "keyname1", - fmt.Sprintf("--%s=%s", flags.FlagHome, kbHome), fmt.Sprintf("--%s=true", flags.FlagUseLedger), fmt.Sprintf("--%s=%s", cli.OutputFlag, OutputFormatText), fmt.Sprintf("--%s=%s", flags.FlagKeyAlgorithm, string(hd.Secp256k1Type)), @@ -97,7 +104,7 @@ func Test_runAddCmdLedger(t *testing.T) { }) mockIn.Reset("test1234\ntest1234\n") - require.NoError(t, cmd.Execute()) + require.NoError(t, cmd.ExecuteContext(ctx)) // Now check that it has been stored properly kb, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendTest, kbHome, mockIn) diff --git a/client/keys/add_test.go b/client/keys/add_test.go index ea9bdf5bebc..aa6f68876a2 100644 --- a/client/keys/add_test.go +++ b/client/keys/add_test.go @@ -1,6 +1,7 @@ package keys import ( + "context" "fmt" "testing" @@ -8,6 +9,7 @@ import ( "github.com/tendermint/tendermint/libs/cli" + "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/flags" "github.com/cosmos/cosmos-sdk/crypto/hd" "github.com/cosmos/cosmos-sdk/crypto/keyring" @@ -21,9 +23,13 @@ func Test_runAddCmdBasic(t *testing.T) { mockIn := testutil.ApplyMockIODiscardOutErr(cmd) kbHome := t.TempDir() - kb, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendTest, kbHome, mockIn) + kb, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendTest, kbHome, mockIn) require.NoError(t, err) + + clientCtx := client.Context{}.WithKeyringDir(kbHome) + ctx := context.WithValue(context.Background(), client.ClientContextKey, &clientCtx) + t.Cleanup(func() { _ = kb.Delete("keyname1") _ = kb.Delete("keyname2") @@ -37,10 +43,10 @@ func Test_runAddCmdBasic(t *testing.T) { fmt.Sprintf("--%s=%s", flags.FlagKeyringBackend, keyring.BackendTest), }) mockIn.Reset("y\n") - require.NoError(t, cmd.Execute()) + require.NoError(t, cmd.ExecuteContext(ctx)) mockIn.Reset("N\n") - require.Error(t, cmd.Execute()) + require.Error(t, cmd.ExecuteContext(ctx)) cmd.SetArgs([]string{ "keyname2", @@ -50,11 +56,11 @@ func Test_runAddCmdBasic(t *testing.T) { fmt.Sprintf("--%s=%s", flags.FlagKeyringBackend, keyring.BackendTest), }) - require.NoError(t, cmd.Execute()) - require.Error(t, cmd.Execute()) + require.NoError(t, cmd.ExecuteContext(ctx)) + require.Error(t, cmd.ExecuteContext(ctx)) mockIn.Reset("y\n") - require.NoError(t, cmd.Execute()) + require.NoError(t, cmd.ExecuteContext(ctx)) cmd.SetArgs([]string{ "keyname4", @@ -64,8 +70,8 @@ func Test_runAddCmdBasic(t *testing.T) { fmt.Sprintf("--%s=%s", flags.FlagKeyringBackend, keyring.BackendTest), }) - require.NoError(t, cmd.Execute()) - require.Error(t, cmd.Execute()) + require.NoError(t, cmd.ExecuteContext(ctx)) + require.Error(t, cmd.ExecuteContext(ctx)) cmd.SetArgs([]string{ "keyname5", @@ -75,7 +81,7 @@ func Test_runAddCmdBasic(t *testing.T) { fmt.Sprintf("--%s=%s", flags.FlagKeyAlgorithm, string(hd.Secp256k1Type)), }) - require.NoError(t, cmd.Execute()) + require.NoError(t, cmd.ExecuteContext(ctx)) // In recovery mode cmd.SetArgs([]string{ @@ -85,11 +91,11 @@ func Test_runAddCmdBasic(t *testing.T) { // use valid mnemonic and complete recovery key generation successfully mockIn.Reset("decide praise business actor peasant farm drastic weather extend front hurt later song give verb rhythm worry fun pond reform school tumble august one\n") - require.NoError(t, cmd.Execute()) + require.NoError(t, cmd.ExecuteContext(ctx)) // use invalid mnemonic and fail recovery key generation mockIn.Reset("invalid mnemonic\n") - require.Error(t, cmd.Execute()) + require.Error(t, cmd.ExecuteContext(ctx)) // In interactive mode cmd.SetArgs([]string{ @@ -102,9 +108,9 @@ func Test_runAddCmdBasic(t *testing.T) { // set password and complete interactive key generation successfully mockIn.Reset("\n" + password + "\n" + password + "\n") - require.NoError(t, cmd.Execute()) + require.NoError(t, cmd.ExecuteContext(ctx)) // passwords don't match and fail interactive key generation mockIn.Reset("\n" + password + "\n" + "fail" + "\n") - require.Error(t, cmd.Execute()) + require.Error(t, cmd.ExecuteContext(ctx)) } diff --git a/client/keys/delete.go b/client/keys/delete.go index 644598109b4..3cc61e6de8e 100644 --- a/client/keys/delete.go +++ b/client/keys/delete.go @@ -3,10 +3,9 @@ package keys import ( "bufio" - "github.com/cosmos/cosmos-sdk/client/flags" + "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/input" "github.com/cosmos/cosmos-sdk/crypto/keyring" - sdk "github.com/cosmos/cosmos-sdk/types" "github.com/spf13/cobra" ) @@ -30,16 +29,10 @@ private keys stored in a ledger device cannot be deleted with the CLI. Args: cobra.MinimumNArgs(1), RunE: func(cmd *cobra.Command, args []string) error { buf := bufio.NewReader(cmd.InOrStdin()) - - backend, _ := cmd.Flags().GetString(flags.FlagKeyringBackend) - homeDir, _ := cmd.Flags().GetString(flags.FlagHome) - kb, err := keyring.New(sdk.KeyringServiceName(), backend, homeDir, buf) - if err != nil { - return err - } + clientCtx := client.GetClientContextFromCmd(cmd) for _, name := range args { - info, err := kb.Key(name) + info, err := clientCtx.Keyring.Key(name) if err != nil { return err } @@ -53,7 +46,7 @@ private keys stored in a ledger device cannot be deleted with the CLI. } } - if err := kb.Delete(name); err != nil { + if err := clientCtx.Keyring.Delete(name); err != nil { return err } diff --git a/client/keys/delete_test.go b/client/keys/delete_test.go index da863ef1253..5a001396719 100644 --- a/client/keys/delete_test.go +++ b/client/keys/delete_test.go @@ -1,11 +1,13 @@ package keys import ( + "context" "fmt" "testing" "github.com/stretchr/testify/require" + "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/flags" "github.com/cosmos/cosmos-sdk/crypto/hd" "github.com/cosmos/cosmos-sdk/crypto/keyring" @@ -41,7 +43,11 @@ func Test_runDeleteCmd(t *testing.T) { require.NoError(t, err) cmd.SetArgs([]string{"blah", fmt.Sprintf("--%s=%s", flags.FlagHome, kbHome)}) - err = cmd.Execute() + + clientCtx := client.Context{}.WithKeyring(kb) + ctx := context.WithValue(context.Background(), client.ClientContextKey, &clientCtx) + + err = cmd.ExecuteContext(ctx) require.Error(t, err) require.Equal(t, "The specified item could not be found in the keyring", err.Error()) diff --git a/client/keys/export.go b/client/keys/export.go index e2248a2f18f..3f75ee2e52e 100644 --- a/client/keys/export.go +++ b/client/keys/export.go @@ -5,10 +5,8 @@ import ( "github.com/spf13/cobra" - "github.com/cosmos/cosmos-sdk/client/flags" + "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/input" - "github.com/cosmos/cosmos-sdk/crypto/keyring" - sdk "github.com/cosmos/cosmos-sdk/types" ) // ExportKeyCommand exports private keys from the key store. @@ -20,20 +18,14 @@ func ExportKeyCommand() *cobra.Command { Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { buf := bufio.NewReader(cmd.InOrStdin()) - - backend, _ := cmd.Flags().GetString(flags.FlagKeyringBackend) - homeDir, _ := cmd.Flags().GetString(flags.FlagHome) - kb, err := keyring.New(sdk.KeyringServiceName(), backend, homeDir, buf) - if err != nil { - return err - } + clientCtx := client.GetClientContextFromCmd(cmd) encryptPassword, err := input.GetPassword("Enter passphrase to encrypt the exported key:", buf) if err != nil { return err } - armored, err := kb.ExportPrivKeyArmor(args[0], encryptPassword) + armored, err := clientCtx.Keyring.ExportPrivKeyArmor(args[0], encryptPassword) if err != nil { return err } diff --git a/client/keys/export_test.go b/client/keys/export_test.go index 7f48795c51c..4276db1c124 100644 --- a/client/keys/export_test.go +++ b/client/keys/export_test.go @@ -1,11 +1,13 @@ package keys import ( + "context" "fmt" "testing" "github.com/stretchr/testify/require" + "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/crypto/hd" "github.com/cosmos/cosmos-sdk/testutil" @@ -41,5 +43,8 @@ func Test_runExportCmd(t *testing.T) { fmt.Sprintf("--%s=%s", flags.FlagKeyringBackend, keyring.BackendTest), }) - require.NoError(t, cmd.Execute()) + clientCtx := client.Context{}.WithKeyring(kb) + ctx := context.WithValue(context.Background(), client.ClientContextKey, &clientCtx) + + require.NoError(t, cmd.ExecuteContext(ctx)) } diff --git a/client/keys/import.go b/client/keys/import.go index 641ac6ef623..ce1cd36b587 100644 --- a/client/keys/import.go +++ b/client/keys/import.go @@ -6,10 +6,8 @@ import ( "github.com/spf13/cobra" - "github.com/cosmos/cosmos-sdk/client/flags" + "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/input" - "github.com/cosmos/cosmos-sdk/crypto/keyring" - sdk "github.com/cosmos/cosmos-sdk/types" ) // ImportKeyCommand imports private keys from a keyfile. @@ -21,13 +19,7 @@ func ImportKeyCommand() *cobra.Command { Args: cobra.ExactArgs(2), RunE: func(cmd *cobra.Command, args []string) error { buf := bufio.NewReader(cmd.InOrStdin()) - - backend, _ := cmd.Flags().GetString(flags.FlagKeyringBackend) - homeDir, _ := cmd.Flags().GetString(flags.FlagHome) - kb, err := keyring.New(sdk.KeyringServiceName(), backend, homeDir, buf) - if err != nil { - return err - } + clientCtx := client.GetClientContextFromCmd(cmd) bz, err := ioutil.ReadFile(args[1]) if err != nil { @@ -39,7 +31,7 @@ func ImportKeyCommand() *cobra.Command { return err } - return kb.ImportPrivKey(args[0], string(bz), passphrase) + return clientCtx.Keyring.ImportPrivKey(args[0], string(bz), passphrase) }, } } diff --git a/client/keys/import_test.go b/client/keys/import_test.go index d6e98a74ec2..1c8d01cb162 100644 --- a/client/keys/import_test.go +++ b/client/keys/import_test.go @@ -1,6 +1,7 @@ package keys import ( + "context" "fmt" "io/ioutil" "path/filepath" @@ -8,6 +9,7 @@ import ( "github.com/stretchr/testify/require" + "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/flags" "github.com/cosmos/cosmos-sdk/crypto/keyring" "github.com/cosmos/cosmos-sdk/testutil" @@ -23,6 +25,9 @@ func Test_runImportCmd(t *testing.T) { kbHome := t.TempDir() kb, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendTest, kbHome, mockIn) + clientCtx := client.Context{}.WithKeyring(kb) + ctx := context.WithValue(context.Background(), client.ClientContextKey, &clientCtx) + require.NoError(t, err) t.Cleanup(func() { kb.Delete("keyname1") // nolint:errcheck @@ -43,8 +48,7 @@ HbP+c6JmeJy9JXe2rbbF1QtCX1gLqGcDQPBXiCtFvP7/8wTZtVOPj8vREzhZ9ElO mockIn.Reset("123456789\n") cmd.SetArgs([]string{ "keyname1", keyfile, - fmt.Sprintf("--%s=%s", flags.FlagHome, kbHome), fmt.Sprintf("--%s=%s", flags.FlagKeyringBackend, keyring.BackendTest), }) - require.NoError(t, cmd.Execute()) + require.NoError(t, cmd.ExecuteContext(ctx)) } diff --git a/client/keys/keyring-test/00ddf62001f91b9aaf7758fb727bea3932bb236e.address b/client/keys/keyring-test/00ddf62001f91b9aaf7758fb727bea3932bb236e.address new file mode 100644 index 00000000000..f53c291b43f --- /dev/null +++ b/client/keys/keyring-test/00ddf62001f91b9aaf7758fb727bea3932bb236e.address @@ -0,0 +1 @@ +eyJhbGciOiJQQkVTMi1IUzI1NitBMTI4S1ciLCJjcmVhdGVkIjoiMjAyMC0xMC0wOCAxMzowNjozOC45MTAzNTggLTA0MDAgRURUIG09KzAuMDUwMTczMjM4IiwiZW5jIjoiQTI1NkdDTSIsInAyYyI6ODE5MiwicDJzIjoiT25IN1lscERZSHZUVFFQcCJ9.27X3naS_OL75csQLEIFoPvvCyYb9R4D573z1Z1obm3TRGn4HyPFN_w.GXNcqKAUkxqM537Q.cT169l1KGKeOra6NXHbx3kEOEDw77Lom-42mwKV0bRQ_5WZU3kG5o6Ix14r7LFL1ajjc8rdXkuiUgKQyVXEXVpo-6WkEfk2-D_CQaaUgq0-UErT-9Pj7djI3FZkPPG-yxlVSiQXB1xMk38I_AxYwAakctpwHlEK_YC0-UycFmk25Qjezar_ni69KDRPyuqCYh3dyhimG6LgdpWF4pQHjtZPy5qIqcaE7TR0OeKvf9MtsaKEzpAQOeAvh.WbbZ_Fs8qk9rsN6FuWa2zg \ No newline at end of file diff --git a/client/keys/keyring-test/252b5fe0336e82b269d1c26e379b11b54474f57a.address b/client/keys/keyring-test/252b5fe0336e82b269d1c26e379b11b54474f57a.address new file mode 100644 index 00000000000..4e99364230f --- /dev/null +++ b/client/keys/keyring-test/252b5fe0336e82b269d1c26e379b11b54474f57a.address @@ -0,0 +1 @@ +eyJhbGciOiJQQkVTMi1IUzI1NitBMTI4S1ciLCJjcmVhdGVkIjoiMjAyMC0xMC0wOCAxMjo1MDoxOC4yMzA4ODggLTA0MDAgRURUIG09KzAuMTYxMTg1Nzk2IiwiZW5jIjoiQTI1NkdDTSIsInAyYyI6ODE5MiwicDJzIjoiU3ZNM1hfZU42VHhpSVg3aiJ9.BjL9xqItRueA3u4ujcWPTp4TJjO6w4NeR9G7p32ndo63ADDGJ7j1JQ.8Fd_XM52yxKhF31U.7Cm3SBAmp0u4QffFwBgeueuU3rWT1npSKI5CUROX5COgKbDpqj5CaT54k6UGeZiUxv8itQXglUpAsg7XsF-1LjbbUAfVxXe9H9n1GcfxrLov0L8_Ia-5JadXMXkbvv9jKyjhVg6kSziQXoHcHaeauF1X0_ij3a-UVH87cLqsdAI_OXtptyU8GonVyt_Q0n8mljonjZhj2c_bmXmHARYXZOmCj52dmzSpmkyQ9vqdhlRPco93-JWR5P1V.5J7fb71-1WKJ91g02D0JGg \ No newline at end of file diff --git a/client/keys/keyring-test/43ca6f8e891fd1ef46a28d67c9be33ffecd8a6f8.address b/client/keys/keyring-test/43ca6f8e891fd1ef46a28d67c9be33ffecd8a6f8.address new file mode 100644 index 00000000000..d03cbd2cbc3 --- /dev/null +++ b/client/keys/keyring-test/43ca6f8e891fd1ef46a28d67c9be33ffecd8a6f8.address @@ -0,0 +1 @@ +eyJhbGciOiJQQkVTMi1IUzI1NitBMTI4S1ciLCJjcmVhdGVkIjoiMjAyMC0xMC0wOCAxMjo1MzoyMy4yNjI0NzEgLTA0MDAgRURUIG09KzAuMDc2MzQ4MjA5IiwiZW5jIjoiQTI1NkdDTSIsInAyYyI6ODE5MiwicDJzIjoiTE02SnY0RUdZNHBPLTQxWSJ9.wsqaCPHz_PlOH4_B3QlKT_4N9nTEjMzqn_Rqjq2ZM3vzf1dTO1_gjA.WVEaKSvNNWJ3ZaTT.eWrtCGCCplDULPw1QEyijVO_totUT5-6yx-TK4KP_BdKmhdEG8Bm319dXU33BchHthFa2VxDyB4NH_hsUenErJSKIJgJGoVc_AMwqrVZr0Wg0qJaay7jRGh1IRNXc0cuEsNpEek1C31tNaXjD2IuJzkicwdDT3BARFLFFdRhY97LG83YTvX0gVKyJFfjx8TAgUHZgpYyJMI4_vVajnneI-v1SYCY_VMbFTaCqWKFZdYOhu3x-hXfFBww.rxnMJbBz5OU4itr8nuyZgA \ No newline at end of file diff --git a/client/keys/keyring-test/454b29a63587e58ce2487836d90bda016c6fff7c.address b/client/keys/keyring-test/454b29a63587e58ce2487836d90bda016c6fff7c.address new file mode 100644 index 00000000000..12fc44167b7 --- /dev/null +++ b/client/keys/keyring-test/454b29a63587e58ce2487836d90bda016c6fff7c.address @@ -0,0 +1 @@ +eyJhbGciOiJQQkVTMi1IUzI1NitBMTI4S1ciLCJjcmVhdGVkIjoiMjAyMC0xMC0wOCAxMjo1MDoxOC4yNDQwMTEgLTA0MDAgRURUIG09KzAuMTc0MzMxMjAxIiwiZW5jIjoiQTI1NkdDTSIsInAyYyI6ODE5MiwicDJzIjoiYmlQLXR5Mnlvc3U2aHBvLSJ9.3drCmgYTeqS3PohaYKQc7i1fyjtOMuEPu_pDqMpT0UStPNDxG_LUDg.VS6Au9HoIruV0RiE._2BmFif-VbT_x4OD1NfsOCVFdL2MZfsG645SkptEKZAncOwHkKmWnBlKiV_LwnNzRBh-9eGGsCGfyou3zjUQRMDDHJOuW2EaVNmufmBWcAb9UoNO8O5kzPHwIvNqqJo5TQyjOviKCoP2PVcJXAwzttqDOw71B-9OuPwt_Ed4G6u8evwGIe08CzV6CKVImzj-AQg-1UI-uL06yFIEJ6CzB1DMdPR0qDQddP8pSYR_RTHnEUsii7HeKK1O.jqlYm4IZhXqe1k5kBQtguA \ No newline at end of file diff --git a/client/keys/keyring-test/900b3a0b8932e443b60fd6273cc3026aa25116c8.address b/client/keys/keyring-test/900b3a0b8932e443b60fd6273cc3026aa25116c8.address new file mode 100644 index 00000000000..4f49e8bddde --- /dev/null +++ b/client/keys/keyring-test/900b3a0b8932e443b60fd6273cc3026aa25116c8.address @@ -0,0 +1 @@ +eyJhbGciOiJQQkVTMi1IUzI1NitBMTI4S1ciLCJjcmVhdGVkIjoiMjAyMC0xMC0wOCAxMjo1MDoxOC4yMDQ1NjYgLTA0MDAgRURUIG09KzAuMTM0ODY0MzE4IiwiZW5jIjoiQTI1NkdDTSIsInAyYyI6ODE5MiwicDJzIjoidjVpMzRMY3NNbXduakEyMiJ9.XTokiwtSrKOIGREG7P7uaSfcV3hEr2ANHVUwaKbvLbuQVlTQO8fALw.bldSMLqfirE4GM9S.kNlvEojt1cavNW-nCaxX-Qk3tNm09xtXbuKppWbmMBUCf-_p-U_TWsnHuKbLon47RH1lxomrc1RpcfXwWhDEsGLwibtsjRdxz_2DGh124jeKOr4-Bl2raoPWdHKimm_cf5Ve17ChFfVy1AOaXwIr97ZdGWSU0FP8hOvv5_z5iUsuMK9T0DLxjz0162-_xSQMWWl4-hLknHz-QdO3oR_FpYo2K2eucNaFKmcN5Rn4s2n8FYLU9dIcopUF.WpNuRheBDoTiv3rK95yNjA \ No newline at end of file diff --git a/client/keys/keyring-test/9a85abde78d0e55e7650f11dcfdbb6bd131038d0.address b/client/keys/keyring-test/9a85abde78d0e55e7650f11dcfdbb6bd131038d0.address new file mode 100644 index 00000000000..9110d23a6b9 --- /dev/null +++ b/client/keys/keyring-test/9a85abde78d0e55e7650f11dcfdbb6bd131038d0.address @@ -0,0 +1 @@ +eyJhbGciOiJQQkVTMi1IUzI1NitBMTI4S1ciLCJjcmVhdGVkIjoiMjAyMC0xMC0wOCAxMjo1MDoxOC4yMjAyOSAtMDQwMCBFRFQgbT0rMC4xNTA1ODc4MjYiLCJlbmMiOiJBMjU2R0NNIiwicDJjIjo4MTkyLCJwMnMiOiJJYXRUWEFMVjBfeXV2ZnNMIn0.kCXD6XXDHeBiXR-GqF10fbMWBvy3qe38r16b92Xu3oLpA5c0a6ByMg.ONW9ggBJFhdfIA8M.IWm_ioQqOCLSK3FbSwjAlEVtzRR4AAW7ceIXpKzv_voaCGDNgcr7xSyRR5N-YK-sVYInwUDrme8rb5T14mjcsNgoGdKKB2QXuApY-GcPwpe2Tf7TyiCxFp91VotHnrbjCh1NvWnjDC-SZNm8HDVolkYtiBPkIkk0uFGh35WWprkVpgEYFyNIFQ0PP3XD4D9A58X0UXdGEu5Q8VcJnt1p86XUyI1le_LufJUrWAz3o_89n3xKj-b6sYzQ.KZSIrdNzE97BxrTSNkMkTw \ No newline at end of file diff --git a/client/keys/keyring-test/keyname1.info b/client/keys/keyring-test/keyname1.info new file mode 100644 index 00000000000..6da5735e6e0 --- /dev/null +++ b/client/keys/keyring-test/keyname1.info @@ -0,0 +1 @@ +eyJhbGciOiJQQkVTMi1IUzI1NitBMTI4S1ciLCJjcmVhdGVkIjoiMjAyMC0xMC0wOCAxMzowNjozOC45MDMwMjcgLTA0MDAgRURUIG09KzAuMDQyODQyNTY4IiwiZW5jIjoiQTI1NkdDTSIsInAyYyI6ODE5MiwicDJzIjoiSjBxN2Zza0pGRUJJR25mYSJ9.W91-I2lpaBfacsUO2Xn2_tCadqztjGX7MjAkA6GKL4uMkqjEHDXyhw.c8uKD9z5w-jpSmq1.XGnt9JaOg0VT1cjg4RAlwC6Bsq9KowSF6wM6Ak1Y16Kq4sV3NnwA4CqJKnluIjAG6D4sfBKEs2FCHy5zux4uaOQ3Y5EJjRxWoTdBP7HahmO2-jsSFX_sPIzr86KIlKIqaYFJAOUqvaObOsQkX3EL_2-vDonSRMz32abg8thFS6mNi7NtM4xGXQ5Knrix-6OgzBmvWbn4Y0v82vNNWh8d4ubKf_RSEBV7CIWfuFg2CxfRq5EbUUmtMINF74eG52F8y8zjTDcn6n3qKLcecdr6s0n1tc7iq-f3s1EHnzPefwROPLFxiq0Zyt7N7vZCSowOElYZtgQWEg0dy6CIyZ274gNPlfLXMHA-kUsZj4Q_3w.sUPc7D8bBR4I3S-njXa4Ww \ No newline at end of file diff --git a/client/keys/keyring-test/keyname2.info b/client/keys/keyring-test/keyname2.info new file mode 100644 index 00000000000..bc7b594c5a4 --- /dev/null +++ b/client/keys/keyring-test/keyname2.info @@ -0,0 +1 @@ +eyJhbGciOiJQQkVTMi1IUzI1NitBMTI4S1ciLCJjcmVhdGVkIjoiMjAyMC0xMC0wOCAxMjo1MzoyMy4yNTg0OTUgLTA0MDAgRURUIG09KzAuMDcyMzcyNTM1IiwiZW5jIjoiQTI1NkdDTSIsInAyYyI6ODE5MiwicDJzIjoiTW1XOFlVU0Nia2JUNW9iSSJ9.JyBrlPAvgtgYWwu0rcfTn6k9qvv6DywUotcWxPUiJncCBue2WPC5cQ.CmWeu5wMFFinUfiE.FA9k3Q_W8mBgSuJRkYV8h_U5YR2mDmW595L4DnFzuSFJ19Us0O1SQF9-xPJQAyjh4jli46o5mfFfsmU0ce1h4HwklW7AdrRJXVXZ0reZLjrdiojCbLvzyM9tsWInRXi6izUcwLggv2lNCXP5UIRpjMpUPiEC4GsHiwNH8qN04_feICxHuSWJ4mKLWEDtgKxHTrBqvaHT304UF6gRD-_W9_hWdEIj66-5HE4jlxcJAe22WdoF2Z1c3ujhm4piSfHaNnWYsZHLI5Jy1WhkFC2eULOe31c6eAeik5DyUUdWKvAoSiEk4H0Z9EcSbNzlW2rrU30WIIb-icK1qLID21WYurbxM8zvXl-CvhSM2VRN1g.tu_usvTlCOy3okBKmC6zHg \ No newline at end of file diff --git a/client/keys/keyring-test/keyname4.info b/client/keys/keyring-test/keyname4.info new file mode 100644 index 00000000000..2a198cd9176 --- /dev/null +++ b/client/keys/keyring-test/keyname4.info @@ -0,0 +1 @@ +eyJhbGciOiJQQkVTMi1IUzI1NitBMTI4S1ciLCJjcmVhdGVkIjoiMjAyMC0xMC0wOCAxMjo1MDoxOC4xOTkwMDUgLTA0MDAgRURUIG09KzAuMTI5MzA0MDMxIiwiZW5jIjoiQTI1NkdDTSIsInAyYyI6ODE5MiwicDJzIjoiYzVYM09VdTBtQmJ4TFd4ayJ9.Pa4p1u-9N9x4E9-5rjUlReLsfH3TvTfAw-Dr1iV8z5ccAfnqLY1UWQ.Q41_cYh4c-C2zi3v.aFna4CwpZeGQBI2_ecOzlJSKypCV0NLCD8PCOnpYvY-k-HqoUFSeouFbuKeN9VaIo12JSZmjzGhfCAupZDBcSJisLVHOvaBAjl5XCOa8k49jb-aSopMI4HXQWatBJcnM65p9Hl1JrYOcnoKPxNKzJ4PiPQnHKv_VgAvWU_CBt6nnSjkwwVJjPMobgvNzeQTEFq-4pyziJNDbDWKUrQSrc-VaO-31JBlhpu6dPOJPFsnOcMyPc76po5cAQQdog-g79d59_Y4vj8s7qd-YPCHnWmoCbgf9w8vbpmJ4Y9evXZQz8A0-c0rFX7F96aZBYtQOeC1ZpRi0BMsbs_WHrpdN678HXej6YpfNDijQmiNYzQ.ayDBvX4W6GiGxAjN3ch8DA \ No newline at end of file diff --git a/client/keys/keyring-test/keyname5.info b/client/keys/keyring-test/keyname5.info new file mode 100644 index 00000000000..5c9bbe4e975 --- /dev/null +++ b/client/keys/keyring-test/keyname5.info @@ -0,0 +1 @@ +eyJhbGciOiJQQkVTMi1IUzI1NitBMTI4S1ciLCJjcmVhdGVkIjoiMjAyMC0xMC0wOCAxMjo1MDoxOC4yMTYwNTkgLTA0MDAgRURUIG09KzAuMTQ2MzU3MTI5IiwiZW5jIjoiQTI1NkdDTSIsInAyYyI6ODE5MiwicDJzIjoidmNnOXNfXzFvY0N1ZC1YTCJ9.hQ21Z1cXTw925V8Xxm_Gywb05S92rKEX54tnELv16xZvkgV8XiEj9A.4Wnaa4LemIi3bdIT.kEJyzWUvmLof5_bYbDePbrYyfjyqTLuUIOdnom3igOuwfzFDHtPuEb3rSLKjxj7LgJOmZVqZGP_ihW1sJQPXbK7ZuWA4zH_Wf-n5T8CFDmNIUhlUIb6sfd_ze-s45CE58hjKRkp7b5k61xBnMujZ5KC5Vk_JHUOUyZB5SqhTuEUJDjSSCFnMDJ1UYKEp23U__XFwcZonent4IMfM0fWvmA6NC2h0qLAMcKw9hbJ_yyNHt2I3lI5twthsAOsXKxUkjhx7c9Tc7BnttFxq-puD_QyjReExP77DzuueDJ-5KBd8PMgeiQMHoYM8e2NAAJU7MXe7voB-D8Ki2QcEgH7GfHNcr6vP1by3hvV5M32OXg.ifBDbtRjrXBOdH_jEORHgw \ No newline at end of file diff --git a/client/keys/keyring-test/keyname6.info b/client/keys/keyring-test/keyname6.info new file mode 100644 index 00000000000..f193785ba6a --- /dev/null +++ b/client/keys/keyring-test/keyname6.info @@ -0,0 +1 @@ +eyJhbGciOiJQQkVTMi1IUzI1NitBMTI4S1ciLCJjcmVhdGVkIjoiMjAyMC0xMC0wOCAxMjo1MDoxOC4yMjY1OTEgLTA0MDAgRURUIG09KzAuMTU2ODg5MTU0IiwiZW5jIjoiQTI1NkdDTSIsInAyYyI6ODE5MiwicDJzIjoiNUtTSWJtdGhCVHVSUnRjYyJ9.qD2C7cQK5P0EHy2Yr-uQZzMHep6U6n57z2LwWTmgxQp6m7ujLw-C-g.rmsltoJfFO4e56RZ.IDpVmduqe2WgyepT_-paXzcosHQzK6sfKY9JH16lT4QRVJ_lAozQOyZrW3X5MbgefrmtXGsoEIEFYhTDYBtXxrW7IqLaBhSCiA5MVwR403H3C2NkcygdGDdR-uDQGW3_bp7xnOhVL_3ofu0-7MQMMhZyz_wEmVW-aG7F6lN68TPaO5KTIqfnI8vOJyyZsSgB0M0gA3f-P4aar64YDTUdjgXPOSBkyRZr07JIOauGhTFXwmHWsDVBvGo3aIIx9ybAg_Blgo8ZAPqOJ6EYmA3J5RE2_LkfJjgI8dEpIFaviBHeWrG54AAN0klQ7trq9MOCpUGPc7PqySwiwTmxb2g4kFH9fR_yQ-g5g6mjj3JYVA.GRnNxd28SYmRt1I9twptPw \ No newline at end of file diff --git a/client/keys/keyring-test/keyname7.info b/client/keys/keyring-test/keyname7.info new file mode 100644 index 00000000000..7b09ba0a783 --- /dev/null +++ b/client/keys/keyring-test/keyname7.info @@ -0,0 +1 @@ +eyJhbGciOiJQQkVTMi1IUzI1NitBMTI4S1ciLCJjcmVhdGVkIjoiMjAyMC0xMC0wOCAxMjo1MDoxOC4yMzg1MTQgLTA0MDAgRURUIG09KzAuMTY4ODExNzMwIiwiZW5jIjoiQTI1NkdDTSIsInAyYyI6ODE5MiwicDJzIjoiUmZDZC0xeGh6OVJ3RzJfViJ9.hMyIYfHwLYAwJs6THPC30rWfhd1SRUl5po4ifTvln5cV_VHyHLW3MQ.ku5jtKB-G5acpq4v.s0oNPaUaRQbFk-X8AL8QitkI_SdBWB2BpBmRRbo2ZMAkq4x81hSC5p7RlSrM3OGTNFZ4yOrRgzdMv43YpCl7ZpJIypF4l7Hyvl_13jTjqzB7o81dEhl_10SI_Fw607VKCnwqq02_VoqD489EpMVuQ05Fg2pUT3M_mJMacGztORYVJrIWwzbyUiHfM4GlnaoUQaKfwbkHS2W2-1wOPTSWTLEBVJlRG1EAZR_upcPJolcAStjl8PY5EfkxXD56c8Xu6SI8LjMrJAXXg7lTqOGNOkt0v8M8UZWd95Gy2zH_KJm3ItYR_YjPoMIHh-_Cb2-0uoXNRyykW4EpGptp08n7QubSYltzXwaw_NgLP9KUmg.67EgfbLDNyvEYCR12Bjoew \ No newline at end of file diff --git a/client/keys/list.go b/client/keys/list.go index 0c6eedab26b..bb848190a58 100644 --- a/client/keys/list.go +++ b/client/keys/list.go @@ -4,9 +4,7 @@ import ( "github.com/spf13/cobra" "github.com/tendermint/tendermint/libs/cli" - "github.com/cosmos/cosmos-sdk/client/flags" - "github.com/cosmos/cosmos-sdk/crypto/keyring" - sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/client" ) const flagListNames = "list-names" @@ -26,14 +24,9 @@ along with their associated name and address.`, } func runListCmd(cmd *cobra.Command, _ []string) error { - backend, _ := cmd.Flags().GetString(flags.FlagKeyringBackend) - homeDir, _ := cmd.Flags().GetString(flags.FlagHome) - kb, err := keyring.New(sdk.KeyringServiceName(), backend, homeDir, cmd.InOrStdin()) - if err != nil { - return err - } + clientCtx := client.GetClientContextFromCmd(cmd) - infos, err := kb.List() + infos, err := clientCtx.Keyring.List() if err != nil { return err } diff --git a/client/keys/list_test.go b/client/keys/list_test.go index 78c4e94857b..5904f74bfdd 100644 --- a/client/keys/list_test.go +++ b/client/keys/list_test.go @@ -1,12 +1,14 @@ package keys import ( + "context" "fmt" "testing" "github.com/spf13/cobra" "github.com/stretchr/testify/require" + "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/flags" "github.com/cosmos/cosmos-sdk/crypto/hd" "github.com/cosmos/cosmos-sdk/crypto/keyring" @@ -25,6 +27,9 @@ func Test_runListCmd(t *testing.T) { kb, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendTest, kbHome2, mockIn) require.NoError(t, err) + clientCtx := client.Context{}.WithKeyring(kb) + ctx := context.WithValue(context.Background(), client.ClientContextKey, &clientCtx) + path := "" //sdk.GetConfig().GetFullFundraiserPath() _, err = kb.NewAccount("something", testutil.TestMnemonic, "", path, hd.Secp256k1) require.NoError(t, err) @@ -55,7 +60,7 @@ func Test_runListCmd(t *testing.T) { fmt.Sprintf("--%s=%s", flags.FlagKeyringBackend, keyring.BackendTest), }) - if err := cmd.Execute(); (err != nil) != tt.wantErr { + if err := cmd.ExecuteContext(ctx); (err != nil) != tt.wantErr { t.Errorf("runListCmd() error = %v, wantErr %v", err, tt.wantErr) } @@ -65,7 +70,7 @@ func Test_runListCmd(t *testing.T) { fmt.Sprintf("--%s=%s", flags.FlagKeyringBackend, keyring.BackendTest), }) - if err := cmd.Execute(); (err != nil) != tt.wantErr { + if err := cmd.ExecuteContext(ctx); (err != nil) != tt.wantErr { t.Errorf("runListCmd() error = %v, wantErr %v", err, tt.wantErr) } }) diff --git a/client/keys/migrate_test.go b/client/keys/migrate_test.go index 2918d1e9346..948e0144fc9 100644 --- a/client/keys/migrate_test.go +++ b/client/keys/migrate_test.go @@ -1,6 +1,7 @@ package keys import ( + "context" "fmt" "testing" @@ -8,6 +9,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/tendermint/tendermint/libs/cli" + "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/flags" "github.com/cosmos/cosmos-sdk/crypto/keyring" "github.com/cosmos/cosmos-sdk/testutil" @@ -20,14 +22,16 @@ func Test_runMigrateCmd(t *testing.T) { kbHome := t.TempDir() + clientCtx := client.Context{}.WithKeyringDir(kbHome) + ctx := context.WithValue(context.Background(), client.ClientContextKey, &clientCtx) + copy.Copy("testdata", kbHome) cmd.SetArgs([]string{ "keyname1", - fmt.Sprintf("--%s=%s", flags.FlagHome, kbHome), fmt.Sprintf("--%s=%s", cli.OutputFlag, OutputFormatText), fmt.Sprintf("--%s=%s", flags.FlagKeyringBackend, keyring.BackendTest), }) - assert.NoError(t, cmd.Execute()) + assert.NoError(t, cmd.ExecuteContext(ctx)) cmd = MigrateCommand() cmd.Flags().AddFlagSet(Commands("home").PersistentFlags()) @@ -40,5 +44,5 @@ func Test_runMigrateCmd(t *testing.T) { }) mockIn.Reset("test1234\ntest1234\n") - assert.NoError(t, cmd.Execute()) + assert.NoError(t, cmd.ExecuteContext(ctx)) } diff --git a/client/keys/root.go b/client/keys/root.go index 59074f551f4..92c78c3abea 100644 --- a/client/keys/root.go +++ b/client/keys/root.go @@ -51,6 +51,7 @@ The pass backend requires GnuPG: https://gnupg.org/ ) cmd.PersistentFlags().String(flags.FlagHome, defaultNodeHome, "The application home directory") + cmd.PersistentFlags().String(flags.FlagKeyringDir, "", "The client Keyring directory; if omitted, the default 'home' directory will be used") cmd.PersistentFlags().String(flags.FlagKeyringBackend, flags.DefaultKeyringBackend, "Select keyring's backend (os|file|test)") cmd.PersistentFlags().String(cli.OutputFlag, "text", "Output format (text|json)") diff --git a/client/keys/show.go b/client/keys/show.go index ffc30342bf1..102f29793b4 100644 --- a/client/keys/show.go +++ b/client/keys/show.go @@ -8,7 +8,7 @@ import ( tmcrypto "github.com/tendermint/tendermint/crypto" "github.com/tendermint/tendermint/libs/cli" - "github.com/cosmos/cosmos-sdk/client/flags" + "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/crypto/keyring" "github.com/cosmos/cosmos-sdk/crypto/keys/multisig" "github.com/cosmos/cosmos-sdk/crypto/ledger" @@ -53,23 +53,17 @@ consisting of all the keys provided by name and multisig threshold.`, func runShowCmd(cmd *cobra.Command, args []string) (err error) { var info keyring.Info - - backend, _ := cmd.Flags().GetString(flags.FlagKeyringBackend) - homeDir, _ := cmd.Flags().GetString(flags.FlagHome) - kb, err := keyring.New(sdk.KeyringServiceName(), backend, homeDir, cmd.InOrStdin()) - if err != nil { - return err - } + clientCtx := client.GetClientContextFromCmd(cmd) if len(args) == 1 { - info, err = fetchKey(kb, args[0]) + info, err = fetchKey(clientCtx.Keyring, args[0]) if err != nil { return fmt.Errorf("%s is not a valid name or address: %v", args[0], err) } } else { pks := make([]tmcrypto.PubKey, len(args)) for i, keyref := range args { - info, err := fetchKey(kb, keyref) + info, err := fetchKey(clientCtx.Keyring, keyref) if err != nil { return fmt.Errorf("%s is not a valid name or address: %v", keyref, err) } diff --git a/client/keys/show_test.go b/client/keys/show_test.go index d07756a7e31..e68107b58ba 100644 --- a/client/keys/show_test.go +++ b/client/keys/show_test.go @@ -1,6 +1,7 @@ package keys import ( + "context" "fmt" "testing" @@ -8,6 +9,7 @@ import ( "github.com/tendermint/tendermint/crypto" + "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/flags" "github.com/cosmos/cosmos-sdk/crypto/hd" "github.com/cosmos/cosmos-sdk/crypto/keyring" @@ -43,18 +45,22 @@ func Test_runShowCmd(t *testing.T) { cmd.Flags().AddFlagSet(Commands("home").PersistentFlags()) mockIn := testutil.ApplyMockIODiscardOutErr(cmd) + kbHome := t.TempDir() + kb, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendTest, kbHome, mockIn) + require.NoError(t, err) + + clientCtx := client.Context{}.WithKeyring(kb) + ctx := context.WithValue(context.Background(), client.ClientContextKey, &clientCtx) + cmd.SetArgs([]string{"invalid"}) - require.EqualError(t, cmd.Execute(), "invalid is not a valid name or address: decoding bech32 failed: invalid bech32 string length 7") + require.EqualError(t, cmd.ExecuteContext(ctx), "invalid is not a valid name or address: decoding bech32 failed: invalid bech32 string length 7") cmd.SetArgs([]string{"invalid1", "invalid2"}) - require.EqualError(t, cmd.Execute(), "invalid1 is not a valid name or address: decoding bech32 failed: invalid index of 1") + require.EqualError(t, cmd.ExecuteContext(ctx), "invalid1 is not a valid name or address: decoding bech32 failed: invalid index of 1") - kbHome := t.TempDir() fakeKeyName1 := "runShowCmd_Key1" fakeKeyName2 := "runShowCmd_Key2" - kb, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendTest, kbHome, mockIn) - require.NoError(t, err) t.Cleanup(func() { kb.Delete("runShowCmd_Key1") kb.Delete("runShowCmd_Key2") @@ -75,7 +81,7 @@ func Test_runShowCmd(t *testing.T) { fmt.Sprintf("--%s=%s", flags.FlagKeyringBackend, keyring.BackendTest), fmt.Sprintf("--%s=", FlagBechPrefix), }) - require.EqualError(t, cmd.Execute(), "invalid Bech32 prefix encoding provided: ") + require.EqualError(t, cmd.ExecuteContext(ctx), "invalid Bech32 prefix encoding provided: ") cmd.SetArgs([]string{ fakeKeyName1, @@ -85,7 +91,7 @@ func Test_runShowCmd(t *testing.T) { }) // try fetch by name - require.NoError(t, cmd.Execute()) + require.NoError(t, cmd.ExecuteContext(ctx)) // try fetch by addr info, err := kb.Key(fakeKeyName1) @@ -97,7 +103,7 @@ func Test_runShowCmd(t *testing.T) { }) require.NoError(t, err) - require.NoError(t, cmd.Execute()) + require.NoError(t, cmd.ExecuteContext(ctx)) // Now try multisig key - set bech to acc cmd.SetArgs([]string{ @@ -107,7 +113,7 @@ func Test_runShowCmd(t *testing.T) { fmt.Sprintf("--%s=%s", FlagBechPrefix, sdk.PrefixAccount), fmt.Sprintf("--%s=0", flagMultiSigThreshold), }) - require.EqualError(t, cmd.Execute(), "threshold must be a positive integer") + require.EqualError(t, cmd.ExecuteContext(ctx), "threshold must be a positive integer") cmd.SetArgs([]string{ fakeKeyName1, fakeKeyName2, @@ -116,7 +122,7 @@ func Test_runShowCmd(t *testing.T) { fmt.Sprintf("--%s=%s", FlagBechPrefix, sdk.PrefixAccount), fmt.Sprintf("--%s=2", flagMultiSigThreshold), }) - require.NoError(t, cmd.Execute()) + require.NoError(t, cmd.ExecuteContext(ctx)) // Now try multisig key - set bech to acc + threshold=2 cmd.SetArgs([]string{ @@ -127,7 +133,7 @@ func Test_runShowCmd(t *testing.T) { fmt.Sprintf("--%s=true", FlagDevice), fmt.Sprintf("--%s=2", flagMultiSigThreshold), }) - require.EqualError(t, cmd.Execute(), "the device flag (-d) can only be used for accounts stored in devices") + require.EqualError(t, cmd.ExecuteContext(ctx), "the device flag (-d) can only be used for accounts stored in devices") cmd.SetArgs([]string{ fakeKeyName1, fakeKeyName2, @@ -137,7 +143,7 @@ func Test_runShowCmd(t *testing.T) { fmt.Sprintf("--%s=true", FlagDevice), fmt.Sprintf("--%s=2", flagMultiSigThreshold), }) - require.EqualError(t, cmd.Execute(), "the device flag (-d) can only be used for accounts") + require.EqualError(t, cmd.ExecuteContext(ctx), "the device flag (-d) can only be used for accounts") cmd.SetArgs([]string{ fakeKeyName1, fakeKeyName2, @@ -148,7 +154,7 @@ func Test_runShowCmd(t *testing.T) { fmt.Sprintf("--%s=2", flagMultiSigThreshold), fmt.Sprintf("--%s=true", FlagPublicKey), }) - require.EqualError(t, cmd.Execute(), "the device flag (-d) can only be used for addresses not pubkeys") + require.EqualError(t, cmd.ExecuteContext(ctx), "the device flag (-d) can only be used for addresses not pubkeys") } func Test_validateMultisigThreshold(t *testing.T) {