fix: Prevent signing from wrong key in multisig (#12446)
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
|
||||
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
|
||||
"github.com/cosmos/cosmos-sdk/crypto/hd"
|
||||
"github.com/cosmos/cosmos-sdk/crypto/keys/multisig"
|
||||
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
|
||||
"github.com/cosmos/cosmos-sdk/types"
|
||||
)
|
||||
@@ -59,6 +60,16 @@ func NewMultiRecord(name string, pk cryptotypes.PubKey) (*Record, error) {
|
||||
return newRecord(name, pk, recordMultiItem)
|
||||
}
|
||||
|
||||
// GetMultisigPubKey fetches a public key of the multi type record
|
||||
func (k *Record) GetMultisigPubKey() (*multisig.LegacyAminoPubKey, error) {
|
||||
pk, ok := k.PubKey.GetCachedValue().(*multisig.LegacyAminoPubKey)
|
||||
if !ok {
|
||||
return nil, errors.New("unable to cast any to multisig.LegacyAminoPubKey")
|
||||
}
|
||||
|
||||
return pk, nil
|
||||
}
|
||||
|
||||
// GetPubKey fetches a public key of the record
|
||||
func (k *Record) GetPubKey() (cryptotypes.PubKey, error) {
|
||||
pk, ok := k.PubKey.GetCachedValue().(cryptotypes.PubKey)
|
||||
|
||||
@@ -10,6 +10,8 @@ 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"
|
||||
)
|
||||
@@ -17,10 +19,11 @@ import (
|
||||
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() {
|
||||
@@ -28,6 +31,14 @@ func (s *RecordTestSuite) SetupSuite() {
|
||||
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() {
|
||||
@@ -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("")
|
||||
|
||||
Reference in New Issue
Block a user