2021-06-11 13:38:51 +00:00
|
|
|
package types
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/big"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
ethtypes "github.com/ethereum/go-ethereum/core/types"
|
|
|
|
)
|
|
|
|
|
2021-07-05 16:39:08 +00:00
|
|
|
var _ TxData = &AccessListTx{}
|
|
|
|
|
|
|
|
// TxData implements the Ethereum transaction tx structure. It is used
|
|
|
|
// solely as intended in Ethereum abiding by the protocol.
|
|
|
|
type TxData interface {
|
|
|
|
// TODO: embed ethtypes.TxData. See https://github.com/ethereum/go-ethereum/issues/23154
|
|
|
|
|
|
|
|
TxType() byte
|
|
|
|
Copy() TxData
|
|
|
|
GetChainID() *big.Int
|
|
|
|
GetAccessList() ethtypes.AccessList
|
|
|
|
GetData() []byte
|
|
|
|
GetNonce() uint64
|
|
|
|
GetGas() uint64
|
|
|
|
GetGasPrice() *big.Int
|
|
|
|
GetValue() *big.Int
|
|
|
|
GetTo() *common.Address
|
|
|
|
|
|
|
|
GetRawSignatureValues() (v, r, s *big.Int)
|
|
|
|
SetSignatureValues(chainID, v, r, s *big.Int)
|
|
|
|
|
|
|
|
AsEthereumData() ethtypes.TxData
|
|
|
|
Validate() error
|
|
|
|
Fee() *big.Int
|
|
|
|
Cost() *big.Int
|
2021-06-11 13:38:51 +00:00
|
|
|
}
|
|
|
|
|
2021-07-05 16:39:08 +00:00
|
|
|
func NewTxDataFromTx(tx *ethtypes.Transaction) TxData {
|
|
|
|
var txData TxData
|
|
|
|
switch tx.Type() {
|
|
|
|
case ethtypes.AccessListTxType:
|
|
|
|
txData = newAccessListTx(tx)
|
|
|
|
default:
|
|
|
|
txData = newLegacyTx(tx)
|
2021-06-11 13:38:51 +00:00
|
|
|
}
|
|
|
|
|
2021-07-05 16:39:08 +00:00
|
|
|
return txData
|
2021-06-11 13:38:51 +00:00
|
|
|
}
|
|
|
|
|
2021-07-05 16:39:08 +00:00
|
|
|
// DeriveChainID derives the chain id from the given v parameter
|
|
|
|
func DeriveChainID(v *big.Int) *big.Int {
|
|
|
|
if v == nil {
|
2021-06-11 13:38:51 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-07-05 16:39:08 +00:00
|
|
|
if v.BitLen() <= 64 {
|
|
|
|
v := v.Uint64()
|
|
|
|
if v == 27 || v == 28 {
|
|
|
|
return new(big.Int)
|
2021-06-11 13:38:51 +00:00
|
|
|
}
|
2021-07-05 16:39:08 +00:00
|
|
|
return new(big.Int).SetUint64((v - 35) / 2)
|
2021-06-11 13:38:51 +00:00
|
|
|
}
|
2021-07-05 16:39:08 +00:00
|
|
|
v = new(big.Int).Sub(v, big.NewInt(35))
|
|
|
|
return v.Div(v, big.NewInt(2))
|
2021-06-11 13:38:51 +00:00
|
|
|
}
|
|
|
|
|
2021-07-05 16:39:08 +00:00
|
|
|
func rawSignatureValues(vBz, rBz, sBz []byte) (v, r, s *big.Int) {
|
|
|
|
if len(vBz) > 0 {
|
|
|
|
v = new(big.Int).SetBytes(vBz)
|
2021-06-11 13:38:51 +00:00
|
|
|
}
|
2021-07-05 16:39:08 +00:00
|
|
|
if len(rBz) > 0 {
|
|
|
|
r = new(big.Int).SetBytes(rBz)
|
2021-06-11 13:38:51 +00:00
|
|
|
}
|
2021-07-05 16:39:08 +00:00
|
|
|
if len(sBz) > 0 {
|
|
|
|
s = new(big.Int).SetBytes(sBz)
|
2021-06-11 13:38:51 +00:00
|
|
|
}
|
|
|
|
return v, r, s
|
|
|
|
}
|
|
|
|
|
2021-07-05 16:39:08 +00:00
|
|
|
func fee(gasPrice *big.Int, gas uint64) *big.Int {
|
|
|
|
gasLimit := new(big.Int).SetUint64(gas)
|
|
|
|
return new(big.Int).Mul(gasPrice, gasLimit)
|
2021-06-11 13:38:51 +00:00
|
|
|
}
|
|
|
|
|
2021-07-05 16:39:08 +00:00
|
|
|
func cost(fee, value *big.Int) *big.Int {
|
|
|
|
if value != nil {
|
|
|
|
return new(big.Int).Add(fee, value)
|
2021-06-11 13:38:51 +00:00
|
|
|
}
|
2021-07-05 16:39:08 +00:00
|
|
|
return fee
|
2021-06-11 13:38:51 +00:00
|
|
|
}
|