''' 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 f = urllib2.urlopen('http://blockchain.info/rawtx/%s?scripts=true' % txhash) print 'got', txhash 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')