github.com/cosmos/cosmos-sdk@v0.50.10/codec/codec.go (about) 1 package codec 2 3 import ( 4 "github.com/cosmos/gogoproto/proto" 5 "google.golang.org/grpc/encoding" 6 protov2 "google.golang.org/protobuf/proto" 7 8 "github.com/cosmos/cosmos-sdk/codec/types" 9 ) 10 11 type ( 12 // Codec defines a functionality for serializing other objects. 13 // Users can defin a custom Protobuf-based serialization. 14 // Note, Amino can still be used without any dependency on Protobuf. 15 // SDK provides to Codec implementations: 16 // 17 // 1. AminoCodec: Provides full Amino serialization compatibility. 18 // 2. ProtoCodec: Provides full Protobuf serialization compatibility. 19 Codec interface { 20 BinaryCodec 21 JSONCodec 22 23 // InterfaceRegistry returns the interface registry. 24 InterfaceRegistry() types.InterfaceRegistry 25 26 // GetMsgAnySigners returns the signers of the given message encoded in a protobuf Any 27 // as well as the decoded google.golang.org/protobuf/proto.Message that was used to 28 // extract the signers so that this can be used in other contexts. 29 GetMsgAnySigners(msg *types.Any) ([][]byte, protov2.Message, error) 30 31 // GetMsgV2Signers returns the signers of the given message. 32 GetMsgV2Signers(msg protov2.Message) ([][]byte, error) 33 34 // GetMsgV1Signers returns the signers of the given message plus the 35 // decoded google.golang.org/protobuf/proto.Message that was used to extract the 36 // signers so that this can be used in other contexts. 37 GetMsgV1Signers(msg proto.Message) ([][]byte, protov2.Message, error) 38 39 // mustEmbedCodec requires that all implementations of Codec embed an official implementation from the codec 40 // package. This allows new methods to be added to the Codec interface without breaking backwards compatibility. 41 mustEmbedCodec() 42 } 43 44 BinaryCodec interface { 45 // Marshal returns binary encoding of v. 46 Marshal(o proto.Message) ([]byte, error) 47 // MustMarshal calls Marshal and panics if error is returned. 48 MustMarshal(o proto.Message) []byte 49 50 // MarshalLengthPrefixed returns binary encoding of v with bytes length prefix. 51 MarshalLengthPrefixed(o proto.Message) ([]byte, error) 52 // MustMarshalLengthPrefixed calls MarshalLengthPrefixed and panics if 53 // error is returned. 54 MustMarshalLengthPrefixed(o proto.Message) []byte 55 56 // Unmarshal parses the data encoded with Marshal method and stores the result 57 // in the value pointed to by v. 58 Unmarshal(bz []byte, ptr proto.Message) error 59 // MustUnmarshal calls Unmarshal and panics if error is returned. 60 MustUnmarshal(bz []byte, ptr proto.Message) 61 62 // Unmarshal parses the data encoded with UnmarshalLengthPrefixed method and stores 63 // the result in the value pointed to by v. 64 UnmarshalLengthPrefixed(bz []byte, ptr proto.Message) error 65 // MustUnmarshalLengthPrefixed calls UnmarshalLengthPrefixed and panics if error 66 // is returned. 67 MustUnmarshalLengthPrefixed(bz []byte, ptr proto.Message) 68 69 // MarshalInterface is a helper method which will wrap `i` into `Any` for correct 70 // binary interface (de)serialization. 71 MarshalInterface(i proto.Message) ([]byte, error) 72 // UnmarshalInterface is a helper method which will parse binary enoded data 73 // into `Any` and unpack any into the `ptr`. It fails if the target interface type 74 // is not registered in codec, or is not compatible with the serialized data 75 UnmarshalInterface(bz []byte, ptr interface{}) error 76 77 types.AnyUnpacker 78 } 79 80 JSONCodec interface { 81 // MarshalJSON returns JSON encoding of v. 82 MarshalJSON(o proto.Message) ([]byte, error) 83 // MustMarshalJSON calls MarshalJSON and panics if error is returned. 84 MustMarshalJSON(o proto.Message) []byte 85 // MarshalInterfaceJSON is a helper method which will wrap `i` into `Any` for correct 86 // JSON interface (de)serialization. 87 MarshalInterfaceJSON(i proto.Message) ([]byte, error) 88 // UnmarshalInterfaceJSON is a helper method which will parse JSON enoded data 89 // into `Any` and unpack any into the `ptr`. It fails if the target interface type 90 // is not registered in codec, or is not compatible with the serialized data 91 UnmarshalInterfaceJSON(bz []byte, ptr interface{}) error 92 93 // UnmarshalJSON parses the data encoded with MarshalJSON method and stores the result 94 // in the value pointed to by v. 95 UnmarshalJSON(bz []byte, ptr proto.Message) error 96 // MustUnmarshalJSON calls Unmarshal and panics if error is returned. 97 MustUnmarshalJSON(bz []byte, ptr proto.Message) 98 } 99 100 // ProtoMarshaler defines an interface a type must implement to serialize itself 101 // as a protocol buffer defined message. 102 // 103 // Deprecated: Use proto.Message instead from github.com/cosmos/gogoproto/proto. 104 ProtoMarshaler interface { 105 proto.Message // for JSON serialization 106 107 Marshal() ([]byte, error) 108 MarshalTo(data []byte) (n int, err error) 109 MarshalToSizedBuffer(dAtA []byte) (int, error) 110 Size() int 111 Unmarshal(data []byte) error 112 } 113 114 // AminoMarshaler defines an interface a type must implement to serialize itself 115 // for Amino codec. 116 AminoMarshaler interface { 117 MarshalAmino() ([]byte, error) 118 UnmarshalAmino([]byte) error 119 MarshalAminoJSON() ([]byte, error) 120 UnmarshalAminoJSON([]byte) error 121 } 122 123 // GRPCCodecProvider is implemented by the Codec 124 // implementations which return a gRPC encoding.Codec. 125 // And it is used to decode requests and encode responses 126 // passed through gRPC. 127 GRPCCodecProvider interface { 128 GRPCCodec() encoding.Codec 129 } 130 )