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
|
|
|
|
2017-12-12 15:40:11 +00:00
|
|
|
from . import messages as proto
|
2016-11-28 15:01:45 +00:00
|
|
|
cache_dir = None
|
2016-10-21 13:24:30 +00:00
|
|
|
|
2017-06-23 19:31:42 +00:00
|
|
|
|
2016-11-08 14:07:58 +00:00
|
|
|
class TxApi(object):
|
|
|
|
|
|
|
|
def __init__(self, network, url):
|
|
|
|
self.network = network
|
|
|
|
self.url = url
|
|
|
|
|
2017-11-25 13:47:41 +00:00
|
|
|
def get_url(self, resource, resourceid):
|
|
|
|
url = '%s%s/%s' % (self.url, resource, resourceid)
|
|
|
|
return url
|
|
|
|
|
2017-06-18 22:00:26 +00:00
|
|
|
def fetch_json(self, resource, resourceid):
|
2016-11-28 15:01:45 +00:00
|
|
|
global cache_dir
|
|
|
|
if cache_dir:
|
|
|
|
cache_file = '%s/%s_%s_%s.json' % (cache_dir, self.network, resource, resourceid)
|
2017-06-23 19:31:42 +00:00
|
|
|
try: # looking into cache first
|
2017-11-25 13:47:41 +00:00
|
|
|
j = json.load(open(cache_file), parse_float=str)
|
2016-11-28 15:01:45 +00:00
|
|
|
return j
|
|
|
|
except:
|
|
|
|
pass
|
2017-12-12 15:40:11 +00:00
|
|
|
|
2016-11-08 14:07:58 +00:00
|
|
|
try:
|
2017-11-25 13:47:41 +00:00
|
|
|
url = self.get_url(resource, resourceid)
|
2017-06-18 22:00:26 +00:00
|
|
|
r = requests.get(url, headers={'User-agent': 'Mozilla/5.0'})
|
2017-11-25 13:47:41 +00:00
|
|
|
j = r.json(parse_float=str)
|
2016-11-08 14:07:58 +00:00
|
|
|
except:
|
2017-11-06 10:09:54 +00:00
|
|
|
raise RuntimeError('URL error: %s' % url)
|
2017-02-26 10:35:36 +00:00
|
|
|
if cache_dir and cache_file:
|
2017-06-23 19:31:42 +00:00
|
|
|
try: # saving into cache
|
2017-02-26 10:35:36 +00:00
|
|
|
json.dump(j, open(cache_file, 'w'))
|
2016-11-28 15:01:45 +00:00
|
|
|
except:
|
|
|
|
pass
|
2016-11-08 14:07:58 +00:00
|
|
|
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
|
|
|
|
2017-06-18 22:00:26 +00:00
|
|
|
data = self.fetch_json('tx', txhash)
|
2016-05-01 12:21:20 +00:00
|
|
|
|
2017-12-12 15:40:11 +00:00
|
|
|
t = proto.TransactionType()
|
2016-11-08 14:07:58 +00:00
|
|
|
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']:
|
2017-12-12 15:40:11 +00:00
|
|
|
i = t._add_inputs()
|
2016-11-08 14:07:58 +00:00
|
|
|
if 'coinbase' in vin.keys():
|
2017-06-23 19:31:42 +00:00
|
|
|
i.prev_hash = b"\0" * 32
|
|
|
|
i.prev_index = 0xffffffff # signed int -1
|
2016-11-08 14:07:58 +00:00
|
|
|
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']:
|
2017-12-12 15:40:11 +00:00
|
|
|
o = t._add_bin_outputs()
|
2017-11-25 13:47:41 +00:00
|
|
|
o.amount = int(Decimal(vout['value']) * 100000000)
|
2016-11-08 14:07:58 +00:00
|
|
|
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:
|
2017-06-23 19:31:42 +00:00
|
|
|
t.extra_data = b'\x00'
|
2016-11-08 14:07:58 +00:00
|
|
|
else:
|
2016-11-28 15:01:45 +00:00
|
|
|
if joinsplit_cnt >= 253:
|
|
|
|
# we assume cnt < 253, so we can treat varIntLen(cnt) as 1
|
|
|
|
raise ValueError('Too many joinsplits')
|
|
|
|
extra_data_len = 1 + joinsplit_cnt * 1802 + 32 + 64
|
2017-06-18 22:00:26 +00:00
|
|
|
raw = self.fetch_json('rawtx', txhash)
|
2016-11-08 14:07:58 +00:00
|
|
|
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
|
|
|
|
2017-06-18 22:00:26 +00:00
|
|
|
data = self.fetch_json('tx', txhash)
|
2016-10-21 13:24:30 +00:00
|
|
|
|
2016-11-08 14:07:58 +00:00
|
|
|
data = data['transaction']
|
|
|
|
|
2017-12-12 15:40:11 +00:00
|
|
|
t = proto.TransactionType()
|
2016-11-08 14:07:58 +00:00
|
|
|
t.version = int(data['version'])
|
|
|
|
t.lock_time = data['locktime']
|
|
|
|
|
|
|
|
for vin in data['inputs']:
|
|
|
|
i = t.inputs.add()
|
|
|
|
if 'coinbase' in vin.keys():
|
2017-06-23 19:31:42 +00:00
|
|
|
i.prev_hash = b"\0" * 32
|
|
|
|
i.prev_index = 0xffffffff # signed int -1
|
2016-11-08 14:07:58 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2017-02-26 14:32:23 +00:00
|
|
|
class TxApiBlockCypher(TxApi):
|
|
|
|
|
|
|
|
def get_tx(self, txhash):
|
|
|
|
|
2017-06-18 22:00:26 +00:00
|
|
|
data = self.fetch_json('txs', txhash)
|
2017-02-26 14:32:23 +00:00
|
|
|
|
2017-12-12 15:40:11 +00:00
|
|
|
t = proto.TransactionType()
|
2017-02-26 14:32:23 +00:00
|
|
|
t.version = data['ver']
|
|
|
|
t.lock_time = data['lock_time']
|
|
|
|
|
|
|
|
for vin in data['inputs']:
|
2017-12-25 23:15:15 +00:00
|
|
|
i = t._add_inputs()
|
2017-02-26 14:32:23 +00:00
|
|
|
if 'prev_hash' not in vin:
|
2017-06-23 19:31:42 +00:00
|
|
|
i.prev_hash = b"\0" * 32
|
|
|
|
i.prev_index = 0xffffffff # signed int -1
|
2017-02-26 14:32:23 +00:00
|
|
|
i.script_sig = binascii.unhexlify(vin['script'])
|
|
|
|
i.sequence = vin['sequence']
|
|
|
|
else:
|
|
|
|
i.prev_hash = binascii.unhexlify(vin['prev_hash'])
|
|
|
|
i.prev_index = vin['output_index']
|
|
|
|
i.script_sig = binascii.unhexlify(vin['script'])
|
|
|
|
i.sequence = vin['sequence']
|
|
|
|
|
|
|
|
for vout in data['outputs']:
|
2017-12-25 23:15:15 +00:00
|
|
|
o = t._add_bin_outputs()
|
2017-02-26 14:32:23 +00:00
|
|
|
o.amount = int(str(vout['value']), 10)
|
|
|
|
o.script_pubkey = binascii.unhexlify(vout['script'])
|
|
|
|
|
|
|
|
return t
|
|
|
|
|
|
|
|
|
2017-09-15 15:05:47 +00:00
|
|
|
TxApiBitcoin = TxApiInsight(network='insight_bitcoin', url='https://btc-bitcore1.trezor.io/api/')
|
|
|
|
TxApiTestnet = TxApiInsight(network='insight_testnet', url='https://testnet-bitcore3.trezor.io/api/')
|
|
|
|
TxApiLitecoin = TxApiInsight(network='insight_litecoin', url='https://ltc-bitcore1.trezor.io/api/')
|
|
|
|
TxApiDash = TxApiInsight(network='insight_dash', url='https://dash-bitcore1.trezor.io/api/')
|
|
|
|
TxApiZcash = TxApiInsight(network='insight_zcash', url='https://zec-bitcore1.trezor.io/api/', zcash=True)
|
|
|
|
TxApiBcash = TxApiInsight(network='insight_zcash', url='https://bch-bitcore2.trezor.io/api/')
|
2017-12-17 13:30:30 +00:00
|
|
|
TxApiDecredTestnet = TxApiInsight(network='insight_decred_testnet', url='https://testnet.decred.org/api/')
|
2017-09-15 15:05:47 +00:00
|
|
|
|
2017-12-25 23:15:15 +00:00
|
|
|
TxApiDogecoin = TxApiBlockCypher(network='blockcypher_dogecoin', url='http://api.blockcypher.com/v1/doge/main/')
|
|
|
|
|
2016-11-08 14:07:58 +00:00
|
|
|
TxApiSegnet = TxApiSmartbit(network='smartbit_segnet', url='https://segnet-api.smartbit.com.au/v1/blockchain/')
|