Skip to content

Commit

Permalink
refactor(client/keys): hide inputting when keys add -i ask for bip3…
Browse files Browse the repository at this point in the history
…9 passphrase (#18743)
  • Loading branch information
Halimao committed Dec 15, 2023
1 parent b80be01 commit 6cca5e3
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 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) [#18743](https://github.com/cosmos/cosmos-sdk/pull/18743) Improve `<appd> keys add -i` by hiding inputting of bip39 passphrase.
* (client/keys) [#18703](https://github.com/cosmos/cosmos-sdk/pull/18703) Improve `<appd> keys add` and `<appd> 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.
Expand Down
15 changes: 15 additions & 0 deletions client/input/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions client/keys/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 4 additions & 0 deletions client/keys/add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit 6cca5e3

Please sign in to comment.