cosmos-sdk/x/auth/tx/encoder.go
Jack Zampolin 9befc6ced8
Wrap ProtoCodec in interface (#7637)
* WIP encoding change

* Add test that describes issue

* WIP debugging

* remove extra code

* Update codec/proto_codec.go

Co-authored-by: colin axnér <25233464+colin-axner@users.noreply.github.com>
2020-10-27 13:53:54 +00:00

48 lines
1.1 KiB
Go

package tx
import (
"fmt"
"github.com/gogo/protobuf/proto"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
txtypes "github.com/cosmos/cosmos-sdk/types/tx"
)
// DefaultTxEncoder returns a default protobuf TxEncoder using the provided Marshaler
func DefaultTxEncoder() sdk.TxEncoder {
return func(tx sdk.Tx) ([]byte, error) {
txWrapper, ok := tx.(*wrapper)
if !ok {
return nil, fmt.Errorf("expected %T, got %T", &wrapper{}, tx)
}
raw := &txtypes.TxRaw{
BodyBytes: txWrapper.getBodyBytes(),
AuthInfoBytes: txWrapper.getAuthInfoBytes(),
Signatures: txWrapper.tx.Signatures,
}
return proto.Marshal(raw)
}
}
// DefaultJSONTxEncoder returns a default protobuf JSON TxEncoder using the provided Marshaler.
func DefaultJSONTxEncoder(cdc codec.ProtoCodecMarshaler) sdk.TxEncoder {
return func(tx sdk.Tx) ([]byte, error) {
txWrapper, ok := tx.(*wrapper)
if ok {
return cdc.MarshalJSON(txWrapper.tx)
}
protoTx, ok := tx.(*txtypes.Tx)
if ok {
return cdc.MarshalJSON(protoTx)
}
return nil, fmt.Errorf("expected %T, got %T", &wrapper{}, tx)
}
}