Skip to content

Commit

Permalink
fix: keys migration issue (cosmos#12122) (cosmos#12128)
Browse files Browse the repository at this point in the history
  • Loading branch information
mergify[bot] authored and Sai Kumar committed Jun 21, 2022
1 parent 3f35c32 commit 629980b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (store) [\#11117](https://github.com/cosmos/cosmos-sdk/pull/11117) Fix data race in store trace component
* (x/authz) [\#11252](https://github.com/cosmos/cosmos-sdk/pull/11252) Allow insufficient funds error for authz simulation
* (crypto) [\#11298](https://github.com/cosmos/cosmos-sdk/pull/11298) Fix cgo secp signature verification and update libscep256k1 library.
* (crypto) [\#12122](https://github.com/cosmos/cosmos-sdk/pull/12122) Fix keyring migration issue.

### Improvements

Expand Down
10 changes: 6 additions & 4 deletions crypto/keyring/keyring.go
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,10 @@ func (ks keystore) List() ([]*Record, error) {
var res []*Record //nolint:prealloc
sort.Strings(keys)
for _, key := range keys {
if strings.HasSuffix(key, addressSuffix) {
// Recall that each key is twice in the keyring:
// - once with the `.info` suffix, which holds the key info
// - another time with the `.address` suffix, which only holds a reference to its associated `.info` key
if !strings.HasSuffix(key, infoSuffix) {
continue
}

Expand Down Expand Up @@ -872,9 +875,8 @@ func (ks keystore) MigrateAll() error {
}

for _, key := range keys {
// The keyring items with `.address` suffix only holds as Data the
// key name uid, so there's nothing to migrate.
if strings.HasSuffix(key, addressSuffix) {
// The keyring items only with `.info` consists the key info.
if !strings.HasSuffix(key, infoSuffix) {
continue
}

Expand Down
14 changes: 13 additions & 1 deletion crypto/keyring/keyring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package keyring
import (
"encoding/hex"
"fmt"
"os"
"path/filepath"
"strings"
"testing"

Expand Down Expand Up @@ -59,7 +61,8 @@ func TestNewKeyring(t *testing.T) {

func TestKeyManagementKeyRing(t *testing.T) {
cdc := getCodec()
kb, err := New("keybasename", "test", t.TempDir(), nil, cdc)
tempDir := t.TempDir()
kb, err := New("keybasename", "test", tempDir, nil, cdc)
require.NoError(t, err)
require.NotNil(t, cdc)

Expand Down Expand Up @@ -151,6 +154,15 @@ func TestKeyManagementKeyRing(t *testing.T) {
require.NoError(t, err)
require.Equal(t, 1, len(keyS))

// create some random directory inside the keyring directory to check migrate ignores
// all files other than *.info
newPath := filepath.Join(tempDir, "random")
require.NoError(t, os.Mkdir(newPath, 0755))
items, err := os.ReadDir(tempDir)
require.GreaterOrEqual(t, len(items), 2)
keyS, err = kb.List()
require.NoError(t, err)

// addr cache gets nuked - and test skip flag
require.NoError(t, kb.Delete(n2))
}
Expand Down

0 comments on commit 629980b

Please sign in to comment.