Python Electrum-如何检查XPUB余额?

时间:2018-12-22 15:09:15

标签: python bitcoin

我如何使用python和lectrum检查比特币xpub余额? 我有很多xpubs,我想检查一下。 (或通过其他任何方式无限制地检查xpubs?)

1 个答案:

答案 0 :(得分:0)

您可以使用blockchain.com: https://www.blockchain.com/btc/xpub/xpub6EBaMYeHPFoEHSpKrvtTyy94Ysg3CK6HLqrXUDzDZk759rHx4k6XZykS3B9Fujibgg32niNddV9J1pCK4qYiQF5kpD41B8UpQulc4Fk6U8A

或使用以下python代码从xpub获取地址

但是我想您无法从xpub获得平衡

#!/usr/bin/python

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


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


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


def satoshis_to_btc(value):
    return float(float(value) / 10 ** 8);


def probe_used_addresses(xpub, account_type):
    xpub_subkey = xpub.subkey(account_type)
    index = 0
    while True:
        addr = xpub_subkey.subkey(index).bitcoin_address()
        print("Type %d (%d): %s" % (account_type, index, addr))

        # Report balance data
        balancedata = getbalance(addr)
        balance = balancedata['confirmed_balance']
        balance_btc = satoshis_to_btc(balance)
        print("  Balance: %s" % balance_btc)

        # List 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 = satoshis_to_btc(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 main():
    xpub = pycoin.key.Key.from_text(sys.argv[1])
    probe_used_addresses(xpub, 0)
    probe_used_addresses(xpub, 1)


main()