2

How can I check whether a transaction hash is valid or not using Python [web3.py]?

The answers I found was in javascript, related:

alper
  • 8,395
  • 11
  • 63
  • 152

1 Answers1

3

With the help of regex at https://ethereum.stackexchange.com/a/34286/4575:

/^0x([A-Fa-f0-9]{64})$/


import re

def is_transaction_valid(tx_hash) -> bool: pattern = re.compile(r"^0x[a-fA-F0-9]{64}") return bool(re.fullmatch(pattern, tx_hash))

tx_hash = "0xd65dc6bf6dcc111237f9acfbfa6003ea4a4d88f2e071f4307d3af81ae877f7be" if is_transaction_valid(tx_hash): print(f"{tx_hash} is an valid transaction.") else: print(f"{tx_hash} is NOT a valid transaction.")

alper
  • 8,395
  • 11
  • 63
  • 152