-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathelectrum_parser.py
More file actions
214 lines (170 loc) · 6.39 KB
/
electrum_parser.py
File metadata and controls
214 lines (170 loc) · 6.39 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
from binascii import hexlify, unhexlify
from struct import pack, Struct
from collections import namedtuple
unpack_int32_from = Struct('<i').unpack_from
unpack_int64_from = Struct('<q').unpack_from
unpack_uint16_from = Struct('<H').unpack_from
unpack_uint32_from = Struct('<I').unpack_from
unpack_uint64_from = Struct('<Q').unpack_from
# Method decorator. To be used for calculations that will always
# deliver the same result. The method cannot take any arguments
# and should be accessed as an attribute.
class cachedproperty(object):
def __init__(self, f):
self.f = f
def __get__(self, obj, type):
obj = obj or type
value = self.f(obj)
setattr(obj, self.f.__name__, value)
return value
class TxDecred(namedtuple("Tx", "version inputs outputs "
"lock_time expiry witnesses")):
'''Class representing a Decred transaction.'''
@cachedproperty
def is_coinbase(self):
return self.inputs[0].is_coinbase
def __str__(self):
return ("Tx(version={}, inputs={}, outputs={}, lock_time={}, expiry={}, witnesses={})"
.format(self.version, self.inputs, self.outputs, self.lock_time, self.expiry, self.witnesses))
class TxDecredInput(namedtuple("TxDecredInput", "prev_hash prev_idx tree sequence")):
'''Class representing a decred transaction input.'''
ZERO = bytes(32)
MINUS_1 = 4294967295
@cachedproperty
def is_coinbase(self):
return (self.prev_hash == TxInput.ZERO and
self.prev_idx == TxInput.MINUS_1)
def __str__(self):
prev_hash = hash_to_str(self.prev_hash)
return ("Input(prev_hash={}, prev_idx={}, tree={}, sequence={})"
.format(prev_hash, self.prev_idx, self.tree, self.sequence))
class TxDecredOutput(namedtuple("TxDecredOutput", "value version script")):
def __str__(self):
script = self.script.hex()
return ("Output(value={}, version={}, script={})"
.format(self.value, self.version, self.script))
class TxDecredWitness(namedtuple("TxDecredWitness", "value blockHeight blockIndex script")):
def __str__(self):
script = self.script.hex()
return ("Witness(value={}, blockHeight={}, blockIndex={}, script={})"
.format(self.value, self.blockHeight, self.blockIndex, script))
class DecredTxParser(object):
'''Deserializes blocks into transactions.
External entry points are read_tx(), read_tx_and_hash(),
read_tx_and_vsize() and read_block().
This code is performance sensitive as it is executed 100s of
millions of times during sync.
'''
def __init__(self, hex, start=0):
binary = unhexlify(hex)
assert isinstance(binary, bytes)
self.binary = binary
self.binary_length = len(binary)
self.cursor = start
def read_tx(self):
version = self._read_le_uint16()
txType = self._read_le_uint16()
inputs = ''
outputs = ''
lock_time = ''
expiry = ''
witnesses = ''
if txType == 0 or txType == 1:
inputs = self._read_inputs()
outputs = self._read_outputs()
lock_time = self._read_le_uint32()
expiry = self._read_le_uint32()
if txType != 1:
witnesses = self._read_witnesses(txType)
return TxDecred(
version,
inputs,
outputs,
lock_time,
expiry,
witnesses
)
def _read_witnesses(self, txType):
read_witness = self._read_witness
return [read_witness(txType) for i in range(self._read_varint())]
def _read_witness(self, txType):
value = ''
blockHeight = ''
blockIndex = ''
script = ''
if txType == 0 or txType == 2:
value = self._read_le_int64()
blockHeight = self._read_le_uint32()
blockIndex = self._read_le_uint32()
script = self._read_varbytes()
if txType == 3:
script = self._read_varbytes()
if txType == 4:
value = self._read_le_int64()
script = self._read_varbytes()
return TxDecredWitness(
value,
blockHeight,
blockIndex,
script
)
def _read_inputs(self):
read_input = self._read_input
return [read_input() for i in range(self._read_varint())]
def _read_input(self):
return TxDecredInput(
self._read_nbytes(32), #prev_hash
self._read_le_uint32(), #prev_idx
self._read_byte(), #tree
self._read_le_uint32() #sequence
)
def _read_outputs(self):
read_output = self._read_output
return [read_output() for i in range(self._read_varint())]
def _read_output(self):
return TxDecredOutput(
self._read_le_int64(), #value
self._read_le_uint16(), #version
self._read_varbytes(), #script
)
def _read_byte(self):
cursor = self.cursor
self.cursor += 1
return self.binary[cursor]
def _read_nbytes(self, n):
cursor = self.cursor
self.cursor = end = cursor + n
assert self.binary_length >= end
return self.binary[cursor:end]
def _read_varbytes(self):
return self._read_nbytes(self._read_varint())
def _read_varint(self):
n = self.binary[self.cursor]
self.cursor += 1
if n < 253:
return n
if n == 253:
return self._read_le_uint16()
if n == 254:
return self._read_le_uint32()
return self._read_le_uint64()
def _read_le_int32(self):
result, = unpack_int32_from(self.binary, self.cursor)
self.cursor += 4
return result
def _read_le_int64(self):
result, = unpack_int64_from(self.binary, self.cursor)
self.cursor += 8
return result
def _read_le_uint16(self):
result, = unpack_uint16_from(self.binary, self.cursor)
self.cursor += 2
return result
def _read_le_uint32(self):
result, = unpack_uint32_from(self.binary, self.cursor)
self.cursor += 4
return result
def _read_le_uint64(self):
result, = unpack_uint64_from(self.binary, self.cursor)
self.cursor += 8
return result