fix: Migrate key names correctly (#10328)

<!--
The default pull request template is for types feat, fix, or refactor.
For other templates, add one of the following parameters to the url:
- template=docs.md
- template=other.md
-->

## Description

Closes: #10219 

Migrate key names correctly when migrating from v0.42.9 to master keyring

---

### Author Checklist

*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*

I have...

- [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [ ] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules)
- [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing)
- [ ] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [ ] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*

I have...

- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed 
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)
This commit is contained in:
likhita-809 2021-10-26 16:36:44 +05:30 committed by GitHub
parent d6699edfa9
commit 3cc5658c9e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 24 additions and 16 deletions

View File

@ -1,4 +1,5 @@
//+build ledger test_ledger_mock
//go:build ledger || test_ledger_mock
// +build ledger test_ledger_mock
package keys
@ -194,7 +195,7 @@ func Test_runAddCmdLedgerDryRun(t *testing.T) {
} else {
_, err = kb.Key("testkey")
require.Error(t, err)
require.Equal(t, "testkey: key not found", err.Error())
require.Equal(t, "testkey.info: key not found", err.Error())
}
})
}

View File

@ -8,7 +8,6 @@ import (
"testing"
"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/libs/cli"
"github.com/cosmos/cosmos-sdk/client"
@ -226,7 +225,7 @@ func Test_runAddCmdDryRun(t *testing.T) {
} else {
_, err = kb.Key("testkey")
require.Error(t, err)
require.Equal(t, "testkey: key not found", err.Error())
require.Equal(t, "testkey.info: key not found", err.Error())
}
})
}

View File

@ -3,11 +3,11 @@ package keys
import (
"bufio"
"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 (

View File

@ -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{

View File

@ -4,10 +4,11 @@ import (
"bufio"
"fmt"
"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.

View File

@ -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{

View File

@ -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"
@ -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
@ -440,6 +440,8 @@ func (ks keystore) Rename(oldName, newName string) error {
return nil
}
// Delete deletes a key in the keyring. `uid` represents the key name, without
// the `.info` suffix.
func (ks keystore) Delete(uid string) error {
k, err := ks.Key(uid)
if err != nil {
@ -456,7 +458,7 @@ func (ks keystore) Delete(uid string) error {
return err
}
err = ks.db.Remove(uid)
err = ks.db.Remove(infoKey(uid))
if err != nil {
return err
}
@ -770,7 +772,7 @@ func (ks keystore) writeRecord(k *Record) error {
return err
}
key := k.Name
key := infoKey(k.Name)
exists, err := ks.existsInDb(addr, key)
if err != nil {
@ -877,6 +879,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, infoSuffix)) && !(strings.HasPrefix(key, sdk.Bech32PrefixAccAddr)) {
key = infoKey(key)
}
item, err := ks.db.Get(key)
if err != nil {
return nil, false, wrapKeyNotFound(err, key)

View File

@ -7,7 +7,7 @@ import (
"testing"
"github.com/99designs/keyring"
bip39 "github.com/cosmos/go-bip39"
"github.com/cosmos/go-bip39"
"github.com/stretchr/testify/require"
"github.com/cosmos/cosmos-sdk/codec"
@ -432,7 +432,7 @@ func TestKeyringKeybaseExportImportPrivKey(t *testing.T) {
// try export non existing key
_, err = kb.ExportPrivKeyArmor("john3", "wrongpassword")
require.EqualError(t, err, "john3: key not found")
require.EqualError(t, err, "john3.info: key not found")
}
func TestInMemoryLanguage(t *testing.T) {

View File

@ -43,6 +43,8 @@ type legacyLocalInfo struct {
Algo hd.PubKeyType `json:"algo"`
}
func infoKey(name string) string { return fmt.Sprintf("%s.%s", name, infoSuffix) }
// GetType implements Info interface
func (i legacyLocalInfo) GetType() KeyType {
return TypeLocal

View File

@ -37,6 +37,7 @@ const (
// bits of entropy to draw when creating a mnemonic
defaultEntropySize = 256
addressSuffix = "address"
infoSuffix = "info"
)
// KeyType reflects a human-readable type for key listing.

View File

@ -3,9 +3,8 @@ package server
import (
"fmt"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
sdk "github.com/cosmos/cosmos-sdk/types"
)