From 5233d5e67c740a07e7ae2300f4688988829733c8 Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Tue, 17 Jan 2023 23:24:02 +0100 Subject: [PATCH] feat: add a new command to list supported algos (#14655) --- CHANGELOG.md | 1 + client/flags/flags.go | 2 +- client/keys/add.go | 14 ++++++++++++-- client/keys/add_ledger_test.go | 4 ++-- client/keys/add_test.go | 10 +++++----- client/keys/list.go | 20 ++++++++++++++++++++ client/keys/list_test.go | 27 ++++++++++++++++++++++++--- client/keys/root.go | 1 + client/keys/root_test.go | 6 +++--- simapp/simd/cmd/testnet.go | 16 +++++++++++++--- 10 files changed, 82 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 37d3a871613e..faa7b5de251a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -39,6 +39,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Features +* (cli) [#14655](https://github.com/cosmos/cosmos-sdk/pull/14655) Add a new command to list supported algos. * (x/crisis) [#14588](https://github.com/cosmos/cosmos-sdk/pull/14588) Use CacheContext() in AssertInvariants() * (client) [#14342](https://github.com/cosmos/cosmos-sdk/pull/14342) Add `simd config` command is now a sub-command, for setting, getting and migrating Cosmos SDK configuration files. * (query) [#14468](https://github.com/cosmos/cosmos-sdk/pull/14468) Implement pagination for collections. diff --git a/client/flags/flags.go b/client/flags/flags.go index 1e209f47be4e..4faae4ef8737 100644 --- a/client/flags/flags.go +++ b/client/flags/flags.go @@ -73,7 +73,7 @@ const ( FlagOffset = "offset" FlagCountTotal = "count-total" FlagTimeoutHeight = "timeout-height" - FlagKeyAlgorithm = "algo" + FlagKeyType = "key-type" FlagFeePayer = "fee-payer" FlagFeeGranter = "fee-granter" FlagReverse = "reverse" diff --git a/client/keys/add.go b/client/keys/add.go index dc43a1882f9c..18f5faca7ef4 100644 --- a/client/keys/add.go +++ b/client/keys/add.go @@ -10,6 +10,7 @@ import ( "github.com/cosmos/go-bip39" "github.com/spf13/cobra" + "github.com/spf13/pflag" "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/flags" @@ -77,7 +78,16 @@ Example: f.Uint32(flagCoinType, sdk.GetConfig().GetCoinType(), "coin type number for HD derivation") f.Uint32(flagAccount, 0, "Account number for HD derivation (less than equal 2147483647)") f.Uint32(flagIndex, 0, "Address index number for HD derivation (less than equal 2147483647)") - f.String(flags.FlagKeyAlgorithm, string(hd.Secp256k1Type), "Key signing algorithm to generate keys for") + f.String(flags.FlagKeyType, string(hd.Secp256k1Type), "Key signing algorithm to generate keys for") + + // support old flags name for backwards compatibility + cmd.PersistentFlags().SetNormalizeFunc(func(f *pflag.FlagSet, name string) pflag.NormalizedName { + if name == "algo" { + name = flags.FlagKeyType + } + + return pflag.NormalizedName(name) + }) return cmd } @@ -113,7 +123,7 @@ func runAddCmd(ctx client.Context, cmd *cobra.Command, args []string, inBuf *buf outputFormat := ctx.OutputFormat keyringAlgos, _ := kb.SupportedAlgorithms() - algoStr, _ := cmd.Flags().GetString(flags.FlagKeyAlgorithm) + algoStr, _ := cmd.Flags().GetString(flags.FlagKeyType) algo, err := keyring.NewSigningAlgoFromString(algoStr, keyringAlgos) if err != nil { return err diff --git a/client/keys/add_ledger_test.go b/client/keys/add_ledger_test.go index cf900785dbc0..c6d4ecc3025b 100644 --- a/client/keys/add_ledger_test.go +++ b/client/keys/add_ledger_test.go @@ -55,7 +55,7 @@ func Test_runAddCmdLedgerWithCustomCoinType(t *testing.T) { fmt.Sprintf("--%s=0", flagIndex), fmt.Sprintf("--%s=330", flagCoinType), fmt.Sprintf("--%s=%s", cli.OutputFlag, OutputFormatText), - fmt.Sprintf("--%s=%s", flags.FlagKeyAlgorithm, hd.Secp256k1Type), + fmt.Sprintf("--%s=%s", flags.FlagKeyType, hd.Secp256k1Type), fmt.Sprintf("--%s=%s", flags.FlagKeyringBackend, keyring.BackendTest), }) @@ -105,7 +105,7 @@ func Test_runAddCmdLedger(t *testing.T) { "keyname1", fmt.Sprintf("--%s=true", flags.FlagUseLedger), fmt.Sprintf("--%s=%s", cli.OutputFlag, OutputFormatText), - fmt.Sprintf("--%s=%s", flags.FlagKeyAlgorithm, hd.Secp256k1Type), + fmt.Sprintf("--%s=%s", flags.FlagKeyType, hd.Secp256k1Type), fmt.Sprintf("--%s=%d", flagCoinType, sdk.CoinType), fmt.Sprintf("--%s=%s", flags.FlagKeyringBackend, keyring.BackendTest), }) diff --git a/client/keys/add_test.go b/client/keys/add_test.go index 5b8bf8229310..fc9d17f9147a 100644 --- a/client/keys/add_test.go +++ b/client/keys/add_test.go @@ -45,7 +45,7 @@ func Test_runAddCmdBasic(t *testing.T) { "keyname1", fmt.Sprintf("--%s=%s", flags.FlagHome, kbHome), fmt.Sprintf("--%s=%s", cli.OutputFlag, OutputFormatText), - fmt.Sprintf("--%s=%s", flags.FlagKeyAlgorithm, hd.Secp256k1Type), + fmt.Sprintf("--%s=%s", flags.FlagKeyType, hd.Secp256k1Type), fmt.Sprintf("--%s=%s", flags.FlagKeyringBackend, keyring.BackendTest), }) mockIn.Reset("y\n") @@ -58,7 +58,7 @@ func Test_runAddCmdBasic(t *testing.T) { "keyname2", fmt.Sprintf("--%s=%s", flags.FlagHome, kbHome), fmt.Sprintf("--%s=%s", cli.OutputFlag, OutputFormatText), - fmt.Sprintf("--%s=%s", flags.FlagKeyAlgorithm, hd.Secp256k1Type), + fmt.Sprintf("--%s=%s", flags.FlagKeyType, hd.Secp256k1Type), fmt.Sprintf("--%s=%s", flags.FlagKeyringBackend, keyring.BackendTest), }) @@ -72,7 +72,7 @@ func Test_runAddCmdBasic(t *testing.T) { "keyname4", fmt.Sprintf("--%s=%s", flags.FlagHome, kbHome), fmt.Sprintf("--%s=%s", cli.OutputFlag, OutputFormatText), - fmt.Sprintf("--%s=%s", flags.FlagKeyAlgorithm, hd.Secp256k1Type), + fmt.Sprintf("--%s=%s", flags.FlagKeyType, hd.Secp256k1Type), fmt.Sprintf("--%s=%s", flags.FlagKeyringBackend, keyring.BackendTest), }) @@ -84,7 +84,7 @@ func Test_runAddCmdBasic(t *testing.T) { fmt.Sprintf("--%s=%s", flags.FlagHome, kbHome), fmt.Sprintf("--%s=true", flags.FlagDryRun), fmt.Sprintf("--%s=%s", cli.OutputFlag, OutputFormatText), - fmt.Sprintf("--%s=%s", flags.FlagKeyAlgorithm, hd.Secp256k1Type), + fmt.Sprintf("--%s=%s", flags.FlagKeyType, hd.Secp256k1Type), }) require.NoError(t, cmd.ExecuteContext(ctx)) @@ -247,7 +247,7 @@ func TestAddRecoverFileBackend(t *testing.T) { "keyname1", fmt.Sprintf("--%s=%s", flags.FlagHome, kbHome), fmt.Sprintf("--%s=%s", cli.OutputFlag, OutputFormatText), - fmt.Sprintf("--%s=%s", flags.FlagKeyAlgorithm, hd.Secp256k1Type), + fmt.Sprintf("--%s=%s", flags.FlagKeyType, hd.Secp256k1Type), fmt.Sprintf("--%s=%s", flags.FlagKeyringBackend, keyring.BackendFile), fmt.Sprintf("--%s", flagRecover), }) diff --git a/client/keys/list.go b/client/keys/list.go index 7d47c8896b26..241c04d8722f 100644 --- a/client/keys/list.go +++ b/client/keys/list.go @@ -48,3 +48,23 @@ func runListCmd(cmd *cobra.Command, _ []string) error { return nil } + +// ListKeyTypesCmd lists all key types. +func ListKeyTypesCmd() *cobra.Command { + return &cobra.Command{ + Use: "list-key-types", + Short: "List all key types", + Long: `Return a list of all supported key types (also known as algos)`, + RunE: func(cmd *cobra.Command, args []string) error { + clientCtx, err := client.GetClientQueryContext(cmd) + if err != nil { + return err + } + + cmd.Println("Supported key types/algos:") + keyring, _ := clientCtx.Keyring.SupportedAlgorithms() + cmd.Printf("%+q\n", keyring) + return nil + }, + } +} diff --git a/client/keys/list_test.go b/client/keys/list_test.go index ec833b8d8fdb..22e33399cfa4 100644 --- a/client/keys/list_test.go +++ b/client/keys/list_test.go @@ -3,9 +3,10 @@ package keys import ( "context" "fmt" + "strings" "testing" - "github.com/stretchr/testify/require" + "gotest.tools/v3/assert" "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/flags" @@ -13,6 +14,7 @@ import ( "github.com/cosmos/cosmos-sdk/crypto/hd" "github.com/cosmos/cosmos-sdk/crypto/keyring" "github.com/cosmos/cosmos-sdk/testutil" + clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli" "github.com/cosmos/cosmos-sdk/testutil/testdata" sdk "github.com/cosmos/cosmos-sdk/types" ) @@ -37,14 +39,14 @@ func Test_runListCmd(t *testing.T) { mockIn := testutil.ApplyMockIODiscardOutErr(cmd) cdc := clienttestutil.MakeTestCodec(t) kb, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendTest, kbHome2, mockIn, cdc) - require.NoError(t, err) + assert.NilError(t, err) clientCtx := client.Context{}.WithKeyring(kb) ctx := context.WithValue(context.Background(), client.ClientContextKey, &clientCtx) path := "" // sdk.GetConfig().GetFullBIP44Path() _, err = kb.NewAccount("something", testdata.TestMnemonic, "", path, hd.Secp256k1) - require.NoError(t, err) + assert.NilError(t, err) t.Cleanup(cleanupKeys(t, kb, "something")) @@ -79,3 +81,22 @@ func Test_runListCmd(t *testing.T) { }) } } + +func Test_runListKeyTypeCmd(t *testing.T) { + cmd := ListKeyTypesCmd() + + cdc := clienttestutil.MakeTestCodec(t) + kbHome := t.TempDir() + mockIn := testutil.ApplyMockIODiscardOutErr(cmd) + + kb, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendTest, kbHome, mockIn, cdc) + assert.NilError(t, err) + + clientCtx := client.Context{}. + WithKeyringDir(kbHome). + WithKeyring(kb) + + out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, []string{}) + assert.NilError(t, err) + assert.Assert(t, strings.Contains(out.String(), string(hd.Secp256k1Type))) +} diff --git a/client/keys/root.go b/client/keys/root.go index 93eb20ddd0c0..0bcbb909f3ea 100644 --- a/client/keys/root.go +++ b/client/keys/root.go @@ -43,6 +43,7 @@ The pass backend requires GnuPG: https://gnupg.org/ ExportKeyCommand(), ImportKeyCommand(), ListKeysCmd(), + ListKeyTypesCmd(), ShowKeysCmd(), DeleteKeyCommand(), RenameKeyCommand(), diff --git a/client/keys/root_test.go b/client/keys/root_test.go index f66ae9265de6..20b3f1a23472 100644 --- a/client/keys/root_test.go +++ b/client/keys/root_test.go @@ -3,13 +3,13 @@ package keys import ( "testing" - "github.com/stretchr/testify/assert" + "gotest.tools/v3/assert" ) func TestCommands(t *testing.T) { rootCommands := Commands("home") - assert.NotNil(t, rootCommands) + assert.Assert(t, rootCommands != nil) // Commands are registered - assert.Equal(t, 10, len(rootCommands.Commands())) + assert.Equal(t, 11, len(rootCommands.Commands())) } diff --git a/simapp/simd/cmd/testnet.go b/simapp/simd/cmd/testnet.go index 9f5cb2e9bc6a..e880fa2ec24e 100644 --- a/simapp/simd/cmd/testnet.go +++ b/simapp/simd/cmd/testnet.go @@ -10,6 +10,7 @@ import ( "path/filepath" "github.com/spf13/cobra" + "github.com/spf13/pflag" tmconfig "github.com/tendermint/tendermint/config" tmrand "github.com/tendermint/tendermint/libs/rand" "github.com/tendermint/tendermint/types" @@ -80,7 +81,16 @@ func addTestnetFlagsToCmd(cmd *cobra.Command) { cmd.Flags().StringP(flagOutputDir, "o", "./.testnets", "Directory to store initialization data for the testnet") cmd.Flags().String(flags.FlagChainID, "", "genesis file chain-id, if left blank will be randomly created") cmd.Flags().String(server.FlagMinGasPrices, fmt.Sprintf("0.000006%s", sdk.DefaultBondDenom), "Minimum gas prices to accept for transactions; All fees in a tx must meet this minimum (e.g. 0.01photino,0.001stake)") - cmd.Flags().String(flags.FlagKeyAlgorithm, string(hd.Secp256k1Type), "Key signing algorithm to generate keys for") + cmd.Flags().String(flags.FlagKeyType, string(hd.Secp256k1Type), "Key signing algorithm to generate keys for") + + // support old flags name for backwards compatibility + cmd.PersistentFlags().SetNormalizeFunc(func(f *pflag.FlagSet, name string) pflag.NormalizedName { + if name == "algo" { + name = flags.FlagKeyType + } + + return pflag.NormalizedName(name) + }) } // NewTestnetCmd creates a root testnet command with subcommands to run an in-process testnet or initialize @@ -134,7 +144,7 @@ Example: args.nodeDaemonHome, _ = cmd.Flags().GetString(flagNodeDaemonHome) args.startingIPAddress, _ = cmd.Flags().GetString(flagStartingIPAddress) args.numValidators, _ = cmd.Flags().GetInt(flagNumValidators) - args.algo, _ = cmd.Flags().GetString(flags.FlagKeyAlgorithm) + args.algo, _ = cmd.Flags().GetString(flags.FlagKeyType) return initTestnetFiles(clientCtx, cmd, config, mbm, genBalIterator, args) }, @@ -167,7 +177,7 @@ Example: args.chainID, _ = cmd.Flags().GetString(flags.FlagChainID) args.minGasPrices, _ = cmd.Flags().GetString(server.FlagMinGasPrices) args.numValidators, _ = cmd.Flags().GetInt(flagNumValidators) - args.algo, _ = cmd.Flags().GetString(flags.FlagKeyAlgorithm) + args.algo, _ = cmd.Flags().GetString(flags.FlagKeyType) args.enableLogging, _ = cmd.Flags().GetBool(flagEnableLogging) args.rpcAddress, _ = cmd.Flags().GetString(flagRPCAddress) args.apiAddress, _ = cmd.Flags().GetString(flagAPIAddress)