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

refactor!: Kerying migration #9222

Closed
wants to merge 133 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
133 commits
Select commit Hold shift + click to select a range
c727fa6
Keyring migration from amino to proto
robert-zaremba Apr 16, 2021
7505e5a
adding proto definitions - wip
robert-zaremba Apr 16, 2021
9160257
add rename in the test
robert-zaremba Apr 16, 2021
16eebea
define proto structs for all structs in keyring/info.go
cyberbono3 Apr 22, 2021
6ae658d
generate types.pb.go
cyberbono3 Apr 26, 2021
bcd9f81
WIP keyring migration
cyberbono3 Apr 28, 2021
36311a3
new proto model,gen types.pb.go and define protoMarshalKeyringEntry,p…
cyberbono3 Apr 29, 2021
c9ae124
KeyringEntry satisfies Info interface
cyberbono3 Apr 29, 2021
2421d02
fix KeyringEntry getters
cyberbono3 Apr 30, 2021
ea31d04
WIP add codec to keystore and dbKeybase, fix protoMarshal,protoUnmars…
cyberbono3 Apr 30, 2021
23a2316
apply some of reviewers changes
cyberbono3 May 4, 2021
1f0ae05
fix protos
cyberbono3 May 4, 2021
059aa39
fix keyring.New
cyberbono3 May 4, 2021
3a420d2
pass *codec.AminoCodec as function argument in keyring.New
cyberbono3 May 4, 2021
52426d4
replace codec.AmintoCodec by codec.Codec
cyberbono3 May 4, 2021
9a4bd80
merge master to kerying-migration
cyberbono3 May 4, 2021
51afe76
add codec.Codec as argument in keyring.New, NewInMemory in keyring pa…
cyberbono3 May 4, 2021
6edb399
WIP cycle dep issue
cyberbono3 May 10, 2021
c99594a
WIP fixed dep cycle but tests still fail
cyberbono3 May 11, 2021
0aca25f
WIP add PubKeyType to crypto/types.proto
cyberbono3 May 11, 2021
e9e0b4f
add pubKeyType to types.proto, fixing tests in progress
cyberbono3 May 12, 2021
9a61180
WIP fixing keyring_test.go tests
cyberbono3 May 12, 2021
cb8c94f
fix all tests keytirng_test.go except encCfg.Marshaler
cyberbono3 May 13, 2021
2bc9cfc
fixed almost all tests in keyring_test.go
cyberbono3 May 13, 2021
83c290a
WIP writeLedgerKey
cyberbono3 May 14, 2021
5a5a9c6
reviewed keyring.go
cyberbono3 May 15, 2021
fba942d
review legacy.go
cyberbono3 May 15, 2021
2059243
review legacy_test.go
cyberbono3 May 15, 2021
860e0d0
WIP remove Info from keyring package entirely
cyberbono3 May 19, 2021
f9b32f0
WIP set record.go
cyberbono3 May 22, 2021
f18de4c
remove dep cycle
cyberbono3 May 23, 2021
ebc6d44
add legacyinfo.go
cyberbono3 May 25, 2021
e465ac6
fix writeLocalKey in keyring.go
cyberbono3 May 25, 2021
26d4672
WIP fix tests
cyberbono3 May 25, 2021
990cee7
remove gogoproto.goproto_stringer=false by structs in protos and fix …
cyberbono3 May 25, 2021
3f00b76
make Codec field on clientCtx struct
cyberbono3 May 25, 2021
79ebb56
set re variable name in GetFromFields in context.go
cyberbono3 May 27, 2021
ab4480f
do not modify the second argument in cdc.RegisterConcrete
cyberbono3 May 27, 2021
c9f6911
rename SavePubKey to SaveOfflineKey
cyberbono3 May 27, 2021
0778fdf
fix some tests in keyring_test.go
cyberbono3 May 27, 2021
cab7c66
generate hd.pb.go inside crypto/hd
cyberbono3 May 27, 2021
35aea35
fix BIP44Params in record.go
cyberbono3 May 27, 2021
7365334
try to fix makeMultiSignCmd()
cyberbono3 May 28, 2021
5865d9d
fix makeMultiSignCmd
cyberbono3 May 28, 2021
d5c3c88
fix client/tx/tx.go and x/auth/client/tx.go
cyberbono3 May 29, 2021
a56cf27
fix x/auth/client/cli/tx_multisign.go
cyberbono3 May 29, 2021
6440a36
WIP fix Unable to cast PubKey to cryptotypes.PubKey error
cyberbono3 May 31, 2021
fe26c52
fix TestNewKeyring test
cyberbono3 Jun 2, 2021
f5d5e6b
fix all tests except tests require migration fix
cyberbono3 Jun 2, 2021
3dc26a5
migration questions
cyberbono3 Jun 2, 2021
6fd3fed
fix migrate func and implement convertFromLegacyInfo
cyberbono3 Jun 7, 2021
c684f59
WIP debugging TestSignVerifyKeyRing
cyberbono3 Jun 7, 2021
372cc34
update migration algorithm
cyberbono3 Jun 9, 2021
6ed3edd
set proper naming k for Record
cyberbono3 Jun 10, 2021
1b16f94
some cosmetic stuff
cyberbono3 Jun 10, 2021
d362139
implement protoUnmarshalRecord
cyberbono3 Jun 10, 2021
68816b7
minor fixes
cyberbono3 Jun 10, 2021
3df8c9b
gen new protos according to robert suggestion
cyberbono3 Jun 10, 2021
5bf2889
fix new methods for local,ledger,empty items
cyberbono3 Jun 10, 2021
36764e3
latest version
cyberbono3 Jun 11, 2021
e3fe793
minor fixes
cyberbono3 Jun 14, 2021
17b45ae
modify VERSION_KEY to 1(string repr)
cyberbono3 Jun 14, 2021
241feb6
trying to fix an error - unable to unpack private key
cyberbono3 Jun 15, 2021
7815f41
any unpack does not work in protoUnmarshalRecord
cyberbono3 Jun 16, 2021
c538c57
minor fixes in record.go
cyberbono3 Jun 16, 2021
0a92755
keyring record marshaling tests
robert-zaremba Jun 17, 2021
9f5678d
TestRecordMarshaling pass
cyberbono3 Jun 17, 2021
23b33b2
TestLocalRecordMarshaling pass
cyberbono3 Jun 18, 2021
520cc56
TODO fix TestExtractPrivKeyFromItem test
cyberbono3 Jun 18, 2021
7a4b74e
TODO fix TestMigrationOneLegacyKey too many failed passphrase attempts
cyberbono3 Jun 18, 2021
d717da0
TestMigrationOneLegacyLocalKey and TestMigrationRecord pass
cyberbono3 Jun 21, 2021
7cd88cb
WIP finishing migration tests
cyberbono3 Jun 21, 2021
45fdfcb
all migration tests pass except TestMigrationLegacyLedgerKey
cyberbono3 Jun 21, 2021
f74cf65
debugging TestExtractPrivKeyFromItem
cyberbono3 Jun 21, 2021
fe68c74
all record tests pass
cyberbono3 Jun 22, 2021
e690fde
fix all tests in migration_test.go
cyberbono3 Jun 22, 2021
f9965e7
remove LegacyInfoWriter interface from legacy_info.go
cyberbono3 Jun 22, 2021
4c48fea
update checkMigrate and migrate funcs
cyberbono3 Jun 22, 2021
8109936
update migration algo and fix migration tests
cyberbono3 Jun 23, 2021
303e7fe
update Migrate,MigrateAll and fix migration tests
cyberbono3 Jun 24, 2021
9d87490
remove VERSION_KEY, add some migration tests
cyberbono3 Jun 28, 2021
dbd041c
fix errors clients/keys package
cyberbono3 Jun 29, 2021
0c751ed
fix migrate logic and migrateCmd tests
cyberbono3 Jun 29, 2021
fe39205
WIP debugging TestKeyManagementKeyRing
cyberbono3 Jun 29, 2021
35dbe9c
fix TestKeyManagementKeyRing test
cyberbono3 Jun 30, 2021
f5af15c
fix MigrateAll and adjust migrations tests
cyberbono3 Jun 30, 2021
f0988b1
fix TestSignVerifyKeyRing
cyberbono3 Jun 30, 2021
381dc27
fix TestExportImportKeyRing
cyberbono3 Jun 30, 2021
89764ba
fix TestSeedPhraseKeyRing test
cyberbono3 Jun 30, 2021
b232cea
fix TestKeyringKeybaseExportImportPrivKey
cyberbono3 Jun 30, 2021
599b413
WIP fixing keyring tests
cyberbono3 Jun 30, 2021
8dcefee
fix all keyring tests except ExampleNew
cyberbono3 Jul 1, 2021
a8856d3
fix TestProtoMarshalJSON
cyberbono3 Jul 1, 2021
4468f7d
fix TestArmorUnarmorPubKey
cyberbono3 Jul 1, 2021
4235a58
fix TestSign
cyberbono3 Jul 1, 2021
1214b2e
WIP debugging client/keys tests
cyberbono3 Jul 1, 2021
0ebbfaf
fix server/init tests
cyberbono3 Jul 1, 2021
654e34e
Test_runAddCmdBasic panic
cyberbono3 Jul 1, 2021
d1bccca
WIP fixing tests
cyberbono3 Jul 2, 2021
e0b0073
cdc is nil
cyberbono3 Jul 6, 2021
4735d9f
fix x/auth/tx/service_test.go test
cyberbono3 Jul 6, 2021
c728543
WIP debugging Test_runAddCmdBasic
cyberbono3 Jul 6, 2021
41b0d03
fix Test_runAddCmdBasic
cyberbono3 Jul 6, 2021
4d88638
fix Test_runDeleteCmd
cyberbono3 Jul 6, 2021
e0e4915
fix all tests in client/keys except export_test
cyberbono3 Jul 7, 2021
78d81be
fix rest tests
cyberbono3 Jul 7, 2021
42a5c62
fix grps_query_test
cyberbono3 Jul 7, 2021
d272991
cleanup
cyberbono3 Jul 7, 2021
c03be48
WIP fix no registered implementations of type types.PrivKey
cyberbono3 Jul 7, 2021
c736b8f
merge master to current branch and resolve conflicts
cyberbono3 Jul 7, 2021
fcf1b70
remove legacy and legacy_test entirely
cyberbono3 Jul 7, 2021
baaeda0
WIP fix extractPrivKeyFromLocal Unable to cast any to cryptotypes.Pr…
cyberbono3 Jul 8, 2021
39cd82c
fix TestSignVerifyKeyRing and cast any to PrivKey issue
cyberbono3 Jul 8, 2021
b6cb87f
WIP fix all keyring tests
cyberbono3 Jul 9, 2021
4c45144
fix all keyring_test package tests
cyberbono3 Jul 9, 2021
5ff7551
fix all client/keys package tests
cyberbono3 Jul 9, 2021
f209635
WIP fix tests
cyberbono3 Jul 9, 2021
faf232e
run make format
cyberbono3 Jul 9, 2021
30d7f3c
WIP fix tests and debug Test_runAddCmdLedgerDryRun
cyberbono3 Jul 11, 2021
f1e2084
fix all tests except TestIntegrationTestSuite
cyberbono3 Jul 12, 2021
3117ce9
fix all tests
cyberbono3 Jul 12, 2021
4882db8
minor fixes
cyberbono3 Jul 12, 2021
c691246
Merge branch 'master' into kerying-migration
cyberbono3 Jul 13, 2021
5514769
remove dead code
cyberbono3 Jul 13, 2021
b0685fe
gofmt
cyberbono3 Jul 13, 2021
4d8c0e8
refactor keyring_test.go
cyberbono3 Jul 13, 2021
24f44e8
add TestMigrateErrEmptyItemData to migration_test.go
cyberbono3 Jul 13, 2021
fe572c9
set proper codec in genaccounts.go
cyberbono3 Jul 13, 2021
dda1851
set proper codec in testnet.go
cyberbono3 Jul 13, 2021
8f239df
remove some comments from x/authz/client/testutil/tx.go
cyberbono3 Jul 13, 2021
1401291
fix lint comments
cyberbono3 Jul 13, 2021
7beebb2
fix ineffectual assignment to err in 2 suite files
cyberbono3 Jul 13, 2021
8652e00
update proto files
cyberbono3 Jul 13, 2021
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ dist
tools-stamp
buf-stamp
artifacts
private/*.sh
test
Comment on lines +25 to +26
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private/*.sh
test


# Data - ideally these don't exist
baseapp/data/*
Expand Down
17 changes: 11 additions & 6 deletions client/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,27 +329,32 @@ func GetFromFields(kr keyring.Keyring, from string, genOnly bool) (sdk.AccAddres
return addr, "", 0, nil
}

var info keyring.Info
k := new(keyring.Record)
if addr, err := sdk.AccAddressFromBech32(from); err == nil {
info, err = kr.KeyByAddress(addr)
k, err = kr.KeyByAddress(addr)
if err != nil {
return nil, "", 0, err
}
} else {
info, err = kr.Key(from)
k, err = kr.Key(from)
if err != nil {
return nil, "", 0, err
}
}

return info.GetAddress(), info.GetName(), info.GetType(), nil
addr, err := k.GetAddress()
if err != nil {
return nil, "", 0, err
}

return addr, k.Name, k.GetType(), nil
}

// NewKeyringFromBackend gets a Keyring object from a backend
func NewKeyringFromBackend(ctx Context, backend string) (keyring.Keyring, error) {
if ctx.GenerateOnly || ctx.Simulate {
return keyring.New(sdk.KeyringServiceName(), keyring.BackendMemory, ctx.KeyringDir, ctx.Input, ctx.KeyringOptions...)
return keyring.New(sdk.KeyringServiceName(), keyring.BackendMemory, ctx.KeyringDir, ctx.Input, ctx.Codec, ctx.KeyringOptions...)
}

return keyring.New(sdk.KeyringServiceName(), backend, ctx.KeyringDir, ctx.Input, ctx.KeyringOptions...)
return keyring.New(sdk.KeyringServiceName(), backend, ctx.KeyringDir, ctx.Input, ctx.Codec, ctx.KeyringOptions...)
}
6 changes: 2 additions & 4 deletions client/docs/statik/statik.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion client/grpc/tmservice/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/cosmos/cosmos-sdk/testutil/network"
qtypes "github.com/cosmos/cosmos-sdk/types/query"
"github.com/cosmos/cosmos-sdk/testutil/rest"
qtypes "github.com/cosmos/cosmos-sdk/types/query"
"github.com/cosmos/cosmos-sdk/version"
)

Expand Down
37 changes: 17 additions & 20 deletions client/keys/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,7 @@ func runAddCmd(ctx client.Context, cmd *cobra.Command, args []string, inBuf *buf
}

if dryRun, _ := cmd.Flags().GetBool(flags.FlagDryRun); dryRun {
// use in memory keybase
kb = keyring.NewInMemory()
kb = keyring.NewInMemory(ctx.Codec)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
kb = keyring.NewInMemory(ctx.Codec)
// use in memory keybase
kb = keyring.NewInMemory(ctx.Codec)

} else {
_, err = kb.Key(name)
if err == nil {
Expand Down Expand Up @@ -153,7 +152,11 @@ func runAddCmd(ctx client.Context, cmd *cobra.Command, args []string, inBuf *buf
return err
}

pks[i] = k.GetPubKey()
key, err := k.GetPubKey()
if err != nil {
return err
}
pks[i] = key
}

if noSort, _ := cmd.Flags().GetBool(flagNoSort); !noSort {
Expand All @@ -163,12 +166,12 @@ func runAddCmd(ctx client.Context, cmd *cobra.Command, args []string, inBuf *buf
}

pk := multisig.NewLegacyAminoPubKey(multisigThreshold, pks)
info, err := kb.SaveMultisig(name, pk)
k, err := kb.SaveMultisig(name, pk)
if err != nil {
return err
}

return printCreate(cmd, info, false, "", outputFormat)
return printCreate(cmd, k, false, "", outputFormat)
}
}

Expand All @@ -179,13 +182,8 @@ func runAddCmd(ctx client.Context, cmd *cobra.Command, args []string, inBuf *buf
if err != nil {
return err
}

info, err := kb.SavePubKey(name, pk, algo.Name())
if err != nil {
return err
}

return printCreate(cmd, info, false, "", outputFormat)
_, err := kb.SaveOfflineKey(name, pk)
return err
}

coinType, _ := cmd.Flags().GetUint32(flagCoinType)
Expand All @@ -203,13 +201,12 @@ func runAddCmd(ctx client.Context, cmd *cobra.Command, args []string, inBuf *buf
// If we're using ledger, only thing we need is the path and the bech32 prefix.
if useLedger {
bech32PrefixAccAddr := sdk.GetConfig().GetBech32AccountAddrPrefix()
info, err := kb.SaveLedgerKey(name, hd.Secp256k1, bech32PrefixAccAddr, coinType, account, index)

k, err := kb.SaveLedgerKey(name, hd.Secp256k1, bech32PrefixAccAddr, coinType, account, index)
if err != nil {
return err
}

return printCreate(cmd, info, false, "", outputFormat)
return printCreate(cmd, k, false, "", outputFormat)
}

// Get bip39 mnemonic
Expand Down Expand Up @@ -271,7 +268,7 @@ func runAddCmd(ctx client.Context, cmd *cobra.Command, args []string, inBuf *buf
}
}

info, err := kb.NewAccount(name, mnemonic, bip39Passphrase, hdPath, algo)
k, err := kb.NewAccount(name, mnemonic, bip39Passphrase, hdPath, algo)
if err != nil {
return err
}
Expand All @@ -283,14 +280,14 @@ func runAddCmd(ctx client.Context, cmd *cobra.Command, args []string, inBuf *buf
mnemonic = ""
}

return printCreate(cmd, info, showMnemonic, mnemonic, outputFormat)
return printCreate(cmd, k, showMnemonic, mnemonic, outputFormat)
}

func printCreate(cmd *cobra.Command, info keyring.Info, showMnemonic bool, mnemonic string, outputFormat string) error {
func printCreate(cmd *cobra.Command, k *keyring.Record, showMnemonic bool, mnemonic, outputFormat string) error {
switch outputFormat {
case OutputFormatText:
cmd.PrintErrln()
printKeyInfo(cmd.OutOrStdout(), info, keyring.MkAccKeyOutput, outputFormat)
printKeyringRecord(cmd.OutOrStdout(), k, keyring.MkAccKeyOutput, outputFormat)

// print mnemonic unless requested not to.
if showMnemonic {
Expand All @@ -300,7 +297,7 @@ func printCreate(cmd *cobra.Command, info keyring.Info, showMnemonic bool, mnemo
fmt.Fprintln(cmd.ErrOrStderr(), mnemonic)
}
case OutputFormatJSON:
out, err := keyring.MkAccKeyOutput(info)
out, err := keyring.MkAccKeyOutput(k)
if err != nil {
return err
}
Expand Down
43 changes: 27 additions & 16 deletions client/keys/add_ledger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/cosmos/cosmos-sdk/simapp"
"github.com/cosmos/cosmos-sdk/testutil"
sdk "github.com/cosmos/cosmos-sdk/types"
)
Expand All @@ -42,7 +43,10 @@ func Test_runAddCmdLedgerWithCustomCoinType(t *testing.T) {
// Prepare a keybase
kbHome := t.TempDir()

clientCtx := client.Context{}.WithKeyringDir(kbHome)
cdc := simapp.MakeTestEncodingConfig().Codec
kb, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendTest, kbHome, nil, cdc)

clientCtx := client.Context{}.WithKeyringDir(kbHome).WithKeyring(kb)
ctx := context.WithValue(context.Background(), client.ClientContextKey, &clientCtx)

cmd.SetArgs([]string{
Expand All @@ -53,15 +57,14 @@ func Test_runAddCmdLedgerWithCustomCoinType(t *testing.T) {
fmt.Sprintf("--%s=330", flagCoinType),
fmt.Sprintf("--%s=%s", cli.OutputFlag, OutputFormatText),
fmt.Sprintf("--%s=%s", flags.FlagKeyAlgorithm, string(hd.Secp256k1Type)),
fmt.Sprintf("--%s=%s", flags.FlagKeyringBackend, keyring.BackendTest),
})

mockIn := testutil.ApplyMockIODiscardOutErr(cmd)
mockIn.Reset("test1234\ntest1234\n")
require.NoError(t, cmd.ExecuteContext(ctx))

// Now check that it has been stored properly
kb, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendTest, kbHome, mockIn)

require.NoError(t, err)
require.NotNil(t, kb)
t.Cleanup(func() {
Expand All @@ -72,11 +75,12 @@ func Test_runAddCmdLedgerWithCustomCoinType(t *testing.T) {
require.NoError(t, err)
require.NotNil(t, key1)

require.Equal(t, "keyname1", key1.GetName())
require.Equal(t, keyring.TypeLedger, key1.GetType())
require.Equal(t, "keyname1", key1.Name)
pub, err := key1.GetPubKey()
require.NoError(t, err)
require.Equal(t,
"PubKeySecp256k1{03028F0D5A9FD41600191CDEFDEA05E77A68DFBCE286241C0190805B9346667D07}",
key1.GetPubKey().String())
pub.String())

config.SetPurpose(44)
config.SetCoinType(118)
Expand All @@ -92,7 +96,12 @@ func Test_runAddCmdLedger(t *testing.T) {
mockIn := testutil.ApplyMockIODiscardOutErr(cmd)
kbHome := t.TempDir()

clientCtx := client.Context{}.WithKeyringDir(kbHome)
encCfg := simapp.MakeTestEncodingConfig()

kb, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendTest, kbHome, mockIn, encCfg.Codec)
require.NoError(t, err)

clientCtx := client.Context{}.WithKeyringDir(kbHome).WithKeyring(kb).WithCodec(encCfg.Codec)
ctx := context.WithValue(context.Background(), client.ClientContextKey, &clientCtx)

cmd.SetArgs([]string{
Expand All @@ -101,15 +110,13 @@ func Test_runAddCmdLedger(t *testing.T) {
fmt.Sprintf("--%s=%s", cli.OutputFlag, OutputFormatText),
fmt.Sprintf("--%s=%s", flags.FlagKeyAlgorithm, string(hd.Secp256k1Type)),
fmt.Sprintf("--%s=%d", flagCoinType, sdk.CoinType),
fmt.Sprintf("--%s=%s", flags.FlagKeyringBackend, keyring.BackendTest),
})
mockIn.Reset("test1234\ntest1234\n")

require.NoError(t, cmd.ExecuteContext(ctx))

// Now check that it has been stored properly
kb, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendTest, kbHome, mockIn)
require.NoError(t, err)

require.NotNil(t, kb)
t.Cleanup(func() {
_ = kb.Delete("keyname1")
Expand All @@ -120,11 +127,12 @@ func Test_runAddCmdLedger(t *testing.T) {
require.NoError(t, err)
require.NotNil(t, key1)

require.Equal(t, "keyname1", key1.GetName())
require.Equal(t, keyring.TypeLedger, key1.GetType())
require.Equal(t, "keyname1", key1.Name)
pub, err := key1.GetPubKey()
require.NoError(t, err)
require.Equal(t,
"PubKeySecp256k1{034FEF9CD7C4C63588D3B03FEB5281B9D232CBA34D6F3D71AEE59211FFBFE1FE87}",
key1.GetPubKey().String())
pub.String())
}

func Test_runAddCmdLedgerDryRun(t *testing.T) {
Expand Down Expand Up @@ -155,17 +163,20 @@ func Test_runAddCmdLedgerDryRun(t *testing.T) {
for _, tt := range testData {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Log("tt.name", tt.name)
cmd := AddKeyCommand()
cmd.Flags().AddFlagSet(Commands("home").PersistentFlags())

kbHome := t.TempDir()
mockIn := testutil.ApplyMockIODiscardOutErr(cmd)
kb, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendTest, kbHome, mockIn)
cdc := simapp.MakeTestEncodingConfig().Codec
kb, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendTest, kbHome, mockIn, cdc)
require.NoError(t, err)

clientCtx := client.Context{}.
WithKeyringDir(kbHome).
WithKeyring(kb)
WithKeyring(kb).
WithCodec(cdc)
ctx := context.WithValue(context.Background(), client.ClientContextKey, &clientCtx)

b := bytes.NewBufferString("")
Expand All @@ -184,7 +195,7 @@ func Test_runAddCmdLedgerDryRun(t *testing.T) {
} else {
_, err = kb.Key("testkey")
require.Error(t, err)
require.Equal(t, "testkey.info: key not found", err.Error())
require.Equal(t, "testkey: key not found", err.Error())
}
})
}
Expand Down
18 changes: 9 additions & 9 deletions client/keys/add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ func Test_runAddCmdBasic(t *testing.T) {
mockIn := testutil.ApplyMockIODiscardOutErr(cmd)
kbHome := t.TempDir()

kb, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendTest, kbHome, mockIn)
encCfg := simapp.MakeTestEncodingConfig()

kb, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendTest, kbHome, mockIn, encCfg.Codec)
require.NoError(t, err)

clientCtx := client.Context{}.WithKeyringDir(kbHome)
clientCtx := client.Context{}.WithKeyringDir(kbHome).WithKeyring(kb).WithCodec(encCfg.Codec)
ctx := context.WithValue(context.Background(), client.ClientContextKey, &clientCtx)

t.Cleanup(func() {
Expand All @@ -43,7 +45,6 @@ func Test_runAddCmdBasic(t *testing.T) {
fmt.Sprintf("--%s=%s", flags.FlagHome, kbHome),
fmt.Sprintf("--%s=%s", cli.OutputFlag, OutputFormatText),
fmt.Sprintf("--%s=%s", flags.FlagKeyAlgorithm, string(hd.Secp256k1Type)),
fmt.Sprintf("--%s=%s", flags.FlagKeyringBackend, keyring.BackendTest),
})
mockIn.Reset("y\n")
require.NoError(t, cmd.ExecuteContext(ctx))
Expand All @@ -56,7 +57,6 @@ func Test_runAddCmdBasic(t *testing.T) {
fmt.Sprintf("--%s=%s", flags.FlagHome, kbHome),
fmt.Sprintf("--%s=%s", cli.OutputFlag, OutputFormatText),
fmt.Sprintf("--%s=%s", flags.FlagKeyAlgorithm, string(hd.Secp256k1Type)),
fmt.Sprintf("--%s=%s", flags.FlagKeyringBackend, keyring.BackendTest),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since keyring.BackendTest is already set in kb, there is no need to set flags.FlagKeyringBackend one more time.

})

require.NoError(t, cmd.ExecuteContext(ctx))
Expand All @@ -70,7 +70,6 @@ func Test_runAddCmdBasic(t *testing.T) {
fmt.Sprintf("--%s=%s", flags.FlagHome, kbHome),
fmt.Sprintf("--%s=%s", cli.OutputFlag, OutputFormatText),
fmt.Sprintf("--%s=%s", flags.FlagKeyAlgorithm, string(hd.Secp256k1Type)),
fmt.Sprintf("--%s=%s", flags.FlagKeyringBackend, keyring.BackendTest),
})

require.NoError(t, cmd.ExecuteContext(ctx))
Expand Down Expand Up @@ -188,7 +187,8 @@ func Test_runAddCmdDryRun(t *testing.T) {

kbHome := t.TempDir()
mockIn := testutil.ApplyMockIODiscardOutErr(cmd)
kb, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendTest, kbHome, mockIn)
encCfg := simapp.MakeTestEncodingConfig()
kb, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendTest, kbHome, mockIn, encCfg.Codec)
require.NoError(t, err)

appCodec := simapp.MakeTestEncodingConfig().Codec
Expand Down Expand Up @@ -216,13 +216,13 @@ func Test_runAddCmdDryRun(t *testing.T) {
_, err = kb.Key("testkey")
require.NoError(t, err)

out, err := ioutil.ReadAll(b)
_, err := ioutil.ReadAll(b)
require.NoError(t, err)
require.Contains(t, string(out), "name: testkey")

} else {
_, err = kb.Key("testkey")
require.Error(t, err)
require.Equal(t, "testkey.info: key not found", err.Error())
require.Equal(t, "testkey: key not found", err.Error())
}
})
}
Expand Down
4 changes: 2 additions & 2 deletions client/keys/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ private keys stored in a ledger device cannot be deleted with the CLI.
}

for _, name := range args {
info, err := clientCtx.Keyring.Key(name)
k, err := clientCtx.Keyring.Key(name)
if err != nil {
return err
}
Expand All @@ -53,7 +53,7 @@ private keys stored in a ledger device cannot be deleted with the CLI.
return err
}

if info.GetType() == keyring.TypeLedger || info.GetType() == keyring.TypeOffline {
if k.GetType() == keyring.TypeLedger || k.GetType() == keyring.TypeOffline {
cmd.PrintErrln("Public key reference deleted")
continue
}
Expand Down
Loading