Find all used addresses using xpub key

2

1

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.

Whyte the Weeabear

Posted 2016-08-22T09:39:31.290

Reputation: 530

There is an answer here: https://bitcoin.stackexchange.com/a/64685/38324

ccpizza 2017-12-09T10:28:49.973

Answers

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()

dark knight

Posted 2016-08-22T09:39:31.290

Reputation: 1 532

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 2016-08-22T22:30:58.803

    >

  • This is the PHP script I was talking about. It takes way too long to process.
  • < – Whyte the Weeabear 2016-08-22T22:31:39.490

    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 https://github.com/richardkiss/pycoin with PYCOIN_NATIVE=openssl, this really helps to speed up.

    dark knight 2016-08-23T05:40:11.357

    added example code in pythondark knight 2016-08-23T11:50:03.763

    I'm not sure how I would put these two together - one is PHP another is Python...Whyte the Weeabear 2016-08-23T11:58:33.247

    @WhytetheWeeabear I added the python code sample, you can directly use that, don't worry about phpdark knight 2016-08-23T12:14:44.373

    Tried your code, gave me "IndexError: list index out of range"Whyte the Weeabear 2016-08-23T12:50:08.847

    You should pass xpub as parameter to programdark knight 2016-08-23T12:53:57.160

    Let us continue this discussion in chat.

    dark knight 2016-08-23T12:57:00.613

    The script does not work with segwit wallets (BIP49).ccpizza 2017-12-09T10:37:52.530