Skip to content

Instantly share code, notes, and snippets.

@cleanunicorn
Created February 3, 2022 14:10
Show Gist options
  • Save cleanunicorn/9f8686747e5b838425082696a24a586b to your computer and use it in GitHub Desktop.
Save cleanunicorn/9f8686747e5b838425082696a24a586b to your computer and use it in GitHub Desktop.
Raw transaction hex of an already signed transaction structure
import json
from hexbytes import HexBytes
from eth_account._utils.legacy_transactions import Transaction, encode_transaction
tx = json.loads(
'''
{
"hash": "0x51a2441bc876735a7d066017ac13b285c7ef348eaa92203c391621bb3c030207",
"accessList": [],
"blockHash": "0x7a285ac892bb9cdaabc2fb5529974c55b75ec5a9b1e6fe968d8ffea80898f76d",
"blockNumber": "0x604b69",
"chainId": "0x5",
"from": "0x093e15f712e2daeed8b45d3d93e59e81d4ad99f4",
"gas": "0x1e1da",
"gasPrice": "0x3b9aca07",
"input": "0x4e71d92d",
"maxFeePerGas": "0x3b9aca0e",
"maxPriorityFeePerGas": "0x3b9aca00",
"nonce": "0x0",
"r": "0x34dbcc48f1c2e1f45b224e3c6ede07d72c044cd3acd69c9f576fa89ccd88be9f",
"s": "0x537ef44506f8123e7f1609e6175d1f0fc5b9ce24463918a27bcedf143e075ca3",
"to": "0x08034634bbd210485c9c8f798afdc5432782fd18",
"transactionIndex": "0x3e",
"type": "0x2",
"v": "0x0",
"value": "0x0"
}
''')
# print(tx)
raw = encode_transaction(
Transaction(
v=tx['v'],
r=tx['r'],
s=tx['s'],
data=HexBytes(tx["input"]),
gas=int(tx["gas"], 16),
gasPrice=int(tx["gasPrice"], 16),
nonce=int(tx["nonce"], 16),
to=HexBytes(tx["to"]) if "to" in tx else None,
value=int(tx["value"], 16),
),
(
int(tx['v'], base=16),
int(tx['r'], base=16),
int(tx['s'], base=16)
)
)
print(raw.hex())
@cleanunicorn
Copy link
Author

Prints

f86880843b9aca078301e1da9408034634bbd210485c9c8f798afdc5432782fd1880844e71d92d80a034dbcc48f1c2e1f45b224e3c6ede07d72c044cd3acd69c9f576fa89ccd88be9fa0537ef44506f8123e7f1609e6175d1f0fc5b9ce24463918a27bcedf143e075ca3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment