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

x/ibc: amino json support #8422

Closed
wants to merge 13 commits into from
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ Ref: https://keepachangelog.com/en/1.0.0/

## [Unreleased]

### State Machine Breaking

* (x/ibc) [\#8266](https://github.com/cosmos/cosmos-sdk/issues/8266) Add amino JSON for IBC messages in order to support Ledger text signing.

### Improvements

* (x/ibc) [\#8404](https://github.com/cosmos/cosmos-sdk/pull/8404) Reorder IBC `ChanOpenAck` and `ChanOpenConfirm` handler execution to perform core handler first, followed by application callbacks.
Expand Down
3 changes: 1 addition & 2 deletions x/auth/client/rest/rest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,7 @@ func (s *IntegrationTestSuite) TestBroadcastIBCTxRequest() {
res, err := rest.PostRequest(fmt.Sprintf("%s/txs", val.APIAddress), "application/json", []byte(req))
s.Require().NoError(err)

// Make sure the error message is correct.
s.Require().Contains(string(res), "this transaction cannot be broadcasted via legacy REST endpoints")
s.Require().NotContains(string(res), "this transaction cannot be broadcasted via legacy REST endpoints", string(res))
}

// Helper function to test querying txs. We will use it to query StdTx and service `Msg`s.
Expand Down
4 changes: 3 additions & 1 deletion x/ibc/applications/transfer/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ func (AppModuleBasic) Name() string {
}

// RegisterLegacyAminoCodec implements AppModuleBasic interface
func (AppModuleBasic) RegisterLegacyAminoCodec(*codec.LegacyAmino) {}
func (AppModuleBasic) RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {
types.RegisterLegacyAminoCodec(cdc)
}

// RegisterInterfaces registers module concrete types into protobuf Any.
func (AppModuleBasic) RegisterInterfaces(registry codectypes.InterfaceRegistry) {
Expand Down
22 changes: 19 additions & 3 deletions x/ibc/applications/transfer/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ import (
"github.com/cosmos/cosmos-sdk/types/msgservice"
)

// RegisterLegacyAminoCodec registers the necessary x/ibc transfer interfaces and concrete types
// on the provided LegacyAmino codec. These types are used for Amino JSON serialization.
func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {
cdc.RegisterConcrete(&MsgTransfer{}, "cosmos-sdk/MsgTransfer", nil)
}

// RegisterInterfaces register the ibc transfer module interfaces to protobuf
// Any.
func RegisterInterfaces(registry codectypes.InterfaceRegistry) {
Expand All @@ -16,10 +22,20 @@ func RegisterInterfaces(registry codectypes.InterfaceRegistry) {
}

var (
// ModuleCdc references the global x/ibc-transfer module codec. Note, the codec
// should ONLY be used in certain instances of tests and for JSON encoding.
amino = codec.NewLegacyAmino()

// ModuleCdc references the global x/ibc transfer codec. Note, the codec should
// ONLY be used in certain instances of tests and for JSON encoding as Amino is
// still used for that purpose.
//
// The actual codec used for serialization should be provided to x/ibc-transfer and
// The actual codec used for serialization should be provided to x/ibc transfer and
// defined at the application level.
ModuleCdc = codec.NewProtoCodec(codectypes.NewInterfaceRegistry())
// AminoCdc is a amino codec created to support amino json compatible msgs.
AminoCdc = codec.NewAminoCodec(amino)
)

func init() {
RegisterLegacyAminoCodec(amino)
amino.Seal()
}
5 changes: 2 additions & 3 deletions x/ibc/applications/transfer/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,9 @@ func (msg MsgTransfer) ValidateBasic() error {
return ValidateIBCDenom(msg.Token.Denom)
}

// GetSignBytes implements sdk.Msg. The function will panic since it is used
// for amino transaction verification which IBC does not support.
// GetSignBytes implements sdk.Msg.
func (msg MsgTransfer) GetSignBytes() []byte {
panic("IBC messages do not support amino")
return sdk.MustSortJSON(AminoCdc.MustMarshalJSON(&msg))
}

// GetSigners implements sdk.Msg
Expand Down
10 changes: 10 additions & 0 deletions x/ibc/applications/transfer/types/msgs_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package types

import (
"fmt"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -51,6 +52,15 @@ func TestMsgTransferType(t *testing.T) {
require.Equal(t, "transfer", msg.Type())
}

func TestMsgTransferGetSignBytes(t *testing.T) {
msg := NewMsgTransfer(validPort, validChannel, coin, addr1, addr2, timeoutHeight, 0)
expected := fmt.Sprintf(`{"type":"cosmos-sdk/MsgTransfer","value":{"receiver":"%s","sender":"%s","source_channel":"testchannel","source_port":"testportid","timeout_height":{"revision_height":"10"},"token":{"amount":"100","denom":"atom"}}}`, addr2, addr1)
require.NotPanics(t, func() {
res := msg.GetSignBytes()
require.Equal(t, expected, string(res))
})
}

// TestMsgTransferValidation tests ValidateBasic for MsgTransfer
func TestMsgTransferValidation(t *testing.T) {
testCases := []struct {
Expand Down
27 changes: 27 additions & 0 deletions x/ibc/core/02-client/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,40 @@ package types
import (
proto "github.com/gogo/protobuf/proto"

"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"
"github.com/cosmos/cosmos-sdk/types/msgservice"
"github.com/cosmos/cosmos-sdk/x/ibc/core/exported"
)

var (
amino = codec.NewLegacyAmino()

// ModuleCdc references the global x/ibc client codec. Note, the codec should
// ONLY be used in certain instances of tests and for JSON encoding as Amino is
// still used for that purpose.
//
// The actual codec used for serialization should be provided to x/ibc client and
// defined at the application level.
ModuleCdc = codec.NewAminoCodec(amino)
)

func init() {
RegisterLegacyAminoCodec(amino)
amino.Seal()
}

// RegisterLegacyAminoCodec registers the necessary x/ibc client interfaces and concrete types
// on the provided LegacyAmino codec. These types are used for Amino JSON serialization.
func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {
cdc.RegisterConcrete(&MsgCreateClient{}, "cosmos-sdk/MsgCreateClient", nil)
cdc.RegisterConcrete(&MsgUpdateClient{}, "cosmos-sdk/MsgUpdateClient", nil)
cdc.RegisterConcrete(&MsgUpgradeClient{}, "cosmos-sdk/MsgUpgradeClient", nil)
cdc.RegisterConcrete(&MsgSubmitMisbehaviour{}, "cosmos-sdk/MsgSubmitMisbehaviour", nil)
}

// RegisterInterfaces registers the client interfaces to protobuf Any.
func RegisterInterfaces(registry codectypes.InterfaceRegistry) {
registry.RegisterInterface(
Expand Down
20 changes: 8 additions & 12 deletions x/ibc/core/02-client/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,9 @@ func (msg MsgCreateClient) ValidateBasic() error {
return consensusState.ValidateBasic()
}

// GetSignBytes implements sdk.Msg. The function will panic since it is used
// for amino transaction verification which IBC does not support.
// GetSignBytes implements sdk.Msg.
func (msg MsgCreateClient) GetSignBytes() []byte {
panic("IBC messages do not support amino")
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg))
}

// GetSigners implements sdk.Msg
Expand Down Expand Up @@ -161,10 +160,9 @@ func (msg MsgUpdateClient) ValidateBasic() error {
return host.ClientIdentifierValidator(msg.ClientId)
}

// GetSignBytes implements sdk.Msg. The function will panic since it is used
// for amino transaction verification which IBC does not support.
// GetSignBytes implements sdk.Msg.
func (msg MsgUpdateClient) GetSignBytes() []byte {
panic("IBC messages do not support amino")
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg))
}

// GetSigners implements sdk.Msg
Expand Down Expand Up @@ -247,10 +245,9 @@ func (msg MsgUpgradeClient) ValidateBasic() error {
return host.ClientIdentifierValidator(msg.ClientId)
}

// GetSignBytes implements sdk.Msg. The function will panic since it is used
// for amino transaction verification which IBC does not support.
// GetSignBytes implements sdk.Msg.
func (msg MsgUpgradeClient) GetSignBytes() []byte {
panic("IBC messages do not support amino")
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg))
}

// GetSigners implements sdk.Msg
Expand Down Expand Up @@ -321,10 +318,9 @@ func (msg MsgSubmitMisbehaviour) ValidateBasic() error {
return host.ClientIdentifierValidator(msg.ClientId)
}

// GetSignBytes implements sdk.Msg. The function will panic since it is used
// for amino transaction verification which IBC does not support.
// GetSignBytes implements sdk.Msg.
func (msg MsgSubmitMisbehaviour) GetSignBytes() []byte {
panic("IBC messages do not support amino")
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg))
}

// GetSigners returns the single expected signer for a MsgSubmitMisbehaviour.
Expand Down
Loading