I'm trying to retrieve the public key of an address from a transaction. I'm getting the transaction via tx = web3.eth.getTransaction('xxx')

According to the docs the public key should be retrievable by pub_key = tx['publicKey']. But when I print the tx dictionary there is no key-field for publicKey. What's

I am using the latest web3.py version.

So what's the easiest way to get the public key?

I thought about using eth_keys in the following way:

tx = web3.eth.getTransaction('xx')
r = tx['r']
s = tx['s']
v = tx['v'] - 27 # why?
r_hex = r.hex()[2:]
r_int = int(r_hex, 16)
s_hex = s.hex()[2:]
s_int = int(s_hex, 16)
sig = (v, r_int, s_int)
sig_obj = eth_keys.keys.Signature(None, sig)
mgs = ??? # what is the actual message? 
pub_key = eth_keys.keys.ecdsa_recover(msg, sig_obj)

But I am stuck at msg. Any help is appreciated.

These are the keys in the dictionary:

>>> list(tx.keys())
['blockHash', 'blockNumber', 'from', 'gas', 'gasPrice', 'hash', 'input', 'nonce', 'r', 's', 'to', 'transactionIndex', 'v', 'value']

This is the output for print(tx)

AttributeDict({'blockHash': HexBytes('0x9cb5c78955ad7836ec07e0460d19a457be74cfe6ecec2ba8a0c29c5057648021'), 'blockNumber': 6539317, 'from': '0x99fe5D6383289CDD56e54Fc0bAF7F67c957A8888', 'gas': 66666, 'gasPrice': 66000000000, 'hash': HexBytes('0x863bb4888489d081e63108fe0af03b366637963599d91692f46abee6900e9c58'), 'input': '0xa9059cbb000000000000000000000000a83cdb20fe49ff56635e2c8712238b4e96c9c5460000000000000000000000000000000000000000000029c07070040f0c070000', 'nonce': 37433, 'r': HexBytes('0xfc865f013dad1aac4ed4b0ff05d084bbc4b0d75c93e35638e9a045d07efdfc93'), 's': HexBytes('0x080d7cdb3930be83e0dcf8473653f63eb583c2d0473fa2054e63a825539abc64'), 'to': '0x9064c91e51d7021A85AD96817e1432aBf6624470', 'transactionIndex': 0, 'v': 37, 'value': 0})
  • What is your output when you run pub_key = tx['publicKey']? Oct 18, 2018 at 13:21
  • 1
    I just get KeyError: 'publicKey'.
    – Chris
    Oct 18, 2018 at 17:26
  • What happens if you print(tx)? Oct 18, 2018 at 17:28
  • 1
    I edited the question and added the dictionary keys. The output doesn't fit in here.
    – Chris
    Oct 18, 2018 at 17:31
  • What kind of node are you connecting to?
    – carver
    Oct 18, 2018 at 18:01

1 Answer 1


Looks like the docs should be updated. publicKey is a non-standard key returned by parity only, I believe.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.