diff --git a/modules/apps/27-interchain-accounts/module.go b/modules/apps/27-interchain-accounts/module.go index 1ac7d58e503..b11c611e0c9 100644 --- a/modules/apps/27-interchain-accounts/module.go +++ b/modules/apps/27-interchain-accounts/module.go @@ -42,10 +42,8 @@ func (AppModuleBasic) Name() string { return types.ModuleName } -// RegisterLegacyAminoCodec implements AppModuleBasic interface -func (AppModuleBasic) RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) { - types.RegisterLegacyAminoCodec(cdc) -} +// RegisterLegacyAminoCodec implements AppModuleBasic. +func (AppModuleBasic) RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {} // RegisterInterfaces registers module concrete types into protobuf Any func (AppModuleBasic) RegisterInterfaces(registry codectypes.InterfaceRegistry) { diff --git a/modules/apps/27-interchain-accounts/types/codec.go b/modules/apps/27-interchain-accounts/types/codec.go index 2ac1f553bcc..a5a0922cabd 100644 --- a/modules/apps/27-interchain-accounts/types/codec.go +++ b/modules/apps/27-interchain-accounts/types/codec.go @@ -4,6 +4,7 @@ import ( "github.com/cosmos/cosmos-sdk/codec" codectypes "github.com/cosmos/cosmos-sdk/codec/types" sdk "github.com/cosmos/cosmos-sdk/types" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" ) @@ -16,13 +17,6 @@ var ( ModuleCdc = codec.NewProtoCodec(codectypes.NewInterfaceRegistry()) ) -// RegisterLegacyAminoCodec registers the account interfaces and concrete types on the -// provided LegacyAmino codec. These types are used for Amino JSON serialization -func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) { - cdc.RegisterInterface((*InterchainAccountI)(nil), nil) - cdc.RegisterConcrete(&InterchainAccount{}, "27-interchain-accounts/InterchainAccount", nil) -} - // RegisterInterfaces registers the concrete InterchainAccount implementation against the associated // x/auth AccountI and GenesisAccount interfaces func RegisterInterfaces(registry codectypes.InterfaceRegistry) { @@ -32,8 +26,13 @@ func RegisterInterfaces(registry codectypes.InterfaceRegistry) { // SerializeCosmosTx serializes a slice of sdk.Msg's using the CosmosTx type. The sdk.Msg's are // packed into Any's and inserted into the Messages field of a CosmosTx. The proto marshaled CosmosTx -// bytes are returned. +// bytes are returned. Only the ProtoCodec is supported for serializing messages. func SerializeCosmosTx(cdc codec.BinaryCodec, msgs []sdk.Msg) (bz []byte, err error) { + // only ProtoCodec is supported + if _, ok := cdc.(*codec.ProtoCodec); !ok { + return nil, sdkerrors.Wrap(ErrInvalidCodec, "only ProtoCodec is supported for receiving messages on the host chain") + } + msgAnys := make([]*codectypes.Any, len(msgs)) for i, msg := range msgs { @@ -56,8 +55,14 @@ func SerializeCosmosTx(cdc codec.BinaryCodec, msgs []sdk.Msg) (bz []byte, err er } // DeserializeCosmosTx unmarshals and unpacks a slice of transaction bytes -// into a slice of sdk.Msg's. +// into a slice of sdk.Msg's. Only the ProtoCodec is supported for message +// deserialization. func DeserializeCosmosTx(cdc codec.BinaryCodec, data []byte) ([]sdk.Msg, error) { + // only ProtoCodec is supported + if _, ok := cdc.(*codec.ProtoCodec); !ok { + return nil, sdkerrors.Wrap(ErrInvalidCodec, "only ProtoCodec is supported for receiving messages on the host chain") + } + var cosmosTx CosmosTx if err := cdc.Unmarshal(data, &cosmosTx); err != nil { return nil, err diff --git a/modules/apps/27-interchain-accounts/types/codec_test.go b/modules/apps/27-interchain-accounts/types/codec_test.go index aee28e88d3e..e027fda9346 100644 --- a/modules/apps/27-interchain-accounts/types/codec_test.go +++ b/modules/apps/27-interchain-accounts/types/codec_test.go @@ -1,6 +1,7 @@ package types_test import ( + "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" @@ -42,8 +43,7 @@ func (mockSdkMsg) GetSigners() []sdk.AccAddress { return []sdk.AccAddress{} } -func (suite *TypesTestSuite) TestSerializeCosmosTx() { - +func (suite *TypesTestSuite) TestSerializeAndDeserializeCosmosTx() { testCases := []struct { name string msgs []sdk.Msg @@ -127,4 +127,26 @@ func (suite *TypesTestSuite) TestSerializeCosmosTx() { suite.Require().Error(err, tc.name) } } + + // test deserializing unknown bytes + msgs, err := types.DeserializeCosmosTx(simapp.MakeTestEncodingConfig().Marshaler, []byte("invalid")) + suite.Require().Error(err) + suite.Require().Empty(msgs) +} + +// unregistered bytes causes amino to panic. +// test that DeserializeCosmosTx gracefully returns an error on +// unsupported amino codec. +func (suite *TypesTestSuite) TestDeserializeAndSerializeCosmosTxWithAmino() { + cdc := codec.NewLegacyAmino() + marshaler := codec.NewAminoCodec(cdc) + + msgs, err := types.SerializeCosmosTx(marshaler, []sdk.Msg{&banktypes.MsgSend{}}) + suite.Require().Error(err) + suite.Require().Empty(msgs) + + bz, err := types.DeserializeCosmosTx(marshaler, []byte{0x10, 0}) + suite.Require().Error(err) + suite.Require().Empty(bz) + } diff --git a/modules/apps/27-interchain-accounts/types/errors.go b/modules/apps/27-interchain-accounts/types/errors.go index 69248df73c3..e0a5c141de9 100644 --- a/modules/apps/27-interchain-accounts/types/errors.go +++ b/modules/apps/27-interchain-accounts/types/errors.go @@ -20,4 +20,5 @@ var ( ErrInvalidControllerPort = sdkerrors.Register(ModuleName, 14, "invalid controller port") ErrInvalidHostPort = sdkerrors.Register(ModuleName, 15, "invalid host port") ErrInvalidTimeoutTimestamp = sdkerrors.Register(ModuleName, 16, "timeout timestamp must be in the future") + ErrInvalidCodec = sdkerrors.Register(ModuleName, 17, "codec is not supported") )