Skip to content

Instantly share code, notes, and snippets.

@masterdam79
Forked from rossigee/xpub_checker.py
Last active November 9, 2023 22:25
Show Gist options
  • Save masterdam79/c9cb58532929dd2266255edd8bb53dd6 to your computer and use it in GitHub Desktop.
Save masterdam79/c9cb58532929dd2266255edd8bb53dd6 to your computer and use it in GitHub Desktop.
Python to fetch Bitcoin addresses/transactions for given XPUB
#!/usr/bin/env python3
from pycoin.symbols.btc import network
import sys
import requests
import json
import datetime
# Function to fetch the balance information for a given address
def getbalance(address):
response = requests.get(f"https://bitaps.com/api/address/{address}")
if response.status_code != 200:
return None
return response.json()
# Function to fetch the list of transactions for a given address
def listtransactions(address):
response = requests.get(f"https://bitaps.com/api/address/transactions/{address}")
if response.status_code != 200:
return None
return response.json()
# Function to convert satoshis to BTC
def satoshis_to_btc(value):
return float(value) / 10**8
# Function to probe used addresses based on xpub and account type
def probe_used_addresses(xpub, account_type):
xpub_subkey = xpub.subkey(account_type)
index = 0
while True:
addr = xpub_subkey.subkey(index).address()
print(f"Type {account_type} ({index}): {addr}")
transactions = None # Initialize transactions outside the if block
# Report balance data
balancedata = getbalance(addr)
if balancedata is not None:
balance = balancedata.get('confirmed_balance', 0)
balance_btc = satoshis_to_btc(balance)
print(f" Balance: {balance_btc}")
# List transactions, if any...
transactions = listtransactions(addr)
if transactions is not None:
for tx in transactions:
tx_time = datetime.datetime.fromtimestamp(float(tx[0]))
tx_type = tx[3]
tx_amount = satoshis_to_btc(tx[7])
print(f" Transaction ({tx_time.isoformat()}): {tx_type} {tx_amount}")
print("")
if transactions is None:
break
index += 1
# Main function to execute the script
def main():
# Get xpub from the command line arguments
xpub_text = sys.argv[1]
xpub = network.parse.bip32(xpub_text)
# Probe used addresses for account type 0 and 1
probe_used_addresses(xpub, 0)
probe_used_addresses(xpub, 1)
# Execute the main function if this script is run
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment