2016-11-25 21:53:55 +00:00
|
|
|
# This file is part of the TREZOR project.
|
|
|
|
#
|
|
|
|
# Copyright (C) 2012-2016 Marek Palatinus <slush@satoshilabs.com>
|
|
|
|
# Copyright (C) 2012-2016 Pavol Rusnak <stick@satoshilabs.com>
|
|
|
|
# Copyright (C) 2016 Jochen Hoenicke <hoenicke@gmail.com>
|
|
|
|
#
|
|
|
|
# This library is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This library is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Lesser General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Lesser General Public License
|
|
|
|
# along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2014-03-28 18:47:53 +00:00
|
|
|
import binascii
|
2014-05-28 12:38:44 +00:00
|
|
|
from decimal import Decimal
|
2016-05-20 14:08:55 +00:00
|
|
|
import requests
|
2016-11-08 14:07:58 +00:00
|
|
|
import json
|
2016-05-26 15:20:44 +00:00
|
|
|
from . import types_pb2 as proto_types
|
|
|
|
|
2016-10-21 13:24:30 +00:00
|
|
|
|
2016-11-08 14:07:58 +00:00
|
|
|
class TxApi(object):
|
|
|
|
|
|
|
|
def __init__(self, network, url):
|
|
|
|
self.network = network
|
|
|
|
self.url = url
|
|
|
|
|
|
|
|
def fetch_json(self, url, resource, resourceid):
|
|
|
|
cachefile = '%s_%s_%s' % (self.network, resource, resourceid)
|
|
|
|
try: # looking into cache first
|
|
|
|
j = json.load(open('txcache/' + cachefile))
|
|
|
|
return j
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
try:
|
|
|
|
r = requests.get('%s/%s/%s' % (self.url, resource, resourceid), headers={'User-agent': 'Mozilla/5.0'})
|
|
|
|
j = r.json()
|
|
|
|
except:
|
|
|
|
raise Exception('URL error: %s' % url)
|
|
|
|
try: # saving into cache
|
|
|
|
json.dump(j, open('txcache/' + cachefile, 'w'))
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
return j
|
2014-03-28 20:34:15 +00:00
|
|
|
|
2016-11-08 14:07:58 +00:00
|
|
|
def get_tx(self, txhash):
|
|
|
|
raise NotImplementedError
|
2016-05-01 12:21:20 +00:00
|
|
|
|
|
|
|
|
2016-11-08 14:07:58 +00:00
|
|
|
class TxApiInsight(TxApi):
|
2016-05-01 12:21:20 +00:00
|
|
|
|
2016-11-08 14:07:58 +00:00
|
|
|
def __init__(self, network, url, zcash=None):
|
|
|
|
super(TxApiInsight, self).__init__(network, url)
|
|
|
|
self.zcash = zcash
|
2016-05-01 12:21:20 +00:00
|
|
|
|
2016-11-08 14:07:58 +00:00
|
|
|
def get_tx(self, txhash):
|
2016-05-01 12:21:20 +00:00
|
|
|
|
2016-11-08 14:07:58 +00:00
|
|
|
data = self.fetch_json(self.url, 'tx', txhash)
|
2016-05-01 12:21:20 +00:00
|
|
|
|
2016-11-08 14:07:58 +00:00
|
|
|
t = proto_types.TransactionType()
|
|
|
|
t.version = data['version']
|
|
|
|
t.lock_time = data['locktime']
|
2016-05-01 12:21:20 +00:00
|
|
|
|
2016-11-08 14:07:58 +00:00
|
|
|
for vin in data['vin']:
|
|
|
|
i = t.inputs.add()
|
|
|
|
if 'coinbase' in vin.keys():
|
|
|
|
i.prev_hash = b"\0"*32
|
|
|
|
i.prev_index = 0xffffffff # signed int -1
|
|
|
|
i.script_sig = binascii.unhexlify(vin['coinbase'])
|
|
|
|
i.sequence = vin['sequence']
|
2014-03-28 20:34:15 +00:00
|
|
|
|
2016-11-08 14:07:58 +00:00
|
|
|
else:
|
|
|
|
i.prev_hash = binascii.unhexlify(vin['txid'])
|
|
|
|
i.prev_index = vin['vout']
|
|
|
|
i.script_sig = binascii.unhexlify(vin['scriptSig']['hex'])
|
|
|
|
i.sequence = vin['sequence']
|
2014-03-28 18:47:53 +00:00
|
|
|
|
2016-11-08 14:07:58 +00:00
|
|
|
for vout in data['vout']:
|
|
|
|
o = t.bin_outputs.add()
|
|
|
|
o.amount = int(Decimal(str(vout['value'])) * 100000000)
|
|
|
|
o.script_pubkey = binascii.unhexlify(vout['scriptPubKey']['hex'])
|
2016-05-20 14:08:55 +00:00
|
|
|
|
2016-11-08 14:07:58 +00:00
|
|
|
if self.zcash:
|
|
|
|
if t.version == 2:
|
|
|
|
joinsplit_cnt = len(data['vjoinsplit'])
|
|
|
|
if joinsplit_cnt == 0:
|
|
|
|
t.extra_data =b'\x00'
|
|
|
|
else:
|
|
|
|
extra_data_len = 1 + joinsplit_cnt * 1802 + 32 + 64 # we assume cnt < 253, so we can treat varIntLen(cnt) as 1
|
|
|
|
raw = fetch_json(self.url, 'rawtx', txhash)
|
|
|
|
raw = binascii.unhexlify(raw['rawtx'])
|
|
|
|
t.extra_data = raw[-extra_data_len:]
|
2014-03-28 18:47:53 +00:00
|
|
|
|
2016-11-08 14:07:58 +00:00
|
|
|
return t
|
2016-05-01 12:21:20 +00:00
|
|
|
|
|
|
|
|
2016-11-08 14:07:58 +00:00
|
|
|
class TxApiSmartbit(TxApi):
|
|
|
|
|
2016-05-01 12:21:20 +00:00
|
|
|
def get_tx(self, txhash):
|
2016-10-21 13:24:30 +00:00
|
|
|
|
2016-11-08 14:07:58 +00:00
|
|
|
data = self.fetch_json(self.url, 'tx', txhash)
|
2016-10-21 13:24:30 +00:00
|
|
|
|
2016-11-08 14:07:58 +00:00
|
|
|
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():
|
|
|
|
i.prev_hash = b"\0"*32
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
TxApiBitcoin = TxApiInsight(network='insight_bitcoin', url='https://insight.bitpay.com/api/')
|
|
|
|
TxApiTestnet = TxApiInsight(network='insight_testnet', url='https://test-insight.bitpay.com/api/')
|
|
|
|
TxApiSegnet = TxApiSmartbit(network='smartbit_segnet', url='https://segnet-api.smartbit.com.au/v1/blockchain/')
|
|
|
|
TxApiZcashTestnet = TxApiInsight(network='insight_zcashtestnet', url='https://explorer.testnet.z.cash/api/', zcash=True)
|