Skip to content

Commit

Permalink
client: fix keys add interactive command (#7193)
Browse files Browse the repository at this point in the history
Fix the command line `keys add` interactive mode
that fails when asked to generate new mnemonic.

Closes: #7048
From:
- #7049
- #7183
Thanks: @puneet2019 @PaddyMc for the patches.
  • Loading branch information
Alessio Treglia authored Aug 31, 2020
1 parent 42c3c57 commit 298fbbc
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
### Bug Fixes

* (types) [\#7084](https://github.com/cosmos/cosmos-sdk/pull/7084) Fix panic when calling `BigInt()` on an uninitialized `Int`.
* (client) [\#7048](https://github.com/cosmos/cosmos-sdk/issues/7048) Fix client `keys add` failure when generating mnemonic in interactive mode.

## [v0.39.1]

Expand Down
20 changes: 12 additions & 8 deletions client/keys/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,21 +216,25 @@ func RunAddCmd(cmd *cobra.Command, args []string, kb keys.Keybase, inBuf *bufio.
}

// Get bip39 mnemonic
var mnemonic string
var bip39Passphrase string
var mnemonic, bip39Passphrase string

if interactive || viper.GetBool(flagRecover) {
bip39Message := "Enter your bip39 mnemonic"
if !viper.GetBool(flagRecover) {
bip39Message = "Enter your bip39 mnemonic, or hit enter to generate one."
recover, _ := cmd.Flags().GetBool(flagRecover)
if recover {
mnemonic, err = input.GetString("Enter your bip39 mnemonic", inBuf)
if err != nil {
return err
}

mnemonic, err = input.GetString(bip39Message, inBuf)
if !bip39.IsMnemonicValid(mnemonic) {
return errors.New("invalid mnemonic")
}
} else if interactive {
mnemonic, err = input.GetString("Enter your bip39 mnemonic, or hit enter to generate one.", inBuf)
if err != nil {
return err
}

if !bip39.IsMnemonicValid(mnemonic) {
if !bip39.IsMnemonicValid(mnemonic) && mnemonic != "" {
return errors.New("invalid mnemonic")
}
}
Expand Down
32 changes: 32 additions & 0 deletions client/keys/add_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keys

import (
"fmt"
"testing"

"github.com/spf13/viper"
Expand Down Expand Up @@ -54,4 +55,35 @@ func Test_runAddCmdBasic(t *testing.T) {
}
err := runAddCmd(cmd, []string{"keyname2"})
assert.NoError(t, err)

// In recovery mode
cmd.SetArgs([]string{
"keyname6",
fmt.Sprintf("--%s=true", flagRecover),
})

// use valid mnemonic and complete recovery key generation successfully
mockIn.Reset("decide praise business actor peasant farm drastic weather extend front hurt later song give verb rhythm worry fun pond reform school tumble august one\n")
require.NoError(t, cmd.Execute())

// use invalid mnemonic and fail recovery key generation
mockIn.Reset("invalid mnemonic\n")
require.Error(t, cmd.Execute())

// In interactive mode
cmd.SetArgs([]string{
"keyname7",
"-i",
fmt.Sprintf("--%s=false", flagRecover),
})

const password = "password1!"

// set password and complete interactive key generation successfully
mockIn.Reset("\n" + password + "\n" + password + "\n")
require.NoError(t, cmd.Execute())

// passwords don't match and fail interactive key generation
mockIn.Reset("\n" + password + "\n" + "fail" + "\n")
require.Error(t, cmd.Execute())
}

0 comments on commit 298fbbc

Please sign in to comment.