diff --git a/CHANGELOG.md b/CHANGELOG.md index b5b90ecd2ba4..9c47a396f27f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -53,6 +53,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Improvements +* (client/keys) [#18745](https://github.com/cosmos/cosmos-sdk/pull/18745) Improve ` keys export` and ` keys mnemonic` by adding --yes option to skip interactive confirmation. * (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. diff --git a/client/keys/export.go b/client/keys/export.go index 0afe6db84133..9abdb684ec11 100644 --- a/client/keys/export.go +++ b/client/keys/export.go @@ -65,15 +65,19 @@ and export your keys in ASCII-armored encrypted format.`, cmd.Flags().Bool(flagUnarmoredHex, false, "Export unarmored hex privkey. Requires --unsafe.") cmd.Flags().Bool(flagUnsafe, false, "Enable unsafe operations. This flag must be switched on along with all unsafe operation-specific options.") cmd.Flags().Bool(flagIndiscreet, false, "Print unarmored hex privkey directly on current terminal (only valid when --unarmored-hex is true)") + cmd.Flags().BoolP(flagYes, "y", false, "Skip confirmation prompt when export unarmored hex privkey") return cmd } func exportUnsafeUnarmored(ctx client.Context, cmd *cobra.Command, uid string, buf *bufio.Reader) error { - if yes, err := input.GetConfirmation("WARNING: The private key will be exported as an unarmored hexadecimal string. USE AT YOUR OWN RISK. Continue?", buf, cmd.ErrOrStderr()); err != nil { - return err - } else if !yes { - return nil + // confirm export unarmored hex privkey, unless -y is passed + if skip, _ := cmd.Flags().GetBool(flagYes); !skip { + if yes, err := input.GetConfirmation("WARNING: The private key will be exported as an unarmored hexadecimal string. USE AT YOUR OWN RISK. Continue?", buf, cmd.ErrOrStderr()); err != nil { + return err + } else if !yes { + return nil + } } hexPrivKey, err := unsafeExportPrivKeyHex(ctx.Keyring.(unsafeExporter), uid) diff --git a/client/keys/export_test.go b/client/keys/export_test.go index 81036feafeae..db49eb5b8c37 100644 --- a/client/keys/export_test.go +++ b/client/keys/export_test.go @@ -49,6 +49,14 @@ func Test_runExportCmd(t *testing.T) { mustFail: true, expectedOutput: "", }, + { + name: "--unsafe --unarmored-hex --yes success", + keyringBackend: keyring.BackendTest, + extraArgs: []string{"--unsafe", "--unarmored-hex", "--yes"}, + userInput: "", + mustFail: false, + expectedOutputContain: "2485e33678db4175dc0ecef2d6e1fc493d4a0d7f7ce83324b6ed70afe77f3485\n", + }, { name: "--unsafe --unarmored-hex success", keyringBackend: keyring.BackendTest, diff --git a/client/keys/mnemonic.go b/client/keys/mnemonic.go index 50d0b74c09c2..245e969432a1 100644 --- a/client/keys/mnemonic.go +++ b/client/keys/mnemonic.go @@ -40,13 +40,15 @@ func MnemonicKeyCommand() *cobra.Command { return fmt.Errorf("256-bits is 43 characters in Base-64, and 100 in Base-6. You entered %v, and probably want more", len(inputEntropy)) } - conf, err := input.GetConfirmation(fmt.Sprintf("> Input length: %d", len(inputEntropy)), buf, cmd.ErrOrStderr()) - if err != nil { - return err - } + if skip, _ := cmd.Flags().GetBool(flagYes); !skip { + yes, err := input.GetConfirmation(fmt.Sprintf("> Input length: %d", len(inputEntropy)), buf, cmd.ErrOrStderr()) + if err != nil { + return err + } - if !conf { - return nil + if !yes { + return nil + } } // hash input entropy to get entropy seed @@ -76,5 +78,6 @@ func MnemonicKeyCommand() *cobra.Command { 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") + cmd.Flags().BoolP(flagYes, "y", false, "Skip confirmation prompt when check input entropy length") return cmd } diff --git a/client/keys/mnemonic_test.go b/client/keys/mnemonic_test.go index 3aa2d34669b2..ed46cf024712 100644 --- a/client/keys/mnemonic_test.go +++ b/client/keys/mnemonic_test.go @@ -52,6 +52,13 @@ func Test_RunMnemonicCmdUser(t *testing.T) { fakeEntropy = strings.Repeat(":)", 40) + "\nn\n" // entropy + accept count mockIn.Reset(fakeEntropy) require.NoError(t, cmd.Execute()) + + // test for skip confirmation + cmd.SetArgs([]string{fmt.Sprintf("--%s", flagUserEntropy), fmt.Sprintf("--%s", flagIndiscreet), fmt.Sprintf("--%s", flagYes)}) + fakeEntropy = strings.Repeat(":)", 40) + "\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()) } func Test_RunMnemonicCmdUserDiscreetly(t *testing.T) {