Skip to content

Commit

Permalink
feat: add a new command to list supported algos (#14655)
Browse files Browse the repository at this point in the history
  • Loading branch information
julienrbrt authored Jan 17, 2023
1 parent 9a11740 commit 5233d5e
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 19 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
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
cmd.PersistentFlags().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 @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
},
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 5233d5e

Please sign in to comment.