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

This commit is contained in:
Halimao 2023-12-12 04:54:03 +08:00 committed by GitHub
parent 8f0d5b15f0
commit c4d816c7cd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 2 deletions

View File

@ -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.

View File

@ -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
}

View File

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