Skip to content

Instantly share code, notes, and snippets.

@CrackerHax
Created September 14, 2019 11:39
Show Gist options
  • Save CrackerHax/ec6964ea030d4b31d47b7d412036c623 to your computer and use it in GitHub Desktop.
Save CrackerHax/ec6964ea030d4b31d47b7d412036c623 to your computer and use it in GitHub Desktop.
Python script for getting ethereum public key from transaction id
import web3
w3 = web3.Web3(web3.HTTPProvider('https://geth.golem.network:55555'))
tx = w3.eth.getTransaction('0x00ee93fd1e6fdbdf841b4458a078210449dce89525502c965fd3534ba397e2e3')
tx.hash
from eth_account._utils.signing import extract_chain_id, to_standard_v
s = w3.eth.account._keys.Signature(vrs=(
to_standard_v(extract_chain_id(tx.v)[1]),
w3.toInt(tx.r),
w3.toInt(tx.s)
))
from eth_account._utils.transactions import ALLOWED_TRANSACTION_KEYS
tt = {k:tx[k] for k in ALLOWED_TRANSACTION_KEYS - {'chainId', 'data'}}
tt['data']=tx.input
tt['chainId']=extract_chain_id(tx.v)[0]
from eth_account._utils.transactions import serializable_unsigned_transaction_from_dict
ut = serializable_unsigned_transaction_from_dict(tt)
s.recover_public_key_from_msg_hash(ut.hash())
@kernoelpanic
Copy link

Thx for the gist and the improvements. I was able to dig into some edge cases:

  • Type 0x03 transactions are not supported
  • Type 0x00 legacy transactions are not handled correctly according to EIP 155 (for values where v=27 or v=28) and there is not chainId
  • Transactions with a non-empty accessList are not handled correctly.

I tried to fix all of those and posted a gist for the slightly longer version here: https://gist.github.com/kernoelpanic/423c61f90e81e4c9d473ff6fda783559

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