2018-08-14 08:19:55 +02:00
|
|
|
#!/usr/bin/env python3
|
2021-10-12 21:23:02 +02:00
|
|
|
import argparse
|
2021-10-15 08:56:53 +02:00
|
|
|
import base58
|
2018-08-14 08:19:55 +02:00
|
|
|
import hashlib
|
|
|
|
import sys
|
|
|
|
|
|
|
|
from logbook import Logger, StreamHandler
|
|
|
|
|
|
|
|
import client
|
|
|
|
|
2023-03-12 21:44:00 +01:00
|
|
|
log = Logger("get_balance")
|
2018-08-14 08:19:55 +02:00
|
|
|
|
2021-10-12 21:23:02 +02:00
|
|
|
|
2021-10-15 08:56:53 +02:00
|
|
|
prefix_dict = {
|
|
|
|
'mainnet': {
|
|
|
|
'xpub': '0488b21e', # P2PKH or P2SH - m/44'/0'
|
|
|
|
'ypub': '049d7cb2', # P2WPKH in P2SH - m/49'/0'
|
|
|
|
'zpub': '04b24746', # P2WPKH - m/84'/0'
|
|
|
|
},
|
|
|
|
'testnet': {
|
|
|
|
'tpub': '043587cf', # P2PKH or P2SH - m/44'/1'
|
|
|
|
'upub': '044a5262', # P2WPKH in P2SH - m/49'/1'
|
|
|
|
'vpub': '045f1cf6', # P2WPKH - m/84'/1'
|
|
|
|
},
|
|
|
|
'regtest': {
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def convert_key(key, target_prefix, network_name):
|
|
|
|
decoded_key_bytes = base58.b58decode_check(key)
|
|
|
|
target_key_bytes = (
|
|
|
|
bytes.fromhex(prefix_dict[network_name][target_prefix]) +
|
|
|
|
decoded_key_bytes[4:])
|
|
|
|
return base58.b58encode_check(target_key_bytes).decode('ascii')
|
|
|
|
|
|
|
|
|
2023-03-12 21:44:00 +01:00
|
|
|
def compute_xpub_balance(xpub, conn, network, details):
|
2021-10-15 08:56:53 +02:00
|
|
|
total = 0
|
|
|
|
for change in (0, 1):
|
|
|
|
empty = 0
|
|
|
|
for n in range(1000):
|
|
|
|
address = xpub.subkey(change).subkey(n).address()
|
|
|
|
script = network.parse.address(address).script()
|
|
|
|
script_hash = hashlib.sha256(script).digest()[::-1].hex()
|
|
|
|
# conn.call([client.request('blockchain.scripthash.subscribe',
|
|
|
|
# script_hash)])
|
|
|
|
result, = conn.call(
|
|
|
|
[client.request('blockchain.scripthash.get_history',
|
|
|
|
script_hash)])
|
|
|
|
ntx = len(result)
|
|
|
|
if len(result):
|
|
|
|
log.debug(result)
|
|
|
|
result, = conn.call(
|
|
|
|
[client.request('blockchain.scripthash.get_balance',
|
|
|
|
script_hash)])
|
|
|
|
confirmed = result['confirmed'] / 1e8
|
|
|
|
total += confirmed
|
|
|
|
|
2023-03-12 21:44:00 +01:00
|
|
|
log.debug(
|
2021-10-15 08:56:53 +02:00
|
|
|
'{}/{}: {} -> {} BTC confirmed, {} BTC unconfirmed, '
|
|
|
|
'{} txs balance = {} BTC', change, n, address,
|
2023-03-12 21:44:00 +01:00
|
|
|
result["confirmed"] / 1e8, result["unconfirmed"] / 1e8, ntx,
|
|
|
|
total)
|
2021-10-15 08:56:53 +02:00
|
|
|
|
|
|
|
if confirmed or ntx:
|
|
|
|
empty = 0
|
2023-03-12 21:44:00 +01:00
|
|
|
if confirmed > 0:
|
|
|
|
details[address] = confirmed
|
2021-10-15 08:56:53 +02:00
|
|
|
else:
|
|
|
|
empty += 1
|
|
|
|
if empty >= 10:
|
|
|
|
break
|
|
|
|
return total
|
|
|
|
|
|
|
|
|
2023-03-12 21:44:00 +01:00
|
|
|
def compute_address_balance(address, conn, network):
|
|
|
|
script = network.parse.address(address).script()
|
|
|
|
script_hash = hashlib.sha256(script).digest()[::-1].hex()
|
|
|
|
result, = conn.call(
|
|
|
|
[client.request('blockchain.scripthash.get_balance',
|
|
|
|
script_hash)])
|
|
|
|
return result['confirmed'] / 1e8
|
|
|
|
|
|
|
|
|
2018-08-14 08:19:55 +02:00
|
|
|
def main():
|
2021-10-12 21:23:02 +02:00
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument('--host', default='localhost')
|
2021-10-15 08:56:53 +02:00
|
|
|
parser.add_argument('--network', default='mainnet',
|
|
|
|
choices=['mainnet', 'testnet', 'regtest'])
|
2023-03-12 21:44:00 +01:00
|
|
|
parser.add_argument('address')
|
2021-10-12 21:23:02 +02:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
2021-10-14 17:44:16 +02:00
|
|
|
if args.network == 'regtest':
|
|
|
|
port = 60401
|
|
|
|
from pycoin.symbols.xrt import network
|
|
|
|
elif args.network == 'testnet':
|
|
|
|
port = 60001
|
|
|
|
from pycoin.symbols.xtn import network
|
|
|
|
elif args.network == 'mainnet':
|
|
|
|
port = 50001
|
|
|
|
from pycoin.symbols.btc import network
|
|
|
|
else:
|
|
|
|
raise ValueError(f"unknown network: {args.network}")
|
|
|
|
|
|
|
|
conn = client.Client((args.host, port))
|
2018-08-14 08:19:55 +02:00
|
|
|
total = 0
|
2023-03-12 21:44:00 +01:00
|
|
|
xpub = (network.parse.bip32(args.address) or
|
|
|
|
network.parse.bip49(args.address) or
|
|
|
|
network.parse.bip84(args.address))
|
2021-10-12 21:23:02 +02:00
|
|
|
|
|
|
|
if xpub is None:
|
2023-03-12 21:44:00 +01:00
|
|
|
total = compute_address_balance(args.address, conn, network)
|
|
|
|
else:
|
|
|
|
details = {}
|
|
|
|
total = compute_xpub_balance(xpub, conn, network, details)
|
2020-04-11 17:28:45 +02:00
|
|
|
|
2023-03-12 21:44:00 +01:00
|
|
|
for prefix in prefix_dict[args.network]:
|
|
|
|
if args.address[:4] != prefix:
|
|
|
|
key = convert_key(args.address, prefix, args.network)
|
|
|
|
log.debug('Trying with {}', key)
|
|
|
|
xpub = (network.parse.bip32(key) or network.parse.bip49(key)
|
|
|
|
or network.parse.bip84(key))
|
|
|
|
total += compute_xpub_balance(xpub, conn, network, details)
|
2021-10-15 08:56:53 +02:00
|
|
|
|
2023-03-12 21:44:00 +01:00
|
|
|
for addr in details:
|
|
|
|
log.info('{} balance: {} BTC', addr, details[addr])
|
2021-10-14 07:56:40 +02:00
|
|
|
|
2018-08-14 08:19:55 +02:00
|
|
|
log.info('total balance: {} BTC', total)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2023-03-12 21:44:00 +01:00
|
|
|
with StreamHandler(sys.stderr, level='DEBUG').applicationbound():
|
2018-08-14 08:19:55 +02:00
|
|
|
main()
|