Skip to content

Commit

Permalink
add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
likhita-809 committed Jul 12, 2022
1 parent dcb2ff0 commit 032d822
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 14 deletions.
2 changes: 1 addition & 1 deletion client/keys/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func Test_multiSigKey_Properties(t *testing.T) {
require.Equal(t, "myMultisig", k.Name)
require.Equal(t, keyring.TypeMulti, k.GetType())

pub, err := k.GetPubKey()
pub, err := k.GetMultisigPubKey()
require.NoError(t, err)
require.Equal(t, "D3923267FA8A3DD367BB768FA8BDC8FF7F89DA3F", pub.Address().String())

Expand Down
2 changes: 1 addition & 1 deletion crypto/keyring/output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestBech32KeysOutput(t *testing.T) {
k, err := NewMultiRecord("multisig", multisigPk)
require.NotNil(t, k)
require.NoError(t, err)
pubKey, err := k.GetPubKey()
pubKey, err := k.GetMultisigPubKey()
require.NoError(t, err)
accAddr := sdk.AccAddress(pubKey.Address())
expectedOutput, err := NewKeyOutput(k.Name, k.GetType(), accAddr, multisigPk)
Expand Down
13 changes: 5 additions & 8 deletions crypto/keyring/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,12 @@ func NewMultiRecord(name string, pk cryptotypes.PubKey) (*Record, error) {

// GetMultisigPubKey fetches a public key of the multi type record
func (k *Record) GetMultisigPubKey() (*multisig.LegacyAminoPubKey, error) {
if k.GetType() == TypeMulti {
pk, ok := k.PubKey.GetCachedValue().(*multisig.LegacyAminoPubKey)
if !ok {
return nil, errors.New("unable to cast any to multisig.LegacyAminoPubKey")
}

return pk, nil
pk, ok := k.PubKey.GetCachedValue().(*multisig.LegacyAminoPubKey)
if !ok {
return nil, errors.New("unable to cast any to multisig.LegacyAminoPubKey")
}
return nil, nil

return pk, nil
}

// GetPubKey fetches a public key of the record
Expand Down
45 changes: 41 additions & 4 deletions crypto/keyring/record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,35 @@ import (
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
"github.com/cosmos/cosmos-sdk/crypto/keys/multisig"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"

cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
)

type RecordTestSuite struct {
suite.Suite

appName string
cdc codec.Codec
priv cryptotypes.PrivKey
pub cryptotypes.PubKey
appName string
cdc codec.Codec
priv cryptotypes.PrivKey
pub cryptotypes.PubKey
multiPub *multisig.LegacyAminoPubKey
}

func (s *RecordTestSuite) SetupSuite() {
s.appName = "cosmos"
s.cdc = getCodec()
s.priv = cryptotypes.PrivKey(ed25519.GenPrivKey())
s.pub = s.priv.PubKey()

multiKey := secp256k1.GenPrivKeyFromSecret([]byte("myMultiKey"))
pk := multisig.NewLegacyAminoPubKey(
1,
[]cryptotypes.PubKey{multiKey.PubKey()},
)
s.multiPub = pk

}

func (s *RecordTestSuite) TestOfflineRecordMarshaling() {
Expand All @@ -47,6 +58,32 @@ func (s *RecordTestSuite) TestOfflineRecordMarshaling() {
s.Require().True(s.pub.Equals(pk2))
}

func (s *RecordTestSuite) TestMultiRecordMarshaling() {
dir := s.T().TempDir()
mockIn := strings.NewReader("")

kb, err := New(s.appName, BackendTest, dir, mockIn, s.cdc)
s.Require().NoError(err)

k, err := NewMultiRecord("testrecord", s.multiPub)
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)
s.Require().True(k.PubKey.Equal(k2.PubKey))

pub2, err := k2.GetMultisigPubKey()
s.Require().NoError(err)
s.Require().True(s.multiPub.Equals(pub2))
}

func (s *RecordTestSuite) TestLocalRecordMarshaling() {
dir := s.T().TempDir()
mockIn := strings.NewReader("")
Expand Down

0 comments on commit 032d822

Please sign in to comment.