diff --git a/CHANGELOG.md b/CHANGELOG.md index d74e51443363..c9577206d408 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -53,6 +53,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Improvements +* (client/keys) [#18687](https://github.com/cosmos/cosmos-sdk/pull/18687) Improve ` keys mnemonic` by displaying mnemonic discreetly on an alternate screen and adding `--indiscreet` option to disable it. * (client/keys) [#18663](https://github.com/cosmos/cosmos-sdk/pull/18663) Improve ` keys add` by displaying mnemonic discreetly on an alternate screen and adding `--indiscreet` option to disable it. * (types) [#18440](https://github.com/cosmos/cosmos-sdk/pull/18440) Add `AmountOfNoValidation` to `sdk.DecCoins`. * (client) [#17503](https://github.com/cosmos/cosmos-sdk/pull/17503) Add `client.Context{}.WithAddressCodec`, `WithValidatorAddressCodec`, `WithConsensusAddressCodec` to provide address codecs to the client context. See the [UPGRADING.md](./UPGRADING.md) for more details. diff --git a/client/keys/mnemonic.go b/client/keys/mnemonic.go index ea7f9638d9be..50d0b74c09c2 100644 --- a/client/keys/mnemonic.go +++ b/client/keys/mnemonic.go @@ -8,6 +8,7 @@ import ( "github.com/cosmos/go-bip39" "github.com/spf13/cobra" + "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/input" ) @@ -64,12 +65,16 @@ func MnemonicKeyCommand() *cobra.Command { if err != nil { return err } - + indiscreet, _ := cmd.Flags().GetBool(flagIndiscreet) + if !indiscreet { + return printDiscreetly(client.GetClientContextFromCmd(cmd), cmd.ErrOrStderr(), "**Important** write this mnemonic phrase in a safe place. Do not share it to anyone.", mnemonic) + } cmd.Println(mnemonic) return nil }, } cmd.Flags().Bool(flagUserEntropy, false, "Prompt the user to supply their own entropy, instead of relying on the system") + cmd.Flags().Bool(flagIndiscreet, false, "Print mnemonic directly on current terminal") return cmd } diff --git a/client/keys/mnemonic_test.go b/client/keys/mnemonic_test.go index c986e6230b5d..3aa2d34669b2 100644 --- a/client/keys/mnemonic_test.go +++ b/client/keys/mnemonic_test.go @@ -21,7 +21,7 @@ func Test_RunMnemonicCmdUser(t *testing.T) { cmd := MnemonicKeyCommand() _ = testutil.ApplyMockIODiscardOutErr(cmd) - cmd.SetArgs([]string{fmt.Sprintf("--%s=1", flagUserEntropy)}) + cmd.SetArgs([]string{fmt.Sprintf("--%s", flagUserEntropy), fmt.Sprintf("--%s", flagIndiscreet)}) err := cmd.Execute() require.Error(t, err) require.Equal(t, "EOF", err.Error()) @@ -35,11 +35,14 @@ func Test_RunMnemonicCmdUser(t *testing.T) { "256-bits is 43 characters in Base-64, and 100 in Base-6. You entered 3, and probably want more", err.Error()) + mockIn, mockOut := testutil.ApplyMockIO(cmd) // Now provide "good" entropy :) fakeEntropy := strings.Repeat(":)", 40) + "\ny\n" // entropy + accept count mockIn.Reset(fakeEntropy) require.NoError(t, cmd.Execute()) + require.Equal(t, "volcano hungry midnight divorce post ship bicycle fitness hospital critic protect ring trim alien there safe fine subway style impulse identify right improve print\n", mockOut.String()) + mockIn = testutil.ApplyMockIODiscardOutErr(cmd) // Now provide "good" entropy but no answer fakeEntropy = strings.Repeat(":)", 40) + "\n" // entropy + accept count mockIn.Reset(fakeEntropy) @@ -50,3 +53,29 @@ func Test_RunMnemonicCmdUser(t *testing.T) { mockIn.Reset(fakeEntropy) require.NoError(t, cmd.Execute()) } + +func Test_RunMnemonicCmdUserDiscreetly(t *testing.T) { + cmd := MnemonicKeyCommand() + _ = testutil.ApplyMockIODiscardOutErr(cmd) + + cmd.SetArgs([]string{fmt.Sprintf("--%s", flagUserEntropy)}) + err := cmd.Execute() + require.Error(t, err) + require.Equal(t, "EOF", err.Error()) + + // Try again + mockIn := testutil.ApplyMockIODiscardOutErr(cmd) + mockIn.Reset("Hi!\n") + err = cmd.Execute() + require.Error(t, err) + require.Equal(t, + "256-bits is 43 characters in Base-64, and 100 in Base-6. You entered 3, and probably want more", + err.Error()) + + mockIn, mockOut := testutil.ApplyMockIO(cmd) + // Now provide "good" entropy :) + fakeEntropy := strings.Repeat(":)", 40) + "\ny\n" // entropy + accept count + mockIn.Reset(fakeEntropy) + require.NoError(t, cmd.Execute()) + require.Contains(t, mockOut.String(), "volcano hungry midnight divorce post ship bicycle fitness hospital critic protect ring trim alien there safe fine subway style impulse identify right improve print") +}