Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(crypto): fix nested multisigs fail to marshal #20404

Closed
22 changes: 22 additions & 0 deletions client/keys/output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,28 @@ func TestBech32KeysOutput(t *testing.T) {
require.Equal(t, "{Name:multisig Type:multi Address:cosmos1nf8lf6n4wa43rzmdzwe6hkrnw5guekhqt595cw PubKey:{\"@type\":\"/cosmos.crypto.multisig.LegacyAminoPubKey\",\"threshold\":1,\"public_keys\":[{\"@type\":\"/cosmos.crypto.secp256k1.PubKey\",\"key\":\"AurroA7jvfPd1AadmmOvWM2rJSwipXfRf8yD6pLbA2DJ\"}]} Mnemonic:}", fmt.Sprintf("%+v", out))
}

// TestBech32KeysOutputNestedMsig tests that the output of a nested multisig key is correct
func TestBech32KeysOutputNestedMsig(t *testing.T) {
sk := secp256k1.PrivKey{Key: []byte{154, 49, 3, 117, 55, 232, 249, 20, 205, 216, 102, 7, 136, 72, 177, 2, 131, 202, 234, 81, 31, 208, 46, 244, 179, 192, 167, 163, 142, 117, 246, 13}}
tmpKey := sk.PubKey()
nestedMultiSig := kmultisig.NewLegacyAminoPubKey(1, []types.PubKey{tmpKey})
multisigPk := kmultisig.NewLegacyAminoPubKey(1, []types.PubKey{nestedMultiSig})
k, err := keyring.NewMultiRecord("multisig", multisigPk)
require.NotNil(t, k)
require.NoError(t, err)
pubKey, err := k.GetPubKey()
fmt.Println(pubKey.Bytes())
tuantran1702 marked this conversation as resolved.
Show resolved Hide resolved
require.NoError(t, err)
accAddr := sdk.AccAddress(pubKey.Address())
expectedOutput, err := NewKeyOutput(k.Name, k.GetType(), accAddr, multisigPk, addresscodec.NewBech32Codec("cosmos"))
require.NoError(t, err)

out, err := MkAccKeyOutput(k, addresscodec.NewBech32Codec("cosmos"))
require.NoError(t, err)
require.Equal(t, expectedOutput, out)
require.Equal(t, "{Name:multisig Type:multi Address:cosmos16snhe32gdcfhzsk62xdtuhnwextdm939e5snax PubKey:{\"@type\":\"/cosmos.crypto.multisig.LegacyAminoPubKey\",\"threshold\":1,\"public_keys\":[{\"@type\":\"/cosmos.crypto.multisig.LegacyAminoPubKey\",\"threshold\":1,\"public_keys\":[{\"@type\":\"/cosmos.crypto.secp256k1.PubKey\",\"key\":\"AurroA7jvfPd1AadmmOvWM2rJSwipXfRf8yD6pLbA2DJ\"}]}]} Mnemonic:}", fmt.Sprintf("%+v", out))
}

func TestProtoMarshalJSON(t *testing.T) {
require := require.New(t)
pubkeys := generatePubKeys(3)
Expand Down
2 changes: 1 addition & 1 deletion crypto/keys/multisig/multisig.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (m *LegacyAminoPubKey) Address() cryptotypes.Address {

// Bytes returns the proto encoded version of the LegacyAminoPubKey
func (m *LegacyAminoPubKey) Bytes() []byte {
return AminoCdc.MustMarshal(m)
return AminoCdc.MustMarshalJSON(m)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not seem the correct way of handling the problem, based off:

image

This is most likely related to the LegacyAminoPubKey not being registered in some codec somewhere.

}

// VerifyMultisignature implements the multisigtypes.PubKey VerifyMultisignature method.
Expand Down
15 changes: 15 additions & 0 deletions crypto/keys/multisig/multisig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,21 @@ func TestAminoBinary(t *testing.T) {
require.Equal(t, msig.Threshold, newMsig.(*kmultisig.LegacyAminoPubKey).Threshold)
}

// func TestAminoBinaryNested(t *testing.T) {
// pubkeys := generatePubKeys(4)
// nestedMsig := kmultisig.NewLegacyAminoPubKey(2, pubkeys[:2])
// msig := kmultisig.NewLegacyAminoPubKey(2, []cryptotypes.PubKey{pubkeys[2], pubkeys[3], nestedMsig})

// // Do a round-trip key->bytes->key.
// bz, err := legacy.Cdc.Marshal(msig)
// require.NoError(t, err)
// var newMsig cryptotypes.PubKey
// err = legacy.Cdc.Unmarshal(bz, &newMsig)
// require.NoError(t, err)
// require.Equal(t, msig.Threshold, newMsig.(*kmultisig.LegacyAminoPubKey).Threshold)
// require.Equal(t, len(msig.PubKeys), len(newMsig.(*kmultisig.LegacyAminoPubKey).PubKeys))
// }

func TestAminoMarshalJSON(t *testing.T) {
pubkeys := generatePubKeys(2)
multisigKey := kmultisig.NewLegacyAminoPubKey(2, pubkeys)
Expand Down
Loading