import requests
def get_publickey(address):
txs = get_txs(address)
publickey = None
found = False
for tx in txs:
for vin in tx["vin"]:
if vin["prevout"]["scriptpubkey_address"] == address:
scriptsig_asm = vin["scriptsig_asm"]
if "OP_PUSHBYTES_33" in scriptsig_asm or "OP_PUSHBYTES_65" in scriptsig_asm:
tokens = scriptsig_asm.split()
OP_token = tokens[-2]
last_token = tokens[-1]
if OP_token == "OP_PUSHBYTES_33" or OP_token == "OP_PUSHBYTES_65" :
publickey = last_token
found = True
if found:
break
if found:
break
return publickey
def get_txs(address):
try:
obj = None
url = ""
if networkname=="bitcoin":
url = "https://mempool.space/api/address/" + address +"/txs"
elif networkname=="testnet":
url = "https://mempool.space/testnet/api/address/" + address +"/txs"
else:
print("Unknow network")
exit()
#print("Request url: {}".format(url))
response = requests.get(url, timeout=10) # Sending an HTTP GET request
if response.status_code == 200:
#print("Response:\n{}".format(response.text))
obj = response.json()
elif response.status_code == 429:
print(f"HTTP: {response.status_code}\nExit...")
obj = []
else:
print(f"HTTP: {response.status_code}\n{response.text}\nExit...")
obj = []
return obj
except Exception as e:
print(f"An error occurred: {e}")
return []
networkname="bitcoin"
address = "1Fo65aKq8s8iquMt6weF1rku1moWVEd5Ua"
publickey = get_publickey(address)
if(publickey is not None):
print("Public key found {}".format(publickey))
else:
print("Public key NOT found")
This code only work for TX already confirmed, Legacy address compressed and uncompressed
I hope you like it.
BTW my bot is already running. I am not going to be in front of my computer this time, last time i was fixing some code bugs, but right now I already tested the code with some other address.