-
Notifications
You must be signed in to change notification settings - Fork 3
/
TxParser.py
280 lines (235 loc) · 10.6 KB
/
TxParser.py
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
274
275
276
277
278
279
280
'''
* Python API for the SatoChip Bitcoin Hardware Wallet
* (c) 2015 by Toporin - 16DMCk4WUaHofchAhpMaQS4UPm4urcy2dN
* Sources available on https://github.com/Toporin
*
* Copyright 2015 by Toporin (https://github.com/Toporin)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
'''
import hashlib
from enum import Enum, auto
class TxState(Enum):
TX_START= auto()
TX_PARSE_INPUT= auto()
TX_PARSE_INPUT_SCRIPT= auto()
TX_PARSE_OUTPUT= auto()
TX_PARSE_OUTPUT_SCRIPT= auto()
TX_PARSE_FINALIZE= auto()
TX_END= auto()
class TxParser:
CHUNK_SIZE=128 # max chunk size of a script
def __init__(self, rawTx):
self.txData= rawTx[:]
self.txRemainingInput=0
self.txCurrentInput=0
self.txRemainingOutput=0
self.txCurrentOutput=0
self.outScripts=[]
self.outAmounts=[]
self.txAmount=0
self.txScriptRemaining=0
self.txOffset=0
self.txRemaining=len(rawTx)
self.txState= TxState.TX_START
self.txDigest=hashlib.sha256()
self.singleHash=None
self.doubleHash=None
self.txChunk=b''
self.inputScript=b''
#print('[TxParser] TxParser: __init__(): txOffset='+str(self.txOffset) + " txRemaining="+str(self.txRemaining) + "chunk="+self.txChunk.hex()) #debugSatochip
def set_remaining_output(self, nb_outputs):
self.txRemainingOutput=nb_outputs
def is_parsed(self):
return (self.txRemaining==0)
def get_tx_hash(self):
return self.singleHash
def get_tx_double_hash(self):
return self.doubleHash
def parse_transaction(self):
self.txChunk=b''
if self.txState == TxState.TX_START:
# max 4+9 bytes accumulated
self.parse_byte(4) # version
self.txRemainingInput= self.parse_var_int()
self.txState= TxState.TX_PARSE_INPUT
if self.txState == TxState.TX_PARSE_INPUT:
# max 36+9 bytes accumulated
if self.txRemainingInput==0:
self.txRemainingOutput= self.parse_var_int()
self.txState= TxState.TX_PARSE_OUTPUT
#break
else:
self.parse_byte(32); # txOutHash
self.parse_byte(4); # txOutIndex
self.txScriptRemaining= self.parse_var_int();
self.txState= TxState.TX_PARSE_INPUT_SCRIPT
self.txRemainingInput-=1
self.txCurrentInput+=1
#break
elif self.txState == TxState.TX_PARSE_INPUT_SCRIPT:
# max MAX_CHUNK_SIZE+4 bytes accumulated
chunkSize= self.txScriptRemaining if self.txScriptRemaining<self.CHUNK_SIZE else self.CHUNK_SIZE
self.parse_byte(chunkSize);
self.txScriptRemaining-=chunkSize
if self.txScriptRemaining==0:
self.parse_byte(4); # sequence
self.txState= TxState.TX_PARSE_INPUT
#break
elif self.txState == TxState.TX_PARSE_OUTPUT:
# max 8+9 bytes accumulated
if self.txRemainingOutput==0:
self.parse_byte(4); #locktime
self.parse_byte(4); #sighash
self.txState= TxState.TX_END
#break
else:
self.parse_byte(8); # amount
self.txScriptRemaining= self.parse_var_int();
self.txState= TxState.TX_PARSE_OUTPUT_SCRIPT
self.txRemainingOutput-=1
self.txCurrentOutput+=1
#//break
elif self.txState == TxState.TX_PARSE_OUTPUT_SCRIPT:
#max MAX_CHUNK_SIZE bytes accumulated
chunkSize= self.txScriptRemaining if self.txScriptRemaining<self.CHUNK_SIZE else self.CHUNK_SIZE
self.parse_byte(chunkSize);
self.txScriptRemaining-=chunkSize
if self.txScriptRemaining==0:
self.txState= TxState.TX_PARSE_OUTPUT
elif self.txState == TxState.TX_END:
pass
# update hash
self.txDigest.update(self.txChunk)
if self.txState == TxState.TX_END:
self.singleHash= self.txDigest.digest()
self.txDigest= hashlib.sha256()
self.txDigest.update(self.singleHash)
self.doubleHash= self.txDigest.digest()
#print('[TxParser] TxParser: parse_transaction(): txOffset='+str(self.txOffset) + " txRemaining="+str(self.txRemaining) + "chunk="+self.txChunk.hex()) #debugSatochip
return self.txChunk
def parse_outputs(self):
self.txChunk=b''
if (self.txState == TxState.TX_START):
self.txRemainingOutput= self.parse_var_int()
self.txState= TxState.TX_PARSE_OUTPUT
if (self.txState == TxState.TX_PARSE_OUTPUT):
if self.txRemainingOutput==0:
self.txState == TxState.TX_END
amount=self.parse_byte(8); # amount
self.outAmounts.append(read_int64(amount, 0)) #[self.txCurrentOutput]= read_int64(amount, 0)
self.txScriptRemaining= self.parse_var_int();
self.outScripts.append(b'')#[self.txCurrentOutput]=b''
self.txState= TxState.TX_PARSE_OUTPUT_SCRIPT
self.txRemainingOutput-=1
self.txCurrentOutput+=1
elif self.txState == TxState.TX_PARSE_OUTPUT_SCRIPT:
#max MAX_CHUNK_SIZE bytes accumulated
chunkSize= self.txScriptRemaining if self.txScriptRemaining<self.CHUNK_SIZE else self.CHUNK_SIZE
self.outScripts[-1]+=self.parse_byte(chunkSize);
self.txScriptRemaining-=chunkSize
if self.txScriptRemaining==0:
self.txState= TxState.TX_PARSE_OUTPUT
elif self.txState == TxState.TX_END:
pass
# update hash
self.txDigest.update(self.txChunk)
if self.txState == TxState.TX_END:
self.singleHash= self.txDigest.digest()
self.txDigest= hashlib.sha256()
self.txDigest.update(self.singleHash)
self.doubleHash= self.txDigest.digest()
#print('[TxParser] TxParser: parse_transaction(): txOffset='+str(self.txOffset) + " txRemaining="+str(self.txRemaining) + "chunk="+self.txChunk.hex()) #debugSatochip
return self.txChunk
def parse_segwit_transaction(self):
self.txChunk=b''
if self.txState == TxState.TX_START:
self.version=self.parse_byte(4)
self.hashPrevouts= self.parse_byte(32)
self.hashSequence= self.parse_byte(32)
# parse outpoint
self.txOutHash= self.parse_byte(32)
self.txOutIndex=self.parse_byte(4)
self.txOutIndexLong= read_int32(self.txOutIndex, 0)
# scriptcode= varint+script
self.txScriptRemaining= self.parse_var_int()
self.txState= TxState.TX_PARSE_INPUT_SCRIPT
elif self.txState == TxState.TX_PARSE_INPUT_SCRIPT:
# max MAX_CHUNK_SIZE+4 bytes accumulated
chunkSize= self.txScriptRemaining if self.txScriptRemaining<self.CHUNK_SIZE else self.CHUNK_SIZE
self.inputScript+=self.parse_byte(chunkSize)
self.txScriptRemaining-=chunkSize
if self.txScriptRemaining==0:
self.txState= TxState.TX_PARSE_FINALIZE
elif self.txState == TxState.TX_PARSE_FINALIZE:
self.inputAmount=self.parse_byte(8)
self.inputAmountLong= read_int64(self.inputAmount, 0)
self.nSequence= self.parse_byte(4)
self.hashOutputs= self.parse_byte(32)
self.nLocktime= self.parse_byte(4)
self.nHashType=self.parse_byte(4)
self.txState= TxState.TX_END
elif self.txState == TxState.TX_END:
pass
# update hash
self.txDigest.update(self.txChunk)
if self.txState == TxState.TX_END:
self.singleHash= self.txDigest.digest()
self.txDigest= hashlib.sha256()
self.txDigest.update(self.singleHash)
self.doubleHash= self.txDigest.digest()
#print('[TxParser] TxParser: parse_transaction(): txOffset='+str(self.txOffset) + " txRemaining="+str(self.txRemaining) + "chunk="+self.txChunk.hex()) #debugSatochip
return self.txChunk
def parse_byte(self, length):
chunk= self.txData[self.txOffset:(self.txOffset+length)]
self.txChunk+=chunk
self.txOffset+=length
self.txRemaining-=length
#print('[TxParser] TxParser: parse_byte(): txOffset='+str(self.txOffset) + " txRemaining="+str(self.txRemaining) + "chunk="+self.txChunk.hex()) #debugSatochip
return chunk
def parse_var_int(self):
first = 0xFF & self.txData[self.txOffset]
val=0
le=0
if first < 253:
# 8 bits
val = first
le=1
elif first == 253:
# 16 bits
val = (0xFF & self.txData[self.txOffset+1]) | ((0xFF & self.txData[self.txOffset+2]) << 8)
le=3
elif first == 254:
# 32 bits
val = read_int32(self.txData, self.txOffset + 1)
le=5
else:
# 64 bits
val = read_int64(self.txData, self.txOffset + 1)
le=9
self.txChunk+=self.txData[self.txOffset:(self.txOffset+le)]
self.txOffset+=le
self.txRemaining-=le
return val
def read_int32(bytes, offset):
out=0
for i in range(4):
out|= (bytes[offset] & 0xff)<<(8*i)
offset+=1
return out
def read_int64(bytes, offset):
out=0
for i in range(8):
out|= (bytes[offset] & 0xff)<<(8*i)
offset+=1
return out