From ecb219e4e22d55d4b1f488ae4c5121f9ae688545 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Thu, 2 Jun 2022 10:32:32 -0400 Subject: [PATCH] fix: keys migration issue (#12122) (#12128) --- CHANGELOG.md | 1 + crypto/keyring/keyring.go | 10 ++++++---- crypto/keyring/keyring_test.go | 14 +++++++++++++- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 027bebf58d80..60a8dfcea5ad 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/crypto/keyring/keyring.go b/crypto/keyring/keyring.go index 303b0ed07fce..5434a27eed62 100644 --- a/crypto/keyring/keyring.go +++ b/crypto/keyring/keyring.go @@ -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 } @@ -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 } diff --git a/crypto/keyring/keyring_test.go b/crypto/keyring/keyring_test.go index a0a006b4f112..235a66d2687e 100644 --- a/crypto/keyring/keyring_test.go +++ b/crypto/keyring/keyring_test.go @@ -3,6 +3,8 @@ package keyring import ( "encoding/hex" "fmt" + "os" + "path/filepath" "strings" "testing" @@ -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) @@ -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)) }