0% found this document useful (0 votes)
14 views

Web3.py Patterns - Decoding Signed Transactions

Uploaded by

boonsom
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Web3.py Patterns - Decoding Signed Transactions

Uploaded by

boonsom
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

5/30/24, 8:03 PM Web3.

py Patterns: Decoding Signed Transactions

Web3.py

Web3.py Patterns: Decoding


Signed Transactions
Marc Garreau
Feb 14, 2022 • 2 min read

There's a small set of use cases where you might need to decode a signed
transaction which has not yet been included in a block. For example, MEV
protocols work with bundles of signed transactions separate from the main
transaction pool. If that series of words means nothing to you, there's a
reasonably good chance that you don't need the contents of this blog post and are
instead interested in fetching mined transaction data. So, let's start there.

Fetching Mined Transactions


If you're interested in fetching transaction data from the Ethereum blockchain, a
straightforward API exists for that. Note that these are transactions that have
been broadcast to the network and already successfully mined into a block.

from web3 import Web3, HTTPProvider

w3 = Web3(HTTPProvider('...'))

w3.eth.get_transaction('0x5c504ed432cb51138bcf09aa5e8a410dd4a1e204ef84bfed1be16d
# AttributeDict({
# 'hash': HexBytes('0x5c504...'),
# 'blockHash': HexBytes('0x4e3a3...'),
# 'blockNumber': 46147,
# 'from': '0xA1E43...',
# 'gas': 21000,
# 'gasPrice': 50000000000000,
# 'input': '0x',
# 'nonce': 0,
# 'r': HexBytes('0x88ff6...'),
# 's': HexBytes('0x45e0a...'),
# 'to': '0x5DF9B...',
# 'transactionIndex': 0,
# 'type': '0x0',
# 'v': 28,
# 'value': 31337
# })

The first-ever transaction on Ethereum Mainnet?

Decoding Signed Transactions


At the time of writing, a dedicated API does not exist for decoding unmined
signed transactions in Web3.py, but the functionality can be built from utilities
found in the py-evm and eth-utils libraries.

Under the hood, the logic for decoding transactions now needs to account for
"typed transactions," which were introduced to Ethereum in the Berlin network
upgrade. Legacy transactions continue to be supported after the upgrades, but
typed transactions have a dedicated range of values for the first byte of the
transaction hash. EIP-1559 transactions, for example, have transaction type of
0x02 , followed by the rlp-encoded transaction body: 0x02 || rlp([chain_id,
nonce, amount, data, ...]) .

By definition, each typed transaction has a unique data payload that must be
encoded and decoded. These mappings are collectively known as sedes, short for
serialization/deserialization. Luckily for us, py-evm hides these implementation
details within a TransactionBuilder class.

Putting it all together – the code below converts the transaction hash to bytes,
then decodes the payload with the latest TransactionBuilder from py-evm.

from eth.vm.forks.arrow_glacier.transactions import ArrowGlacierTransactionBuild


from eth_utils import (
encode_hex,
to_bytes,
)

https://snakecharmers.ethereum.org/web3-py-patterns-decoding-signed-transactions/ 1/2
5/30/24, 8:03 PM Web3.py Patterns: Decoding Signed Transactions

# 1) the signed transaction to decode:


original_hexstr = '0x02f86b010...'

# 2) convert the hex string to bytes:


signed_tx_as_bytes = to_bytes(hexstr=original_hexstr)

# 3) deserialize the transaction using the latest transaction builder:


decoded_tx = TransactionBuilder().decode(signed_tx_as_bytes)
print(decoded_tx.__dict__)
# {'type_id': 2, '_inner': DynamicFeeTransaction(chain_id=1, nonce=4, max_priori

# 4) the (human-readable) sender's address:


sender = encode_hex(decoded_tx.sender)
print(sender)
# 0xe9cb1f...

Want new posts in your inbox?


Enter your email Subscribe

web3.py Patterns: Bloom Filters


Have you ever queried an Ethereum block and wondered what that "logsBloom" was? Have you gone
looking for the most efficient way to find an event within a block? If you answered "yes" to either of
the above, then you're in for a good

Apr 24, 2024 4 min read

Snake Charmers © 2024

Powered by Ghost

https://snakecharmers.ethereum.org/web3-py-patterns-decoding-signed-transactions/ 2/2

You might also like