2014-03-28 18:47:53 +00:00
|
|
|
import binascii
|
2014-05-28 12:38:44 +00:00
|
|
|
from decimal import Decimal
|
2016-05-19 09:50:33 +00:00
|
|
|
# from filecache import filecache, DAY
|
2016-05-20 14:08:55 +00:00
|
|
|
import requests
|
2016-05-26 15:20:44 +00:00
|
|
|
from . import types_pb2 as proto_types
|
|
|
|
|
|
|
|
def fetch_json(url):
|
|
|
|
try:
|
|
|
|
r = requests.get(url, headers={'User-agent': 'Mozilla/5.0'})
|
|
|
|
return r.json()
|
|
|
|
except:
|
|
|
|
raise Exception('URL error: %s' % url)
|
2016-05-05 02:57:14 +00:00
|
|
|
|
2015-03-03 23:36:51 +00:00
|
|
|
def insight_tx(url, rawdata=False):
|
|
|
|
if not rawdata:
|
2016-05-26 15:20:44 +00:00
|
|
|
data = fetch_json(url)
|
2015-03-03 23:36:51 +00:00
|
|
|
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():
|
2016-06-27 21:17:20 +00:00
|
|
|
i.prev_hash = b"\0"*32
|
2014-05-28 12:38:44 +00:00
|
|
|
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']
|
2015-05-10 14:05:31 +00:00
|
|
|
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()
|
2015-03-03 23:37:32 +00:00
|
|
|
o.amount = int(Decimal(str(vout['value'])) * 100000000)
|
2015-05-10 14:05:31 +00:00
|
|
|
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
|
|
|
|
|
2016-05-01 12:21:20 +00:00
|
|
|
def smartbit_tx(url, rawdata=False):
|
|
|
|
if not rawdata:
|
2016-05-26 15:20:44 +00:00
|
|
|
data = fetch_json(url)
|
2016-05-01 12:21:20 +00:00
|
|
|
else:
|
|
|
|
data = url
|
|
|
|
|
|
|
|
data = data['transaction']
|
|
|
|
|
|
|
|
t = proto_types.TransactionType()
|
|
|
|
t.version = int(data['version'])
|
|
|
|
t.lock_time = data['locktime']
|
|
|
|
|
|
|
|
for vin in data['inputs']:
|
|
|
|
i = t.inputs.add()
|
|
|
|
if 'coinbase' in vin.keys():
|
2016-06-27 21:17:20 +00:00
|
|
|
i.prev_hash = b"\0"*32
|
2016-05-01 12:21:20 +00:00
|
|
|
i.prev_index = 0xffffffff # signed int -1
|
|
|
|
i.script_sig = binascii.unhexlify(vin['coinbase'])
|
|
|
|
i.sequence = vin['sequence']
|
|
|
|
|
|
|
|
else:
|
|
|
|
i.prev_hash = binascii.unhexlify(vin['txid'])
|
|
|
|
i.prev_index = vin['vout']
|
|
|
|
i.script_sig = binascii.unhexlify(vin['script_sig']['hex'])
|
|
|
|
i.sequence = vin['sequence']
|
|
|
|
|
|
|
|
for vout in data['outputs']:
|
|
|
|
o = t.bin_outputs.add()
|
|
|
|
o.amount = int(Decimal(vout['value']) * 100000000)
|
|
|
|
o.script_pubkey = binascii.unhexlify(vout['script_pub_key']['hex'])
|
|
|
|
|
|
|
|
return t
|
|
|
|
|
2014-03-28 20:34:15 +00:00
|
|
|
class TXAPIBitcoin(object):
|
|
|
|
|
2016-05-19 09:50:33 +00:00
|
|
|
# @filecache(DAY)
|
2014-03-28 20:34:15 +00:00
|
|
|
def get_tx(self, txhash):
|
2016-06-27 21:17:20 +00:00
|
|
|
url = 'https://insight.bitpay.com/api/tx/%s' % txhash.decode('ascii')
|
2014-12-02 02:58:26 +00:00
|
|
|
return insight_tx(url)
|
2014-03-28 18:47:53 +00:00
|
|
|
|
2016-05-20 14:08:55 +00:00
|
|
|
|
2014-03-28 20:34:15 +00:00
|
|
|
class TXAPITestnet(object):
|
2014-03-28 18:47:53 +00:00
|
|
|
|
2016-05-19 09:50:33 +00:00
|
|
|
# @filecache(DAY)
|
2014-03-28 20:34:15 +00:00
|
|
|
def get_tx(self, txhash):
|
2016-06-27 21:17:20 +00:00
|
|
|
url = 'https://test-insight.bitpay.com/api/tx/%s' % txhash.decode('ascii')
|
2014-12-02 02:58:26 +00:00
|
|
|
return insight_tx(url)
|
2016-05-01 12:21:20 +00:00
|
|
|
|
|
|
|
class TXAPISegnet(object):
|
|
|
|
|
2016-05-19 09:56:38 +00:00
|
|
|
# @filecache(DAY)
|
2016-05-01 12:21:20 +00:00
|
|
|
def get_tx(self, txhash):
|
2016-06-27 21:17:20 +00:00
|
|
|
url = 'https://segnet-api.smartbit.com.au/v1/blockchain/tx/%s' % txhash.decode('ascii')
|
2016-05-01 12:21:20 +00:00
|
|
|
return smartbit_tx(url)
|