Skip to content

Instantly share code, notes, and snippets.

@pipermerriam
Created September 25, 2017 18:38
Show Gist options
  • Save pipermerriam/38602f2ea4b2ebd762410f0afaa599d1 to your computer and use it in GitHub Desktop.
Save pipermerriam/38602f2ea4b2ebd762410f0afaa599d1 to your computer and use it in GitHub Desktop.
import rlp
from eth_keys import keys
from eth_utils import (
decode_hex,
is_same_address,
big_endian_to_int,
int_to_big_endian,
)
txn_hex = '0xf8640d843b9aca00830e57e0945b2063246f2191f18f2675cedb8b28102e957458018025a00c753084e5a8290219324c1a3a86d4064ded2d15979b1ea790734aaa2ceaafc1a0229ca4538106819fd3a5509dd383e8fe4b731c6870339556a5c06feb9cf330bb'
expected_address = '0xFeC2079e80465cc8C687fFF9EE6386ca447aFec4'
txn_parts = rlp.decode(decode_hex(txn_hex))
raw_v, r, s = tuple(map(big_endian_to_int, txn_parts[-3:]))
if raw_v <= 28:
v = raw_v
message = rlp.encode(txn_parts[:-3])
else:
if raw_v % 2: # is odd
chain_id = (raw_v - 35) // 2
v = 27
else:
chain_id = (raw_v - 36) // 2
v = 28
message = rlp.encode(txn_parts[:-3] + [int_to_big_endian(chain_id), b'', b''])
signature = keys.Signature(vrs=(v, r, s))
public_key = signature.recover_public_key_from_msg(message)
signed_by_address = public_key.to_address()
assert is_same_address(signed_by_address, expected_address)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment