2021-06-04 13:14:45 +00:00
|
|
|
package encoding
|
2021-04-17 10:00:07 +00:00
|
|
|
|
|
|
|
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"
|
2021-06-04 13:14:45 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/types/module"
|
2021-04-17 10:00:07 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth/tx"
|
2021-06-02 08:06:12 +00:00
|
|
|
|
|
|
|
ethtypes "github.com/ethereum/go-ethereum/core/types"
|
2021-04-17 10:00:07 +00:00
|
|
|
|
2021-06-22 10:49:18 +00:00
|
|
|
enccodec "github.com/tharsis/ethermint/encoding/codec"
|
|
|
|
evmtypes "github.com/tharsis/ethermint/x/evm/types"
|
2021-04-17 10:00:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// MakeEncodingConfig creates an EncodingConfig for testing
|
2021-06-04 13:14:45 +00:00
|
|
|
func MakeConfig(mb module.BasicManager) params.EncodingConfig {
|
2021-04-17 10:00:07 +00:00
|
|
|
cdc := amino.NewLegacyAmino()
|
|
|
|
interfaceRegistry := types.NewInterfaceRegistry()
|
|
|
|
marshaler := amino.NewProtoCodec(interfaceRegistry)
|
|
|
|
|
|
|
|
encodingConfig := params.EncodingConfig{
|
|
|
|
InterfaceRegistry: interfaceRegistry,
|
|
|
|
Marshaler: marshaler,
|
|
|
|
TxConfig: NewTxConfig(marshaler),
|
|
|
|
Amino: cdc,
|
|
|
|
}
|
|
|
|
|
2021-06-04 13:45:37 +00:00
|
|
|
enccodec.RegisterLegacyAminoCodec(encodingConfig.Amino)
|
2021-06-04 13:14:45 +00:00
|
|
|
mb.RegisterLegacyAminoCodec(encodingConfig.Amino)
|
2021-06-04 13:45:37 +00:00
|
|
|
enccodec.RegisterInterfaces(encodingConfig.InterfaceRegistry)
|
2021-06-04 13:14:45 +00:00
|
|
|
mb.RegisterInterfaces(encodingConfig.InterfaceRegistry)
|
2021-04-17 10:00:07 +00:00
|
|
|
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) {
|
2021-06-02 08:06:12 +00:00
|
|
|
msg, ok := tx.(*evmtypes.MsgEthereumTx)
|
2021-04-17 10:00:07 +00:00
|
|
|
if ok {
|
2021-06-02 08:06:12 +00:00
|
|
|
return msg.AsTransaction().MarshalBinary()
|
2021-04-17 10:00:07 +00:00
|
|
|
}
|
|
|
|
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) {
|
2021-06-02 08:06:12 +00:00
|
|
|
tx := ðtypes.Transaction{}
|
2021-04-17 10:00:07 +00:00
|
|
|
|
2021-06-02 08:06:12 +00:00
|
|
|
err := tx.UnmarshalBinary(txBytes)
|
2021-04-17 10:00:07 +00:00
|
|
|
if err == nil {
|
2021-06-02 08:06:12 +00:00
|
|
|
msg := &evmtypes.MsgEthereumTx{}
|
|
|
|
msg.FromEthereumTx(tx)
|
|
|
|
return msg, nil
|
2021-04-17 10:00:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return g.TxConfig.TxDecoder()(txBytes)
|
|
|
|
}
|
|
|
|
}
|