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: keys migration issue (backport #12122) #12128

Merged
merged 1 commit into from
Jun 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,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