laconicd-deprecated/encoding/config.go

78 lines
2.2 KiB
Go
Raw Normal View History

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"
"github.com/cosmos/cosmos-sdk/types/module"
2021-04-17 10:00:07 +00:00
"github.com/cosmos/cosmos-sdk/x/auth/tx"
ethtypes "github.com/ethereum/go-ethereum/core/types"
2021-04-17 10:00:07 +00:00
2021-06-04 13:45:37 +00:00
enccodec "github.com/cosmos/ethermint/encoding/codec"
evmtypes "github.com/cosmos/ethermint/x/evm/types"
2021-04-17 10:00:07 +00:00
)
// MakeEncodingConfig creates an EncodingConfig for testing
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)
mb.RegisterLegacyAminoCodec(encodingConfig.Amino)
2021-06-04 13:45:37 +00:00
enccodec.RegisterInterfaces(encodingConfig.InterfaceRegistry)
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) {
msg, ok := tx.(*evmtypes.MsgEthereumTx)
2021-04-17 10:00:07 +00:00
if ok {
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) {
tx := &ethtypes.Transaction{}
2021-04-17 10:00:07 +00:00
err := tx.UnmarshalBinary(txBytes)
2021-04-17 10:00:07 +00:00
if err == nil {
msg := &evmtypes.MsgEthereumTx{}
msg.FromEthereumTx(tx)
return msg, nil
2021-04-17 10:00:07 +00:00
}
return g.TxConfig.TxDecoder()(txBytes)
}
}