cosmos-sdk/crypto/keyring/record_test.go
Amaury b2b29d4909
chore: Audit crypto folder (#11932)
## Description

ref: #11362 

I did **NOT** review the following folders, as they contain cryptography which I don't think I'm competent enough to give a useful review:
- [ ] `crypto/xsalsa20symmetric` (new in v046, ported from TM i think)
- [ ] `crypto/keys/secp256k1` (some new stuff in v046 too)

Also performed some manual tests as part of #11939:

  - [x] Create keys on v0.45, make sure they still work in v0.46 https://github.com/cosmos/cosmos-sdk/issues/11939#issuecomment-1124881492
  - [x] Create new keys in v0.46 https://github.com/cosmos/cosmos-sdk/issues/11939#issuecomment-1124881492
  - [x] `--multisig` flag works with an address that's not in the keyring (see [repro](https://github.com/cosmos/cosmos-sdk/issues/9553))



---

### 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/main/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/main/docs/building-modules)
- [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/main/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)
2022-05-15 13:02:53 +00:00

139 lines
3.4 KiB
Go

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)
}
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))
}