-
Notifications
You must be signed in to change notification settings - Fork 98
/
blockchain-parser.py
254 lines (246 loc) · 9.08 KB
/
blockchain-parser.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
# -*- coding: utf-8 -*-
#
# Blockchain parser
# Copyright (c) 2015-2023 Denis Leonov <[email protected]>
#
import os
import datetime
import hashlib
def reverse(input):
L = len(input)
if (L % 2) != 0:
return None
else:
Res = ''
L = L // 2
for i in range(L):
T = input[i*2] + input[i*2+1]
Res = T + Res
T = ''
return (Res);
def merkle_root(lst): # https://gist.github.com/anonymous/7eb080a67398f648c1709e41890f8c44
sha256d = lambda x: hashlib.sha256(hashlib.sha256(x).digest()).digest()
hash_pair = lambda x, y: sha256d(x[::-1] + y[::-1])[::-1]
if len(lst) == 1: return lst[0]
if len(lst) % 2 == 1:
lst.append(lst[-1])
return merkle_root([hash_pair(x,y) for x, y in zip(*[iter(lst)]*2)])
def read_bytes(file,n,byte_order = 'L'):
data = file.read(n)
if byte_order == 'L':
data = data[::-1]
data = data.hex().upper()
return data
def read_varint(file):
b = file.read(1)
bInt = int(b.hex(),16)
c = 0
data = ''
if bInt < 253:
c = 1
data = b.hex().upper()
if bInt == 253: c = 3
if bInt == 254: c = 5
if bInt == 255: c = 9
for j in range(1,c):
b = file.read(1)
b = b.hex().upper()
data = b + data
return data
dirA = './blocks/' # Directory where blk*.dat files are stored
#dirA = sys.argv[1]
dirB = './result/' # Directory where to save parsing results
#dirA = sys.argv[2]
fList = os.listdir(dirA)
fList = [x for x in fList if (x.endswith('.dat') and x.startswith('blk'))]
fList.sort()
for i in fList:
nameSrc = i
nameRes = nameSrc.replace('.dat','.txt')
resList = []
a = 0
t = dirA + nameSrc
resList.append('Start ' + t + ' in ' + str(datetime.datetime.now()))
print ('Start ' + t + ' in ' + str(datetime.datetime.now()))
f = open(t,'rb')
tmpHex = ''
fSize = os.path.getsize(t)
while f.tell() != fSize:
while tmpHex != 'D9B4BEF9': # it is for to skip zeroes in some blk files
tmpHex = read_bytes(f,4)
resList.append('Magic number = ' + tmpHex)
tmpHex = read_bytes(f,4)
resList.append('Block size = ' + tmpHex)
tmpPos3 = f.tell()
tmpHex = read_bytes(f,80,'B')
tmpHex = bytes.fromhex(tmpHex)
tmpHex = hashlib.new('sha256', tmpHex).digest()
tmpHex = hashlib.new('sha256', tmpHex).digest()
tmpHex = tmpHex[::-1]
tmpHex = tmpHex.hex().upper()
resList.append('SHA256 hash of the current block hash = ' + tmpHex)
f.seek(tmpPos3,0)
tmpHex = read_bytes(f,4)
resList.append('Version number = ' + tmpHex)
tmpHex = read_bytes(f,32)
resList.append('SHA256 hash of the previous block hash = ' + tmpHex)
tmpHex = read_bytes(f,32)
resList.append('MerkleRoot hash = ' + tmpHex)
MerkleRoot = tmpHex
tmpHex = read_bytes(f,4)
resList.append('Time stamp = ' + tmpHex)
tmpHex = read_bytes(f,4)
resList.append('Difficulty = ' + tmpHex)
tmpHex = read_bytes(f,4)
resList.append('Random number = ' + tmpHex)
tmpHex = read_varint(f)
txCount = int(tmpHex,16)
resList.append('Transactions count = ' + str(txCount))
resList.append('')
tmpHex = ''; RawTX = ''; tx_hashes = []
for k in range(txCount):
tmpHex = read_bytes(f,4)
resList.append('TX version number = ' + tmpHex)
RawTX = reverse(tmpHex)
tmpHex = ''
Witness = False
b = f.read(1)
tmpB = b.hex().upper()
bInt = int(b.hex(),16)
if bInt == 0:
tmpB = ''
f.seek(1,1)
c = 0
c = f.read(1)
bInt = int(c.hex(),16)
tmpB = c.hex().upper()
Witness = True
c = 0
if bInt < 253:
c = 1
tmpHex = hex(bInt)[2:].upper().zfill(2)
tmpB = ''
if bInt == 253: c = 3
if bInt == 254: c = 5
if bInt == 255: c = 9
for j in range(1,c):
b = f.read(1)
b = b.hex().upper()
tmpHex = b + tmpHex
inCount = int(tmpHex,16)
resList.append('Inputs count = ' + tmpHex)
tmpHex = tmpHex + tmpB
RawTX = RawTX + reverse(tmpHex)
for m in range(inCount):
tmpHex = read_bytes(f,32)
resList.append('TX from hash = ' + tmpHex)
RawTX = RawTX + reverse(tmpHex)
tmpHex = read_bytes(f,4)
resList.append('N output = ' + tmpHex)
RawTX = RawTX + reverse(tmpHex)
tmpHex = ''
b = f.read(1)
tmpB = b.hex().upper()
bInt = int(b.hex(),16)
c = 0
if bInt < 253:
c = 1
tmpHex = b.hex().upper()
tmpB = ''
if bInt == 253: c = 3
if bInt == 254: c = 5
if bInt == 255: c = 9
for j in range(1,c):
b = f.read(1)
b = b.hex().upper()
tmpHex = b + tmpHex
scriptLength = int(tmpHex,16)
tmpHex = tmpHex + tmpB
RawTX = RawTX + reverse(tmpHex)
tmpHex = read_bytes(f,scriptLength,'B')
resList.append('Input script = ' + tmpHex)
RawTX = RawTX + tmpHex
tmpHex = read_bytes(f,4,'B')
resList.append('Sequence number = ' + tmpHex)
RawTX = RawTX + tmpHex
tmpHex = ''
b = f.read(1)
tmpB = b.hex().upper()
bInt = int(b.hex(),16)
c = 0
if bInt < 253:
c = 1
tmpHex = b.hex().upper()
tmpB = ''
if bInt == 253: c = 3
if bInt == 254: c = 5
if bInt == 255: c = 9
for j in range(1,c):
b = f.read(1)
b = b.hex().upper()
tmpHex = b + tmpHex
outputCount = int(tmpHex,16)
tmpHex = tmpHex + tmpB
resList.append('Outputs count = ' + str(outputCount))
RawTX = RawTX + reverse(tmpHex)
for m in range(outputCount):
tmpHex = read_bytes(f,8)
Value = tmpHex
RawTX = RawTX + reverse(tmpHex)
tmpHex = ''
b = f.read(1)
tmpB = b.hex().upper()
bInt = int(b.hex(),16)
c = 0
if bInt < 253:
c = 1
tmpHex = b.hex().upper()
tmpB = ''
if bInt == 253: c = 3
if bInt == 254: c = 5
if bInt == 255: c = 9
for j in range(1,c):
b = f.read(1)
b = b.hex().upper()
tmpHex = b + tmpHex
scriptLength = int(tmpHex,16)
tmpHex = tmpHex + tmpB
RawTX = RawTX + reverse(tmpHex)
tmpHex = read_bytes(f,scriptLength,'B')
resList.append('Value = ' + Value)
resList.append('Output script = ' + tmpHex)
RawTX = RawTX + tmpHex
tmpHex = ''
if Witness == True:
for m in range(inCount):
tmpHex = read_varint(f)
WitnessLength = int(tmpHex,16)
for j in range(WitnessLength):
tmpHex = read_varint(f)
WitnessItemLength = int(tmpHex,16)
tmpHex = read_bytes(f,WitnessItemLength)
resList.append('Witness ' + str(m) + ' ' + str(j) + ' ' + str(WitnessItemLength) + ' ' + tmpHex)
tmpHex = ''
Witness = False
tmpHex = read_bytes(f,4)
resList.append('Lock time = ' + tmpHex)
RawTX = RawTX + reverse(tmpHex)
tmpHex = RawTX
tmpHex = bytes.fromhex(tmpHex)
tmpHex = hashlib.new('sha256', tmpHex).digest()
tmpHex = hashlib.new('sha256', tmpHex).digest()
tmpHex = tmpHex[::-1]
tmpHex = tmpHex.hex().upper()
resList.append('TX hash = ' + tmpHex)
tx_hashes.append(tmpHex)
resList.append(''); tmpHex = ''; RawTX = ''
a += 1
tx_hashes = [bytes.fromhex(h) for h in tx_hashes]
tmpHex = merkle_root(tx_hashes).hex().upper()
if tmpHex != MerkleRoot:
print ('Merkle roots does not match! >',MerkleRoot,tmpHex)
f.close()
f = open(dirB + nameRes,'w')
for j in resList:
f.write(j + '\n')
f.close()