laconicd/app/encoding.go

73 lines
2.0 KiB
Go
Raw Normal View History

2021-04-17 10:00:07 +00:00
package app
import (
"github.com/cosmos/cosmos-sdk/client"
amino "github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/simapp/params"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth/tx"
2021-04-18 16:39:15 +00:00
evmtypes "github.com/cosmos/ethermint/x/evm/types"
2021-04-17 10:00:07 +00:00
"github.com/cosmos/ethermint/codec"
)
// MakeEncodingConfig creates an EncodingConfig for testing
func MakeEncodingConfig() params.EncodingConfig {
cdc := amino.NewLegacyAmino()
interfaceRegistry := types.NewInterfaceRegistry()
marshaler := amino.NewProtoCodec(interfaceRegistry)
encodingConfig := params.EncodingConfig{
InterfaceRegistry: interfaceRegistry,
Marshaler: marshaler,
TxConfig: NewTxConfig(marshaler),
Amino: cdc,
}
codec.RegisterLegacyAminoCodec(encodingConfig.Amino)
ModuleBasics.RegisterLegacyAminoCodec(encodingConfig.Amino)
codec.RegisterInterfaces(encodingConfig.InterfaceRegistry)
ModuleBasics.RegisterInterfaces(encodingConfig.InterfaceRegistry)
return encodingConfig
}
type txConfig struct {
cdc amino.ProtoCodecMarshaler
client.TxConfig
}
// NewTxConfig returns a new protobuf TxConfig using the provided ProtoCodec and sign modes. The
// first enabled sign mode will become the default sign mode.
func NewTxConfig(marshaler amino.ProtoCodecMarshaler) client.TxConfig {
return &txConfig{
marshaler,
tx.NewTxConfig(marshaler, tx.DefaultSignModes),
}
}
2021-04-18 16:39:15 +00:00
// TxEncoder overwrites sdk.TxEncoder to support MsgEthereumTx
2021-04-17 10:00:07 +00:00
func (g txConfig) TxEncoder() sdk.TxEncoder {
return func(tx sdk.Tx) ([]byte, error) {
ethtx, ok := tx.(*evmtypes.MsgEthereumTx)
if ok {
return g.cdc.MarshalBinaryBare(ethtx)
}
return g.TxConfig.TxEncoder()(tx)
}
}
2021-04-18 16:39:15 +00:00
// TxDecoder overwrites sdk.TxDecoder to support MsgEthereumTx
2021-04-17 10:00:07 +00:00
func (g txConfig) TxDecoder() sdk.TxDecoder {
return func(txBytes []byte) (sdk.Tx, error) {
var ethtx evmtypes.MsgEthereumTx
err := g.cdc.UnmarshalBinaryBare(txBytes, &ethtx)
if err == nil {
return &ethtx, nil
}
return g.TxConfig.TxDecoder()(txBytes)
}
}