Skip to content

Commit

Permalink
fix: unmarshalling issue with multisig keys in master (backport cosmo…
Browse files Browse the repository at this point in the history
…s#10061) (cosmos#10169)

* fix: unmarshalling issue with multisig keys in master (cosmos#10061)

(cherry picked from commit 3d3bc7c)

# Conflicts:
#	CHANGELOG.md

* fix conflicts

Co-authored-by: Henrik Aasted Sørensen <[email protected]>
Co-authored-by: Robert Zaremba <[email protected]>
  • Loading branch information
3 people authored and JeancarloBarrios committed Sep 28, 2024
1 parent d25f452 commit acc8d65
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 21 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
### Bug Fixes

* (x/genutil) [#10104](https://github.com/cosmos/cosmos-sdk/pull/10104) Ensure the `init` command reads the `--home` flag value correctly.
* [\#10061](https://github.com/cosmos/cosmos-sdk/pull/10061) Ensure that `LegacyAminoPubKey` struct correctly unmarshals from JSON

## [v0.44.0](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.44.0) - 2021-09-01

Expand Down
25 changes: 4 additions & 21 deletions crypto/keys/multisig/amino.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,28 +80,11 @@ func (m *LegacyAminoPubKey) UnmarshalAminoJSON(tmPk tmMultisig) error {
// Instead of just doing `*m = *protoPk`, we prefer to modify in-place the
// existing Anys inside `m` (instead of allocating new Anys), as so not to
// break the `.compat` fields in the existing Anys.
if m.PubKeys == nil {
m.PubKeys = make([]*types.Any, len(tmPk.PubKeys))
}
m.PubKeys = make([]*types.Any, len(protoPk.PubKeys))
for i := range m.PubKeys {
if m.PubKeys[i] == nil {
// create the compat jsonBz value
bz, err := AminoCdc.MarshalJSON(tmPk.PubKeys[i])
if err != nil {
return err
}

m.PubKeys[i] = protoPk.PubKeys[i]
// UnmarshalJSON():
// just sets the compat.jsonBz value.
// always succeeds: err == nil
if err := m.PubKeys[i].UnmarshalJSON(bz); err != nil {
return err
}
} else {
m.PubKeys[i].TypeUrl = protoPk.PubKeys[i].TypeUrl
m.PubKeys[i].Value = protoPk.PubKeys[i].Value
}
m.PubKeys[i] = &types.Any{}
m.PubKeys[i].TypeUrl = protoPk.PubKeys[i].TypeUrl
m.PubKeys[i].Value = protoPk.PubKeys[i].Value
}
m.Threshold = protoPk.Threshold

Expand Down
27 changes: 27 additions & 0 deletions crypto/keys/multisig/multisig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,3 +453,30 @@ func TestAminoUnmarshalJSON(t *testing.T) {
require.NoError(t, err)
}
}

func TestProtoMarshalJSON(t *testing.T) {
require := require.New(t)
pubkeys := generatePubKeys(3)
msig := kmultisig.NewLegacyAminoPubKey(2, pubkeys)

registry := types.NewInterfaceRegistry()
cryptocodec.RegisterInterfaces(registry)
cdc := codec.NewProtoCodec(registry)

bz, err := cdc.MarshalInterfaceJSON(msig)
require.NoError(err)

var pk2 cryptotypes.PubKey
err = cdc.UnmarshalInterfaceJSON(bz, &pk2)
require.NoError(err)
require.True(pk2.Equals(msig))

// Test that we can correctly unmarshal key from keyring output

for _, key := range pk.(*kmultisig.LegacyAminoPubKey).PubKeys {
require.NotNil(t, key)
pk := secp256k1.PubKey{}
err := pk.Unmarshal(key.Value)
require.NoError(t, err)
}
}

0 comments on commit acc8d65

Please sign in to comment.