-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathtx_api.py
More file actions
273 lines (223 loc) · 10.4 KB
/
Copy pathtx_api.py
File metadata and controls
273 lines (223 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# 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/>.
import binascii
from decimal import Decimal
import requests
import json
import os
import struct
from . import types_pb2 as proto_types
cache_dir = None
offline_only = False
class OfflineFixtureError(Exception):
"""An authoritative transaction fixture is missing or malformed."""
def configure_offline_fixtures(path):
"""Make transaction lookup fail closed against a fixed fixture tree."""
global cache_dir, offline_only
cache_dir = os.path.abspath(path)
offline_only = True
def pack_varint(n):
if n < 253:
return struct.pack("<B", n)
elif n <= 0xFFFF:
return struct.pack("<BH", 253, n)
elif n <= 0xFFFFFFFF:
return struct.pack("<BL", 254, n)
else:
return struct.pack("<BQ", 255, n)
class TxApi(object):
def __init__(self, network, url):
self.network = network
self.url = url
def fetch_json(self, url, resource, resourceid):
global cache_dir, offline_only
cache_file = None
if cache_dir:
fixture_name = '%s_%s_%s.json' % (
self.network, resource, resourceid)
if os.path.basename(fixture_name) != fixture_name:
raise OfflineFixtureError(
'Invalid fixture key: network=%s resource=%s id=%s' %
(self.network, resource, resourceid))
cache_file = os.path.join(cache_dir, fixture_name)
try: # looking into cache first
with open(cache_file) as f:
return json.load(f)
except OSError as exc:
if offline_only:
raise OfflineFixtureError(
'Missing offline transaction fixture: '
'network=%s resource=%s id=%s path=%s' %
(self.network, resource, resourceid, cache_file)
) from exc
except (TypeError, ValueError) as exc:
if offline_only:
raise OfflineFixtureError(
'Invalid offline transaction fixture: '
'network=%s resource=%s id=%s path=%s' %
(self.network, resource, resourceid, cache_file)
) from exc
if offline_only:
raise OfflineFixtureError(
'Offline transaction fixtures are enabled without a fixture '
'directory: network=%s resource=%s id=%s' %
(self.network, resource, resourceid))
try:
# print('request %s/%s/%s' % (self.url, resource, resourceid))
r = requests.get('%s/%s/%s' % (self.url, resource, resourceid), headers={'User-agent': 'Mozilla/5.0'})
r.raise_for_status() # raises exception when not a 2xx response
if r.status_code != 204:
return r.json()
j = r.json()
except:
raise Exception('URL error: %s' % url)
if cache_file:
try: # saving into cache
json.dump(j, open(cache_file, 'w'))
except:
pass
return j
def get_tx(self, txhash):
raise NotImplementedError
class TxApiInsight(TxApi):
def __init__(self, network, url, zcash=None):
super(TxApiInsight, self).__init__(network, url)
self.zcash = zcash
def get_tx(self, txhash):
data = self.fetch_json(self.url, 'tx', txhash)
# print(json.dumps(data, indent=2))
t = proto_types.TransactionType()
t.version = data['version']
t.lock_time = data['locktime']
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']
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']
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'])
if self.zcash:
if t.version == 2:
joinsplit_cnt = len(data['vjoinsplit'])
if joinsplit_cnt == 0:
t.extra_data =b'\x00'
else:
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
raw = self.fetch_json(self.url, 'rawtx', txhash)
raw = binascii.unhexlify(raw['rawtx'])
t.extra_data = raw[-extra_data_len:]
if "_dash" in self.network:
dip2_type = data.get("type", 0)
if t.version == 3 and dip2_type != 0:
# It's a DIP2 special TX with payload
if "extraPayloadSize" not in data or "extraPayload" not in data:
raise ValueError("Payload data missing in DIP2 transaction")
if data["extraPayloadSize"] * 2 != len(data["extraPayload"]):
raise ValueError("length mismatch")
t.extra_data = pack_varint(data["extraPayloadSize"]) + binascii.unhexlify(
data["extraPayload"]
)
# Trezor (and therefore KeepKey) firmware doesn't understand the
# split of version and type, so let's mimic the old serialization
# format
t.version |= dip2_type << 16
return t
def get_raw_tx(self, txhash):
data = self.fetch_json(self.url, 'rawtx', txhash)['rawtx']
return data
class TxApiBs(TxApi):
# parser for blockstream.info api
def __init__(self, network, url, zcash=None):
super(TxApiBs, self).__init__(network, url)
self.zcash = zcash
def get_tx(self, txhash):
data = self.fetch_json(self.url, 'tx', txhash)
print(json.dumps(data, indent=2))
t = proto_types.TransactionType()
t.version = data['version']
t.lock_time = data['locktime']
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']
else:
i.prev_hash = binascii.unhexlify(vin['txid'])
i.prev_index = vin['vout']
# i.script_sig = binascii.unhexlify(vin['scriptsig'])
i.script_sig = bytes.fromhex(vin['scriptsig'])
i.sequence = vin['sequence']
for vout in data['vout']:
o = t.bin_outputs.add()
o.amount = int(Decimal(str(vout['value'])) * 100000000)
o.script_pubkey = bytes.fromhex(vout['scriptpubkey'])
if self.zcash:
if t.version == 2:
joinsplit_cnt = len(data['vjoinsplit'])
if joinsplit_cnt == 0:
t.extra_data =b'\x00'
else:
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
raw = self.fetch_json(self.url, 'rawtx', txhash)
raw = binascii.unhexlify(raw['rawtx'])
t.extra_data = raw[-extra_data_len:]
if "_dash" in self.network:
dip2_type = data.get("type", 0)
if t.version == 3 and dip2_type != 0:
# It's a DIP2 special TX with payload
if "extrapayloadsize" not in data or "extrapayload" not in data:
raise ValueError("Payload data missing in DIP2 transaction")
if data["extrapayloadsize"] * 2 != len(data["extrasayload"]):
raise ValueError("length mismatch")
t.extra_data = pack_varint(data["extrapayloadsize"]) + binascii.unhexlify(
data["extrapayload"]
)
# Trezor (and therefore KeepKey) firmware doesn't understand the
# split of version and type, so let's mimic the old serialization
# format
t.version |= dip2_type << 16
return t
def get_raw_tx(self, txhash):
data = self.fetch_json(self.url, 'rawtx', txhash)['rawtx']
return data
TxApiBitcoin = TxApiInsight(network='insight_bitcoin', url='https://btc.coinquery.com/api')
TxApiTestnet = TxApiInsight(network='insight_testnet', url='https://test-insight.bitpay.com/api')
# TxApiTestnet = TxApiBs(network='blockstream_testnet', url='https://blockstream.info/testnet/api')
TxApiZcashTestnet = TxApiInsight(network='insight_zcashtestnet', url='https://explorer.testnet.z.cash/api', zcash=True)
TxApiBitcoinGold = TxApiInsight(network='insight_bitcoingold', url='https://btg.coinquery.com/api')
TxApiGroestlcoin = TxApiInsight(network='insight_groestlcoin', url='https://groestlsight.groestlcoin.org/api')
TxApiGroestlcoinTestnet = TxApiInsight(network='insight_groestlcoin_testnet', url='https://groestlsight-test.groestlcoin.org/api')
TxApiDash = TxApiInsight(network='insight_dash', url='https://dash.coinquery.com/api')