-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalyseCoinbaseTransactions.py
executable file
·196 lines (155 loc) · 5.69 KB
/
analyseCoinbaseTransactions.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
# author: Eugenio CORTESI, Sofiane KIRATI
from __future__ import absolute_import, division, print_function, unicode_literals
import ssl
import hashlib
import bitcoin
from bitcoin.core import *
from bitcoin.messages import *
from bitcoin.wallet import CBitcoinAddress, CBitcoinSecret
from bitcoin.core import *
from bitcoin.rpc import *
from bitcoin.signature import *
from bitcoin.core.key import *
from bitcoin.wallet import CBitcoinAddress, CBitcoinSecret, P2PKHBitcoinAddress, CBitcoinAddressError
import binascii
import struct
import sys
import os
import time
import json
import pickle
import datetime
import bitcoin.core.script as script
from bitcoin.core.script import CScript, CScriptWitness, CScriptOp, OP_RETURN, OP_DUP, OP_HASH160, OP_EQUALVERIFY, OP_CHECKSIG, IsLowDERSignature
from bitcoin.core.scripteval import *
from bitcoin.core.serialize import *
from bitcoin import SelectParams
def convert_time(hexstamp):
return datetime.datetime.fromtimestamp(int(hexstamp)).strftime('%Y-%m-%d %H:%M:%S')
def boucle_outputs(transaction, receiverAddress, timeStamp, nameMP, hsh):
senderAddress = 'No Inputs, Newly Generated Coins'
value = (transaction.vout[0].nValue) * 0.00000001
if value != 0:
if nameMP != 'Other':
allCoinbaseTransactions['CoinbaseTransactions'].append({'Sender': senderAddress , 'receiver': receiverAddress, 'ammount': value, 'time': timeStamp, 'nameOfMP': nameMP, 'txHash': hsh})
if receiverAddress in minPoolsAddrs[nameMP]:
pass
else:
minPoolsAddrs[nameMP].append(receiverAddress)
else:
allOthers['CoinbaseTransactions'].append({'Sender': senderAddress , 'receiver': receiverAddress, 'ammount': value, 'time': timeStamp, 'nameOfMP': nameMP, 'txHash': hsh})
if receiverAddress in otherPoolsAdds:
pass
else:
otherPoolsAdds.append(receiverAddress)
def getMiningPool(transaction):
p = 'Other'
id = b2x(transaction.vin[0].scriptSig)
if '4d696e656420627920416e74506f6f6c' in id:
p = 'AntPool'
elif '706f6f6c696e2e636f6d' in id:
p = 'PoolinPool'
elif '4254432e434f4d' in id or '4254432e636f6d' in id or '42697464656572' in id:
p = 'BTCPool'
elif '48756f4269' in id or '48756f6269' in id:
p = 'HuobiPool'
elif '4632506f6f6c' in id:
p = 'F2Pool'
#else:
#print(id)
#ascii_string = id.decode("hex")
#print(ascii_string)
#print ('\n')
return p
def get_receiver_Address(transaction):
rec = ''
try:
scrPubKey = b2x(transaction.vout[0].scriptPubKey)
ScriptPubKey = script.CScript(x(scrPubKey))
rec = CBitcoinAddress.from_scriptPubKey(ScriptPubKey)
rec1 = str(rec)
except CBitcoinAddressError:
rec1 = "no Address found"
return(rec1)
blocksDirectory = sys.argv[1] #/home/lip6/.bitcoin/blocks
firstBlock = input('first')
lastBlock = input('last')
listofDirectories = []
a = os.listdir('./')
interval = []
for i in range(firstBlock, lastBlock+1):
dir = 'res_blk%05d.dat' %i
interval.append(dir)
for i in a:
for j in range(len(interval)):
if interval[j] == i:
listofDirectories.append(i)
listofDirectories.sort()
for directory in listofDirectories:
print('NEW DIRECTORY %s' %directory)
abs_dir_path = os.getcwd() + '/' + directory
numBlock = abs_dir_path[-9:]
fichier='blk'+numBlock
path = blocksDirectory + '/' + fichier
allCoinbaseTransactions = {}
allCoinbaseTransactions['CoinbaseTransactions'] = []
allOthers = {}
allOthers['CoinbaseTransactions'] = []
minPoolsAddrs = {'BTCPool' : [], 'PoolinPool' : [], 'AntPool' : [], 'F2Pool' : [], 'HuobiPool': []}
otherPoolsAdds = []
#get the zise of the file
sizeFile = os.path.getsize(path)
print("the size of the file is %s " % sizeFile)
#open the file to read with "r" option
monFichier1 = open(path,"r")
#we can do it by idx=0
idx = monFichier1.tell()
while idx < sizeFile:
#read 8 bytes which contain magicBytes an the size of the upComing Block
r1 = (ser_read(monFichier1,8))
idx = monFichier1.tell()
#read the header of the block
header = CBlockHeader().stream_deserialize(monFichier1)
#print("The header is ===> %s " % header)
idx = monFichier1.tell()
#get timestamp
hexstamp = header.nTime
timeStamp = convert_time(hexstamp)
# read the transaction count
txCount = VarIntSerializer.stream_deserialize(monFichier1)
for i in range(txCount):
idx = monFichier1.tell()
# read the transaction
transaction = CTransaction.stream_deserialize(monFichier1)
#print("The transaction is ===> %s " % transaction)
idx = monFichier1.tell()
#test if it's a new generation of coins
isBase = transaction.is_coinbase()
# coin base it means : new generation of coins
if isBase:
s = Serializable.serialize(transaction)
hsh = b2lx(transaction.GetTxid())
pool = getMiningPool(transaction)
if pool != 'Other':
receiverAddress = get_receiver_Address(transaction)
boucle_outputs(transaction, receiverAddress, timeStamp, pool, hsh)
else:
receiverAddress = get_receiver_Address(transaction)
boucle_outputs(transaction, receiverAddress, timeStamp, pool, hsh)
else:
pass
abs_file_path1 = os.getcwd() + '/' + directory + '/allCoinbaseTransa.txt'
abs_file_path2 = os.getcwd() + '/' + directory +'/minPoolAdd.txt'
abs_file_path3 = os.getcwd() + '/' + directory + '/allPoolsCoinbase.txt'
abs_file_path4 = os.getcwd() + '/' + directory + '/otherPoolsAdds.txt'
with open(abs_file_path1, 'w') as outfile:
json.dump(allCoinbaseTransactions, outfile)
with open(abs_file_path2, 'w') as outfile:
json.dump(minPoolsAddrs, outfile)
with open(abs_file_path3, 'w') as outfile:
json.dump(allOthers, outfile)
with open(abs_file_path4, 'w') as outfile:
json.dump(otherPoolsAdds, outfile)
nbrCoinBase = len(allCoinbaseTransactions['CoinbaseTransactions'])
print("the number of coinbase transaction %s " % nbrCoinBase)
monFichier1.close()