cosmos-sdk/crypto/keyring/record_test.go

140 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 (
"strings"
"testing"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/stretchr/testify/suite"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
)
type RecordTestSuite struct {
suite.Suite
appName string
cdc codec.Codec
priv cryptotypes.PrivKey
pub cryptotypes.PubKey
}
func (s *RecordTestSuite) SetupSuite() {
s.appName = "cosmos"
s.cdc = getCodec()
s.priv = cryptotypes.PrivKey(ed25519.GenPrivKey())
s.pub = s.priv.PubKey()
}
func (s *RecordTestSuite) TestOfflineRecordMarshaling() {
k, err := NewOfflineRecord("testrecord", s.pub)
s.Require().NoError(err)
bz, err := s.cdc.Marshal(k)
s.Require().NoError(err)
var k2 Record
s.Require().NoError(s.cdc.Unmarshal(bz, &k2))
s.Require().Equal(k.Name, k2.Name)
s.Require().True(k.PubKey.Equal(k2.PubKey))
pk2, err := k2.GetPubKey()
s.Require().NoError(err)
s.Require().True(s.pub.Equals(pk2))
}
func (s *RecordTestSuite) TestLocalRecordMarshaling() {
dir := s.T().TempDir()
mockIn := strings.NewReader("")
kb, err := New(s.appName, BackendTest, dir, mockIn, s.cdc)
s.Require().NoError(err)
k, err := NewLocalRecord("testrecord", s.priv, s.pub)
s.Require().NoError(err)
ks, ok := kb.(keystore)
s.Require().True(ok)
bz, err := ks.cdc.Marshal(k)
s.Require().NoError(err)
k2, err := ks.protoUnmarshalRecord(bz)
s.Require().NoError(err)
s.Require().Equal(k.Name, k2.Name)
// not sure if this will work -- we can remove this line, the later check is better.
s.Require().True(k.PubKey.Equal(k2.PubKey))
pub2, err := k2.GetPubKey()
s.Require().NoError(err)
s.Require().True(s.pub.Equals(pub2))
localRecord2 := k2.GetLocal()
s.Require().NotNil(localRecord2)
anyPrivKey, err := codectypes.NewAnyWithValue(s.priv)
s.Require().NoError(err)
s.Require().Equal(localRecord2.PrivKey, anyPrivKey)
s.Require().Equal(localRecord2.PrivKeyType, s.priv.Type())
}
func (s *RecordTestSuite) TestLedgerRecordMarshaling() {
dir := s.T().TempDir()
mockIn := strings.NewReader("")
kb, err := New(s.appName, BackendTest, dir, mockIn, s.cdc)
s.Require().NoError(err)
path := hd.NewFundraiserParams(4, 12345, 57)
k, err := NewLedgerRecord("testrecord", s.pub, path)
s.Require().NoError(err)
ks, ok := kb.(keystore)
s.Require().True(ok)
bz, err := ks.cdc.Marshal(k)
s.Require().NoError(err)
k2, err := ks.protoUnmarshalRecord(bz)
s.Require().NoError(err)
s.Require().Equal(k.Name, k2.Name)
// not sure if this will work -- we can remove this line, the later check is better.
s.Require().True(k.PubKey.Equal(k2.PubKey))
pub2, err := k2.GetPubKey()
s.Require().NoError(err)
s.Require().True(s.pub.Equals(pub2))
ledgerRecord2 := k2.GetLedger()
s.Require().NotNil(ledgerRecord2)
s.Require().Nil(k2.GetLocal())
s.Require().Equal(ledgerRecord2.Path.String(), path.String())
}
func (s *RecordTestSuite) TestExtractPrivKeyFromLocalRecord() {
// use proto serialize
k, err := NewLocalRecord("testrecord", s.priv, s.pub)
s.Require().NoError(err)
privKey2, err := extractPrivKeyFromRecord(k)
s.Require().NoError(err)
s.Require().True(privKey2.Equals(s.priv))
}
func (s *RecordTestSuite) TestExtractPrivKeyFromOfflineRecord() {
k, err := NewOfflineRecord("testrecord", s.pub)
s.Require().NoError(err)
privKey2, err := extractPrivKeyFromRecord(k)
s.Require().Error(err)
s.Require().Nil(privKey2)
}
func TestRecordTestSuite(t *testing.T) {
suite.Run(t, new(RecordTestSuite))
}