mirror of
https://github.com/bitcoinbook/bitcoinbook
synced 2024-12-30 18:30:56 +00:00
32 lines
989 B
Python
32 lines
989 B
Python
# get unspent outputs from blockchain API
|
|
|
|
import json
|
|
import requests
|
|
|
|
# example address
|
|
address = '1Dorian4RoXcnBv9hnQ4Y2C1an6NJ4UrjX'
|
|
|
|
# The API URL is https://blockchain.info/unspent?active=<address>
|
|
# It returns a JSON object with a list "unspent_outputs", containing UTXO, like this:
|
|
# { "unspent_outputs":[
|
|
# {
|
|
# "tx_hash":"ebadfaa92f1fd29e2fe296eda702c48bd11ffd52313e986e99ddad9084062167",
|
|
# "tx_index":51919767,
|
|
# "tx_output_n": 1,
|
|
# "script":"76a9148c7e252f8d64b0b6e313985915110fcfefcf4a2d88ac",
|
|
# "value": 8000000,
|
|
# "value_hex": "7a1200",
|
|
# "confirmations":28691
|
|
# },
|
|
# ...
|
|
# ]}
|
|
|
|
resp = requests.get('https://blockchain.info/unspent?active=%s' % address)
|
|
utxo_set = json.loads(resp.text)["unspent_outputs"]
|
|
|
|
for utxo in utxo_set:
|
|
print("%s:%d - %ld Satoshis" % (utxo['tx_hash'], utxo['tx_output_n'],
|
|
utxo['value']))
|
|
# Or try...
|
|
# print("{tx_hash}:{tx_output_n} - {value} Satoshis".format(**utxo))
|