1
0
mirror of https://github.com/bitcoinbook/bitcoinbook synced 2024-11-18 06:08:18 +00:00
bitcoinbook/code/get-utxo.py

32 lines
989 B
Python
Raw Normal View History

2014-08-29 16:35:42 +00:00
# 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:
2018-02-03 00:33:46 +00:00
# { "unspent_outputs":[
2014-08-29 16:35:42 +00:00
# {
# "tx_hash":"ebadfaa92f1fd29e2fe296eda702c48bd11ffd52313e986e99ddad9084062167",
# "tx_index":51919767,
# "tx_output_n": 1,
# "script":"76a9148c7e252f8d64b0b6e313985915110fcfefcf4a2d88ac",
# "value": 8000000,
# "value_hex": "7a1200",
# "confirmations":28691
# },
# ...
2018-02-03 00:33:46 +00:00
# ]}
2014-08-29 16:35:42 +00:00
resp = requests.get('https://blockchain.info/unspent?active=%s' % address)
utxo_set = json.loads(resp.text)["unspent_outputs"]
for utxo in utxo_set:
2017-08-24 16:20:22 +00:00
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))