Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added get_xpub_addrs #136

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions bitcoin/bci.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,3 +526,48 @@ def get_tx_composite(inputs, outputs, output_value, change_address=None, network
return txh.encode("utf-8")

blockcypher_mktx = get_tx_composite


def get_xpub_unspent_addrs(*args):
"""Takes bip32 xpub (or xprv) and returns addresses with balance"""
from bitcoin.main import multiaccess, pubtoaddr
from bitcoin.deterministic import bip32_descend, bip32_ckd, bip32_privtopub
xpubs = [bip32_privtopub(x) if x.startswith("xprv") else x for x in args]
data = {"addr": " ".join(xpubs)}
jdata = json.loads(make_request("https://www.blockonomics.co/api/balance", json.dumps(data)))
jdata = jdata.get("response")
addrs, values = multiaccess(jdata,"addr"), multiaccess(jdata,"confirmed")
d = dict.fromkeys(xpubs, {})
for xpub in xpubs:
c, i = 0, 0
while c <= 1:
addr = pubtoaddr(bip32_descend(bip32_ckd(xpub, c), i))
if addr in addrs:
d[xpub].update({
"m/%d/%d" % (c, i): "%s:%d" % (addr, values[addrs.index(addr)])
})
else:
c += 1
i += 1
return d


def get_xpub_addrs(*args):
from bitcoin.main import multiaccess
from bitcoin.deterministic import bip32_descend, bip32_ckd, bip32_privtopub
xpubs = [bip32_privtopub(x) if x.startswith("xprv") else x for x in args]
jdata = json.loads(make_request("https://www.blockonomics.co/api/balance", \
json.dumps({"addr": " ".join(xpubs)}))).get("response")
addrs = multiaccess(jdata, "addr")
return addrs


def get_xpub_outputs(*args):
from bitcoin.main import multiaccess
from bitcoin.deterministic import bip32_descend, bip32_ckd, bip32_privtopub
xpubs = [bip32_privtopub(x) if x.startswith("xprv") else x for x in args]
jdata = json.loads(make_request("https://www.blockonomics.co/api/balance",
json.dumps({"addr": " ".join(xpubs)}))).get("response")
addrs = multiaccess(jdata, 'addrs')
values = map(str, multiaccess(jdata, "confirmed" or "unconfirmed"))
return [":".join(y) for y in [x for x in zip(addrs, values)]]