refactor(client): use address codec (#17503)
This commit is contained in:
+12
-7
@@ -184,7 +184,7 @@ func runAddCmd(ctx client.Context, cmd *cobra.Command, args []string, inBuf *buf
|
||||
return err
|
||||
}
|
||||
|
||||
return printCreate(cmd, k, false, "", outputFormat)
|
||||
return printCreate(ctx, cmd, k, false, "", outputFormat)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -200,7 +200,7 @@ func runAddCmd(ctx client.Context, cmd *cobra.Command, args []string, inBuf *buf
|
||||
return err
|
||||
}
|
||||
|
||||
return printCreate(cmd, k, false, "", outputFormat)
|
||||
return printCreate(ctx, cmd, k, false, "", outputFormat)
|
||||
}
|
||||
|
||||
coinType, _ := cmd.Flags().GetUint32(flagCoinType)
|
||||
@@ -223,7 +223,7 @@ func runAddCmd(ctx client.Context, cmd *cobra.Command, args []string, inBuf *buf
|
||||
return err
|
||||
}
|
||||
|
||||
return printCreate(cmd, k, false, "", outputFormat)
|
||||
return printCreate(ctx, cmd, k, false, "", outputFormat)
|
||||
}
|
||||
|
||||
// Get bip39 mnemonic
|
||||
@@ -297,14 +297,19 @@ func runAddCmd(ctx client.Context, cmd *cobra.Command, args []string, inBuf *buf
|
||||
mnemonic = ""
|
||||
}
|
||||
|
||||
return printCreate(cmd, k, showMnemonic, mnemonic, outputFormat)
|
||||
return printCreate(ctx, cmd, k, showMnemonic, mnemonic, outputFormat)
|
||||
}
|
||||
|
||||
func printCreate(cmd *cobra.Command, k *keyring.Record, showMnemonic bool, mnemonic, outputFormat string) error {
|
||||
func printCreate(ctx client.Context, cmd *cobra.Command, k *keyring.Record, showMnemonic bool, mnemonic, outputFormat string) error {
|
||||
switch outputFormat {
|
||||
case flags.OutputFormatText:
|
||||
cmd.PrintErrln()
|
||||
if err := printKeyringRecord(cmd.OutOrStdout(), k, MkAccKeyOutput, outputFormat); err != nil {
|
||||
ko, err := MkAccKeyOutput(k, ctx.AddressCodec)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := printKeyringRecord(cmd.OutOrStdout(), ko, outputFormat); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -315,7 +320,7 @@ func printCreate(cmd *cobra.Command, k *keyring.Record, showMnemonic bool, mnemo
|
||||
}
|
||||
}
|
||||
case flags.OutputFormatJSON:
|
||||
out, err := MkAccKeyOutput(k)
|
||||
out, err := MkAccKeyOutput(k, ctx.AddressCodec)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ import (
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/client"
|
||||
"github.com/cosmos/cosmos-sdk/client/flags"
|
||||
addresscodec "github.com/cosmos/cosmos-sdk/codec/address"
|
||||
"github.com/cosmos/cosmos-sdk/crypto/hd"
|
||||
"github.com/cosmos/cosmos-sdk/crypto/keyring"
|
||||
"github.com/cosmos/cosmos-sdk/testutil"
|
||||
@@ -44,7 +45,13 @@ func Test_runAddCmdLedgerWithCustomCoinType(t *testing.T) {
|
||||
kbHome := t.TempDir()
|
||||
|
||||
cdc := moduletestutil.MakeTestEncodingConfig().Codec
|
||||
clientCtx := client.Context{}.WithKeyringDir(kbHome).WithCodec(cdc)
|
||||
clientCtx := client.Context{}.
|
||||
WithKeyringDir(kbHome).
|
||||
WithCodec(cdc).
|
||||
WithAddressCodec(addresscodec.NewBech32Codec("cosmos")).
|
||||
WithValidatorAddressCodec(addresscodec.NewBech32Codec("cosmosvaloper")).
|
||||
WithConsensusAddressCodec(addresscodec.NewBech32Codec("cosmosvalcons"))
|
||||
|
||||
ctx := context.WithValue(context.Background(), client.ClientContextKey, &clientCtx)
|
||||
|
||||
cmd.SetArgs([]string{
|
||||
@@ -97,7 +104,13 @@ func Test_runAddCmdLedger(t *testing.T) {
|
||||
kbHome := t.TempDir()
|
||||
cdc := moduletestutil.MakeTestEncodingConfig().Codec
|
||||
|
||||
clientCtx := client.Context{}.WithKeyringDir(kbHome).WithCodec(cdc)
|
||||
clientCtx := client.Context{}.
|
||||
WithKeyringDir(kbHome).
|
||||
WithCodec(cdc).
|
||||
WithAddressCodec(addresscodec.NewBech32Codec("cosmos")).
|
||||
WithValidatorAddressCodec(addresscodec.NewBech32Codec("cosmosvaloper")).
|
||||
WithConsensusAddressCodec(addresscodec.NewBech32Codec("cosmosvalcons"))
|
||||
|
||||
ctx := context.WithValue(context.Background(), client.ClientContextKey, &clientCtx)
|
||||
|
||||
cmd.SetArgs([]string{
|
||||
@@ -176,7 +189,10 @@ func Test_runAddCmdLedgerDryRun(t *testing.T) {
|
||||
clientCtx := client.Context{}.
|
||||
WithKeyringDir(kbHome).
|
||||
WithKeyring(kb).
|
||||
WithCodec(cdc)
|
||||
WithCodec(cdc).
|
||||
WithAddressCodec(addresscodec.NewBech32Codec("cosmos")).
|
||||
WithValidatorAddressCodec(addresscodec.NewBech32Codec("cosmosvaloper")).
|
||||
WithConsensusAddressCodec(addresscodec.NewBech32Codec("cosmosvalcons"))
|
||||
ctx := context.WithValue(context.Background(), client.ClientContextKey, &clientCtx)
|
||||
b := bytes.NewBufferString("")
|
||||
cmd.SetOut(b)
|
||||
|
||||
+21
-3
@@ -12,6 +12,7 @@ import (
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/client"
|
||||
"github.com/cosmos/cosmos-sdk/client/flags"
|
||||
addresscodec "github.com/cosmos/cosmos-sdk/codec/address"
|
||||
"github.com/cosmos/cosmos-sdk/crypto/hd"
|
||||
"github.com/cosmos/cosmos-sdk/crypto/keyring"
|
||||
"github.com/cosmos/cosmos-sdk/testutil"
|
||||
@@ -31,7 +32,14 @@ func Test_runAddCmdBasic(t *testing.T) {
|
||||
kb, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendTest, kbHome, mockIn, cdc)
|
||||
require.NoError(t, err)
|
||||
|
||||
clientCtx := client.Context{}.WithKeyringDir(kbHome).WithInput(mockIn).WithCodec(cdc)
|
||||
clientCtx := client.Context{}.
|
||||
WithKeyringDir(kbHome).
|
||||
WithInput(mockIn).
|
||||
WithCodec(cdc).
|
||||
WithAddressCodec(addresscodec.NewBech32Codec("cosmos")).
|
||||
WithValidatorAddressCodec(addresscodec.NewBech32Codec("cosmosvaloper")).
|
||||
WithConsensusAddressCodec(addresscodec.NewBech32Codec("cosmosvalcons"))
|
||||
|
||||
ctx := context.WithValue(context.Background(), client.ClientContextKey, &clientCtx)
|
||||
|
||||
t.Cleanup(func() {
|
||||
@@ -197,7 +205,10 @@ func Test_runAddCmdDryRun(t *testing.T) {
|
||||
clientCtx := client.Context{}.
|
||||
WithCodec(cdc).
|
||||
WithKeyringDir(kbHome).
|
||||
WithKeyring(kb)
|
||||
WithKeyring(kb).
|
||||
WithAddressCodec(addresscodec.NewBech32Codec("cosmos")).
|
||||
WithValidatorAddressCodec(addresscodec.NewBech32Codec("cosmosvaloper")).
|
||||
WithConsensusAddressCodec(addresscodec.NewBech32Codec("cosmosvalcons"))
|
||||
ctx := context.WithValue(context.Background(), client.ClientContextKey, &clientCtx)
|
||||
|
||||
path := sdk.GetConfig().GetFullBIP44Path()
|
||||
@@ -238,7 +249,14 @@ func TestAddRecoverFileBackend(t *testing.T) {
|
||||
mockIn := testutil.ApplyMockIODiscardOutErr(cmd)
|
||||
kbHome := t.TempDir()
|
||||
|
||||
clientCtx := client.Context{}.WithKeyringDir(kbHome).WithInput(mockIn).WithCodec(cdc)
|
||||
clientCtx := client.Context{}.
|
||||
WithKeyringDir(kbHome).
|
||||
WithInput(mockIn).
|
||||
WithCodec(cdc).
|
||||
WithAddressCodec(addresscodec.NewBech32Codec("cosmos")).
|
||||
WithValidatorAddressCodec(addresscodec.NewBech32Codec("cosmosvaloper")).
|
||||
WithConsensusAddressCodec(addresscodec.NewBech32Codec("cosmosvalcons"))
|
||||
|
||||
ctx := context.WithValue(context.Background(), client.ClientContextKey, &clientCtx)
|
||||
|
||||
cmd.SetArgs([]string{
|
||||
|
||||
+1
-1
@@ -40,7 +40,7 @@ func runListCmd(cmd *cobra.Command, _ []string) error {
|
||||
}
|
||||
|
||||
if ok, _ := cmd.Flags().GetBool(flagListNames); !ok {
|
||||
return printKeyringRecords(cmd.OutOrStdout(), records, clientCtx.OutputFormat)
|
||||
return printKeyringRecords(clientCtx, cmd.OutOrStdout(), records, clientCtx.OutputFormat)
|
||||
}
|
||||
|
||||
for _, k := range records {
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/client"
|
||||
"github.com/cosmos/cosmos-sdk/client/flags"
|
||||
addresscodec "github.com/cosmos/cosmos-sdk/codec/address"
|
||||
"github.com/cosmos/cosmos-sdk/crypto/hd"
|
||||
"github.com/cosmos/cosmos-sdk/crypto/keyring"
|
||||
"github.com/cosmos/cosmos-sdk/testutil"
|
||||
@@ -42,7 +43,12 @@ func Test_runListCmd(t *testing.T) {
|
||||
kb, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendTest, kbHome2, mockIn, cdc)
|
||||
assert.NilError(t, err)
|
||||
|
||||
clientCtx := client.Context{}.WithKeyring(kb)
|
||||
clientCtx := client.Context{}.
|
||||
WithKeyring(kb).
|
||||
WithAddressCodec(addresscodec.NewBech32Codec("cosmos")).
|
||||
WithValidatorAddressCodec(addresscodec.NewBech32Codec("cosmosvaloper")).
|
||||
WithConsensusAddressCodec(addresscodec.NewBech32Codec("cosmosvalcons"))
|
||||
|
||||
ctx := context.WithValue(context.Background(), client.ClientContextKey, &clientCtx)
|
||||
|
||||
path := "" // sdk.GetConfig().GetFullBIP44Path()
|
||||
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
"github.com/cosmos/cosmos-sdk/client"
|
||||
"github.com/cosmos/cosmos-sdk/client/flags"
|
||||
"github.com/cosmos/cosmos-sdk/codec"
|
||||
addresscodec "github.com/cosmos/cosmos-sdk/codec/address"
|
||||
"github.com/cosmos/cosmos-sdk/crypto/keyring"
|
||||
"github.com/cosmos/cosmos-sdk/crypto/keys/multisig"
|
||||
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
|
||||
@@ -34,6 +35,10 @@ type MigrateTestSuite struct {
|
||||
pub cryptotypes.PubKey
|
||||
}
|
||||
|
||||
func TestMigrateTestSuite(t *testing.T) {
|
||||
suite.Run(t, new(MigrateTestSuite))
|
||||
}
|
||||
|
||||
func (s *MigrateTestSuite) SetupSuite() {
|
||||
s.dir = s.T().TempDir()
|
||||
s.cdc = moduletestutil.MakeTestEncodingConfig().Codec
|
||||
@@ -71,7 +76,12 @@ func (s *MigrateTestSuite) Test_runListAndShowCmd() {
|
||||
s.Require().True(ok)
|
||||
s.Require().NoError(setter.SetItem(item))
|
||||
|
||||
clientCtx := client.Context{}.WithKeyring(kb)
|
||||
clientCtx := client.Context{}.
|
||||
WithKeyring(kb).
|
||||
WithAddressCodec(addresscodec.NewBech32Codec("cosmos")).
|
||||
WithValidatorAddressCodec(addresscodec.NewBech32Codec("cosmosvaloper")).
|
||||
WithConsensusAddressCodec(addresscodec.NewBech32Codec("cosmosvalcons"))
|
||||
|
||||
ctx := context.WithValue(context.Background(), client.ClientContextKey, &clientCtx)
|
||||
|
||||
cmd.SetArgs([]string{
|
||||
@@ -147,7 +157,3 @@ func (s *MigrateTestSuite) Test_runMigrateCmdLegacyMultiInfo() {
|
||||
ctx := context.WithValue(context.Background(), client.ClientContextKey, &clientCtx)
|
||||
s.Require().NoError(cmd.ExecuteContext(ctx))
|
||||
}
|
||||
|
||||
func TestMigrateTestSuite(t *testing.T) {
|
||||
suite.Run(t, new(MigrateTestSuite))
|
||||
}
|
||||
|
||||
+21
-17
@@ -1,11 +1,12 @@
|
||||
package keys
|
||||
|
||||
import (
|
||||
"cosmossdk.io/core/address"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/codec"
|
||||
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
|
||||
"github.com/cosmos/cosmos-sdk/crypto/keyring"
|
||||
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
)
|
||||
|
||||
// Use protobuf interface marshaler rather then generic JSON
|
||||
@@ -21,65 +22,68 @@ type KeyOutput struct {
|
||||
}
|
||||
|
||||
// NewKeyOutput creates a default KeyOutput instance without Mnemonic, Threshold and PubKeys
|
||||
func NewKeyOutput(name string, keyType keyring.KeyType, a sdk.Address, pk cryptotypes.PubKey) (KeyOutput, error) {
|
||||
func NewKeyOutput(name string, keyType keyring.KeyType, addr []byte, pk cryptotypes.PubKey, addressCodec address.Codec) (KeyOutput, error) {
|
||||
apk, err := codectypes.NewAnyWithValue(pk)
|
||||
if err != nil {
|
||||
return KeyOutput{}, err
|
||||
}
|
||||
|
||||
bz, err := codec.ProtoMarshalJSON(apk, nil)
|
||||
if err != nil {
|
||||
return KeyOutput{}, err
|
||||
}
|
||||
|
||||
addrStr, err := addressCodec.BytesToString(addr)
|
||||
if err != nil {
|
||||
return KeyOutput{}, err
|
||||
}
|
||||
|
||||
return KeyOutput{
|
||||
Name: name,
|
||||
Type: keyType.String(),
|
||||
Address: a.String(),
|
||||
Address: addrStr,
|
||||
PubKey: string(bz),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// MkConsKeyOutput create a KeyOutput in with "cons" Bech32 prefixes.
|
||||
func MkConsKeyOutput(k *keyring.Record) (KeyOutput, error) {
|
||||
// MkConsKeyOutput create a KeyOutput for consensus addresses.
|
||||
func MkConsKeyOutput(k *keyring.Record, consensusAddressCodec address.Codec) (KeyOutput, error) {
|
||||
pk, err := k.GetPubKey()
|
||||
if err != nil {
|
||||
return KeyOutput{}, err
|
||||
}
|
||||
addr := sdk.ConsAddress(pk.Address())
|
||||
return NewKeyOutput(k.Name, k.GetType(), addr, pk)
|
||||
return NewKeyOutput(k.Name, k.GetType(), pk.Address(), pk, consensusAddressCodec)
|
||||
}
|
||||
|
||||
// MkValKeyOutput create a KeyOutput in with "val" Bech32 prefixes.
|
||||
func MkValKeyOutput(k *keyring.Record) (KeyOutput, error) {
|
||||
// MkValKeyOutput create a KeyOutput for validator addresses.
|
||||
func MkValKeyOutput(k *keyring.Record, validatorAddressCodec address.Codec) (KeyOutput, error) {
|
||||
pk, err := k.GetPubKey()
|
||||
if err != nil {
|
||||
return KeyOutput{}, err
|
||||
}
|
||||
|
||||
addr := sdk.ValAddress(pk.Address())
|
||||
|
||||
return NewKeyOutput(k.Name, k.GetType(), addr, pk)
|
||||
return NewKeyOutput(k.Name, k.GetType(), pk.Address(), pk, validatorAddressCodec)
|
||||
}
|
||||
|
||||
// MkAccKeyOutput create a KeyOutput in with "acc" Bech32 prefixes. If the
|
||||
// public key is a multisig public key, then the threshold and constituent
|
||||
// public keys will be added.
|
||||
func MkAccKeyOutput(k *keyring.Record) (KeyOutput, error) {
|
||||
func MkAccKeyOutput(k *keyring.Record, addressCodec address.Codec) (KeyOutput, error) {
|
||||
pk, err := k.GetPubKey()
|
||||
if err != nil {
|
||||
return KeyOutput{}, err
|
||||
}
|
||||
addr := sdk.AccAddress(pk.Address())
|
||||
return NewKeyOutput(k.Name, k.GetType(), addr, pk)
|
||||
return NewKeyOutput(k.Name, k.GetType(), pk.Address(), pk, addressCodec)
|
||||
}
|
||||
|
||||
// MkAccKeysOutput returns a slice of KeyOutput objects, each with the "acc"
|
||||
// Bech32 prefixes, given a slice of Record objects. It returns an error if any
|
||||
// call to MkKeyOutput fails.
|
||||
func MkAccKeysOutput(records []*keyring.Record) ([]KeyOutput, error) {
|
||||
func MkAccKeysOutput(records []*keyring.Record, addressCodec address.Codec) ([]KeyOutput, error) {
|
||||
kos := make([]KeyOutput, len(records))
|
||||
var err error
|
||||
for i, r := range records {
|
||||
kos[i], err = MkAccKeyOutput(r)
|
||||
kos[i], err = MkAccKeyOutput(r, addressCodec)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/codec"
|
||||
addresscodec "github.com/cosmos/cosmos-sdk/codec/address"
|
||||
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
|
||||
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
|
||||
"github.com/cosmos/cosmos-sdk/crypto/keyring"
|
||||
@@ -35,10 +36,10 @@ func TestBech32KeysOutput(t *testing.T) {
|
||||
pubKey, err := k.GetPubKey()
|
||||
require.NoError(t, err)
|
||||
accAddr := sdk.AccAddress(pubKey.Address())
|
||||
expectedOutput, err := NewKeyOutput(k.Name, k.GetType(), accAddr, multisigPk)
|
||||
expectedOutput, err := NewKeyOutput(k.Name, k.GetType(), accAddr, multisigPk, addresscodec.NewBech32Codec("cosmos"))
|
||||
require.NoError(t, err)
|
||||
|
||||
out, err := MkAccKeyOutput(k)
|
||||
out, err := MkAccKeyOutput(k, addresscodec.NewBech32Codec("cosmos"))
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, expectedOutput, out)
|
||||
require.Equal(t, "{Name:multisig Type:multi Address:cosmos1nf8lf6n4wa43rzmdzwe6hkrnw5guekhqt595cw PubKey:{\"@type\":\"/cosmos.crypto.multisig.LegacyAminoPubKey\",\"threshold\":1,\"public_keys\":[{\"@type\":\"/cosmos.crypto.secp256k1.PubKey\",\"key\":\"AurroA7jvfPd1AadmmOvWM2rJSwipXfRf8yD6pLbA2DJ\"}]} Mnemonic:}", fmt.Sprintf("%+v", out))
|
||||
@@ -61,11 +62,17 @@ func TestProtoMarshalJSON(t *testing.T) {
|
||||
require.NoError(err)
|
||||
require.True(pk2.Equals(msig))
|
||||
|
||||
addressCodec := addresscodec.NewBech32Codec("cosmos")
|
||||
|
||||
// Test that we can correctly unmarshal key from output
|
||||
k, err := keyring.NewMultiRecord("my multisig", msig)
|
||||
require.NoError(err)
|
||||
ko, err := MkAccKeyOutput(k)
|
||||
ko, err := MkAccKeyOutput(k, addressCodec)
|
||||
require.NoError(err)
|
||||
require.Equal(ko.Address, sdk.AccAddress(pk2.Address()).String())
|
||||
|
||||
expectedOutput, err := addressCodec.BytesToString(pk2.Address())
|
||||
require.NoError(err)
|
||||
|
||||
require.Equal(ko.Address, expectedOutput)
|
||||
require.Equal(ko.PubKey, string(bz))
|
||||
}
|
||||
|
||||
@@ -79,17 +79,15 @@ func ParseKeyStringCommand() *cobra.Command {
|
||||
hexadecimal into bech32 cosmos prefixed format and vice versa.
|
||||
`,
|
||||
Args: cobra.ExactArgs(1),
|
||||
RunE: parseKey,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
config, _ := sdk.GetSealedConfig(cmd.Context())
|
||||
return doParseKey(cmd, config, args)
|
||||
},
|
||||
}
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
||||
func parseKey(cmd *cobra.Command, args []string) error {
|
||||
config, _ := sdk.GetSealedConfig(cmd.Context())
|
||||
return doParseKey(cmd, config, args)
|
||||
}
|
||||
|
||||
func doParseKey(cmd *cobra.Command, config *sdk.Config, args []string) error {
|
||||
addr := strings.TrimSpace(args[0])
|
||||
outstream := cmd.OutOrStdout()
|
||||
|
||||
+12
-15
@@ -6,6 +6,7 @@ import (
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"cosmossdk.io/core/address"
|
||||
errorsmod "cosmossdk.io/errors"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/client"
|
||||
@@ -61,14 +62,14 @@ func runShowCmd(cmd *cobra.Command, args []string) (err error) {
|
||||
outputFormat := clientCtx.OutputFormat
|
||||
|
||||
if len(args) == 1 {
|
||||
k, err = fetchKey(clientCtx.Keyring, args[0])
|
||||
k, err = fetchKey(clientCtx.Keyring, args[0], clientCtx.AddressCodec)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s is not a valid name or address: %w", args[0], err)
|
||||
}
|
||||
} else {
|
||||
pks := make([]cryptotypes.PubKey, len(args))
|
||||
for i, keyref := range args {
|
||||
k, err := fetchKey(clientCtx.Keyring, keyref)
|
||||
k, err := fetchKey(clientCtx.Keyring, keyref, clientCtx.AddressCodec)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s is not a valid name or address: %w", keyref, err)
|
||||
}
|
||||
@@ -111,7 +112,7 @@ func runShowCmd(cmd *cobra.Command, args []string) (err error) {
|
||||
}
|
||||
|
||||
bechPrefix, _ := cmd.Flags().GetString(FlagBechPrefix)
|
||||
bechKeyOut, err := getBechKeyOut(bechPrefix)
|
||||
ko, err := getKeyOutput(clientCtx, bechPrefix, k)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -122,10 +123,6 @@ func runShowCmd(cmd *cobra.Command, args []string) (err error) {
|
||||
|
||||
switch {
|
||||
case isShowAddr, isShowPubKey:
|
||||
ko, err := bechKeyOut(k)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
out := ko.Address
|
||||
if isShowPubKey {
|
||||
out = ko.PubKey
|
||||
@@ -135,7 +132,7 @@ func runShowCmd(cmd *cobra.Command, args []string) (err error) {
|
||||
return err
|
||||
}
|
||||
default:
|
||||
if err := printKeyringRecord(cmd.OutOrStdout(), k, bechKeyOut, outputFormat); err != nil {
|
||||
if err := printKeyringRecord(cmd.OutOrStdout(), ko, outputFormat); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -169,7 +166,7 @@ func runShowCmd(cmd *cobra.Command, args []string) (err error) {
|
||||
return nil
|
||||
}
|
||||
|
||||
func fetchKey(kb keyring.Keyring, keyref string) (*keyring.Record, error) {
|
||||
func fetchKey(kb keyring.Keyring, keyref string, addressCodec address.Codec) (*keyring.Record, error) {
|
||||
// firstly check if the keyref is a key name of a key registered in a keyring.
|
||||
k, err := kb.Key(keyref)
|
||||
// if the key is not there or if we have a problem with a keyring itself then we move to a
|
||||
@@ -179,7 +176,7 @@ func fetchKey(kb keyring.Keyring, keyref string) (*keyring.Record, error) {
|
||||
return k, err
|
||||
}
|
||||
|
||||
accAddr, err := sdk.AccAddressFromBech32(keyref)
|
||||
accAddr, err := addressCodec.StringToBytes(keyref)
|
||||
if err != nil {
|
||||
return k, err
|
||||
}
|
||||
@@ -199,15 +196,15 @@ func validateMultisigThreshold(k, nKeys int) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func getBechKeyOut(bechPrefix string) (bechKeyOutFn, error) {
|
||||
func getKeyOutput(clientCtx client.Context, bechPrefix string, k *keyring.Record) (KeyOutput, error) {
|
||||
switch bechPrefix {
|
||||
case sdk.PrefixAccount:
|
||||
return MkAccKeyOutput, nil
|
||||
return MkAccKeyOutput(k, clientCtx.AddressCodec)
|
||||
case sdk.PrefixValidator:
|
||||
return MkValKeyOutput, nil
|
||||
return MkValKeyOutput(k, clientCtx.ValidatorAddressCodec)
|
||||
case sdk.PrefixConsensus:
|
||||
return MkConsKeyOutput, nil
|
||||
return MkConsKeyOutput(k, clientCtx.ConsensusAddressCodec)
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("invalid Bech32 prefix encoding provided: %s", bechPrefix)
|
||||
return KeyOutput{}, fmt.Errorf("invalid Bech32 prefix encoding provided: %s", bechPrefix)
|
||||
}
|
||||
|
||||
@@ -7,8 +7,11 @@ import (
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"cosmossdk.io/core/address"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/client"
|
||||
"github.com/cosmos/cosmos-sdk/client/flags"
|
||||
addresscodec "github.com/cosmos/cosmos-sdk/codec/address"
|
||||
"github.com/cosmos/cosmos-sdk/crypto/hd"
|
||||
"github.com/cosmos/cosmos-sdk/crypto/keyring"
|
||||
"github.com/cosmos/cosmos-sdk/crypto/keys/multisig"
|
||||
@@ -59,7 +62,11 @@ func Test_runShowCmd(t *testing.T) {
|
||||
|
||||
clientCtx := client.Context{}.
|
||||
WithKeyringDir(kbHome).
|
||||
WithCodec(cdc)
|
||||
WithCodec(cdc).
|
||||
WithAddressCodec(addresscodec.NewBech32Codec("cosmos")).
|
||||
WithValidatorAddressCodec(addresscodec.NewBech32Codec("cosmosvaloper")).
|
||||
WithConsensusAddressCodec(addresscodec.NewBech32Codec("cosmosvalcons"))
|
||||
|
||||
ctx := context.WithValue(context.Background(), client.ClientContextKey, &clientCtx)
|
||||
|
||||
cmd.SetArgs([]string{"invalid"})
|
||||
@@ -196,13 +203,22 @@ func Test_validateMultisigThreshold(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_getBechKeyOut(t *testing.T) {
|
||||
ctx := client.Context{}.
|
||||
WithAddressCodec(addresscodec.NewBech32Codec("cosmos")).
|
||||
WithValidatorAddressCodec(addresscodec.NewBech32Codec("cosmosvaloper")).
|
||||
WithConsensusAddressCodec(addresscodec.NewBech32Codec("cosmosvalcons"))
|
||||
|
||||
tmpKey1 := secp256k1.GenPrivKeyFromSecret([]byte("mySecret"))
|
||||
k, err := keyring.NewLocalRecord("foo", tmpKey1, tmpKey1.PubKey())
|
||||
require.NoError(t, err)
|
||||
|
||||
type args struct {
|
||||
bechPrefix string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want bechKeyOutFn
|
||||
want func(k *keyring.Record, addressCodec address.Codec) (KeyOutput, error)
|
||||
wantErr bool
|
||||
}{
|
||||
{"empty", args{""}, nil, true},
|
||||
@@ -214,12 +230,12 @@ func Test_getBechKeyOut(t *testing.T) {
|
||||
for _, tt := range tests {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, err := getBechKeyOut(tt.args.bechPrefix)
|
||||
output, err := getKeyOutput(ctx, tt.args.bechPrefix, k)
|
||||
if tt.wantErr {
|
||||
require.Error(t, err)
|
||||
} else {
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, got)
|
||||
require.NotNil(t, output)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
+4
-10
@@ -7,18 +7,12 @@ import (
|
||||
|
||||
"sigs.k8s.io/yaml"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/client"
|
||||
"github.com/cosmos/cosmos-sdk/client/flags"
|
||||
cryptokeyring "github.com/cosmos/cosmos-sdk/crypto/keyring"
|
||||
)
|
||||
|
||||
type bechKeyOutFn func(k *cryptokeyring.Record) (KeyOutput, error)
|
||||
|
||||
func printKeyringRecord(w io.Writer, k *cryptokeyring.Record, bechKeyOut bechKeyOutFn, output string) error {
|
||||
ko, err := bechKeyOut(k)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
func printKeyringRecord(w io.Writer, ko KeyOutput, output string) error {
|
||||
switch output {
|
||||
case flags.OutputFormatText:
|
||||
if err := printTextRecords(w, []KeyOutput{ko}); err != nil {
|
||||
@@ -39,8 +33,8 @@ func printKeyringRecord(w io.Writer, k *cryptokeyring.Record, bechKeyOut bechKey
|
||||
return nil
|
||||
}
|
||||
|
||||
func printKeyringRecords(w io.Writer, records []*cryptokeyring.Record, output string) error {
|
||||
kos, err := MkAccKeysOutput(records)
|
||||
func printKeyringRecords(clientCtx client.Context, w io.Writer, records []*cryptokeyring.Record, output string) error {
|
||||
kos, err := MkAccKeysOutput(records, clientCtx.AddressCodec)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user