github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/cosmos-sdk/codec/codec_adapter.go (about) 1 package codec 2 3 import ( 4 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/codec/types" 5 "github.com/gogo/protobuf/proto" 6 ) 7 8 type ( 9 // Marshaler defines the interface module codecs must implement in order to support 10 // backwards compatibility with Amino while allowing custom Protobuf-based 11 // serialization. Note, Amino can still be used without any dependency on 12 // Protobuf. There are two typical implementations that fulfill this contract: 13 // 14 // 1. AminoCodec: Provides full Amino serialization compatibility. 15 // 2. ProtoCodec: Provides full Protobuf serialization compatibility. 16 Marshaler interface { 17 BinaryMarshaler 18 JSONMarshaler 19 } 20 21 BinaryMarshaler interface { 22 MarshalBinaryBare(o ProtoMarshaler) ([]byte, error) 23 MustMarshalBinaryBare(o ProtoMarshaler) []byte 24 25 MarshalBinaryLengthPrefixed(o ProtoMarshaler) ([]byte, error) 26 MustMarshalBinaryLengthPrefixed(o ProtoMarshaler) []byte 27 28 UnmarshalBinaryBare(bz []byte, ptr ProtoMarshaler) error 29 MustUnmarshalBinaryBare(bz []byte, ptr ProtoMarshaler) 30 31 UnmarshalBinaryLengthPrefixed(bz []byte, ptr ProtoMarshaler) error 32 MustUnmarshalBinaryLengthPrefixed(bz []byte, ptr ProtoMarshaler) 33 34 MarshalInterface(i proto.Message) ([]byte, error) 35 UnmarshalInterface(bz []byte, ptr interface{}) error 36 37 types.AnyUnpacker 38 } 39 40 JSONMarshaler interface { 41 MarshalJSON(o proto.Message) ([]byte, error) 42 MustMarshalJSON(o proto.Message) []byte 43 MarshalInterfaceJSON(i proto.Message) ([]byte, error) 44 UnmarshalInterfaceJSON(bz []byte, ptr interface{}) error 45 46 UnmarshalJSON(bz []byte, ptr proto.Message) error 47 MustUnmarshalJSON(bz []byte, ptr proto.Message) 48 } 49 50 // ProtoMarshaler defines an interface a type must implement as protocol buffer 51 // defined message. 52 ProtoMarshaler interface { 53 proto.Message // for JSON serialization 54 55 Marshal() ([]byte, error) 56 MarshalTo(data []byte) (n int, err error) 57 MarshalToSizedBuffer(dAtA []byte) (int, error) 58 Size() int 59 Unmarshal(data []byte) error 60 } 61 62 // AminoMarshaler defines an interface where Amino marshalling can be 63 // overridden by custom marshalling. 64 AminoMarshaler interface { 65 MarshalAmino() ([]byte, error) 66 UnmarshalAmino([]byte) error 67 MarshalAminoJSON() ([]byte, error) 68 UnmarshalAminoJSON([]byte) error 69 } 70 // 71 IbcCodec interface { 72 BinaryCodec 73 JSONCodec 74 } 75 76 BinaryCodec interface { 77 // Marshal returns binary encoding of v. 78 Marshal(o ProtoMarshaler) ([]byte, error) 79 // MustMarshal calls Marshal and panics if error is returned. 80 MustMarshal(o ProtoMarshaler) []byte 81 82 // MarshalLengthPrefixed returns binary encoding of v with bytes length prefix. 83 MarshalLengthPrefixed(o ProtoMarshaler) ([]byte, error) 84 // MustMarshalLengthPrefixed calls MarshalLengthPrefixed and panics if 85 // error is returned. 86 MustMarshalLengthPrefixed(o ProtoMarshaler) []byte 87 88 // Unmarshal parses the data encoded with Marshal method and stores the result 89 // in the value pointed to by v. 90 Unmarshal(bz []byte, ptr ProtoMarshaler) error 91 // MustUnmarshal calls Unmarshal and panics if error is returned. 92 MustUnmarshal(bz []byte, ptr ProtoMarshaler) 93 94 // Unmarshal parses the data encoded with UnmarshalLengthPrefixed method and stores 95 // the result in the value pointed to by v. 96 UnmarshalLengthPrefixed(bz []byte, ptr ProtoMarshaler) error 97 // MustUnmarshalLengthPrefixed calls UnmarshalLengthPrefixed and panics if error 98 // is returned. 99 MustUnmarshalLengthPrefixed(bz []byte, ptr ProtoMarshaler) 100 101 // MarshalInterface is a helper method which will wrap `i` into `Any` for correct 102 // binary interface (de)serialization. 103 MarshalInterface(i proto.Message) ([]byte, error) 104 // UnmarshalInterface is a helper method which will parse binary enoded data 105 // into `Any` and unpack any into the `ptr`. It fails if the target interface type 106 // is not registered in codec, or is not compatible with the serialized data 107 UnmarshalInterface(bz []byte, ptr interface{}) error 108 109 types.AnyUnpacker 110 } 111 112 JSONCodec interface { 113 // MarshalJSON returns JSON encoding of v. 114 MarshalJSON(o proto.Message) ([]byte, error) 115 // MustMarshalJSON calls MarshalJSON and panics if error is returned. 116 MustMarshalJSON(o proto.Message) []byte 117 // MarshalInterfaceJSON is a helper method which will wrap `i` into `Any` for correct 118 // JSON interface (de)serialization. 119 MarshalInterfaceJSON(i proto.Message) ([]byte, error) 120 // UnmarshalInterfaceJSON is a helper method which will parse JSON enoded data 121 // into `Any` and unpack any into the `ptr`. It fails if the target interface type 122 // is not registered in codec, or is not compatible with the serialized data 123 UnmarshalInterfaceJSON(bz []byte, ptr interface{}) error 124 125 // UnmarshalJSON parses the data encoded with MarshalJSON method and stores the result 126 // in the value pointed to by v. 127 UnmarshalJSON(bz []byte, ptr proto.Message) error 128 // MustUnmarshalJSON calls Unmarshal and panics if error is returned. 129 MustUnmarshalJSON(bz []byte, ptr proto.Message) 130 } 131 ) 132 133 // /////// 134 var ( 135 _ CdcAbstraction = (*CodecProxy)(nil) 136 ) 137 138 type CodecProxy struct { 139 protoCodec *ProtoCodec 140 cdc *Codec 141 } 142 143 func (mp *CodecProxy) UnmarshalBinaryLengthPrefixedWithRegisteredUbmarshaller(bz []byte, ptr interface{}) (interface{}, error) { 144 return mp.cdc.UnmarshalBinaryLengthPrefixedWithRegisteredUbmarshaller(bz, ptr) 145 } 146 147 func (mp *CodecProxy) UnmarshalBinaryLengthPrefixed(bz []byte, ptr interface{}) error { 148 return mp.cdc.UnmarshalBinaryLengthPrefixed(bz, ptr) 149 } 150 151 func NewCodecProxy(protoCodec *ProtoCodec, cdc *Codec) *CodecProxy { 152 return &CodecProxy{protoCodec: protoCodec, cdc: cdc} 153 } 154 155 func (mp *CodecProxy) GetCdc() *Codec { 156 return mp.cdc 157 } 158 159 func (mp *CodecProxy) GetProtocMarshal() *ProtoCodec { 160 return mp.protoCodec 161 }