2018-11-28 22:19:22 +00:00
|
|
|
package types
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"math/big"
|
|
|
|
"sync/atomic"
|
2019-11-01 15:26:53 +00:00
|
|
|
|
2018-12-18 16:10:04 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
2018-11-28 22:19:22 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2020-04-16 15:47:39 +00:00
|
|
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
2018-11-28 22:19:22 +00:00
|
|
|
"github.com/cosmos/ethermint/types"
|
|
|
|
|
|
|
|
ethcmn "github.com/ethereum/go-ethereum/common"
|
|
|
|
ethtypes "github.com/ethereum/go-ethereum/core/types"
|
|
|
|
ethcrypto "github.com/ethereum/go-ethereum/crypto"
|
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
|
|
|
)
|
|
|
|
|
2018-12-18 16:10:04 +00:00
|
|
|
var (
|
2020-04-01 18:49:21 +00:00
|
|
|
_ sdk.Msg = MsgEthereumTx{}
|
|
|
|
_ sdk.Tx = MsgEthereumTx{}
|
2018-12-18 16:10:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var big8 = big.NewInt(8)
|
2018-11-28 22:19:22 +00:00
|
|
|
|
|
|
|
// message type and route constants
|
|
|
|
const (
|
2020-04-01 18:49:21 +00:00
|
|
|
TypeMsgEthereumTx = "ethereum"
|
2018-11-28 22:19:22 +00:00
|
|
|
)
|
|
|
|
|
2020-04-01 18:49:21 +00:00
|
|
|
// MsgEthereumTx encapsulates an Ethereum transaction as an SDK message.
|
2018-11-28 22:19:22 +00:00
|
|
|
type (
|
2020-04-01 18:49:21 +00:00
|
|
|
MsgEthereumTx struct {
|
2018-11-28 22:19:22 +00:00
|
|
|
Data TxData
|
|
|
|
|
|
|
|
// caches
|
|
|
|
hash atomic.Value
|
|
|
|
size atomic.Value
|
|
|
|
from atomic.Value
|
|
|
|
}
|
|
|
|
|
|
|
|
// TxData implements the Ethereum transaction data structure. It is used
|
|
|
|
// solely as intended in Ethereum abiding by the protocol.
|
|
|
|
TxData struct {
|
|
|
|
AccountNonce uint64 `json:"nonce"`
|
|
|
|
Price *big.Int `json:"gasPrice"`
|
|
|
|
GasLimit uint64 `json:"gas"`
|
|
|
|
Recipient *ethcmn.Address `json:"to" rlp:"nil"` // nil means contract creation
|
|
|
|
Amount *big.Int `json:"value"`
|
|
|
|
Payload []byte `json:"input"`
|
|
|
|
|
|
|
|
// signature values
|
|
|
|
V *big.Int `json:"v"`
|
|
|
|
R *big.Int `json:"r"`
|
|
|
|
S *big.Int `json:"s"`
|
|
|
|
|
|
|
|
// hash is only used when marshaling to JSON
|
|
|
|
Hash *ethcmn.Hash `json:"hash" rlp:"-"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// sigCache is used to cache the derived sender and contains the signer used
|
|
|
|
// to derive it.
|
|
|
|
sigCache struct {
|
|
|
|
signer ethtypes.Signer
|
|
|
|
from ethcmn.Address
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2020-04-01 18:49:21 +00:00
|
|
|
// NewMsgEthereumTx returns a reference to a new Ethereum transaction message.
|
|
|
|
func NewMsgEthereumTx(
|
2019-10-30 18:30:24 +00:00
|
|
|
nonce uint64, to *ethcmn.Address, amount *big.Int,
|
2018-11-28 22:19:22 +00:00
|
|
|
gasLimit uint64, gasPrice *big.Int, payload []byte,
|
2020-04-01 18:49:21 +00:00
|
|
|
) MsgEthereumTx {
|
2018-11-28 22:19:22 +00:00
|
|
|
|
2020-04-01 18:49:21 +00:00
|
|
|
return newMsgEthereumTx(nonce, to, amount, gasLimit, gasPrice, payload)
|
2018-11-28 22:19:22 +00:00
|
|
|
}
|
|
|
|
|
2020-04-01 18:49:21 +00:00
|
|
|
// NewMsgEthereumTxContract returns a reference to a new Ethereum transaction
|
2018-11-28 22:19:22 +00:00
|
|
|
// message designated for contract creation.
|
2020-04-01 18:49:21 +00:00
|
|
|
func NewMsgEthereumTxContract(
|
2018-11-28 22:19:22 +00:00
|
|
|
nonce uint64, amount *big.Int, gasLimit uint64, gasPrice *big.Int, payload []byte,
|
2020-04-01 18:49:21 +00:00
|
|
|
) MsgEthereumTx {
|
2018-11-28 22:19:22 +00:00
|
|
|
|
2020-04-01 18:49:21 +00:00
|
|
|
return newMsgEthereumTx(nonce, nil, amount, gasLimit, gasPrice, payload)
|
2018-11-28 22:19:22 +00:00
|
|
|
}
|
|
|
|
|
2020-04-01 18:49:21 +00:00
|
|
|
func newMsgEthereumTx(
|
2018-11-28 22:19:22 +00:00
|
|
|
nonce uint64, to *ethcmn.Address, amount *big.Int,
|
|
|
|
gasLimit uint64, gasPrice *big.Int, payload []byte,
|
2020-04-01 18:49:21 +00:00
|
|
|
) MsgEthereumTx {
|
2018-11-28 22:19:22 +00:00
|
|
|
|
|
|
|
if len(payload) > 0 {
|
|
|
|
payload = ethcmn.CopyBytes(payload)
|
|
|
|
}
|
|
|
|
|
|
|
|
txData := TxData{
|
|
|
|
AccountNonce: nonce,
|
|
|
|
Recipient: to,
|
|
|
|
Payload: payload,
|
|
|
|
GasLimit: gasLimit,
|
|
|
|
Amount: new(big.Int),
|
|
|
|
Price: new(big.Int),
|
|
|
|
V: new(big.Int),
|
|
|
|
R: new(big.Int),
|
|
|
|
S: new(big.Int),
|
|
|
|
}
|
|
|
|
|
|
|
|
if amount != nil {
|
|
|
|
txData.Amount.Set(amount)
|
|
|
|
}
|
|
|
|
if gasPrice != nil {
|
|
|
|
txData.Price.Set(gasPrice)
|
|
|
|
}
|
|
|
|
|
2020-04-01 18:49:21 +00:00
|
|
|
return MsgEthereumTx{Data: txData}
|
2018-11-28 22:19:22 +00:00
|
|
|
}
|
|
|
|
|
2020-04-01 18:49:21 +00:00
|
|
|
// Route returns the route value of an MsgEthereumTx.
|
|
|
|
func (msg MsgEthereumTx) Route() string { return RouterKey }
|
2018-11-28 22:19:22 +00:00
|
|
|
|
2020-04-01 18:49:21 +00:00
|
|
|
// Type returns the type value of an MsgEthereumTx.
|
|
|
|
func (msg MsgEthereumTx) Type() string { return TypeMsgEthereumTx }
|
2018-11-28 22:19:22 +00:00
|
|
|
|
|
|
|
// ValidateBasic implements the sdk.Msg interface. It performs basic validation
|
2020-04-01 18:49:21 +00:00
|
|
|
// checks of a Transaction. If returns an error if validation fails.
|
|
|
|
func (msg MsgEthereumTx) ValidateBasic() sdk.Error {
|
2018-11-28 22:19:22 +00:00
|
|
|
if msg.Data.Price.Sign() != 1 {
|
2020-04-16 15:47:39 +00:00
|
|
|
return sdk.ConvertError(
|
|
|
|
sdkerrors.Wrapf(types.ErrInvalidValue, "price must be positive %s", msg.Data.Price),
|
|
|
|
)
|
2018-11-28 22:19:22 +00:00
|
|
|
}
|
|
|
|
|
2019-09-18 20:14:39 +00:00
|
|
|
// Amount can be 0
|
|
|
|
if msg.Data.Amount.Sign() == -1 {
|
2020-04-16 15:47:39 +00:00
|
|
|
return sdk.ConvertError(
|
|
|
|
sdkerrors.Wrapf(types.ErrInvalidValue, "amount cannot be negative %s", msg.Data.Amount),
|
|
|
|
)
|
2018-11-28 22:19:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-12-18 16:10:04 +00:00
|
|
|
// To returns the recipient address of the transaction. It returns nil if the
|
|
|
|
// transaction is a contract creation.
|
2020-04-01 18:49:21 +00:00
|
|
|
func (msg MsgEthereumTx) To() *ethcmn.Address {
|
2018-12-18 16:10:04 +00:00
|
|
|
return msg.Data.Recipient
|
|
|
|
}
|
|
|
|
|
2020-04-01 18:49:21 +00:00
|
|
|
// GetMsgs returns a single MsgEthereumTx as an sdk.Msg.
|
|
|
|
func (msg MsgEthereumTx) GetMsgs() []sdk.Msg {
|
2018-12-18 16:10:04 +00:00
|
|
|
return []sdk.Msg{msg}
|
|
|
|
}
|
|
|
|
|
2018-11-28 22:19:22 +00:00
|
|
|
// GetSigners returns the expected signers for an Ethereum transaction message.
|
|
|
|
// For such a message, there should exist only a single 'signer'.
|
|
|
|
//
|
|
|
|
// NOTE: This method cannot be used as a chain ID is needed to recover the signer
|
|
|
|
// from the signature. Use 'VerifySig' instead.
|
2020-04-01 18:49:21 +00:00
|
|
|
func (msg MsgEthereumTx) GetSigners() []sdk.AccAddress {
|
2018-11-28 22:19:22 +00:00
|
|
|
panic("must use 'VerifySig' with a chain ID to get the signer")
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetSignBytes returns the Amino bytes of an Ethereum transaction message used
|
|
|
|
// for signing.
|
|
|
|
//
|
|
|
|
// NOTE: This method cannot be used as a chain ID is needed to create valid bytes
|
|
|
|
// to sign over. Use 'RLPSignBytes' instead.
|
2020-04-01 18:49:21 +00:00
|
|
|
func (msg MsgEthereumTx) GetSignBytes() []byte {
|
2018-11-28 22:19:22 +00:00
|
|
|
panic("must use 'RLPSignBytes' with a chain ID to get the valid bytes to sign")
|
|
|
|
}
|
|
|
|
|
|
|
|
// RLPSignBytes returns the RLP hash of an Ethereum transaction message with a
|
|
|
|
// given chainID used for signing.
|
2020-04-01 18:49:21 +00:00
|
|
|
func (msg MsgEthereumTx) RLPSignBytes(chainID *big.Int) ethcmn.Hash {
|
2018-11-28 22:19:22 +00:00
|
|
|
return rlpHash([]interface{}{
|
|
|
|
msg.Data.AccountNonce,
|
|
|
|
msg.Data.Price,
|
|
|
|
msg.Data.GasLimit,
|
|
|
|
msg.Data.Recipient,
|
|
|
|
msg.Data.Amount,
|
|
|
|
msg.Data.Payload,
|
|
|
|
chainID, uint(0), uint(0),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// EncodeRLP implements the rlp.Encoder interface.
|
2020-04-01 18:49:21 +00:00
|
|
|
func (msg *MsgEthereumTx) EncodeRLP(w io.Writer) error {
|
2018-11-28 22:19:22 +00:00
|
|
|
return rlp.Encode(w, &msg.Data)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DecodeRLP implements the rlp.Decoder interface.
|
2020-04-01 18:49:21 +00:00
|
|
|
func (msg *MsgEthereumTx) DecodeRLP(s *rlp.Stream) error {
|
2018-11-28 22:19:22 +00:00
|
|
|
_, size, _ := s.Kind()
|
|
|
|
|
|
|
|
err := s.Decode(&msg.Data)
|
|
|
|
if err == nil {
|
|
|
|
msg.size.Store(ethcmn.StorageSize(rlp.ListSize(size)))
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sign calculates a secp256k1 ECDSA signature and signs the transaction. It
|
|
|
|
// takes a private key and chainID to sign an Ethereum transaction according to
|
|
|
|
// EIP155 standard. It mutates the transaction as it populates the V, R, S
|
|
|
|
// fields of the Transaction's Signature.
|
2020-04-01 18:49:21 +00:00
|
|
|
func (msg *MsgEthereumTx) Sign(chainID *big.Int, priv *ecdsa.PrivateKey) {
|
2018-11-28 22:19:22 +00:00
|
|
|
txHash := msg.RLPSignBytes(chainID)
|
|
|
|
|
|
|
|
sig, err := ethcrypto.Sign(txHash[:], priv)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(sig) != 65 {
|
|
|
|
panic(fmt.Sprintf("wrong size for signature: got %d, want 65", len(sig)))
|
|
|
|
}
|
|
|
|
|
|
|
|
r := new(big.Int).SetBytes(sig[:32])
|
|
|
|
s := new(big.Int).SetBytes(sig[32:64])
|
|
|
|
|
|
|
|
var v *big.Int
|
|
|
|
|
|
|
|
if chainID.Sign() == 0 {
|
|
|
|
v = new(big.Int).SetBytes([]byte{sig[64] + 27})
|
|
|
|
} else {
|
|
|
|
v = big.NewInt(int64(sig[64] + 35))
|
|
|
|
chainIDMul := new(big.Int).Mul(chainID, big.NewInt(2))
|
|
|
|
|
|
|
|
v.Add(v, chainIDMul)
|
|
|
|
}
|
|
|
|
|
|
|
|
msg.Data.V = v
|
|
|
|
msg.Data.R = r
|
|
|
|
msg.Data.S = s
|
|
|
|
}
|
|
|
|
|
|
|
|
// VerifySig attempts to verify a Transaction's signature for a given chainID.
|
|
|
|
// A derived address is returned upon success or an error if recovery fails.
|
2020-04-01 18:49:21 +00:00
|
|
|
func (msg *MsgEthereumTx) VerifySig(chainID *big.Int) (ethcmn.Address, error) {
|
2018-11-28 22:19:22 +00:00
|
|
|
signer := ethtypes.NewEIP155Signer(chainID)
|
|
|
|
|
|
|
|
if sc := msg.from.Load(); sc != nil {
|
|
|
|
sigCache := sc.(sigCache)
|
|
|
|
// If the signer used to derive from in a previous call is not the same as
|
|
|
|
// used current, invalidate the cache.
|
|
|
|
if sigCache.signer.Equal(signer) {
|
|
|
|
return sigCache.from, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// do not allow recovery for transactions with an unprotected chainID
|
|
|
|
if chainID.Sign() == 0 {
|
2018-12-18 16:10:04 +00:00
|
|
|
return ethcmn.Address{}, errors.New("chainID cannot be zero")
|
2018-11-28 22:19:22 +00:00
|
|
|
}
|
|
|
|
|
2018-12-18 16:10:04 +00:00
|
|
|
chainIDMul := new(big.Int).Mul(chainID, big.NewInt(2))
|
|
|
|
V := new(big.Int).Sub(msg.Data.V, chainIDMul)
|
|
|
|
V.Sub(V, big8)
|
2018-11-28 22:19:22 +00:00
|
|
|
|
2018-12-18 16:10:04 +00:00
|
|
|
sigHash := msg.RLPSignBytes(chainID)
|
|
|
|
sender, err := recoverEthSig(msg.Data.R, msg.Data.S, V, sigHash)
|
2018-11-28 22:19:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return ethcmn.Address{}, err
|
|
|
|
}
|
|
|
|
|
2018-12-18 16:10:04 +00:00
|
|
|
msg.from.Store(sigCache{signer: signer, from: sender})
|
|
|
|
return sender, nil
|
|
|
|
}
|
2018-11-28 22:19:22 +00:00
|
|
|
|
2018-12-18 16:10:04 +00:00
|
|
|
// Cost returns amount + gasprice * gaslimit.
|
2020-04-01 18:49:21 +00:00
|
|
|
func (msg MsgEthereumTx) Cost() *big.Int {
|
2018-12-18 16:10:04 +00:00
|
|
|
total := msg.Fee()
|
|
|
|
total.Add(total, msg.Data.Amount)
|
|
|
|
return total
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fee returns gasprice * gaslimit.
|
2020-04-01 18:49:21 +00:00
|
|
|
func (msg MsgEthereumTx) Fee() *big.Int {
|
2018-12-18 16:10:04 +00:00
|
|
|
return new(big.Int).Mul(msg.Data.Price, new(big.Int).SetUint64(msg.Data.GasLimit))
|
|
|
|
}
|
|
|
|
|
2019-09-24 18:39:17 +00:00
|
|
|
// ChainID returns which chain id this transaction was signed for (if at all)
|
2020-04-01 18:49:21 +00:00
|
|
|
func (msg *MsgEthereumTx) ChainID() *big.Int {
|
2019-09-24 18:39:17 +00:00
|
|
|
return deriveChainID(msg.Data.V)
|
|
|
|
}
|
|
|
|
|
|
|
|
// deriveChainID derives the chain id from the given v parameter
|
|
|
|
func deriveChainID(v *big.Int) *big.Int {
|
|
|
|
if v.BitLen() <= 64 {
|
|
|
|
v := v.Uint64()
|
|
|
|
if v == 27 || v == 28 {
|
|
|
|
return new(big.Int)
|
|
|
|
}
|
|
|
|
return new(big.Int).SetUint64((v - 35) / 2)
|
|
|
|
}
|
|
|
|
v = new(big.Int).Sub(v, big.NewInt(35))
|
|
|
|
return v.Div(v, big.NewInt(2))
|
|
|
|
}
|
|
|
|
|
2018-12-18 16:10:04 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Auxiliary
|
|
|
|
|
|
|
|
// TxDecoder returns an sdk.TxDecoder that can decode both auth.StdTx and
|
2020-04-01 18:49:21 +00:00
|
|
|
// MsgEthereumTx transactions.
|
2018-12-18 16:10:04 +00:00
|
|
|
func TxDecoder(cdc *codec.Codec) sdk.TxDecoder {
|
|
|
|
return func(txBytes []byte) (sdk.Tx, sdk.Error) {
|
|
|
|
var tx sdk.Tx
|
|
|
|
|
|
|
|
if len(txBytes) == 0 {
|
2020-04-16 15:47:39 +00:00
|
|
|
return nil, sdk.ConvertError(
|
|
|
|
sdkerrors.Wrap(sdkerrors.ErrTxDecode, "tx bytes are empty"),
|
|
|
|
)
|
2018-12-18 16:10:04 +00:00
|
|
|
}
|
|
|
|
|
2020-04-16 15:47:39 +00:00
|
|
|
// sdk.Tx is an interface. The concrete message types
|
|
|
|
// are registered by MakeTxCodec
|
2018-12-18 16:10:04 +00:00
|
|
|
err := cdc.UnmarshalBinaryLengthPrefixed(txBytes, &tx)
|
|
|
|
if err != nil {
|
2020-04-16 15:47:39 +00:00
|
|
|
return nil, sdk.ConvertError(
|
|
|
|
sdkerrors.Wrap(sdkerrors.ErrTxDecode, err.Error()),
|
|
|
|
)
|
2018-12-18 16:10:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return tx, nil
|
|
|
|
}
|
2018-11-28 22:19:22 +00:00
|
|
|
}
|
|
|
|
|
2018-12-18 16:10:04 +00:00
|
|
|
// recoverEthSig recovers a signature according to the Ethereum specification and
|
|
|
|
// returns the sender or an error.
|
|
|
|
//
|
|
|
|
// Ref: Ethereum Yellow Paper (BYZANTIUM VERSION 69351d5) Appendix F
|
2020-03-16 22:53:24 +00:00
|
|
|
// nolint: gocritic
|
2018-12-18 16:10:04 +00:00
|
|
|
func recoverEthSig(R, S, Vb *big.Int, sigHash ethcmn.Hash) (ethcmn.Address, error) {
|
|
|
|
if Vb.BitLen() > 8 {
|
|
|
|
return ethcmn.Address{}, errors.New("invalid signature")
|
|
|
|
}
|
2018-11-28 22:19:22 +00:00
|
|
|
|
2018-12-18 16:10:04 +00:00
|
|
|
V := byte(Vb.Uint64() - 27)
|
|
|
|
if !ethcrypto.ValidateSignatureValues(V, R, S, true) {
|
|
|
|
return ethcmn.Address{}, errors.New("invalid signature")
|
|
|
|
}
|
|
|
|
|
|
|
|
// encode the signature in uncompressed format
|
2018-11-28 22:19:22 +00:00
|
|
|
r, s := R.Bytes(), S.Bytes()
|
|
|
|
sig := make([]byte, 65)
|
|
|
|
|
|
|
|
copy(sig[32-len(r):32], r)
|
|
|
|
copy(sig[64-len(s):64], s)
|
2018-12-18 16:10:04 +00:00
|
|
|
sig[64] = V
|
2018-11-28 22:19:22 +00:00
|
|
|
|
2018-12-18 16:10:04 +00:00
|
|
|
// recover the public key from the signature
|
|
|
|
pub, err := ethcrypto.Ecrecover(sigHash[:], sig)
|
|
|
|
if err != nil {
|
|
|
|
return ethcmn.Address{}, err
|
|
|
|
}
|
2018-11-28 22:19:22 +00:00
|
|
|
|
2018-12-18 16:10:04 +00:00
|
|
|
if len(pub) == 0 || pub[0] != 4 {
|
|
|
|
return ethcmn.Address{}, errors.New("invalid public key")
|
2018-11-28 22:19:22 +00:00
|
|
|
}
|
|
|
|
|
2018-12-18 16:10:04 +00:00
|
|
|
var addr ethcmn.Address
|
|
|
|
copy(addr[:], ethcrypto.Keccak256(pub[1:])[12:])
|
|
|
|
|
|
|
|
return addr, nil
|
2019-11-01 15:26:53 +00:00
|
|
|
}
|