1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-17 11:58:13 +00:00
trezor-firmware/trezorlib/tx_api.py

56 lines
1.5 KiB
Python
Raw Normal View History

2014-03-28 18:47:53 +00:00
import binascii
import urllib2
import json
2014-05-28 12:38:44 +00:00
from decimal import Decimal
from filecache import filecache, DAY
2014-03-28 18:47:53 +00:00
import types_pb2 as proto_types
def insight_tx(url, rawdata=False):
if not rawdata:
try:
f = urllib2.urlopen(url)
data = json.load(f)
except:
raise Exception('URL error: %s' % url)
else:
data = url
2014-03-28 18:47:53 +00:00
2014-03-28 20:34:15 +00:00
t = proto_types.TransactionType()
t.version = data['version']
t.lock_time = data['locktime']
2014-03-28 18:47:53 +00:00
2014-03-28 20:34:15 +00:00
for vin in data['vin']:
2014-03-28 18:47:53 +00:00
i = t.inputs.add()
2014-05-28 12:38:44 +00:00
if 'coinbase' in vin.keys():
i.prev_hash = "\0"*32
i.prev_index = 0xffffffff # signed int -1
i.script_sig = binascii.unhexlify(vin['coinbase'])
i.sequence = vin['sequence']
2014-12-02 02:58:26 +00:00
else:
2014-05-28 12:38:44 +00:00
i.prev_hash = binascii.unhexlify(vin['txid'])
i.prev_index = vin['vout']
i.script_sig = binascii.unhexlify(vin['scriptSig']['hex'])
2014-05-28 12:38:44 +00:00
i.sequence = vin['sequence']
2014-03-28 18:47:53 +00:00
2014-03-28 20:34:15 +00:00
for vout in data['vout']:
2014-04-07 14:25:03 +00:00
o = t.bin_outputs.add()
o.amount = int(Decimal(str(vout['value'])) * 100000000)
o.script_pubkey = binascii.unhexlify(vout['scriptPubKey']['hex'])
2014-03-28 18:47:53 +00:00
2014-03-28 20:34:15 +00:00
return t
class TXAPIBitcoin(object):
2015-02-22 13:38:33 +00:00
@filecache(DAY)
2014-03-28 20:34:15 +00:00
def get_tx(self, txhash):
2014-12-02 02:58:26 +00:00
url = 'https://insight.bitpay.com/api/tx/%s' % txhash
return insight_tx(url)
2014-03-28 18:47:53 +00:00
2014-03-28 20:34:15 +00:00
class TXAPITestnet(object):
2014-03-28 18:47:53 +00:00
2015-02-22 13:38:33 +00:00
@filecache(DAY)
2014-03-28 20:34:15 +00:00
def get_tx(self, txhash):
2015-01-23 20:24:15 +00:00
url = 'https://test-insight.bitpay.com/api/tx/%s' % txhash
2014-12-02 02:58:26 +00:00
return insight_tx(url)