From 6cca5e37fd8f97087193031b18f5e04516e5e843 Mon Sep 17 00:00:00 2001 From: Halimao <1065621723@qq.com> Date: Fri, 15 Dec 2023 19:24:49 +0800 Subject: [PATCH] refactor(client/keys): hide inputting when `keys add -i` ask for bip39 passphrase (#18743) --- CHANGELOG.md | 1 + client/input/input.go | 15 +++++++++++++++ client/keys/add.go | 6 +++--- client/keys/add_test.go | 4 ++++ 4 files changed, 23 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e11c1e31e5..b5b90ecd2b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -53,6 +53,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Improvements +* (client/keys) [#18743](https://github.com/cosmos/cosmos-sdk/pull/18743) Improve ` keys add -i` by hiding inputting of bip39 passphrase. * (client/keys) [#18703](https://github.com/cosmos/cosmos-sdk/pull/18703) Improve ` keys add` and ` keys show` by checking whether there are duplicate keys in the multisig case. * (x/gov) [#18707](https://github.com/cosmos/cosmos-sdk/pull/18707) Improve genesis validation. * (x/bank) [#18636](https://github.com/cosmos/cosmos-sdk/pull/18636) `SendCoinsFromModuleToAccount`, `SendCoinsFromModuleToModule`, `SendCoinsFromAccountToModule`, `DelegateCoinsFromAccountToModule`, `UndelegateCoinsFromModuleToAccount`, `MintCoins` and `BurnCoins` methods now returns an error instead of panicking if any module accounts does not exist or unauthorized. diff --git a/client/input/input.go b/client/input/input.go index 31daa99be3..58b2d206df 100644 --- a/client/input/input.go +++ b/client/input/input.go @@ -77,6 +77,21 @@ func GetString(prompt string, buf *bufio.Reader) (string, error) { return strings.TrimSpace(out), nil } +// GetSecretString returns the secret string output of a given reader. +func GetSecretString(prompt string, buf *bufio.Reader) (secret string, err error) { + if inputIsTty() { + secret, err = speakeasy.FAsk(os.Stderr, prompt) + } else { + secret, err = readLineFromBuf(buf) + } + + if err != nil { + return "", err + } + + return secret, nil +} + // inputIsTty returns true iff we have an interactive prompt, // where we can disable echo and request to repeat the password. // If false, we can optimize for piped input from another command diff --git a/client/keys/add.go b/client/keys/add.go index a30a2d865a..13dd35ec47 100644 --- a/client/keys/add.go +++ b/client/keys/add.go @@ -313,16 +313,16 @@ func runAddCmd(ctx client.Context, cmd *cobra.Command, args []string, inBuf *buf // override bip39 passphrase if interactive { - bip39Passphrase, err = input.GetString( + bip39Passphrase, err = input.GetSecretString( "Enter your bip39 passphrase. This is combined with the mnemonic to derive the seed. "+ - "Most users should just hit enter to use the default, \"\"", inBuf) + "Most users should just hit enter to use the default, \"\"\n", inBuf) if err != nil { return err } // if they use one, make them re-enter it if len(bip39Passphrase) != 0 { - p2, err := input.GetString("Repeat the passphrase:", inBuf) + p2, err := input.GetSecretString("Repeat the passphrase:\n", inBuf) if err != nil { return err } diff --git a/client/keys/add_test.go b/client/keys/add_test.go index 53ce3af279..6dff014b22 100644 --- a/client/keys/add_test.go +++ b/client/keys/add_test.go @@ -118,6 +118,10 @@ func Test_runAddCmdBasic(t *testing.T) { const password = "password1!" + // set password default interactive key generation successfully + mockIn.Reset("\n\n") + require.NoError(t, cmd.ExecuteContext(ctx)) + // set password and complete interactive key generation successfully mockIn.Reset("\n" + password + "\n" + password + "\n") require.NoError(t, cmd.ExecuteContext(ctx))