6

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.

1

1 Answer 1

4

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()
10
  • 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. Commented Aug 22, 2016 at 22:30
  • 2. This is the PHP script I was talking about. It takes way too long to process. Commented Aug 22, 2016 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. Commented Aug 23, 2016 at 5:40
  • added example code in python Commented Aug 23, 2016 at 11:50
  • 1
    The script does not work with segwit wallets (BIP49).
    – ccpizza
    Commented Dec 9, 2017 at 10:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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