From 01c2af90b72e183049c440287015219d58b28827 Mon Sep 17 00:00:00 2001 From: Halimao <1065621723@qq.com> Date: Mon, 11 Dec 2023 18:12:28 +0800 Subject: [PATCH 1/2] feat: support display discreetly for keys mnemonic --- client/keys/mnemonic.go | 7 ++++++- client/keys/mnemonic_test.go | 31 ++++++++++++++++++++++++++++++- 2 files changed, 36 insertions(+), 2 deletions(-) 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") +} From 0c608282e6e807777f2338721d7b94df4764657d Mon Sep 17 00:00:00 2001 From: Halimao <1065621723@qq.com> Date: Mon, 11 Dec 2023 18:16:11 +0800 Subject: [PATCH 2/2] add changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1f2c93f1862b..1fef94006685 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -56,6 +56,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. * (telemetry) [#18646] (https://github.com/cosmos/cosmos-sdk/pull/18646) Enable statsd and dogstatsd telemetry sinks * (types) [#18440](https://github.com/cosmos/cosmos-sdk/pull/18440) Add `AmountOfNoValidation` to `sdk.DecCoins`.