TY!, here is an improved script that extracts the different addresses where Pubkey is exposed:
import requests
import json
import logging
logging.basicConfig(level=logging.INFO)
def latestblk_h():
latestblk_url = "https://blockchain.info/latestblock"
response = requests.get(latestblk_url)
response.raise_for_status()
return response.json()['height']
def blk_h(block_h):
blk_url = f"https://blockchain.info/block-height/{block_h}?format=json"
response = requests.get(blk_url)
response.raise_for_status()
return response.json()['blocks'][0]
def pubkey_script(script):
pubkey = None
if len(script) >= 66 and script[-66:][:2] in ["02", "03", "04"]:
pubkey = script[-66:]
elif len(script) >= 130 and script[-130:][:2] == "04":
pubkey = script[-130:]
return pubkey
def pubkey_witness(witness):
pubkey = None
if witness:
items = witness.split()
for item in items:
if len(item) == 66 and item[:2] in ["02", "03"]:
pubkey = item
elif len(item) == 130 and item[:2] == "04":
pubkey = item
return pubkey
def extract_witness(witness):
pubkey = None
if len(witness) >= 66 and witness[-66:][:2] in ["02", "03"]:
pubkey = witness[-66:]
return pubkey
def pubkey_txs(tx):
pubaddr = set()
for input in tx['inputs']:
address = input.get('prev_out', {}).get('addr', None)
pubkey = None
if 'witness' in input and input['witness']:
pubkey = extract_witness(input['witness'])
if not pubkey and 'script' in input and input['script']:
pubkey = pubkey_script(input['script'])
if pubkey and address:
pubaddr.add((pubkey, address))
return pubaddr
def pubkey_fblk(block):
pubaddr = set()
for tx in block['tx']:
pubaddr.update(pubkey_txs(tx))
return pubaddr
def save_pubs(pubaddr, filename):
with open(filename, "a") as f:
for key, address in pubaddr:
f.write(f"{key} {address}\n")
def process_blks(start_block, end_block):
for block_h in range(start_block, end_block + 1):
logging.info(f"Processing block: {block_h}")
try:
block = blk_h(block_h)
if block is None:
logging.error(f"Block {block_h} is None")
continue
pubaddr = pubkey_fblk(block)
if not pubaddr:
logging.warning(f"No public keys found in block {block_h}")
save_pubs(pubaddr, "pubkeys.txt")
logging.info(f"Saved {len(pubaddr)} unique public keys and addresses from block {block_h} to pubkeys.txt")
except requests.RequestException as e:
logging.error(f"Error fetching block {block_h}: {e}")
except Exception as e:
logging.error(f"An unexpected error occurred while processing block {block_h}: {e}")
if __name__ == "__main__":
start_block = int(input("Start block height: "))
end_block = int(input("End block height: "))
if end_block < start_block:
end_block = latestblk_h()
process_blks(start_block, end_block)
>>Start block height: 874101
End block height: 874102
INFO:root:Processing block: 874101
INFO:root:Saved 3814 unique public keys and addresses from block 874101 to pubkeys.txt
INFO:root:Processing block: 874102
INFO:root:Saved 2918 unique public keys and addresses from block 874102 to pubkeys.txt
0273cc2577d91dcc32689ab06bdba77a96f26bc9b71c2f0016b04caeb7c967576c bc1qljrae59r436qle6dm99e45t8fl5kyxtxee586d
0214b3586527dd4d24fea97d6f324481d8392b28a06a46cb3045b90bb50dff2bbb 1D1hGwbvWNQRwaPr3YVyZCvusccuXyHVZR
03055999feb78831388be1478f4b2cc3a0ad2ef20f5ccbca8ab6150dd06dad26c7 bc1qq08wt6eg2xnsqcat435alqwxvna6gmmp7unuwk
03bfb4136c822a139353e191207f2fe2ae5d7c78e116c8db44ae61cf67f1736766 bc1q93qx52m95sh7nhrf6t62aqsqx3m056u8yzkwwd
0356cf529f22fdfed7a5f3e7ac284734f906371c48df6ec30ce37710d11c5e1bad bc1qdr8vu90j999n02auhx8uk9j6w2r3gdz9eysdf2
029b34e77806b6966742f50b40062bf4764908103724155e9bc06922c0fb3c6e66 bc1qfvsv9wpfkp2fvxnda33hpn0cdh8hek8nnzjwak
025ef0295f7e7c48bd0a4ccd05b210ee0a9df4bce89d28bf029473cf88a1d7f6b0 bc1qdzcukgzyajm6gjwvat8cnd080ayw3gqzd8fv8p
03772afd39628c07b19ade508a6f8a17d0f104a2742a807fb3807a622d2c564202 bc1q4kdm0xmr0yx5gsqd4jag26rz5z7mj0qcwytyy8
0213490d33bcd2bda6a7dda4d323ed49e7135294f8eee3a7750f0f7e84356deee7 1kV5Umn4fSmyzb79jgbct7KNbybAZ1hhP
020b82c7807ee12b323d1795bfa55dd06c76161e86fda5e80fe84d7f3fc7b0d1f2 1Kv6h2SEAomf8ZCsZf7EoX9A6wzhRszpED
02cbb45df5700e595e4c6e01118048829860fb2d072d1fe687ba24d8a9d584127d bc1qr90yhemusr4ygaxnd2g9lazdhvle36usjmu47k
03cf674b3eb3e164ef54043b8f2b3675342a71cf8538deac4bb9878c8faec7e2e7 3QpP1ka9ssPm3Dr6A7Y3eb4oVJ2bc554fC
02bfc7f2d70072e5b4fb0613a747d114cb71bef175c56956ca361696ba02ff104b 34Ax3BzSMUmRXo5uCBzcDqe2f23J5Hji7r
03e8e05f199c4b287dba39727969c56ea4942a629efeb816c322eb54cbdfc142a5 3QBvvsgag4RJa3sykGWbJYe2j4WhcqSBVR
02c4ac15a3e30bd0c21b6e4d6d9787236d3e89affcb84afc9274b7cec55d50bd26 bc1q6mrj0ue8nc9sctqx769lgydhfd83yv3wf9vqsz
and for a unique TX:
import requests
import json
import logging
logging.basicConfig(level=logging.INFO)
def pubkey_script(script):
pubkey = None
if len(script) >= 66 and script[-66:][:2] in ["02", "03", "04"]:
pubkey = script[-66:]
elif len(script) >= 130 and script[-130:][:2] == "04":
pubkey = script[-130:]
return pubkey
def extract_witness(witness):
pubkey = None
if len(witness) >= 66 and witness[-66:][:2] in ["02", "03"]:
pubkey = witness[-66:]
return pubkey
def pubkey_txs(tx):
pubaddr = set()
for input in tx['inputs']:
address = input.get('prev_out', {}).get('addr', None)
pubkey = None
if 'witness' in input and input['witness']:
pubkey = extract_witness(input['witness'])
if not pubkey and 'script' in input and input['script']:
pubkey = pubkey_script(input['script'])
if pubkey and address:
pubaddr.add((pubkey, address))
return pubaddr
def fetch_tx(tx_hash):
tx_url = f"https://blockchain.info/rawtx/{tx_hash}"
response = requests.get(tx_url)
response.raise_for_status()
return response.json()
def process_tx(tx_hash, filename):
logging.info(f"Processing tx: {tx_hash}")
try:
tx = fetch_tx(tx_hash)
pubaddr = pubkey_txs(tx)
if not pubaddr:
logging.warning(f"No public keys found in transaction {tx_hash}")
save_pubs(pubaddr, filename)
logging.info(f"Saved {len(pubaddr)} unique public keys and addresses from transaction {tx_hash} to {filename}")
except requests.RequestException as e:
logging.error(f"Error fetching transaction {tx_hash}: {e}")
except Exception as e:
logging.error(f"An unexpected error occurred while processing transaction {tx_hash}: {e}")
def save_pubs(pubaddr, filename):
with open(filename, "a") as f:
for key, address in pubaddr:
f.write(f"{key} {address}\n")
if __name__ == "__main__":
tx_hash = input("Tx hash: ")
filename = "pubs_tx.txt"
process_tx(tx_hash, filename)
>>Tx hash: 4c51abc2d2972e9ad4044ad55d1d26c5c6a41c77ab3ac68fc91249d1920837e6
INFO:root:Processing tx: 4c51abc2d2972e9ad4044ad55d1d26c5c6a41c77ab3ac68fc91249d1920837e6
INFO:root:Saved 4 unique public keys and addresses from transaction 4c51abc2d2972e9ad4044ad55d1d26c5c6a41c77ab3ac68fc91249d1920837e6 to pubs_tx.txt
0391430e83a8837203ce028000788b31eb441c3c1624d0329b3539458d71aa8e81 bc1q7c88lltmwl8gc0w5525hyts5wgfmhe5xkztnxw
038fe55aa311a2181055f539c1b0fc21a196c99dfae188a1077dd99fb229f9f52f bc1qc2hx9c36kfdytutx52yls2ppq34ynz2auy0v3h
022ec6776423d030e3494a478abf0f712720c55a41eb9d2a1ccceaadd401b85ea2 bc1qr9t70zqwdguhrdfj4de6pp530vymu35tpfdk0n
03df04464926b15e0c648985571f6f5d24709f41b908efd832e4668cd39ccdcfd0 bc1qf9assp75qqu3jvwmuy47scsfmxrjpke2vgje04