Skip to content

python – Handle from xpub


I made a small webapplication to maintain monitor of my wallets. So I need to seize every thing i’ve, utilizing my xpub. Looping via each adress. On the Web I might discover diffrent scripts, however I might none of them attending to work.

I’m utilizing python, and tried pycoin.

A script that appears high-quality, however I could not get to work.

from pycoin.key import Key
import sys
import requests
import json
import datetime

def getbalance(handle):
  response = requests.get("https://bitaps.com/api/handle/%s" % handle)
  if response.status_code != 200:
    return None
  return response.json()
  
def listtransactions(handle):
  response = requests.get("https://bitaps.com/api/handle/transactions/%s" % handle)
  if response.status_code != 200:
    return None
  return response.json()

def probe_used_addresses(xpub, account_type):
  xpub_subkey = xpub.subkey(account_type)
  index = 0
  whereas True:
    addr = xpub_subkey.subkey(index).bitcoin_address()
    print("Kind %d (%d): %s" % (account_type, index, addr))
    
    # Report steadiness knowledge
    balancedata = getbalance(addr)
    steadiness = balancedata['confirmed_balance']
    balance_btc = steadiness # satoshis_to_btc(steadiness)
    print("  Stability: %s" % balance_btc)
    
    # Checklist transactions, if any...
    transactions = listtransactions(addr)
    if transactions != None:
      for tx in transactions:
        tx_time = datetime.datetime.fromtimestamp(float(tx[0]))
        tx_hash = tx[1]
        tx_data = tx[2]
        tx_type = tx[3]
        tx_status = tx[4]
        tx_confirmations = tx[5]
        tx_block = tx[6]
        tx_amount = tx[7]
        tx_timestr = tx_time.isoformat()
        print("    Transaction (%s): %s %s" % (tx_timestr, tx_type, tx_amount))

    print("")
    
    if transactions == None:
        break
    index += 1

def essential():

  xpub = Key.from_text('xpub')
  probe_used_addresses(xpub, 0)
  probe_used_addresses(xpub, 1)

essential()

This script refuses to learn my xpub, first I gonna use some block explorer and in a subsequent step I need to use the information from my node.

continue reading