@@ -29,9 +29,33 @@ def merkle_root(lst): # https://gist.github.com/anonymous/7eb080a67398f648c1709e
29
29
lst .append (lst [- 1 ])
30
30
return merkle_root ([hash_pair (x ,y ) for x , y in zip (* [iter (lst )]* 2 )])
31
31
32
- dirA = './_blocks/' # Directory where blk*.dat files are stored
32
+ def read_bytes (file ,n ,byte_order = 'L' ):
33
+ data = file .read (n )
34
+ if byte_order == 'L' :
35
+ data = data [::- 1 ]
36
+ data = data .hex ().upper ()
37
+ return data
38
+
39
+ def read_varint (file ):
40
+ b = file .read (1 )
41
+ bInt = int (b .hex (),16 )
42
+ c = 0
43
+ data = ''
44
+ if bInt < 253 :
45
+ c = 1
46
+ data = b .hex ().upper ()
47
+ if bInt == 253 : c = 3
48
+ if bInt == 254 : c = 5
49
+ if bInt == 255 : c = 9
50
+ for j in range (1 ,c ):
51
+ b = f .read (1 )
52
+ b = b .hex ().upper ()
53
+ data = b + data
54
+ return data
55
+
56
+ dirA = 'e:/_blocks/' # Directory where blk*.dat files are stored
33
57
#dirA = sys.argv[1]
34
- dirB = '. /_result/' # Directory where to save parsing results
58
+ dirB = 'e: /_result/' # Directory where to save parsing results
35
59
#dirA = sys.argv[2]
36
60
37
61
fList = os .listdir (dirA )
@@ -50,113 +74,54 @@ def merkle_root(lst): # https://gist.github.com/anonymous/7eb080a67398f648c1709e
50
74
tmpHex = ''
51
75
fSize = os .path .getsize (t )
52
76
while f .tell () != fSize :
53
- for j in range (4 ):
54
- b = f .read (1 )
55
- b = b .hex ().upper ()
56
- tmpHex = b + tmpHex
57
- tmpHex = ''
58
- for j in range (4 ):
59
- b = f .read (1 )
60
- b = b .hex ().upper ()
61
- tmpHex = b + tmpHex
77
+ tmpHex = read_bytes (f ,4 )
78
+ resList .append ('Magic number = ' + tmpHex )
79
+ tmpHex = read_bytes (f ,4 )
62
80
resList .append ('Block size = ' + tmpHex )
63
- tmpHex = ''
64
81
tmpPos3 = f .tell ()
65
- while f .tell () != tmpPos3 + 80 :
66
- b = f .read (1 )
67
- b = b .hex ().upper ()
68
- tmpHex = tmpHex + b
82
+ tmpHex = read_bytes (f ,80 ,'B' )
69
83
tmpHex = bytes .fromhex (tmpHex )
70
84
tmpHex = hashlib .new ('sha256' , tmpHex ).digest ()
71
85
tmpHex = hashlib .new ('sha256' , tmpHex ).digest ()
72
- tmpHex = tmpHex .hex ()
73
- tmpHex = tmpHex .upper ()
74
- tmpHex = reverse (tmpHex )
86
+ tmpHex = tmpHex [::- 1 ]
87
+ tmpHex = tmpHex .hex ().upper ()
75
88
resList .append ('SHA256 hash of the current block hash = ' + tmpHex )
76
89
f .seek (tmpPos3 ,0 )
77
- tmpHex = ''
78
- for j in range (4 ):
79
- b = f .read (1 )
80
- b = b .hex ().upper ()
81
- tmpHex = b + tmpHex
90
+ tmpHex = read_bytes (f ,4 )
82
91
resList .append ('Version number = ' + tmpHex )
83
- tmpHex = ''
84
- for j in range (32 ):
85
- b = f .read (1 )
86
- b = b .hex ().upper ()
87
- tmpHex = b + tmpHex
92
+ tmpHex = read_bytes (f ,32 )
88
93
resList .append ('SHA256 hash of the previous block hash = ' + tmpHex )
89
- tmpHex = ''
90
- for j in range (32 ):
91
- b = f .read (1 )
92
- b = b .hex ().upper ()
93
- tmpHex = b + tmpHex
94
+ tmpHex = read_bytes (f ,32 )
94
95
resList .append ('MerkleRoot hash = ' + tmpHex )
95
96
MerkleRoot = tmpHex
96
- tmpHex = ''
97
- for j in range (4 ):
98
- b = f .read (1 )
99
- b = b .hex ().upper ()
100
- tmpHex = b + tmpHex
101
- resList .append ('Time stamp > ' + tmpHex )
102
- tmpHex = ''
103
- for j in range (4 ):
104
- b = f .read (1 )
105
- b = b .hex ().upper ()
106
- tmpHex = b + tmpHex
97
+ tmpHex = read_bytes (f ,4 )
98
+ resList .append ('Time stamp = ' + tmpHex )
99
+ tmpHex = read_bytes (f ,4 )
107
100
resList .append ('Difficulty = ' + tmpHex )
108
- tmpHex = ''
109
- for j in range (4 ):
110
- b = f .read (1 )
111
- b = b .hex ().upper ()
112
- tmpHex = b + tmpHex
113
- resList .append ('Random number > ' + tmpHex )
114
- tmpHex = ''
115
- b = f .read (1 )
116
- bInt = int (b .hex (),16 )
117
- c = 0
118
- if bInt < 253 :
119
- c = 1
120
- tmpHex = b .hex ().upper ()
121
- if bInt == 253 : c = 3
122
- if bInt == 254 : c = 5
123
- if bInt == 255 : c = 9
124
- for j in range (1 ,c ):
125
- b = f .read (1 )
126
- b = b .hex ().upper ()
127
- tmpHex = b + tmpHex
101
+ tmpHex = read_bytes (f ,4 )
102
+ resList .append ('Random number = ' + tmpHex )
103
+ tmpHex = read_varint (f )
128
104
txCount = int (tmpHex ,16 )
129
105
resList .append ('Transactions count = ' + str (txCount ))
130
106
resList .append ('' )
131
- tmpHex = ''
132
- tmpPos1 = 0
133
- tmpPos2 = 0
134
- RawTX = ''
135
- tx_hashes = []
107
+ tmpHex = '' ; RawTX = '' ; tx_hashes = []
136
108
for k in range (txCount ):
137
- tmpPos1 = f .tell ()
138
- for j in range (4 ):
139
- b = f .read (1 )
140
- b = b .hex ().upper ()
141
- tmpHex = b + tmpHex
142
- resList .append ('transactionVersionNumber = ' + tmpHex )
109
+ tmpHex = read_bytes (f ,4 )
110
+ resList .append ('TX version number = ' + tmpHex )
143
111
RawTX = reverse (tmpHex )
144
112
tmpHex = ''
113
+ Witness = False
145
114
b = f .read (1 )
146
115
tmpB = b .hex ().upper ()
147
116
bInt = int (b .hex (),16 )
148
- Witness = False
149
117
if bInt == 0 :
150
118
tmpB = ''
151
- c = 0
152
- c = f .read (1 )
153
- bInt = int (c .hex (),16 )
119
+ f .seek (1 ,1 )
154
120
c = 0
155
121
c = f .read (1 )
156
122
bInt = int (c .hex (),16 )
157
123
tmpB = c .hex ().upper ()
158
124
Witness = True
159
- resList .append ('Witness activated >>' )
160
125
c = 0
161
126
if bInt < 253 :
162
127
c = 1
@@ -173,19 +138,11 @@ def merkle_root(lst): # https://gist.github.com/anonymous/7eb080a67398f648c1709e
173
138
resList .append ('Inputs count = ' + tmpHex )
174
139
tmpHex = tmpHex + tmpB
175
140
RawTX = RawTX + reverse (tmpHex )
176
- tmpHex = ''
177
141
for m in range (inCount ):
178
- for j in range (32 ):
179
- b = f .read (1 )
180
- b = b .hex ().upper ()
181
- tmpHex = b + tmpHex
142
+ tmpHex = read_bytes (f ,32 )
182
143
resList .append ('TX from hash = ' + tmpHex )
183
144
RawTX = RawTX + reverse (tmpHex )
184
- tmpHex = ''
185
- for j in range (4 ):
186
- b = f .read (1 )
187
- b = b .hex ().upper ()
188
- tmpHex = b + tmpHex
145
+ tmpHex = read_bytes (f ,4 )
189
146
resList .append ('N output = ' + tmpHex )
190
147
RawTX = RawTX + reverse (tmpHex )
191
148
tmpHex = ''
@@ -207,19 +164,11 @@ def merkle_root(lst): # https://gist.github.com/anonymous/7eb080a67398f648c1709e
207
164
scriptLength = int (tmpHex ,16 )
208
165
tmpHex = tmpHex + tmpB
209
166
RawTX = RawTX + reverse (tmpHex )
210
- tmpHex = ''
211
- for j in range (scriptLength ):
212
- b = f .read (1 )
213
- b = b .hex ().upper ()
214
- tmpHex = tmpHex + b
167
+ tmpHex = read_bytes (f ,scriptLength ,'B' )
215
168
resList .append ('Input script = ' + tmpHex )
216
169
RawTX = RawTX + tmpHex
217
- tmpHex = ''
218
- for j in range (4 ):
219
- b = f .read (1 )
220
- b = b .hex ().upper ()
221
- tmpHex = tmpHex + b
222
- resList .append ('sequenceNumber = ' + tmpHex )
170
+ tmpHex = read_bytes (f ,4 ,'B' )
171
+ resList .append ('Sequence number = ' + tmpHex )
223
172
RawTX = RawTX + tmpHex
224
173
tmpHex = ''
225
174
b = f .read (1 )
@@ -241,12 +190,8 @@ def merkle_root(lst): # https://gist.github.com/anonymous/7eb080a67398f648c1709e
241
190
tmpHex = tmpHex + tmpB
242
191
resList .append ('Outputs count = ' + str (outputCount ))
243
192
RawTX = RawTX + reverse (tmpHex )
244
- tmpHex = ''
245
193
for m in range (outputCount ):
246
- for j in range (8 ):
247
- b = f .read (1 )
248
- b = b .hex ().upper ()
249
- tmpHex = b + tmpHex
194
+ tmpHex = read_bytes (f ,8 )
250
195
Value = tmpHex
251
196
RawTX = RawTX + reverse (tmpHex )
252
197
tmpHex = ''
@@ -268,91 +213,41 @@ def merkle_root(lst): # https://gist.github.com/anonymous/7eb080a67398f648c1709e
268
213
scriptLength = int (tmpHex ,16 )
269
214
tmpHex = tmpHex + tmpB
270
215
RawTX = RawTX + reverse (tmpHex )
271
- tmpHex = ''
272
- for j in range (scriptLength ):
273
- b = f .read (1 )
274
- b = b .hex ().upper ()
275
- tmpHex = tmpHex + b
216
+ tmpHex = read_bytes (f ,scriptLength ,'B' )
276
217
resList .append ('Value = ' + Value )
277
218
resList .append ('Output script = ' + tmpHex )
278
219
RawTX = RawTX + tmpHex
279
220
tmpHex = ''
280
221
if Witness == True :
281
222
for m in range (inCount ):
282
- tmpHex = ''
283
- b = f .read (1 )
284
- bInt = int (b .hex (),16 )
285
- c = 0
286
- if bInt < 253 :
287
- c = 1
288
- tmpHex = b .hex ().upper ()
289
- if bInt == 253 : c = 3
290
- if bInt == 254 : c = 5
291
- if bInt == 255 : c = 9
292
- for j in range (1 ,c ):
293
- b = f .read (1 )
294
- b = b .hex ().upper ()
295
- tmpHex = b + tmpHex
223
+ tmpHex = read_varint (f )
296
224
WitnessLength = int (tmpHex ,16 )
297
- tmpHex = ''
298
225
for j in range (WitnessLength ):
299
- tmpHex = ''
300
- b = f .read (1 )
301
- bInt = int (b .hex (),16 )
302
- c = 0
303
- if bInt < 253 :
304
- c = 1
305
- tmpHex = b .hex ().upper ()
306
- if bInt == 253 : c = 3
307
- if bInt == 254 : c = 5
308
- if bInt == 255 : c = 9
309
- for j in range (1 ,c ):
310
- b = f .read (1 )
311
- b = b .hex ().upper ()
312
- tmpHex = b + tmpHex
226
+ tmpHex = read_varint (f )
313
227
WitnessItemLength = int (tmpHex ,16 )
314
- tmpHex = ''
315
- for p in range (WitnessItemLength ):
316
- b = f .read (1 )
317
- b = b .hex ().upper ()
318
- tmpHex = b + tmpHex
228
+ tmpHex = read_bytes (f ,WitnessItemLength )
319
229
resList .append ('Witness ' + str (m ) + ' ' + str (j ) + ' ' + str (WitnessItemLength ) + ' ' + tmpHex )
320
230
tmpHex = ''
321
231
Witness = False
322
- for j in range (4 ):
323
- b = f .read (1 )
324
- b = b .hex ().upper ()
325
- tmpHex = b + tmpHex
232
+ tmpHex = read_bytes (f ,4 )
326
233
resList .append ('Lock time = ' + tmpHex )
327
234
RawTX = RawTX + reverse (tmpHex )
328
- tmpHex = ''
329
235
tmpHex = RawTX
330
236
tmpHex = bytes .fromhex (tmpHex )
331
237
tmpHex = hashlib .new ('sha256' , tmpHex ).digest ()
332
238
tmpHex = hashlib .new ('sha256' , tmpHex ).digest ()
333
- tmpHex = tmpHex .hex ()
334
- tmpHex = tmpHex .upper ()
335
- tmpHex = reverse (tmpHex )
239
+ tmpHex = tmpHex [::- 1 ]
240
+ tmpHex = tmpHex .hex ().upper ()
336
241
resList .append ('TX hash = ' + tmpHex )
337
242
tx_hashes .append (tmpHex )
338
- tmpHex = ''
339
- resList .append ('' )
340
- RawTX = ''
243
+ resList .append ('' ); tmpHex = '' ; RawTX = ''
341
244
a += 1
342
245
tx_hashes = [bytes .fromhex (h ) for h in tx_hashes ]
343
246
tmpHex = merkle_root (tx_hashes ).hex ().upper ()
344
247
if tmpHex != MerkleRoot :
345
248
print ('Merkle roots does not match! >' ,MerkleRoot ,tmpHex )
346
- tmpHex = ''
347
249
f .close ()
348
250
f = open (dirB + nameRes ,'w' )
349
251
for j in resList :
350
252
f .write (j + '\n ' )
351
253
f .close ()
352
- nameSrc = ''
353
- nameRes = ''
354
- dirA = ''
355
- dirB = ''
356
- tmpC = ''
357
- resList = []
358
- fList = []
0 commit comments