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

Enter the new keyring interface #5904

Merged
merged 164 commits into from
Apr 8, 2020
Merged
Show file tree
Hide file tree
Changes from 136 commits
Commits
Show all changes
164 commits
Select commit Hold shift + click to select a range
705a417
New keyring interface
Mar 26, 2020
13a973d
temp commit
jgimeno Mar 26, 2020
d5c2018
temp commit
jgimeno Mar 26, 2020
1baf2b6
add test for list
jgimeno Mar 26, 2020
ece95ef
test adding new key
jgimeno Mar 26, 2020
cdada42
add test for list
jgimeno Mar 26, 2020
760b167
implement and test Key
jgimeno Mar 26, 2020
de39a80
create and test GetByAddress
jgimeno Mar 26, 2020
85c4fd5
add delete function
jgimeno Mar 27, 2020
36548a7
add delete by address function
jgimeno Mar 27, 2020
57f7d5a
add new pub key and test
jgimeno Mar 27, 2020
2d22fbd
create function for writing multisign
jgimeno Mar 27, 2020
a9aab77
use addrKeyByString
jgimeno Mar 27, 2020
63fbdf3
implement and test multisig
jgimeno Mar 27, 2020
7255a32
add test for unsupported signing algo
jgimeno Mar 27, 2020
f3e0624
refactor algorithm
jgimeno Mar 27, 2020
0edc2d8
supported algorithm changed
jgimeno Mar 27, 2020
758af07
update al keyring
jgimeno Mar 27, 2020
a990c74
update options
jgimeno Mar 27, 2020
d999ad1
add supported algorithm test and SAveLedgerKey
jgimeno Mar 27, 2020
04b03a7
fix imports
jgimeno Mar 27, 2020
1c736f1
add signature into keyring
jgimeno Mar 30, 2020
9ede030
add method to sign by address
jgimeno Mar 30, 2020
869b35a
implement import export
jgimeno Mar 30, 2020
67c4942
implement the alt keyring
jgimeno Mar 30, 2020
482f55f
finish implementing altKeyring
jgimeno Mar 30, 2020
6434960
refactor thekey
jgimeno Mar 30, 2020
d86b467
fix the tests
jgimeno Mar 30, 2020
a0304e5
fixt ci linter errors
jgimeno Mar 30, 2020
717dae3
fix other lint
jgimeno Mar 30, 2020
f58970d
New keyring interface
Mar 26, 2020
1ff6830
temp commit
jgimeno Mar 26, 2020
21121cb
temp commit
jgimeno Mar 26, 2020
f3e7d8a
add test for list
jgimeno Mar 26, 2020
518ace9
test adding new key
jgimeno Mar 26, 2020
195de90
add test for list
jgimeno Mar 26, 2020
cdc534c
implement and test Key
jgimeno Mar 26, 2020
516ec8b
create and test GetByAddress
jgimeno Mar 26, 2020
1d795bf
add delete function
jgimeno Mar 27, 2020
2e3a8ac
add delete by address function
jgimeno Mar 27, 2020
b94cfff
add new pub key and test
jgimeno Mar 27, 2020
de0976e
create function for writing multisign
jgimeno Mar 27, 2020
46a5436
use addrKeyByString
jgimeno Mar 27, 2020
b475b6c
implement and test multisig
jgimeno Mar 27, 2020
5f961e3
add test for unsupported signing algo
jgimeno Mar 27, 2020
b704745
refactor algorithm
jgimeno Mar 27, 2020
0f4e1ed
supported algorithm changed
jgimeno Mar 27, 2020
a9fdd20
update al keyring
jgimeno Mar 27, 2020
d5ca45e
update options
jgimeno Mar 27, 2020
5c66495
add supported algorithm test and SAveLedgerKey
jgimeno Mar 27, 2020
cc2b794
fix imports
jgimeno Mar 27, 2020
a39fd3d
add signature into keyring
jgimeno Mar 30, 2020
59f9b4d
add method to sign by address
jgimeno Mar 30, 2020
a7c397e
implement import export
jgimeno Mar 30, 2020
dabff2d
implement the alt keyring
jgimeno Mar 30, 2020
639b1ca
finish implementing altKeyring
jgimeno Mar 30, 2020
8ad06f9
refactor thekey
jgimeno Mar 30, 2020
39ab64a
fix the tests
jgimeno Mar 30, 2020
ce541b1
fixt ci linter errors
jgimeno Mar 30, 2020
e98322a
fix other lint
jgimeno Mar 30, 2020
c0201b1
Merge branch 'jonathan/keyring-refactor-interface' of github.com:cosm…
jgimeno Mar 31, 2020
d48b298
Merge branch 'master' into alessio/new-keyring-interface
Mar 31, 2020
04ebcb3
Import interface from jonathan's branch
Mar 31, 2020
7a79f58
Merge branch 'alessio/new-keyring-interface' into jonathan/keyring-re…
Mar 31, 2020
ec5a657
clean up the diff
Mar 31, 2020
f62f1d2
fix tests after merge
jgimeno Mar 31, 2020
fbf08c2
add interface for signing algo
jgimeno Mar 31, 2020
54f27e1
add option to provide signingalgos and testse
jgimeno Mar 31, 2020
dbeea70
change Name to String in AltSigningAlgo
jgimeno Mar 31, 2020
0f73f74
Revert "change Name to String in AltSigningAlgo"
jgimeno Mar 31, 2020
b06b3ec
Update crypto/keyring/altkeyring.go
jgimeno Apr 1, 2020
f4e9e83
Merge branch 'master' into alessio/new-keyring-interface
Apr 1, 2020
fd74489
Merge pull request #5879 from cosmos/jonathan/keyring-refactor-interface
Apr 1, 2020
5bb0cbd
Rename old SigningAlgo
Apr 1, 2020
02b70cb
Move the new interface into keyring.go
Apr 1, 2020
4474f77
Rename NewAltKeyring() -> New()
Apr 1, 2020
303a334
More renaming
Apr 1, 2020
2f525d2
add constructor
jgimeno Apr 2, 2020
37056bd
update context
jgimeno Apr 2, 2020
ad0284d
update factory tx
jgimeno Apr 2, 2020
773342a
update show
jgimeno Apr 2, 2020
0b55faf
update server init
jgimeno Apr 2, 2020
8bcc106
update tx builder
jgimeno Apr 2, 2020
51e81ee
Switch NewInMemory over new implementation
Apr 2, 2020
b68b0fd
Simplify tests
Apr 2, 2020
1abec01
Don't use infoKey with the new keyring
Apr 2, 2020
6d8031f
Fix server tests
Apr 2, 2020
9b90d3f
Merge branch 'master' into alessio/new-keyring-interface
Apr 2, 2020
d8e4640
Revert "Don't use infoKey with the new keyring"
Apr 2, 2020
c53a452
Fix tests
Apr 2, 2020
519939e
fix armor test
jgimeno Apr 2, 2020
92acbff
migrate some tests
jgimeno Apr 2, 2020
d03a7e3
migrate more tests
jgimeno Apr 2, 2020
93d16c9
migrate TestSignVerifyKeyRing
jgimeno Apr 2, 2020
fc3596c
migrate tests
jgimeno Apr 2, 2020
0b1633f
migrate TestExportImportPubKeyKeyRing function
jgimeno Apr 2, 2020
d0a59fc
migrate some tests
jgimeno Apr 2, 2020
e254e62
New interface Migrator
Apr 2, 2020
572d3f6
Merge branch 'alessio/new-keyring-interface' of github.com:cosmos/cos…
Apr 2, 2020
4ba7e39
solve tests
jgimeno Apr 2, 2020
b90e8f2
Rename
Apr 2, 2020
6ca433e
Merge branch 'alessio/new-keyring-interface' of github.com:cosmos/cos…
Apr 2, 2020
87ef1cb
migrate more tests
jgimeno Apr 2, 2020
36cc921
migrate tests
jgimeno Apr 2, 2020
88489da
Add tests
Apr 2, 2020
5128749
Merge branch 'master' into alessio/new-keyring-interface
Apr 2, 2020
6432e55
migrate more tests
jgimeno Apr 2, 2020
29214ef
migrate more tests
jgimeno Apr 2, 2020
40b32b6
remove export object
jgimeno Apr 2, 2020
2fbd136
migrate more tests
jgimeno Apr 2, 2020
095fcb7
migrate unit tests
jgimeno Apr 2, 2020
1c37647
end migrate tests
jgimeno Apr 2, 2020
97f6c8e
Update client tests
Apr 2, 2020
b211603
Substitue deprecate types with new ones
Apr 2, 2020
c974a9f
Remove baseKeybase
Apr 2, 2020
412ba96
Make golangci-lint happy
Apr 2, 2020
367ffd3
Merge branch 'master' into alessio/new-keyring-interface
Apr 2, 2020
11934f7
Rename, cleanup
Apr 2, 2020
cd85d7c
Cleanup
Apr 2, 2020
2af9073
Tidy up
Apr 2, 2020
dcb05ae
Tidy up, more tests
Apr 2, 2020
fd7ca14
temporarily fix singleton
Apr 2, 2020
1a1717b
remove unused members
Apr 2, 2020
b37a2f7
Try remove config singleton
Apr 2, 2020
6a2a1f5
Fix race condition, remove config from keyring
Apr 2, 2020
0794cc9
Merge branch 'master' into alessio/new-keyring-interface
Apr 3, 2020
7ef86c8
fix bug when importing from same seed but different keys made that ke…
jgimeno Apr 3, 2020
321a574
improve tests
Apr 3, 2020
abf382c
change options struct to make it public
jgimeno Apr 3, 2020
78508f1
move algo
jgimeno Apr 3, 2020
df54d24
refactor naming
jgimeno Apr 3, 2020
c044b6f
add types
jgimeno Apr 3, 2020
82d91a5
renama package
jgimeno Apr 3, 2020
5e49fba
make format
jgimeno Apr 3, 2020
3fd18c1
refactor some naming
jgimeno Apr 3, 2020
a81c3f5
add test
Apr 4, 2020
0a4422d
Proposed interface
Apr 4, 2020
f33a26d
Fix linter warning
Apr 4, 2020
4068b60
Rename, tests
Apr 4, 2020
8392cd5
Rename
Apr 4, 2020
7c7d284
Merge branch 'master' into alessio/new-keyring-interface
Apr 4, 2020
35ab52c
Tidy up crypto/hd tests
Apr 5, 2020
e0c825d
Create a doc.go
Apr 5, 2020
fe68ac0
Merge branch 'master' into alessio/new-keyring-interface
Apr 6, 2020
128d58d
add better existance checking
jgimeno Apr 6, 2020
5b91eda
refactor and fix check
jgimeno Apr 6, 2020
fdc522a
bits of refactoring
Apr 6, 2020
4cea6bf
Add few more doc strings
Apr 6, 2020
2e5e287
add checking to overwrite
jgimeno Apr 6, 2020
acb5e7f
fix bugs
Apr 6, 2020
8939284
Merge branch 'master' into alessio/new-keyring-interface
Apr 6, 2020
c16d93e
client/input: remove unused GetCheckPassword
Apr 6, 2020
fc81ef9
client/input: remove redundant PrintPrefixed function
Apr 6, 2020
cbfbc3e
client/input: GetConfirmation takes io.Writer to communicate with users.
Apr 6, 2020
e40b978
Simplify, don't include app name in keyring directories
Apr 6, 2020
2a674f7
tidy up, few more tests
Apr 6, 2020
67eb038
Split tests that require ledger support
Apr 6, 2020
70ca9a7
Instantiate in-memory if --gen-only is passed
Apr 7, 2020
4b8d3a1
Fallback to in memory backend
Apr 7, 2020
e72eb95
Update CHANGELOG
Apr 7, 2020
ef8eed5
Merge branch 'master' into alessio/new-keyring-interface
Apr 7, 2020
640cbec
Sort changelog, tidy up
Apr 7, 2020
2d0f3c3
Fix CHANGELOG formatting
Apr 7, 2020
c7e7534
Merge branch 'master' into alessio/new-keyring-interface
Apr 8, 2020
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
7 changes: 3 additions & 4 deletions client/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ type CLIContext struct {
Client rpcclient.Client
ChainID string
Marshaler codec.Marshaler
Keybase keyring.Keybase
jgimeno marked this conversation as resolved.
Show resolved Hide resolved
Input io.Reader
Output io.Writer
OutputFormat string
Expand Down Expand Up @@ -321,20 +320,20 @@ func GetFromFields(input io.Reader, from string, genOnly bool) (sdk.AccAddress,
return addr, "", nil
}

keybase, err := keyring.NewKeyring(sdk.KeyringServiceName(),
keybase, err := keyring.New(sdk.KeyringServiceName(),
viper.GetString(flags.FlagKeyringBackend), viper.GetString(flags.FlagHome), input)
if err != nil {
return nil, "", err
}

var info keyring.Info
if addr, err := sdk.AccAddressFromBech32(from); err == nil {
info, err = keybase.GetByAddress(addr)
info, err = keybase.KeyByAddress(addr)
if err != nil {
return nil, "", err
}
} else {
info, err = keybase.Get(from)
info, err = keybase.Key(from)
if err != nil {
return nil, "", err
}
Expand Down
43 changes: 21 additions & 22 deletions client/keys/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ import (
"io"
"sort"

algo2 "github.com/cosmos/cosmos-sdk/crypto/privkey"

bip39 "github.com/cosmos/go-bip39"

"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/input"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/cosmos/cosmos-sdk/crypto/keys/hd"
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/spf13/cobra"
Expand All @@ -27,6 +30,7 @@ const (
flagInteractive = "interactive"
flagRecover = "recover"
flagNoBackup = "no-backup"
flagCoinType = "coin-type"
flagAccount = "account"
flagIndex = "index"
flagMultisig = "multisig"
Expand Down Expand Up @@ -73,19 +77,20 @@ the flag --nosort is set.
cmd.Flags().Bool(flagNoBackup, false, "Don't print out seed phrase (if others are watching the terminal)")
cmd.Flags().Bool(flags.FlagDryRun, false, "Perform action, but don't add key to local keystore")
cmd.Flags().String(flagHDPath, "", "Manual HD Path derivation (overrides BIP44 config)")
cmd.Flags().Uint32(flagCoinType, sdk.CoinType, "coin type number for HD derivation")
cmd.Flags().Uint32(flagAccount, 0, "Account number for HD derivation")
cmd.Flags().Uint32(flagIndex, 0, "Address index number for HD derivation")
cmd.Flags().Bool(flags.FlagIndentResponse, false, "Add indent to JSON response")
cmd.Flags().String(flagKeyAlgo, string(keyring.Secp256k1), "Key signing algorithm to generate keys for")
cmd.Flags().String(flagKeyAlgo, string(algo2.Secp256k1Type), "Key signing algorithm to generate keys for")
return cmd
}

func getKeybase(transient bool, buf io.Reader) (keyring.Keybase, error) {
func getKeybase(transient bool, buf io.Reader) (keyring.Keyring, error) {
if transient {
return keyring.NewKeyring(sdk.KeyringServiceName(), keyring.BackendMemory, viper.GetString(flags.FlagHome), buf)
return keyring.New(sdk.KeyringServiceName(), keyring.BackendMemory, viper.GetString(flags.FlagHome), buf)
}

return keyring.NewKeyring(sdk.KeyringServiceName(), viper.GetString(flags.FlagKeyringBackend), viper.GetString(flags.FlagHome), buf)
return keyring.New(sdk.KeyringServiceName(), viper.GetString(flags.FlagKeyringBackend), viper.GetString(flags.FlagHome), buf)
}

func runAddCmd(cmd *cobra.Command, args []string) error {
Expand All @@ -107,24 +112,21 @@ input
output
- armor encrypted private key (saved to file)
*/
func RunAddCmd(cmd *cobra.Command, args []string, kb keyring.Keybase, inBuf *bufio.Reader) error {
func RunAddCmd(cmd *cobra.Command, args []string, kb keyring.Keyring, inBuf *bufio.Reader) error {
var err error

name := args[0]

interactive := viper.GetBool(flagInteractive)
showMnemonic := !viper.GetBool(flagNoBackup)

algo := keyring.SigningAlgo(viper.GetString(flagKeyAlgo))
if algo == keyring.SigningAlgo("") {
algo = keyring.Secp256k1
}
if !keyring.IsSupportedAlgorithm(kb.SupportedAlgos(), algo) {
return keyring.ErrUnsupportedSigningAlgo
algo, err := keyring.NewSigningAlgoFromString(viper.GetString(flagKeyAlgo))
if err != nil {
algo = algo2.Secp256k1
}

if !viper.GetBool(flags.FlagDryRun) {
_, err = kb.Get(name)
_, err = kb.Key(name)
if err == nil {
// account exists, ask for user confirmation
response, err2 := input.GetConfirmation(fmt.Sprintf("override the existing name %s", name), inBuf)
Expand All @@ -146,7 +148,7 @@ func RunAddCmd(cmd *cobra.Command, args []string, kb keyring.Keybase, inBuf *buf
}

for _, keyname := range multisigKeys {
k, err := kb.Get(keyname)
k, err := kb.Key(keyname)
if err != nil {
return err
}
Expand All @@ -161,7 +163,7 @@ func RunAddCmd(cmd *cobra.Command, args []string, kb keyring.Keybase, inBuf *buf
}

pk := multisig.NewPubKeyMultisigThreshold(multisigThreshold, pks)
if _, err := kb.CreateMulti(name, pk); err != nil {
if _, err := kb.SaveMultisig(name, pk); err != nil {
return err
}

Expand All @@ -175,21 +177,22 @@ func RunAddCmd(cmd *cobra.Command, args []string, kb keyring.Keybase, inBuf *buf
if err != nil {
return err
}
_, err = kb.CreateOffline(name, pk, algo)
_, err = kb.SavePubKey(name, pk, algo.Name())
if err != nil {
return err
}
return nil
}

coinType := uint32(viper.GetInt(flagCoinType))
account := uint32(viper.GetInt(flagAccount))
index := uint32(viper.GetInt(flagIndex))

useBIP44 := !viper.IsSet(flagHDPath)
var hdPath string

if useBIP44 {
hdPath = keyring.CreateHDPath(account, index).String()
hdPath = hd.CreateHDPath(coinType, account, index).String()
} else {
hdPath = viper.GetString(flagHDPath)
}
Expand All @@ -201,12 +204,8 @@ func RunAddCmd(cmd *cobra.Command, args []string, kb keyring.Keybase, inBuf *buf
return errors.New("cannot set custom bip32 path with ledger")
}

if !keyring.IsSupportedAlgorithm(kb.SupportedAlgosLedger(), algo) {
return keyring.ErrUnsupportedSigningAlgo
}

bech32PrefixAccAddr := sdk.GetConfig().GetBech32AccountAddrPrefix()
info, err := kb.CreateLedger(name, keyring.Secp256k1, bech32PrefixAccAddr, account, index)
info, err := kb.SaveLedgerKey(name, algo2.Secp256k1, bech32PrefixAccAddr, coinType, account, index)
if err != nil {
return err
}
Expand Down Expand Up @@ -269,7 +268,7 @@ func RunAddCmd(cmd *cobra.Command, args []string, kb keyring.Keybase, inBuf *buf
}
}

info, err := kb.CreateAccount(name, mnemonic, bip39Passphrase, DefaultKeyPass, hdPath, algo)
info, err := kb.NewAccount(name, mnemonic, bip39Passphrase, hdPath, algo)
if err != nil {
return err
}
Expand Down
16 changes: 10 additions & 6 deletions client/keys/add_ledger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ func Test_runAddCmdLedgerWithCustomCoinType(t *testing.T) {
t.Cleanup(kbCleanUp)
viper.Set(flags.FlagHome, kbHome)
viper.Set(flags.FlagUseLedger, true)
viper.Set(flagAccount, "0")
viper.Set(flagIndex, "0")
viper.Set(flagCoinType, "330")

/// Test Text
viper.Set(cli.OutputFlag, OutputFormatText)
Expand All @@ -50,14 +53,14 @@ func Test_runAddCmdLedgerWithCustomCoinType(t *testing.T) {
require.NoError(t, runAddCmd(cmd, []string{"keyname1"}))

// Now check that it has been stored properly
kb, err := keyring.NewKeyring(sdk.KeyringServiceName(), viper.GetString(flags.FlagKeyringBackend), viper.GetString(flags.FlagHome), mockIn)
kb, err := keyring.New(sdk.KeyringServiceName(), viper.GetString(flags.FlagKeyringBackend), viper.GetString(flags.FlagHome), mockIn)
require.NoError(t, err)
require.NotNil(t, kb)
t.Cleanup(func() {
kb.Delete("keyname1", "", false)
kb.Delete("keyname1")
})
mockIn.Reset("test1234\n")
key1, err := kb.Get("keyname1")
key1, err := kb.Key("keyname1")
require.NoError(t, err)
require.NotNil(t, key1)

Expand Down Expand Up @@ -90,17 +93,18 @@ func Test_runAddCmdLedger(t *testing.T) {
viper.Set(cli.OutputFlag, OutputFormatText)
// Now enter password
mockIn.Reset("test1234\ntest1234\n")
viper.Set(flagCoinType, sdk.CoinType)
require.NoError(t, runAddCmd(cmd, []string{"keyname1"}))

// Now check that it has been stored properly
kb, err := keyring.NewKeyring(sdk.KeyringServiceName(), viper.GetString(flags.FlagKeyringBackend), kbHome, mockIn)
kb, err := keyring.New(sdk.KeyringServiceName(), viper.GetString(flags.FlagKeyringBackend), kbHome, mockIn)
require.NoError(t, err)
require.NotNil(t, kb)
t.Cleanup(func() {
kb.Delete("keyname1", "", false)
kb.Delete("keyname1")
})
mockIn.Reset("test1234\n")
key1, err := kb.Get("keyname1")
key1, err := kb.Key("keyname1")
require.NoError(t, err)
require.NotNil(t, key1)

Expand Down
6 changes: 3 additions & 3 deletions client/keys/add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ func Test_runAddCmdBasic(t *testing.T) {
viper.Set(cli.OutputFlag, OutputFormatText)

mockIn.Reset("y\n")
kb, err := keyring.NewKeyring(sdk.KeyringServiceName(), viper.GetString(flags.FlagKeyringBackend), kbHome, mockIn)
kb, err := keyring.New(sdk.KeyringServiceName(), viper.GetString(flags.FlagKeyringBackend), kbHome, mockIn)
require.NoError(t, err)
t.Cleanup(func() {
kb.Delete("keyname1", "", false) // nolint:errcheck
kb.Delete("keyname2", "", false) // nolint:errcheck
kb.Delete("keyname1") // nolint:errcheck
kb.Delete("keyname2") // nolint:errcheck
})
assert.NoError(t, runAddCmd(cmd, []string{"keyname1"}))

Expand Down
24 changes: 8 additions & 16 deletions client/keys/codec_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package keys
package keys_test

import (
"fmt"
"reflect"
"bytes"
"testing"

"github.com/stretchr/testify/require"

"github.com/cosmos/cosmos-sdk/client/keys"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
)

Expand Down Expand Up @@ -58,15 +58,9 @@ func TestMarshalJSON(t *testing.T) {
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
got, err := MarshalJSON(tt.args.o)
if (err != nil) != tt.wantErr {
t.Errorf("MarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
return
}
fmt.Printf("%s\n", got)
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("MarshalJSON() = %v, want %v", got, tt.want)
}
got, err := keys.MarshalJSON(tt.args.o)
require.Equal(t, tt.wantErr, err != nil)
require.True(t, bytes.Equal(got, tt.want))
})
}
}
Expand Down Expand Up @@ -94,10 +88,8 @@ func TestUnmarshalJSON(t *testing.T) {
for idx, tt := range tests {
idx, tt := idx, tt
t.Run(tt.name, func(t *testing.T) {
if err := UnmarshalJSON(tt.args.bz, tt.args.ptr); (err != nil) != tt.wantErr {
t.Errorf("unmarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
}

err := keys.UnmarshalJSON(tt.args.bz, tt.args.ptr)
require.Equal(t, tt.wantErr, err != nil)
// Confirm deserialized objects are the same
require.Equal(t, data.Keys[idx], data.Answers[idx])
})
Expand Down
40 changes: 13 additions & 27 deletions client/keys/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package keys

import (
"bufio"
"errors"

"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/input"
Expand Down Expand Up @@ -43,49 +42,36 @@ private keys stored in a ledger device cannot be deleted with the CLI.
func runDeleteCmd(cmd *cobra.Command, args []string) error {
buf := bufio.NewReader(cmd.InOrStdin())

kb, err := keyring.NewKeyring(sdk.KeyringServiceName(), viper.GetString(flags.FlagKeyringBackend), viper.GetString(flags.FlagHome), buf)
kb, err := keyring.New(sdk.KeyringServiceName(), viper.GetString(flags.FlagKeyringBackend), viper.GetString(flags.FlagHome), buf)
if err != nil {
return err
}

for _, name := range args {
info, err := kb.Get(name)
info, err := kb.Key(name)
if err != nil {
return err
}

if info.GetType() == keyring.TypeLedger || info.GetType() == keyring.TypeOffline {
// confirm deletion, unless -y is passed
if !viper.GetBool(flagYes) {
if err := confirmDeletion(buf); err != nil {
return err
}
}

if err := kb.Delete(name, "", true); err != nil {
// confirm deletion, unless -y is passed
if !viper.GetBool(flagYes) {
if yes, err := input.GetConfirmation("Key reference will be deleted. Continue?", buf); err != nil {
return err
} else if !yes {
continue
}
cmd.PrintErrln("Public key reference deleted")
return nil
}

// old password and skip flag arguments are ignored
if err := kb.Delete(name, "", true); err != nil {
if err := kb.Delete(name); err != nil {
return err
}

if info.GetType() == keyring.TypeLedger || info.GetType() == keyring.TypeOffline {
cmd.PrintErrln("Public key reference deleted")
continue
}
cmd.PrintErrln("Key deleted forever (uh oh!)")
}

return nil
}

func confirmDeletion(buf *bufio.Reader) error {
answer, err := input.GetConfirmation("Key reference will be deleted. Continue?", buf)
if err != nil {
return err
}
if !answer {
return errors.New("aborted")
}
return nil
}
Loading