cosmos-sdk/crypto/keyring/record.go

133 lines
3.5 KiB
Go
Raw Normal View History

refactor!: Keyring migration (#9695) <!-- 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 The draft PR #9222 Closes: #7108 <!-- Add a description of the changes that this PR introduces and the files that are the most critical to review. --> - implement proto definition for `Record` - rename `Info.go` to `legacyInfo.go` within `keyring` package - implement CLI `migrate` command that migrates all keys from legacyInfo to proto according to @robert-zaremba migration [algorithm](https://github.com/cosmos/cosmos-sdk/pull/9222/#discussion_r624683839) - remove legacy keybase entirely. - add `Migrate` and `MigrateAll` functions in `keyring.go` for single key and all keys migration - add tests - fix tests --- ### 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) - [x] 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 - [x] 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)
2021-09-20 05:02:15 -07:00
package keyring
import (
"errors"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/crypto/hd"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/cosmos/cosmos-sdk/types"
)
// ErrPrivKeyExtr is used to output an error if extraction of a private key from Local item fails
var ErrPrivKeyExtr = errors.New("private key extraction works only for Local")
func newRecord(name string, pk cryptotypes.PubKey, item isRecord_Item) (*Record, error) {
any, err := codectypes.NewAnyWithValue(pk)
if err != nil {
return nil, err
}
return &Record{name, any, item}, nil
}
// NewLocalRecord creates a new Record with local key item
func NewLocalRecord(name string, priv cryptotypes.PrivKey, pk cryptotypes.PubKey) (*Record, error) {
any, err := codectypes.NewAnyWithValue(priv)
if err != nil {
return nil, err
}
recordLocal := &Record_Local{any, priv.Type()}
recordLocalItem := &Record_Local_{recordLocal}
return newRecord(name, pk, recordLocalItem)
}
// NewLedgerRecord creates a new Record with ledger item
func NewLedgerRecord(name string, pk cryptotypes.PubKey, path *hd.BIP44Params) (*Record, error) {
recordLedger := &Record_Ledger{path}
recordLedgerItem := &Record_Ledger_{recordLedger}
return newRecord(name, pk, recordLedgerItem)
}
func (rl *Record_Ledger) GetPath() *hd.BIP44Params {
return rl.Path
}
// NewOfflineRecord creates a new Record with offline item
func NewOfflineRecord(name string, pk cryptotypes.PubKey) (*Record, error) {
recordOffline := &Record_Offline{}
recordOfflineItem := &Record_Offline_{recordOffline}
return newRecord(name, pk, recordOfflineItem)
}
// NewMultiRecord creates a new Record with multi item
func NewMultiRecord(name string, pk cryptotypes.PubKey) (*Record, error) {
recordMulti := &Record_Multi{}
recordMultiItem := &Record_Multi_{recordMulti}
return newRecord(name, pk, recordMultiItem)
}
// GetPubKey fetches a public key of the record
func (k *Record) GetPubKey() (cryptotypes.PubKey, error) {
pk, ok := k.PubKey.GetCachedValue().(cryptotypes.PubKey)
if !ok {
return nil, errors.New("unable to cast any to cryptotypes.PubKey")
}
return pk, nil
}
// GetAddress fetches an address of the record
func (k Record) GetAddress() (types.AccAddress, error) {
pk, err := k.GetPubKey()
if err != nil {
return nil, err
}
return pk.Address().Bytes(), nil
}
// GetType fetches type of the record
func (k Record) GetType() KeyType {
switch {
case k.GetLocal() != nil:
return TypeLocal
case k.GetLedger() != nil:
return TypeLedger
case k.GetMulti() != nil:
return TypeMulti
case k.GetOffline() != nil:
return TypeOffline
default:
panic("unrecognized record type")
}
}
// UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces
func (k *Record) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
var pk cryptotypes.PubKey
if err := unpacker.UnpackAny(k.PubKey, &pk); err != nil {
return err
}
if l := k.GetLocal(); l != nil {
var priv cryptotypes.PrivKey
return unpacker.UnpackAny(l.PrivKey, &priv)
}
return nil
}
func extractPrivKeyFromRecord(k *Record) (cryptotypes.PrivKey, error) {
rl := k.GetLocal()
if rl == nil {
return nil, ErrPrivKeyExtr
}
return extractPrivKeyFromLocal(rl)
}
func extractPrivKeyFromLocal(rl *Record_Local) (cryptotypes.PrivKey, error) {
if rl.PrivKey == nil {
return nil, errors.New("private key is not available")
}
priv, ok := rl.PrivKey.GetCachedValue().(cryptotypes.PrivKey)
if !ok {
return nil, errors.New("unable to cast any to cryptotypes.PrivKey")
}
return priv, nil
}