github.com/Finschia/finschia-sdk@v0.49.1/x/auth/tx/encoder.go (about) 1 package tx 2 3 import ( 4 "fmt" 5 6 "github.com/gogo/protobuf/proto" 7 8 "github.com/Finschia/finschia-sdk/codec" 9 sdk "github.com/Finschia/finschia-sdk/types" 10 txtypes "github.com/Finschia/finschia-sdk/types/tx" 11 ) 12 13 // DefaultTxEncoder returns a default protobuf TxEncoder using the provided Marshaler 14 func DefaultTxEncoder() sdk.TxEncoder { 15 return func(tx sdk.Tx) ([]byte, error) { 16 txWrapper, ok := tx.(*wrapper) 17 if !ok { 18 return nil, fmt.Errorf("expected %T, got %T", &wrapper{}, tx) 19 } 20 21 raw := &txtypes.TxRaw{ 22 BodyBytes: txWrapper.getBodyBytes(), 23 AuthInfoBytes: txWrapper.getAuthInfoBytes(), 24 Signatures: txWrapper.tx.Signatures, 25 } 26 27 return proto.Marshal(raw) 28 } 29 } 30 31 // DefaultJSONTxEncoder returns a default protobuf JSON TxEncoder using the provided Marshaler. 32 func DefaultJSONTxEncoder(cdc codec.ProtoCodecMarshaler) sdk.TxEncoder { 33 return func(tx sdk.Tx) ([]byte, error) { 34 txWrapper, ok := tx.(*wrapper) 35 if ok { 36 return cdc.MarshalJSON(txWrapper.tx) 37 } 38 39 protoTx, ok := tx.(*txtypes.Tx) 40 if ok { 41 return cdc.MarshalJSON(protoTx) 42 } 43 44 return nil, fmt.Errorf("expected %T, got %T", &wrapper{}, tx) 45 } 46 }