From 29de94048a59507954b845c279a675a4a54f8377 Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Tue, 17 Jan 2023 23:24:02 +0100 Subject: [PATCH 1/3] feat: add a new command to list supported algos (#14655) (cherry picked from commit 5233d5e67c740a07e7ae2300f4688988829733c8) # Conflicts: # CHANGELOG.md # simapp/simd/cmd/testnet.go --- CHANGELOG.md | 7 +++++++ 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 | 23 ++++++++++++++++++++--- 10 files changed, 95 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b6c6a269b7ca..2f7afb43fa37 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -39,8 +39,15 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Features +<<<<<<< HEAD * (x/gov) [#15151](https://github.com/cosmos/cosmos-sdk/pull/15151) Add `burn_vote_quorum`, `burn_proposal_deposit_prevote` and `burn_vote_veto` params to allow applications to decide if they would like to burn deposits * (client) [#14509](https://github.com/cosmos/cosmos-sdk/pull/#14509) Added `AddKeyringFlags` function. +======= +* (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. +>>>>>>> 5233d5e67 (feat: add a new command to list supported algos (#14655)) * (x/bank) [#14045](https://github.com/cosmos/cosmos-sdk/pull/14045) Add CLI command `spendable-balances`, which also accepts the flag `--denom`. * (x/slashing, x/staking) [#14363](https://github.com/cosmos/cosmos-sdk/pull/14363) Add the infraction a validator commited type as an argument to a `SlashWithInfractionReason` keeper method. * (client) [#14051](https://github.com/cosmos/cosmos-sdk/pull/14051) Add `--grpc` client option. diff --git a/client/flags/flags.go b/client/flags/flags.go index 3049b71b0211..46dfed9daadc 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 72a5e7cd08f1..9680fc5783f3 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 c42e40665e5e..19602fce42a5 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 202bf48838ae..3fe04f447bd0 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 49ad951f4239..6faf2f462ce2 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 62ce0898b6ae..8d54de5c3d1a 100644 --- a/simapp/simd/cmd/testnet.go +++ b/simapp/simd/cmd/testnet.go @@ -15,6 +15,14 @@ import ( "github.com/cometbft/cometbft/types" tmtime "github.com/cometbft/cometbft/types/time" "github.com/spf13/cobra" +<<<<<<< HEAD +======= + "github.com/spf13/pflag" + tmconfig "github.com/tendermint/tendermint/config" + tmrand "github.com/tendermint/tendermint/libs/rand" + "github.com/tendermint/tendermint/types" + tmtime "github.com/tendermint/tendermint/types/time" +>>>>>>> 5233d5e67 (feat: add a new command to list supported algos (#14655)) "cosmossdk.io/math" @@ -81,7 +89,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 @@ -135,7 +152,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) }, @@ -168,7 +185,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) From 64c0452f7bf77b9b304003f901e07be812e886ef Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Mon, 20 Mar 2023 15:39:43 +0100 Subject: [PATCH 2/3] fix conflicts and follow-up fix from the pr --- CHANGELOG.md | 13 ++++++------- client/keys/add.go | 2 +- simapp/simd/cmd/testnet.go | 9 +-------- 3 files changed, 8 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2f7afb43fa37..5aa142f45c10 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,19 +35,18 @@ Ref: https://keepachangelog.com/en/1.0.0/ # Changelog +## [Unreleased] + +### Features + +* (cli) [#14655](https://github.com/cosmos/cosmos-sdk/pull/14655) Add a new command to list supported algos. + ## [v0.47.0](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.47.0) - 2023-03-14 ### Features -<<<<<<< HEAD * (x/gov) [#15151](https://github.com/cosmos/cosmos-sdk/pull/15151) Add `burn_vote_quorum`, `burn_proposal_deposit_prevote` and `burn_vote_veto` params to allow applications to decide if they would like to burn deposits * (client) [#14509](https://github.com/cosmos/cosmos-sdk/pull/#14509) Added `AddKeyringFlags` function. -======= -* (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. ->>>>>>> 5233d5e67 (feat: add a new command to list supported algos (#14655)) * (x/bank) [#14045](https://github.com/cosmos/cosmos-sdk/pull/14045) Add CLI command `spendable-balances`, which also accepts the flag `--denom`. * (x/slashing, x/staking) [#14363](https://github.com/cosmos/cosmos-sdk/pull/14363) Add the infraction a validator commited type as an argument to a `SlashWithInfractionReason` keeper method. * (client) [#14051](https://github.com/cosmos/cosmos-sdk/pull/14051) Add `--grpc` client option. diff --git a/client/keys/add.go b/client/keys/add.go index 9680fc5783f3..d8c8fc096772 100644 --- a/client/keys/add.go +++ b/client/keys/add.go @@ -81,7 +81,7 @@ Example: 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 { + cmd.Flags().SetNormalizeFunc(func(f *pflag.FlagSet, name string) pflag.NormalizedName { if name == "algo" { name = flags.FlagKeyType } diff --git a/simapp/simd/cmd/testnet.go b/simapp/simd/cmd/testnet.go index 8d54de5c3d1a..58a6223c89aa 100644 --- a/simapp/simd/cmd/testnet.go +++ b/simapp/simd/cmd/testnet.go @@ -15,14 +15,7 @@ import ( "github.com/cometbft/cometbft/types" tmtime "github.com/cometbft/cometbft/types/time" "github.com/spf13/cobra" -<<<<<<< HEAD -======= "github.com/spf13/pflag" - tmconfig "github.com/tendermint/tendermint/config" - tmrand "github.com/tendermint/tendermint/libs/rand" - "github.com/tendermint/tendermint/types" - tmtime "github.com/tendermint/tendermint/types/time" ->>>>>>> 5233d5e67 (feat: add a new command to list supported algos (#14655)) "cosmossdk.io/math" @@ -92,7 +85,7 @@ func addTestnetFlagsToCmd(cmd *cobra.Command) { 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 { + cmd.Flags().SetNormalizeFunc(func(f *pflag.FlagSet, name string) pflag.NormalizedName { if name == "algo" { name = flags.FlagKeyType } From 88e484b52ea17a96dc1f2ec1d3de9e8d04264ed5 Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Mon, 20 Mar 2023 15:40:19 +0100 Subject: [PATCH 3/3] updates --- client/keys/add.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/keys/add.go b/client/keys/add.go index d8c8fc096772..560b29ed3252 100644 --- a/client/keys/add.go +++ b/client/keys/add.go @@ -81,7 +81,7 @@ Example: f.String(flags.FlagKeyType, string(hd.Secp256k1Type), "Key signing algorithm to generate keys for") // support old flags name for backwards compatibility - cmd.Flags().SetNormalizeFunc(func(f *pflag.FlagSet, name string) pflag.NormalizedName { + f.SetNormalizeFunc(func(f *pflag.FlagSet, name string) pflag.NormalizedName { if name == "algo" { name = flags.FlagKeyType }