github.com/cosmos/cosmos-sdk@v0.50.10/x/auth/tx/encoder.go (about) 1 package tx 2 3 import ( 4 "fmt" 5 6 "github.com/cosmos/gogoproto/proto" 7 8 "github.com/cosmos/cosmos-sdk/codec" 9 sdk "github.com/cosmos/cosmos-sdk/types" 10 txtypes "github.com/cosmos/cosmos-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.Codec) 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 return nil, fmt.Errorf("expected %T, got %T", &wrapper{}, tx) 40 } 41 }