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

fix: Migrate key names correctly #10328

Merged
merged 35 commits into from
Oct 26, 2021
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
5656854
migrate key names correctly
likhita-809 Oct 7, 2021
7e9400e
fix keynames migration
likhita-809 Oct 8, 2021
9a5da88
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into li…
likhita-809 Oct 8, 2021
603d3a3
fix something
likhita-809 Oct 8, 2021
2b6402f
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into li…
likhita-809 Oct 8, 2021
0ff4ffa
small fix
likhita-809 Oct 8, 2021
ea1ef5f
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into li…
likhita-809 Oct 11, 2021
2b173f2
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into li…
likhita-809 Oct 11, 2021
f6f5e0d
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into li…
likhita-809 Oct 11, 2021
3c3cc74
migrate key names correctly
likhita-809 Oct 12, 2021
9b10a09
address review comments
likhita-809 Oct 13, 2021
ad7220e
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into li…
likhita-809 Oct 13, 2021
b23c1b9
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into li…
likhita-809 Oct 13, 2021
5936464
fix failing tests
likhita-809 Oct 13, 2021
73ba70b
fix failing tests
likhita-809 Oct 13, 2021
7ccdf66
try fix failing tests
likhita-809 Oct 13, 2021
0d5c4c7
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into li…
likhita-809 Oct 13, 2021
48ab5ed
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into li…
likhita-809 Oct 14, 2021
0c81146
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into li…
likhita-809 Oct 18, 2021
7565c92
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into li…
likhita-809 Oct 18, 2021
db383eb
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into li…
likhita-809 Oct 18, 2021
7e65f07
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into li…
likhita-809 Oct 18, 2021
8cb5ced
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into li…
likhita-809 Oct 19, 2021
1b48b73
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into li…
likhita-809 Oct 19, 2021
87c2cec
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into li…
likhita-809 Oct 20, 2021
7207fee
address review comments
likhita-809 Oct 20, 2021
de79be4
fix failing tests
likhita-809 Oct 20, 2021
1552a13
small fix
likhita-809 Oct 20, 2021
99fdeca
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into li…
likhita-809 Oct 20, 2021
7247840
small fix
likhita-809 Oct 20, 2021
079aa5f
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into li…
likhita-809 Oct 21, 2021
574065b
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into li…
likhita-809 Oct 22, 2021
f3cb280
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into li…
likhita-809 Oct 25, 2021
a221b7f
fix nits
likhita-809 Oct 26, 2021
0d375f1
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into li…
likhita-809 Oct 26, 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
9 changes: 5 additions & 4 deletions client/keys/add_ledger_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//+build ledger test_ledger_mock
//go:build ledger || test_ledger_mock
// +build ledger test_ledger_mock
likhita-809 marked this conversation as resolved.
Show resolved Hide resolved

package keys

Expand Down Expand Up @@ -185,16 +186,16 @@ func Test_runAddCmdLedgerDryRun(t *testing.T) {
require.NoError(t, cmd.ExecuteContext(ctx))

if tt.added {
_, err = kb.Key("testkey")
_, err = kb.Key(string(keyring.InfoKey("testkey")))
require.NoError(t, err)

out, err := io.ReadAll(b)
require.NoError(t, err)
require.Contains(t, string(out), "name: testkey")
} else {
_, err = kb.Key("testkey")
_, err = kb.Key(string(keyring.InfoKey("testkey")))
require.Error(t, err)
require.Equal(t, "testkey: key not found", err.Error())
require.Equal(t, "testkey.info: key not found", err.Error())
likhita-809 marked this conversation as resolved.
Show resolved Hide resolved
}
})
}
Expand Down
15 changes: 7 additions & 8 deletions client/keys/add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"testing"

"github.com/stretchr/testify/require"

"github.com/tendermint/tendermint/libs/cli"

"github.com/cosmos/cosmos-sdk/client"
Expand Down Expand Up @@ -65,8 +64,8 @@ func Test_runAddCmdBasic(t *testing.T) {
require.NoError(t, cmd.ExecuteContext(ctx))
require.Error(t, cmd.ExecuteContext(ctx))

mockIn.Reset("y\n")
require.NoError(t, cmd.ExecuteContext(ctx))
mockIn.Reset("N\n")
require.Error(t, cmd.ExecuteContext(ctx))

cmd.SetArgs([]string{
"keyname4",
Expand Down Expand Up @@ -152,7 +151,7 @@ func Test_runAddCmdDryRun(t *testing.T) {
args: []string{
"testkey",
fmt.Sprintf("--%s=%s", flags.FlagDryRun, "false"),
fmt.Sprintf("--%s=%s", flagMultisig, "subkey"),
fmt.Sprintf("--%s=%s", flagMultisig, string(keyring.InfoKey("subkey"))),
},
added: true,
},
Expand Down Expand Up @@ -217,16 +216,16 @@ func Test_runAddCmdDryRun(t *testing.T) {
require.NoError(t, cmd.ExecuteContext(ctx))

if tt.added {
_, err := kb.Key("testkey")
_, err := kb.Key(string(keyring.InfoKey("testkey")))
require.NoError(t, err)

out, err := io.ReadAll(b)
require.NoError(t, err)
require.Contains(t, string(out), "name: testkey")
} else {
_, err = kb.Key("testkey")
_, err = kb.Key(string(keyring.InfoKey("testkey")))
require.Error(t, err)
require.Equal(t, "testkey: key not found", err.Error())
require.Equal(t, "testkey.info: key not found", err.Error())
}
})
}
Expand Down Expand Up @@ -272,7 +271,7 @@ func TestAddRecoverFileBackend(t *testing.T) {
})

mockIn.Reset(fmt.Sprintf("%s\n%s\n", keyringPassword, keyringPassword))
k, err := kb.Key("keyname1")
k, err := kb.Key(string(keyring.InfoKey("keyname1")))
require.NoError(t, err)
require.Equal(t, "keyname1", k.Name)
}
8 changes: 6 additions & 2 deletions client/keys/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package keys

import (
"bufio"
"strings"

"github.com/spf13/cobra"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/input"
"github.com/cosmos/cosmos-sdk/crypto/keyring"

"github.com/spf13/cobra"
)

const (
Expand Down Expand Up @@ -35,6 +36,9 @@ private keys stored in a ledger device cannot be deleted with the CLI.
}

for _, name := range args {
if !strings.HasSuffix(name, keyring.InfoSuffix) {
name = string(keyring.InfoKey(name))
}
k, err := clientCtx.Keyring.Key(name)
if err != nil {
return err
Expand Down
10 changes: 5 additions & 5 deletions client/keys/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func Test_runDeleteCmd(t *testing.T) {

err = cmd.ExecuteContext(ctx)
require.Error(t, err)
require.EqualError(t, err, "blah: key not found")
require.EqualError(t, err, "blah.info: key not found")

// User confirmation missing
cmd.SetArgs([]string{
Expand All @@ -65,7 +65,7 @@ func Test_runDeleteCmd(t *testing.T) {
require.Error(t, err)
require.Equal(t, "EOF", err.Error())

_, err = kb.Key(fakeKeyName1)
_, err = kb.Key(string(keyring.InfoKey(fakeKeyName1)))
require.NoError(t, err)

// Now there is a confirmation
Expand All @@ -77,10 +77,10 @@ func Test_runDeleteCmd(t *testing.T) {
})
require.NoError(t, cmd.Execute())

_, err = kb.Key(fakeKeyName1)
_, err = kb.Key(string(keyring.InfoKey(fakeKeyName1)))
require.Error(t, err) // Key1 is gone

_, err = kb.Key(fakeKeyName2)
_, err = kb.Key(string(keyring.InfoKey(fakeKeyName2)))
require.NoError(t, err)

cmd.SetArgs([]string{
Expand All @@ -91,6 +91,6 @@ func Test_runDeleteCmd(t *testing.T) {
})
require.NoError(t, cmd.Execute())

_, err = kb.Key(fakeKeyName2)
_, err = kb.Key(string(keyring.InfoKey(fakeKeyName2)))
require.Error(t, err) // Key2 is gone
}
5 changes: 5 additions & 0 deletions client/keys/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keys
import (
"bufio"
"fmt"
"strings"

"github.com/spf13/cobra"

Expand Down Expand Up @@ -39,6 +40,10 @@ and export your keys in ASCII-armored encrypted format.`,
unarmored, _ := cmd.Flags().GetBool(flagUnarmoredHex)
unsafe, _ := cmd.Flags().GetBool(flagUnsafe)

if !strings.HasSuffix(args[0], keyring.InfoSuffix) {
args[0] = string(keyring.InfoKey(args[0]))
}

if unarmored && unsafe {
return exportUnsafeUnarmored(cmd, args[0], buf, clientCtx.Keyring)
} else if unarmored || unsafe {
Expand Down
6 changes: 6 additions & 0 deletions client/keys/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package keys
import (
"bufio"
"os"
"strings"

"github.com/spf13/cobra"

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

// ImportKeyCommand imports private keys from a keyfile.
Expand All @@ -34,6 +36,10 @@ func ImportKeyCommand() *cobra.Command {
return err
}

if !strings.HasSuffix(args[0], keyring.InfoSuffix) {
args[0] = string(keyring.InfoKey(args[0]))
}

return clientCtx.Keyring.ImportPrivKey(args[0], string(bz), passphrase)
},
}
Expand Down
7 changes: 6 additions & 1 deletion client/keys/rename.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package keys
import (
"bufio"
"fmt"
"strings"

"github.com/spf13/cobra"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/input"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/spf13/cobra"
)

// RenameKeyCommand renames a key from the key store.
Expand All @@ -30,6 +32,9 @@ private keys stored in a ledger device cannot be renamed with the CLI.
}

oldName, newName := args[0], args[1]
if !strings.HasSuffix(oldName, keyring.InfoSuffix) {
oldName = string(keyring.InfoKey(oldName))
}

k, err := clientCtx.Keyring.Key(oldName)
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions client/keys/rename_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func Test_runRenameCmd(t *testing.T) {
cmd.SetArgs([]string{"blah", "blaah", fmt.Sprintf("--%s=%s", flags.FlagHome, kbHome)})
err = cmd.ExecuteContext(ctx)
require.Error(t, err)
require.EqualError(t, err, "blah: key not found")
require.EqualError(t, err, "blah.info: key not found")

// User confirmation missing
cmd.SetArgs([]string{
Expand All @@ -62,7 +62,7 @@ func Test_runRenameCmd(t *testing.T) {
require.Error(t, err)
require.Equal(t, "EOF", err.Error())

oldKey, err := kb.Key(fakeKeyName1)
oldKey, err := kb.Key(string(keyring.InfoKey(fakeKeyName1)))
require.NoError(t, err)

// add a confirmation
Expand All @@ -76,11 +76,11 @@ func Test_runRenameCmd(t *testing.T) {
require.NoError(t, cmd.Execute())

// key1 is gone
_, err = kb.Key(fakeKeyName1)
_, err = kb.Key(string(keyring.InfoKey(fakeKeyName1)))
require.Error(t, err)

// key2 exists now
renamedKey, err := kb.Key(fakeKeyName2)
renamedKey, err := kb.Key(string(keyring.InfoKey(fakeKeyName2)))
require.NoError(t, err)
oldPk, err := oldKey.GetPubKey()
require.NoError(t, err)
Expand Down
7 changes: 7 additions & 0 deletions client/keys/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keys
import (
"errors"
"fmt"
"strings"

"github.com/spf13/cobra"
"github.com/tendermint/tendermint/libs/cli"
Expand Down Expand Up @@ -163,6 +164,12 @@ func runShowCmd(cmd *cobra.Command, args []string) (err error) {
}

func fetchKey(kb keyring.Keyring, keyref string) (*keyring.Record, error) {
var key []byte
if !strings.HasPrefix(keyref, "cosmos") && !strings.HasSuffix(keyref, keyring.InfoSuffix) {
key = keyring.InfoKey(keyref)
keyref = string(key)
}

// 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
Expand Down
4 changes: 2 additions & 2 deletions client/keys/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func Test_runShowCmd(t *testing.T) {
ctx := context.WithValue(context.Background(), client.ClientContextKey, &clientCtx)

cmd.SetArgs([]string{"invalid"})
require.EqualError(t, cmd.ExecuteContext(ctx), "invalid is not a valid name or address: decoding bech32 failed: invalid bech32 string length 7")
require.EqualError(t, cmd.ExecuteContext(ctx), "invalid is not a valid name or address: decoding bech32 failed: invalid separator index -1")

cmd.SetArgs([]string{"invalid1", "invalid2"})
require.EqualError(t, cmd.ExecuteContext(ctx), "invalid1 is not a valid name or address: decoding bech32 failed: invalid separator index 7")
Expand Down Expand Up @@ -103,7 +103,7 @@ func Test_runShowCmd(t *testing.T) {
require.NoError(t, cmd.ExecuteContext(ctx))

// try fetch by addr
k, err := kb.Key(fakeKeyName1)
k, err := kb.Key(string(keyring.InfoKey(fakeKeyName1)))
require.NoError(t, err)
addr, err := k.GetAddress()
require.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion client/tx/tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func TestSign(t *testing.T) {
// create a new key using a mnemonic generator and test if we can reuse seed to recreate that account
_, seed, err := kb.NewMnemonic(from1, keyring.English, path, keyring.DefaultBIP39Passphrase, hd.Secp256k1)
requireT.NoError(err)
requireT.NoError(kb.Delete(from1))
requireT.NoError(kb.Delete(string(keyring.InfoKey(from1))))
k1, _, err := kb.NewMnemonic(from1, keyring.English, path, keyring.DefaultBIP39Passphrase, hd.Secp256k1)
requireT.NoError(err)

Expand Down
13 changes: 8 additions & 5 deletions crypto/keyring/keyring.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"strings"

"github.com/99designs/keyring"
"github.com/cosmos/go-bip39"
"github.com/pkg/errors"
"github.com/tendermint/crypto/bcrypt"
tmcrypto "github.com/tendermint/tendermint/crypto"
Expand All @@ -24,6 +23,7 @@ import (
"github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/go-bip39"
)

// Backend options for Keyring
Expand Down Expand Up @@ -410,7 +410,7 @@ func (ks keystore) DeleteByAddress(address sdk.Address) error {
return err
}

err = ks.Delete(k.Name)
err = ks.Delete(string(InfoKey(k.Name)))
if err != nil {
return err
}
Expand Down Expand Up @@ -770,9 +770,9 @@ func (ks keystore) writeRecord(k *Record) error {
return err
}

key := k.Name
key := InfoKey(k.Name)

exists, err := ks.existsInDb(addr, key)
exists, err := ks.existsInDb(addr, string(key))
if err != nil {
return err
}
Expand All @@ -786,7 +786,7 @@ func (ks keystore) writeRecord(k *Record) error {
}

item := keyring.Item{
Key: key,
Key: string(key),
likhita-809 marked this conversation as resolved.
Show resolved Hide resolved
Data: serializedRecord,
}

Expand Down Expand Up @@ -877,6 +877,9 @@ func (ks keystore) MigrateAll() (bool, error) {

// migrate converts keyring.Item from amino to proto serialization format.
func (ks keystore) migrate(key string) (*Record, bool, error) {
if !(strings.HasSuffix(key, "info")) && !(strings.HasPrefix(key, "cosmos")) {
key = string(InfoKey(key))
}
item, err := ks.db.Get(key)
if err != nil {
return nil, false, wrapKeyNotFound(err, key)
Expand Down
Loading