Skip to content

Commit

Permalink
feat(client/keys): support display discreetly for keys mnemonic (cosm…
Browse files Browse the repository at this point in the history
  • Loading branch information
Halimao authored Dec 11, 2023
1 parent 8f0d5b1 commit c4d816c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<appd> 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 `<appd> 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.
Expand Down
7 changes: 6 additions & 1 deletion client/keys/mnemonic.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}
31 changes: 30 additions & 1 deletion client/keys/mnemonic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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)
Expand All @@ -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")
}

0 comments on commit c4d816c

Please sign in to comment.