Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 4 commits into from
Mar 20, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ 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
Expand Down
2 changes: 1 addition & 1 deletion client/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
14 changes: 12 additions & 2 deletions client/keys/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions client/keys/add_ledger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
})

Expand Down Expand Up @@ -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),
})
Expand Down
10 changes: 5 additions & 5 deletions client/keys/add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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),
})

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

Expand All @@ -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))
Expand Down Expand Up @@ -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),
})
Expand Down
20 changes: 20 additions & 0 deletions client/keys/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
}
}
27 changes: 24 additions & 3 deletions client/keys/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@ 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"
clienttestutil "github.com/cosmos/cosmos-sdk/client/testutil"
"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"
)
Expand All @@ -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"))

Expand Down Expand Up @@ -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)))
}
1 change: 1 addition & 0 deletions client/keys/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ The pass backend requires GnuPG: https://gnupg.org/
ExportKeyCommand(),
ImportKeyCommand(),
ListKeysCmd(),
ListKeyTypesCmd(),
ShowKeysCmd(),
DeleteKeyCommand(),
RenameKeyCommand(),
Expand Down
6 changes: 3 additions & 3 deletions client/keys/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
}
16 changes: 13 additions & 3 deletions simapp/simd/cmd/testnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

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