2019-08-05 20:03:41 +00:00
|
|
|
import
|
2023-01-20 04:34:31 +00:00
|
|
|
./eth_types_rlp
|
2022-09-02 14:57:52 +00:00
|
|
|
|
|
|
|
export eth_types_rlp
|
2019-08-05 20:03:41 +00:00
|
|
|
|
|
|
|
const
|
2024-06-11 17:13:11 +00:00
|
|
|
EIP155_CHAIN_ID_OFFSET* = 35'u64
|
2023-01-10 16:56:27 +00:00
|
|
|
|
2021-06-26 08:18:42 +00:00
|
|
|
func rlpEncodeLegacy(tx: Transaction): auto =
|
|
|
|
var w = initRlpWriter()
|
|
|
|
w.startList(6)
|
|
|
|
w.append(tx.nonce)
|
|
|
|
w.append(tx.gasPrice)
|
|
|
|
w.append(tx.gasLimit)
|
|
|
|
w.append(tx.to)
|
|
|
|
w.append(tx.value)
|
|
|
|
w.append(tx.payload)
|
|
|
|
w.finish()
|
2019-08-05 20:03:41 +00:00
|
|
|
|
2021-06-26 08:18:42 +00:00
|
|
|
func rlpEncodeEip155(tx: Transaction): auto =
|
|
|
|
let chainId = (tx.V - EIP155_CHAIN_ID_OFFSET) div 2
|
|
|
|
var w = initRlpWriter()
|
|
|
|
w.startList(9)
|
|
|
|
w.append(tx.nonce)
|
|
|
|
w.append(tx.gasPrice)
|
|
|
|
w.append(tx.gasLimit)
|
|
|
|
w.append(tx.to)
|
|
|
|
w.append(tx.value)
|
|
|
|
w.append(tx.payload)
|
|
|
|
w.append(chainId)
|
2024-06-11 17:13:11 +00:00
|
|
|
w.append(0'u8)
|
|
|
|
w.append(0'u8)
|
2021-06-26 08:18:42 +00:00
|
|
|
w.finish()
|
2019-08-05 20:03:41 +00:00
|
|
|
|
2021-06-26 08:18:42 +00:00
|
|
|
func rlpEncodeEip2930(tx: Transaction): auto =
|
|
|
|
var w = initRlpWriter()
|
2023-01-20 04:34:31 +00:00
|
|
|
w.append(TxEip2930)
|
2021-06-26 08:18:42 +00:00
|
|
|
w.startList(8)
|
|
|
|
w.append(tx.chainId.uint64)
|
|
|
|
w.append(tx.nonce)
|
|
|
|
w.append(tx.gasPrice)
|
|
|
|
w.append(tx.gasLimit)
|
|
|
|
w.append(tx.to)
|
|
|
|
w.append(tx.value)
|
|
|
|
w.append(tx.payload)
|
|
|
|
w.append(tx.accessList)
|
|
|
|
w.finish()
|
2021-05-15 07:26:51 +00:00
|
|
|
|
2021-06-26 08:18:42 +00:00
|
|
|
func rlpEncodeEip1559(tx: Transaction): auto =
|
|
|
|
var w = initRlpWriter()
|
2023-01-20 04:34:31 +00:00
|
|
|
w.append(TxEip1559)
|
2021-06-26 08:18:42 +00:00
|
|
|
w.startList(9)
|
|
|
|
w.append(tx.chainId.uint64)
|
|
|
|
w.append(tx.nonce)
|
2024-06-11 17:13:11 +00:00
|
|
|
w.append(tx.maxPriorityFeePerGas)
|
|
|
|
w.append(tx.maxFeePerGas)
|
2021-06-26 08:18:42 +00:00
|
|
|
w.append(tx.gasLimit)
|
|
|
|
w.append(tx.to)
|
|
|
|
w.append(tx.value)
|
|
|
|
w.append(tx.payload)
|
|
|
|
w.append(tx.accessList)
|
|
|
|
w.finish()
|
2021-05-15 07:26:51 +00:00
|
|
|
|
2023-01-20 04:34:31 +00:00
|
|
|
func rlpEncodeEip4844(tx: Transaction): auto =
|
|
|
|
var w = initRlpWriter()
|
|
|
|
w.append(TxEip4844)
|
|
|
|
w.startList(11)
|
|
|
|
w.append(tx.chainId.uint64)
|
|
|
|
w.append(tx.nonce)
|
2024-06-11 17:13:11 +00:00
|
|
|
w.append(tx.maxPriorityFeePerGas)
|
|
|
|
w.append(tx.maxFeePerGas)
|
2023-01-20 04:34:31 +00:00
|
|
|
w.append(tx.gasLimit)
|
|
|
|
w.append(tx.to)
|
|
|
|
w.append(tx.value)
|
|
|
|
w.append(tx.payload)
|
|
|
|
w.append(tx.accessList)
|
2023-07-28 03:19:00 +00:00
|
|
|
w.append(tx.maxFeePerBlobGas)
|
2023-01-20 04:34:31 +00:00
|
|
|
w.append(tx.versionedHashes)
|
|
|
|
w.finish()
|
|
|
|
|
2021-06-26 08:18:42 +00:00
|
|
|
func rlpEncode*(tx: Transaction): auto =
|
|
|
|
case tx.txType
|
|
|
|
of TxLegacy:
|
|
|
|
if tx.V >= EIP155_CHAIN_ID_OFFSET:
|
2022-07-21 08:58:17 +00:00
|
|
|
tx.rlpEncodeEip155
|
2021-06-26 08:18:42 +00:00
|
|
|
else:
|
|
|
|
tx.rlpEncodeLegacy
|
|
|
|
of TxEip2930:
|
|
|
|
tx.rlpEncodeEip2930
|
|
|
|
of TxEip1559:
|
|
|
|
tx.rlpEncodeEip1559
|
2023-01-20 04:34:31 +00:00
|
|
|
of TxEip4844:
|
|
|
|
tx.rlpEncodeEip4844
|
2021-05-14 15:34:32 +00:00
|
|
|
|
|
|
|
func txHashNoSignature*(tx: Transaction): Hash256 =
|
2021-06-26 08:18:42 +00:00
|
|
|
# Hash transaction without signature
|
2022-09-02 14:57:52 +00:00
|
|
|
keccakHash(rlpEncode(tx))
|