cosmos-sdk/x/auth/tx/encoder.go
Julien Robert 2a5ff384fa
refactor(x/**): rewrite ante handlers as tx validators (#20488)
Co-authored-by: Likhita Polavarapu <78951027+likhita-809@users.noreply.github.com>
2024-06-03 16:47:58 +00:00

38 lines
977 B
Go

package tx
import (
"fmt"
"google.golang.org/protobuf/encoding/protojson"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// DefaultTxEncoder returns a default protobuf TxEncoder using the provided Marshaler
func DefaultTxEncoder() sdk.TxEncoder {
return func(tx sdk.Tx) ([]byte, error) {
gogoWrapper, ok := tx.(*gogoTxWrapper)
if !ok {
return nil, fmt.Errorf("unexpected tx type: %T", tx)
}
return marshalOption.Marshal(gogoWrapper.TxRaw)
}
}
// DefaultJSONTxEncoder returns a default protobuf JSON TxEncoder using the provided Marshaler.
func DefaultJSONTxEncoder(cdc codec.Codec) sdk.TxEncoder {
jsonMarshaler := protojson.MarshalOptions{
Indent: "",
UseProtoNames: true,
UseEnumNumbers: false,
}
return func(tx sdk.Tx) ([]byte, error) {
gogoWrapper, ok := tx.(*gogoTxWrapper)
if !ok {
return nil, fmt.Errorf("unexpected tx type: %T", tx)
}
return jsonMarshaler.Marshal(gogoWrapper.Tx)
}
}