2

I currently use a Ledger wallet and find it a hassle to plug it in every time I want to check my balance.

I have access to my master xpub key, how would I find out all the addresses corresponding to it? Is there any site that hosts this service and has an API?

I found a script online that does this, but it's written in PHP and is quite slow. I'm looking for something compatible with python.

3

You can use blockonomics, its pretty fast. It also has API to give balance of given xpub.

If you don't want to submit your xpub to server you can use https://github.com/dan-da/hd-wallet-addrs. This will select random balance API service for each address and do HD walkthrough.

Added example code using blockr api, fetches balances for 20 addreses at once, should be pretty fast

import pycoin.key
import sys
import os
os.environ["PYCOIN_NATIVE"]="openssl"
import requests
BATCH_SIZE = 20
BLOCKR_URL= "http://btc.blockr.io/api/v1/address/info"
def get_used_addresses(xpub, account_type):
  xpub_subkey = xpub.subkey(account_type)
  index = 0
  addr_batch = []
  output = []
  while True:
    addr = xpub_subkey.subkey(index).bitcoin_address()
    addr_batch.append(addr)
    if (index+1)%BATCH_SIZE==0:
      results = requests.get("{}/{}".format(BLOCKR_URL, ",".join(addr_batch))).json()
      addr_batch = []
      used_addrs = [x["address"] for x in results['data'] if not x["is_unknown"]]   
      if (used_addrs):                       
        output.extend(used_addrs)
      else:  
        break                     
    index += 1
  return output

def main():
  xpub = pycoin.key.Key.from_text(sys.argv[1])
  result = []
  result.extend(get_used_addresses(xpub, 0))
  result.extend(get_used_addresses(xpub, 1))
  print result

if __name__ == "__main__":
  main()
  • 1
    1. Blockonomics has a 50 address limit. If I wanted to send my pubkey to a third party server, then I would use blockchain.ifo/xpub. It's faster. – Whyte the Weeabear Aug 22 '16 at 22:30
  • 2. This is the PHP script I was talking about. It takes way too long to process. – Whyte the Weeabear Aug 22 '16 at 22:31
  • Not sure if you looked at --batch-size parameter in the PHP script. That way it will lookup multiple addresses at once and should be faster. Another bottleneck is the generation of subkey, with python u can use github.com/richardkiss/pycoin with PYCOIN_NATIVE=openssl, this really helps to speed up. – dark knight Aug 23 '16 at 5:40
  • added example code in python – dark knight Aug 23 '16 at 11:50
  • I'm not sure how I would put these two together - one is PHP another is Python... – Whyte the Weeabear Aug 23 '16 at 11:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.