Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(client/keys): support display discreetly for keys mnemonic #18688

Merged
merged 3 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Halimao marked this conversation as resolved.
Show resolved Hide resolved
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")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the default value and the flag description don't line up. By default, the mnemonic will be displayed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the default value and the flag description don't line up. By default, the mnemonic will be displayed.

Default behaviour shoud be displaying mnemonic on an alternate screen IMO, as this is more safe than displaying directly on current terminal. And if we provide "--indiscreet", this means, "Print mnemonic directly on current terminal"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh yes, you're right. I didn't see the printDiscreetly call. Thanks!

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")
}
Loading