diff --git a/x/ophost/types/bridge_config.go b/x/ophost/types/bridge_config.go index 42e6f99..d8e3726 100644 --- a/x/ophost/types/bridge_config.go +++ b/x/ophost/types/bridge_config.go @@ -8,8 +8,12 @@ import ( "cosmossdk.io/core/address" "cosmossdk.io/errors" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" + "github.com/cosmos/gogoproto/jsonpb" ) +var _ jsonpb.JSONPBMarshaler = (*BatchInfo_ChainType)(nil) +var _ jsonpb.JSONPBUnmarshaler = (*BatchInfo_ChainType)(nil) + func (config BridgeConfig) Validate(ac address.Codec) error { if _, err := ac.StringToBytes(config.Challenger); err != nil { return err @@ -101,6 +105,16 @@ func (cy *BatchInfo_ChainType) UnmarshalJSON(b []byte) error { return nil } +// MarshalJSONPB marshals the BatchInfo_ChainType to JSON +func (cy BatchInfo_ChainType) MarshalJSONPB(_ *jsonpb.Marshaler) ([]byte, error) { + return cy.MarshalJSON() +} + +// UnmarshalJSONPB unmarshals the BatchInfo_ChainType from JSON +func (cy *BatchInfo_ChainType) UnmarshalJSONPB(_ *jsonpb.Unmarshaler, inputBz []byte) error { + return cy.UnmarshalJSON(inputBz) +} + // StringWithoutPrefix returns the string representation of a BatchInfo_ChainType without the prefix func (cy BatchInfo_ChainType) StringWithoutPrefix() string { return cy.String()[len(chainTypePrefix):] diff --git a/x/ophost/types/bridge_config_test.go b/x/ophost/types/bridge_config_test.go index ea02279..16274ea 100644 --- a/x/ophost/types/bridge_config_test.go +++ b/x/ophost/types/bridge_config_test.go @@ -2,9 +2,19 @@ package types import ( "encoding/json" + "fmt" "testing" "github.com/stretchr/testify/require" + + "cosmossdk.io/x/tx/signing" + "github.com/cosmos/cosmos-sdk/codec" + codecaddress "github.com/cosmos/cosmos-sdk/codec/address" + codectypes "github.com/cosmos/cosmos-sdk/codec/types" + "github.com/cosmos/cosmos-sdk/std" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/tx" + "github.com/cosmos/gogoproto/proto" ) func Test_JSONMarshalUnmarshal(t *testing.T) { @@ -45,3 +55,31 @@ func Test_ValidateBridgeConfig(t *testing.T) { config.BatchInfo.ChainType = 100 require.Error(t, config.ValidateWithNoAddrValidation()) } + +func TestGoGoProtoJsonPB(t *testing.T) { + interfaceRegistry, err := codectypes.NewInterfaceRegistryWithOptions(codectypes.InterfaceRegistryOptions{ + ProtoFiles: proto.HybridResolver, + SigningOptions: signing.Options{ + AddressCodec: codecaddress.NewBech32Codec(sdk.GetConfig().GetBech32AccountAddrPrefix()), + ValidatorAddressCodec: codecaddress.NewBech32Codec(sdk.GetConfig().GetBech32ValidatorAddrPrefix()), + }, + }) + if err != nil { + panic(err) + } + RegisterInterfaces(interfaceRegistry) + std.RegisterInterfaces(interfaceRegistry) + appCodec := codec.NewProtoCodec(interfaceRegistry) + const CHAIN_TYPE = "CELESTIA" + jsonText := fmt.Sprintf(`{"body":{"messages":[{"@type":"/opinit.ophost.v1.MsgCreateBridge","creator":"init1fgwvmvr8y5ul03aty9647vny7uj0uzt4g5zxev","config":{"challenger":"init1n8x7h4l96wmazlmxpqurccfg37fayrdafpmhfr","proposer":"init1knt8ehj03wk6hrzr4j35rmx2m7gqf4mwwcmrze","batch_info":{"submitter":"celestia13ycasdutemyk6pw6dy3ct3rxwknxrz6ygzjmu7","chain_type":"%s"},"submission_interval":"60s","finalization_period":"604800s","submission_start_height":"1","oracle_enabled":true,"metadata":"eyJwZXJtX2NoYW5uZWxzIjpbeyJwb3J0X2lkIjoibmZ0LXRyYW5zZmVyIiwiY2hhbm5lbF9pZCI6ImNoYW5uZWwtMzUzIn0seyJwb3J0X2lkIjoidHJhbnNmZXIiLCJjaGFubmVsX2lkIjoiY2hhbm5lbC0zNTIifV19"}}],"memo":"","timeout_height":"0","extension_options":[],"non_critical_extension_options":[]},"auth_info":{"signer_infos":[{"public_key":{"@type":"/cosmos.crypto.secp256k1.PubKey","key":"AwKanlZ3AymXu7E54dOD5gL5kSrSBvTdrcow/vOsTVfl"},"mode_info":{"single":{"mode":"SIGN_MODE_DIRECT"}},"sequence":"7"}],"fee":{"amount":[{"denom":"uinit","amount":"3000"}],"gas_limit":"200000","payer":"","granter":""},"tip":null},"signatures":["4x52q4ac4+Xbg/7PXzz73vhQrZgnL+FKsLuCYDK4f1EFOnsXvFAtyn+cqXwzXTJiKY+ZUoi4QfC4u+LEj0IZtg=="]}`, CHAIN_TYPE) + + var tx tx.Tx + err = appCodec.UnmarshalJSON([]byte(jsonText), &tx) + require.NoError(t, err) + require.Len(t, tx.Body.Messages, 1) + + var msg sdk.Msg + err = appCodec.UnpackAny(tx.Body.Messages[0], &msg) + require.NoError(t, err) + require.Equal(t, BatchInfo_CHAIN_TYPE_CELESTIA, msg.(*MsgCreateBridge).Config.BatchInfo.ChainType) +}