From 3d3bc7c43218c37c6d0033d578cc7b24968a9be2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Henrik=20Aasted=20S=C3=B8rensen?= <has@bitcraft.dk>
Date: Fri, 10 Sep 2021 21:32:40 +0200
Subject: [PATCH] fix: unmarshalling issue with multisig keys in master
 (#10061)

---
 CHANGELOG.md                          | 2 ++
 crypto/keys/multisig/amino.go         | 2 ++
 crypto/keys/multisig/multisig_test.go | 8 ++++++++
 3 files changed, 12 insertions(+)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 54f1ee8c82..306caec94a 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -107,8 +107,10 @@ Ref: https://keepachangelog.com/en/1.0.0/
 * [\#9829](https://github.com/cosmos/cosmos-sdk/pull/9829) Fixed Coin denom sorting not being checked during `Balance.Validate` check. Refactored the Validation logic to use `Coins.Validate` for `Balance.Coins`.
 + [\#9965](https://github.com/cosmos/cosmos-sdk/pull/9965) Fixed `simd version` command output to report the right release tag.
 + [\#9980](https://github.com/cosmos/cosmos-sdk/pull/9980) Returning the error when the invalid argument is passed to bank query total supply cli.
++ [\#10061](https://github.com/cosmos/cosmos-sdk/pull/10061) Ensure that `LegacyAminoPubKey` struct correctly unmarshals from JSON
 * (server) [#10016](https://github.com/cosmos/cosmos-sdk/issues/10016) Fix marshaling of index-events into server config file.
 
+
 ### State Machine Breaking
 
 * (x/auth)[\#9596](https://github.com/cosmos/cosmos-sdk/pull/9596) Enable creating periodic vesting accounts with a transactions instead of requiring them to be created in genesis.
diff --git a/crypto/keys/multisig/amino.go b/crypto/keys/multisig/amino.go
index 4849a23173..a1394fb0e9 100644
--- a/crypto/keys/multisig/amino.go
+++ b/crypto/keys/multisig/amino.go
@@ -78,7 +78,9 @@ 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.
+	m.PubKeys = make([]*types.Any, len(protoPk.PubKeys))
 	for i := range m.PubKeys {
+		m.PubKeys[i] = &types.Any{}
 		m.PubKeys[i].TypeUrl = protoPk.PubKeys[i].TypeUrl
 		m.PubKeys[i].Value = protoPk.PubKeys[i].Value
 	}
diff --git a/crypto/keys/multisig/multisig_test.go b/crypto/keys/multisig/multisig_test.go
index 0a7319323b..e208f9db73 100644
--- a/crypto/keys/multisig/multisig_test.go
+++ b/crypto/keys/multisig/multisig_test.go
@@ -428,6 +428,14 @@ func TestAminoUnmarshalJSON(t *testing.T) {
 	require.NoError(t, err)
 	lpk := pk.(*kmultisig.LegacyAminoPubKey)
 	require.Equal(t, uint32(3), lpk.Threshold)
+	require.Equal(t, 5, len(pk.(*kmultisig.LegacyAminoPubKey).PubKeys))
+
+	for _, key := range pk.(*kmultisig.LegacyAminoPubKey).PubKeys {
+		require.NotNil(t, key)
+		pk := secp256k1.PubKey{}
+		err := pk.Unmarshal(key.Value)
+		require.NoError(t, err)
+	}
 }
 
 func TestProtoMarshalJSON(t *testing.T) {