python-trezor/trezorlib/tx_api.py

51 lines
1.4 KiB
Python
Raw Normal View History

2014-03-28 11:47:53 -07:00
import binascii
import urllib2
import json
try:
from filecache import filecache, MONTH
except:
def filecache(x):
def _inner(y):
return y
return _inner
MONTH = None
import types_pb2 as proto_types
2014-03-28 13:34:15 -07:00
def bitcore_tx(url):
f = urllib2.urlopen(url)
data = json.load(f)
2014-03-28 11:47:53 -07:00
2014-03-28 13:34:15 -07:00
t = proto_types.TransactionType()
t.version = data['version']
t.lock_time = data['locktime']
2014-03-28 11:47:53 -07:00
2014-03-28 13:34:15 -07:00
for vin in data['vin']:
2014-03-28 11:47:53 -07:00
i = t.inputs.add()
2014-03-28 13:34:15 -07:00
i.prev_hash = binascii.unhexlify(vin['txid'])
i.prev_index = vin['vout']
asm = [ binascii.unhexlify(x) for x in vin['scriptSig']['asm'].split(' ') ]
i.script_sig = chr(len(asm[0])) + asm[0] + chr(len(asm[1])) + asm[1] # TODO: should be op_push(x) instead of chr(len(x))
2014-03-28 11:47:53 -07:00
2014-03-28 13:34:15 -07:00
for vout in data['vout']:
2014-03-28 11:47:53 -07:00
o = t.outputs.add()
2014-03-28 13:34:15 -07:00
o.amount = int(vout['value'] * 100000000)
asm = vout['scriptPubKey']['asm'].split(' ') # we suppose it's OP_DUP OP_HASH160 pubkey OP_EQUALVERIFY OP_CHECKSIG
o.script_pubkey = binascii.unhexlify('76a914' + asm[2] + '88ac')
2014-03-28 11:47:53 -07:00
2014-03-28 13:34:15 -07:00
return t
class TXAPIBitcoin(object):
@filecache(MONTH)
def get_tx(self, txhash):
url = 'http://live.bitcore.io/api/tx/%s' % txhash
return bitcore_tx(url)
2014-03-28 11:47:53 -07:00
2014-03-28 13:34:15 -07:00
class TXAPITestnet(object):
2014-03-28 11:47:53 -07:00
2014-03-28 13:34:15 -07:00
@filecache(MONTH)
def get_tx(self, txhash):
url = 'http://test.bitcore.io/api/tx/%s' % txhash
return bitcore_tx(url)