|
1 | 1 | package types
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "bytes" |
| 5 | + |
4 | 6 | "github.com/cosmos/cosmos-sdk/codec"
|
5 | 7 | codectypes "github.com/cosmos/cosmos-sdk/codec/types"
|
6 | 8 | sdk "github.com/cosmos/cosmos-sdk/types"
|
7 | 9 | "github.com/cosmos/cosmos-sdk/types/msgservice"
|
| 10 | + "github.com/gogo/protobuf/jsonpb" |
| 11 | + "github.com/gogo/protobuf/proto" |
8 | 12 | )
|
9 | 13 |
|
10 | 14 | // RegisterLegacyAminoCodec registers the necessary x/ibc transfer interfaces and concrete types
|
@@ -39,3 +43,32 @@ func init() {
|
39 | 43 | RegisterLegacyAminoCodec(amino)
|
40 | 44 | amino.Seal()
|
41 | 45 | }
|
| 46 | + |
| 47 | +// mustProtoMarshalJSON provides an auxiliary function to return Proto3 JSON encoded |
| 48 | +// bytes of a message. |
| 49 | +// NOTE: Copied from https://github.com/cosmos/cosmos-sdk/blob/971c542453e0972ef1dfc5a80159ad5049c7211c/codec/json.go |
| 50 | +// and modified in order to allow `EmitDefaults` to be set to false for ics20 packet marshalling. |
| 51 | +// This allows for the introduction of the memo field to be backwards compatible. |
| 52 | +func mustProtoMarshalJSON(msg proto.Message) []byte { |
| 53 | + anyResolver := codectypes.NewInterfaceRegistry() |
| 54 | + |
| 55 | + // EmitDefaults is set to false to prevent marshalling of unpopulated fields (memo) |
| 56 | + // OrigName and the anyResovler match the fields the original SDK function would expect |
| 57 | + // in order to minimize changes. |
| 58 | + |
| 59 | + // OrigName is true since there is no particular reason to use camel case |
| 60 | + // The any resolver is empty, but provided anyways. |
| 61 | + jm := &jsonpb.Marshaler{OrigName: true, EmitDefaults: false, AnyResolver: anyResolver} |
| 62 | + |
| 63 | + err := codectypes.UnpackInterfaces(msg, codectypes.ProtoJSONPacker{JSONPBMarshaler: jm}) |
| 64 | + if err != nil { |
| 65 | + panic(err) |
| 66 | + } |
| 67 | + |
| 68 | + buf := new(bytes.Buffer) |
| 69 | + if err := jm.Marshal(buf, msg); err != nil { |
| 70 | + panic(err) |
| 71 | + } |
| 72 | + |
| 73 | + return buf.Bytes() |
| 74 | +} |
0 commit comments