1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-28 17:18:29 +00:00
trezor-firmware/trezorlib/api_blockchain.py
2014-02-06 10:44:10 +01:00

43 lines
1.3 KiB
Python

'''
Blockchain API implementing Blockchain.info interface
'''
import binascii
import urllib2
import json
import types_pb2 as proto_types
class BlockchainApi(object):
def _raw_tx(self, txhash):
# Download tx data from blockchain.info
url = 'http://blockchain.info/rawtx/%s?scripts=true' % txhash
print "Downloading", url
f = urllib2.urlopen(url)
return json.load(f)
def submit(self, tx):
raise Exception("Not implemented yet")
def get_tx(self, txhash):
# Build protobuf transaction structure from blockchain.info
d = self._raw_tx(txhash)
t = proto_types.TransactionType()
for inp in d['inputs']:
di = self._raw_tx(inp['prev_out']['tx_index'])
i = t.inputs.add()
i.prev_hash = binascii.unhexlify(di['hash'])
i.prev_index = inp['prev_out']['n']
i.script_sig = binascii.unhexlify(inp['script'])
for output in d['out']:
o = t.outputs.add()
o.amount = output['value']
o.script_pubkey = binascii.unhexlify(output['script'])
return t
if __name__ == '__main__':
api = BlockchainApi()
print api.get_tx('b9f382b8dfc34accc05491712a1ad8f7f075a02056dc4821d1f60702fb3fdb2f')