feat: add a new command to list supported algos (backport #14655) (#15468)

Co-authored-by: Julien Robert <julien@rbrt.fr>
This commit is contained in:
mergify[bot] 2023-03-20 15:57:46 +00:00 committed by GitHub
parent e350809209
commit cbef7f32f0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 82 additions and 19 deletions

View File

@ -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/genutil,cli) [#15147](https://github.com/cosmos/cosmos-sdk/pull/15147) Add `--initial-height` flag to cli init cmd to provide `genesis.json` with user defined initial block height.
### Improvements

View File

@ -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"

View File

@ -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
f.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

View File

@ -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),
})

View File

@ -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),
})

View File

@ -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
},
}
}

View File

@ -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)))
}

View File

@ -43,6 +43,7 @@ The pass backend requires GnuPG: https://gnupg.org/
ExportKeyCommand(),
ImportKeyCommand(),
ListKeysCmd(),
ListKeyTypesCmd(),
ShowKeysCmd(),
DeleteKeyCommand(),
RenameKeyCommand(),

View File

@ -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()))
}

View File

@ -15,6 +15,7 @@ import (
"github.com/cometbft/cometbft/types"
tmtime "github.com/cometbft/cometbft/types/time"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"cosmossdk.io/math"
@ -81,7 +82,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.Flags().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 +145,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 +178,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)