Skip to content

Commit 30162ef

Browse files
authored
Update blockchain-parser.py
1 parent b0a1da0 commit 30162ef

File tree

1 file changed

+60
-165
lines changed

1 file changed

+60
-165
lines changed

blockchain-parser.py

Lines changed: 60 additions & 165 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,33 @@ def merkle_root(lst): # https://gist.github.com/anonymous/7eb080a67398f648c1709e
2929
lst.append(lst[-1])
3030
return merkle_root([hash_pair(x,y) for x, y in zip(*[iter(lst)]*2)])
3131

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
3357
#dirA = sys.argv[1]
34-
dirB = './_result/' # Directory where to save parsing results
58+
dirB = 'e:/_result/' # Directory where to save parsing results
3559
#dirA = sys.argv[2]
3660

3761
fList = os.listdir(dirA)
@@ -50,113 +74,54 @@ def merkle_root(lst): # https://gist.github.com/anonymous/7eb080a67398f648c1709e
5074
tmpHex = ''
5175
fSize = os.path.getsize(t)
5276
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)
6280
resList.append('Block size = ' + tmpHex)
63-
tmpHex = ''
6481
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')
6983
tmpHex = bytes.fromhex(tmpHex)
7084
tmpHex = hashlib.new('sha256', tmpHex).digest()
7185
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()
7588
resList.append('SHA256 hash of the current block hash = ' + tmpHex)
7689
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)
8291
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)
8893
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)
9495
resList.append('MerkleRoot hash = ' + tmpHex)
9596
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)
107100
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)
128104
txCount = int(tmpHex,16)
129105
resList.append('Transactions count = ' + str(txCount))
130106
resList.append('')
131-
tmpHex = ''
132-
tmpPos1 = 0
133-
tmpPos2 = 0
134-
RawTX = ''
135-
tx_hashes = []
107+
tmpHex = ''; RawTX = ''; tx_hashes = []
136108
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)
143111
RawTX = reverse(tmpHex)
144112
tmpHex = ''
113+
Witness = False
145114
b = f.read(1)
146115
tmpB = b.hex().upper()
147116
bInt = int(b.hex(),16)
148-
Witness = False
149117
if bInt == 0:
150118
tmpB = ''
151-
c = 0
152-
c = f.read(1)
153-
bInt = int(c.hex(),16)
119+
f.seek(1,1)
154120
c = 0
155121
c = f.read(1)
156122
bInt = int(c.hex(),16)
157123
tmpB = c.hex().upper()
158124
Witness = True
159-
resList.append('Witness activated >>')
160125
c = 0
161126
if bInt < 253:
162127
c = 1
@@ -173,19 +138,11 @@ def merkle_root(lst): # https://gist.github.com/anonymous/7eb080a67398f648c1709e
173138
resList.append('Inputs count = ' + tmpHex)
174139
tmpHex = tmpHex + tmpB
175140
RawTX = RawTX + reverse(tmpHex)
176-
tmpHex = ''
177141
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)
182143
resList.append('TX from hash = ' + tmpHex)
183144
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)
189146
resList.append('N output = ' + tmpHex)
190147
RawTX = RawTX + reverse(tmpHex)
191148
tmpHex = ''
@@ -207,19 +164,11 @@ def merkle_root(lst): # https://gist.github.com/anonymous/7eb080a67398f648c1709e
207164
scriptLength = int(tmpHex,16)
208165
tmpHex = tmpHex + tmpB
209166
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')
215168
resList.append('Input script = ' + tmpHex)
216169
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)
223172
RawTX = RawTX + tmpHex
224173
tmpHex = ''
225174
b = f.read(1)
@@ -241,12 +190,8 @@ def merkle_root(lst): # https://gist.github.com/anonymous/7eb080a67398f648c1709e
241190
tmpHex = tmpHex + tmpB
242191
resList.append('Outputs count = ' + str(outputCount))
243192
RawTX = RawTX + reverse(tmpHex)
244-
tmpHex = ''
245193
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)
250195
Value = tmpHex
251196
RawTX = RawTX + reverse(tmpHex)
252197
tmpHex = ''
@@ -268,91 +213,41 @@ def merkle_root(lst): # https://gist.github.com/anonymous/7eb080a67398f648c1709e
268213
scriptLength = int(tmpHex,16)
269214
tmpHex = tmpHex + tmpB
270215
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')
276217
resList.append('Value = ' + Value)
277218
resList.append('Output script = ' + tmpHex)
278219
RawTX = RawTX + tmpHex
279220
tmpHex = ''
280221
if Witness == True:
281222
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)
296224
WitnessLength = int(tmpHex,16)
297-
tmpHex = ''
298225
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)
313227
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)
319229
resList.append('Witness ' + str(m) + ' ' + str(j) + ' ' + str(WitnessItemLength) + ' ' + tmpHex)
320230
tmpHex = ''
321231
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)
326233
resList.append('Lock time = ' + tmpHex)
327234
RawTX = RawTX + reverse(tmpHex)
328-
tmpHex = ''
329235
tmpHex = RawTX
330236
tmpHex = bytes.fromhex(tmpHex)
331237
tmpHex = hashlib.new('sha256', tmpHex).digest()
332238
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()
336241
resList.append('TX hash = ' + tmpHex)
337242
tx_hashes.append(tmpHex)
338-
tmpHex = ''
339-
resList.append('')
340-
RawTX = ''
243+
resList.append(''); tmpHex = ''; RawTX = ''
341244
a += 1
342245
tx_hashes = [bytes.fromhex(h) for h in tx_hashes]
343246
tmpHex = merkle_root(tx_hashes).hex().upper()
344247
if tmpHex != MerkleRoot:
345248
print ('Merkle roots does not match! >',MerkleRoot,tmpHex)
346-
tmpHex = ''
347249
f.close()
348250
f = open(dirB + nameRes,'w')
349251
for j in resList:
350252
f.write(j + '\n')
351253
f.close()
352-
nameSrc = ''
353-
nameRes = ''
354-
dirA= ''
355-
dirB = ''
356-
tmpC = ''
357-
resList = []
358-
fList = []

0 commit comments

Comments
 (0)