-
Notifications
You must be signed in to change notification settings - Fork 1
/
transactions_chain.py
579 lines (491 loc) · 23.1 KB
/
transactions_chain.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
import requests
import datetime
import threading
import Graph
STANDART_WHITE_LIST = ['coinexchange',
'NodeSBroker',
'NodeS',
'ducofaucet',
'exchange-bot',
'revox',
'wDUCO']
class Transaction:
def __init__(self,hash:str,raw_data:dict):
self.amount:float = raw_data['amount']
self.sender:str = raw_data['sender']
self.recipient:str = raw_data['recipient']
self.hash:str = hash
self.datetime:int = int(datetime.datetime.strptime(raw_data['datetime'],
"%d/%m/%Y %H:%M:%S").timestamp())
def __str__(self):
return f"[{self.datetime}]: {self.sender} -> {self.recipient} | {self.amount} | {self.hash}"
def __repr__(self):
return f"{self.sender} -> {self.recipient} | {self.amount}"
def __eq__(self,tr):
return self.hash == tr.hash
def __lt__(self,tr):
return self.datetime < tr.datetime
def __le__(self,tr):
return self.datetime <= tr.datetime
def __ge__(self,tr):
return self.datetime >= tr.datetime
def get_sender(self):
return self.sender
def get_recipient(self):
return self.recipient
class TrasnsactionsChain:
'''Main class for working with connected transactions'''
def __init__(self, root_username, transactions=[], sort = True):
self.transactions = transactions
self.sort = sort
self.root_username = root_username
if sort:
self._sort_transactions()
self.graph = None
def _binary_search(self,transaction:Transaction, start:int, end:int) -> int:
if start >= end:
if end<len(self.transactions) and self.transactions[end] > transaction:
return end
else:
return end+1
half_start = start + ((end-start)>>1)
if transaction < self.transactions[half_start]:
return self._binary_search(transaction, start, half_start-1)
elif transaction > self.transactions[half_start]:
return self._binary_search(transaction, half_start+1, end)
else:
return half_start
def _sort_transactions(self):
def sorting_criteria(element:Transaction):
return element.datetime
self.transactions.sort(key=sorting_criteria)
def _search_transaction_by_hash(self, hash:str) -> Transaction:
for transaction in self.transactions:
if transaction.hash == hash:
return transaction
def append_transaction(self, transaction:Transaction, ensure_no_copy=True):
if len(self.transactions) == 0:
self.transactions.append(transaction)
return
if self.sort:
index = self._binary_search(transaction,0,len(self.transactions))
if ensure_no_copy:
first_half = self.transactions[:index]
for tr in first_half[::-1]:
if tr.datetime != transaction.datetime:
break
if tr.hash == transaction.hash:
return
second_half = self.transactions[index:]
for tr in second_half:
if tr.datetime != transaction.datetime:
break
if tr.hash == transaction.hash:
return
self.transactions.insert(index,transaction)
else:
if self[transaction.hash] == None:
self.transactions.append(transaction)
def __getitem__(self, key):
if isinstance(key,int):
return self.transactions[key]
elif isinstance(key,str):
return self._search_transaction_by_hash(key)
def __str__(self):
to_return = ''
for transaction in self.transactions[:-1]:
to_return += str(transaction) + '\n'
try:
to_return += str(transactions[-1])
except:
pass
return to_return
def __repr__(self):
for transaction in self.transactions:
print(transaction.__repr__)
def search_transactions_by_recipient(self, username:str) -> list:
to_return = []
for transaction in self.transactions:
if transaction.recipient == username:
to_return.append(transaction)
return to_return
def search_transactions_by_sender(self, username:str) -> list:
to_return = []
for transaction in self.transactions:
if transaction.sender == username:
to_return.append(transaction)
return to_return
def get_top_senders(self, recipient_username = None, top = 10):
'''returns list[sender_username, amount_sent]'''
if recipient_username == None:
recipient_username = self.root_username
amounts = [] # [username,amount]
lookup_table = {} # username:index
transactions = self.search_transactions_by_recipient(recipient_username)
for transaction in transactions:
index = lookup_table.get(transaction.get_sender(),len(amounts))
if index == len(amounts):
amounts.append([transaction.get_sender(),0.0])
lookup_table[transaction.get_sender()] = index
amounts[index][1] += transaction.amount
def sorting_criteria(element):
return element[-1]
amounts.sort(reverse=True, key=sorting_criteria)
return amounts[:top]
def get_top_recipients(self, sender_username = None, top = 10):
'''returns list[recipient_username, amount_recieved_from_sender]'''
if sender_username == None:
sender_username = self.root_username
amounts = [] # [username,amount]
lookup_table = {} # username:index
transactions = self.search_transactions_by_sender(sender_username)
for transaction in transactions:
index = lookup_table.get(transaction.get_recipient(),len(amounts))
if index == len(amounts):
amounts.append([transaction.get_recipient(),0.0])
lookup_table[transaction.get_recipient()] = index
amounts[index][1] += transaction.amount
def sorting_criteria(element):
return element[-1]
amounts.sort(reverse=True, key=sorting_criteria)
return amounts[:top]
def search_one_way_senders(self, recipient=None) -> list:
if recipient == None:
recipient = self.root_username
recieved_transactions = self.search_transactions_by_recipient(recipient)
#sent_by_recipient_transactions = self.search_transactions_by_sender(recipient)
processed_senders = {} # username:True
to_return = []
for transaction in recieved_transactions:
if transaction.get_sender() in processed_senders:
continue
else:
processed_senders[transaction.get_sender()] = True
only_one_recipient = 1
only_sending = 1
same_recipient_sender = 1
recieved_by_sender_transactions = self.search_transactions_by_recipient(transaction.get_sender())
if len(recieved_by_sender_transactions) > 0:
only_sending = 0
count_fails = 1
count_passes = 1
for recieved_transaction in recieved_by_sender_transactions:
if recieved_transaction.get_sender() == recipient:
count_fails += 1
else:
count_passes += 1
#same_recipient_sender = count_fails//count_passes
if count_fails >= count_passes:
same_recipient_sender = 1
count_fails = 1
count_passes = 1
sent_transactions = self.search_transactions_by_sender(transaction.get_sender())
for sent_transaction in sent_transactions:
if sent_transaction.get_recipient() != recipient:
count_passes += 1
else:
count_fails += 1
#only_one_recipient = count_fails//count_passes
if count_fails >= count_passes:
only_one_recipient = 1
if only_one_recipient + only_sending + same_recipient_sender >= 2:
to_return.append(transaction.get_sender())
return to_return
def search_one_way_recipients(self, sender = None) -> list:
if sender == None:
sender = self.root_username
sent_by_recipient_transactions = self.search_transactions_by_sender(sender)
processed_recipients = {} # username:True
to_return = []
for transaction in sent_by_recipient_transactions:
if transaction.get_recipient() in processed_recipients:
continue
else:
processed_recipients[transaction.get_recipient()] = True
only_one_recipient = 1
only_recieving = 1
same_recipient_sender = 1
sent_by_reciever_transactions = self.search_transactions_by_sender(transaction.get_recipient())
if len(sent_by_reciever_transactions) > 0:
only_recieving = 0
recieved_by_reciever_transactions = self.search_transactions_by_recipient(transaction.get_recipient())
count_fails = 1
count_passes = 1
for recieved_transaction in recieved_by_reciever_transactions:
if recieved_transaction.get_sender() == sender:
count_fails += 1
else:
count_passes += 1
#same_recipient_sender = count_fails//count_passes
if count_fails >= count_passes:
same_recipient_sender = 1
count_fails = 1
count_passes = 1
sent_transactions = self.search_transactions_by_sender(transaction.get_sender())
for sent_transaction in sent_transactions:
if sent_transaction.get_recipient() != sender:
count_passes += 1
else:
count_fails += 1
#only_one_recipient = count_fails//count_passes
if count_fails >= count_passes:
only_one_recipient = 1
if only_one_recipient + only_recieving + same_recipient_sender >= 2:
to_return.append(transaction.get_recipient())
return to_return
def total_recieved(self, username=None) -> float:
if username == None:
username = self.root_username
transactions = self.search_transactions_by_recipient(username)
to_return = 0.0
for transaction in transactions:
to_return += transaction.amount
return to_return
def total_sent(self, username=None) -> float:
if username == None:
username = self.root_username
transactions = self.search_transactions_by_sender(username)
to_return = 0.0
for transaction in transactions:
to_return += transaction.amount
return to_return
def is_suspicious(self, username = None) -> bool:
if username == None:
username = self.root_username
return self.total_recieved(username) > self.total_sent(username)
'''GRAPH FUNCTIONS'''
def get_nodes(self) -> list:
unique_usernames = {}
for transaction in self.transactions:
if transaction.get_sender() not in unique_usernames:
unique_usernames[transaction.get_sender()] = True
if transaction.get_recipient() not in unique_usernames:
unique_usernames[transaction.get_recipient()] = True
return list(unique_usernames.keys())
def create_graph(self):
'''
0 - no connection
1 - sender
2 - reciever
3 - all connections
'''
nodes = self.get_nodes()
nodes_index_lookup_table = {}
nodes_amount = len(nodes)
connection_matrix = []
weights = []
for i in range(nodes_amount):
connection_matrix.append([0]*nodes_amount)
weights.append([0]*nodes_amount)
nodes_index_lookup_table[nodes[i]] = i
for transaction in self.transactions:
sender_index = nodes_index_lookup_table[transaction.get_sender()]
recipient_index = nodes_index_lookup_table[transaction.get_recipient()]
if connection_matrix[sender_index][recipient_index] == 0:
connection_matrix[sender_index][recipient_index] = 1
elif connection_matrix[sender_index][recipient_index] == 2:
connection_matrix[sender_index][recipient_index] = 3
if connection_matrix[recipient_index][sender_index] == 0:
connection_matrix[recipient_index][sender_index] = 2
elif connection_matrix[recipient_index][sender_index] == 1:
connection_matrix[recipient_index][sender_index] = 3
weights[sender_index][recipient_index] += 1
weights[recipient_index][sender_index] += 1
self.graph = Graph.Graph(connection_matrix,nodes,weights)
return self.graph
def get_transactions(username:str) -> list:
'''gets transactions for 1 user'''
while True:
try:
transactions_json = requests.get(
f"https://server.duinocoin.com/user_transactions/{username}").json()
break
except Exception as e:
pass
to_return = []
for transaction in transactions_json['result']:
hash = transaction['hash']
to_return.append(Transaction(hash,transaction))
return to_return
def _get_transactions_threads_handler(username:str, buffer:list):
results = get_transactions(username)
for result in results:
buffer.append(result)
def _get_transactions_threads_master(usernames:list) -> list:
buffer = []
threads_pool = []
for username in usernames:
threads_pool.append(threading.Thread(target = _get_transactions_threads_handler,
name = f'{username} getter',
args = (username,buffer)))
for thread in threads_pool:
thread.start()
for thread in threads_pool:
thread.join()
return buffer
def trace_transactions(username:str,
white_list=[],
use_threads = True,
max_bunch = 10) -> TrasnsactionsChain:
'''
traces all transactions for username and all related transactions
'''
usernames_to_skip = {}
for username_to_skip in white_list:
usernames_to_skip[username_to_skip] = True
transactions_chain = TrasnsactionsChain(username)
processed_usernames = {} # username:True
usernames_to_process = [username]
if not use_threads:
while len(usernames_to_process) > 0:
username = usernames_to_process.pop(0)
transactions = get_transactions(username)
processed_usernames[username] = True
for transaction in transactions:
if transaction.get_sender() not in processed_usernames\
and transaction.get_sender() not in usernames_to_process\
and transaction.get_sender() not in usernames_to_skip:
usernames_to_process.append(transaction.get_sender())
elif transaction.get_recipient() not in processed_usernames\
and transaction.get_recipient() not in usernames_to_process\
and transaction.get_recipient() not in usernames_to_skip:
usernames_to_process.append(transaction.get_recipient())
transactions_chain.append_transaction(transaction)
else:
while len(usernames_to_process) > 0:
if len(usernames_to_process) == 0:
username = usernames_to_process.pop(0)
transactions = get_transactions(username)
processed_usernames[username] = True
elif max_bunch == -1 or len(usernames_to_process) < max_bunch:
transactions = _get_transactions_threads_master(usernames_to_process)
for username in usernames_to_process:
processed_usernames[username] = True
usernames_to_process = []
else:
usernames = usernames_to_process[:max_bunch]
usernames_to_process = usernames_to_process[max_bunch:]
transactions = _get_transactions_threads_master(usernames)
for username in usernames:
processed_usernames[username] = True
for transaction in transactions:
if transaction.get_sender() not in processed_usernames\
and transaction.get_sender() not in usernames_to_process\
and transaction.get_sender() not in usernames_to_skip:
usernames_to_process.append(transaction.get_sender())
elif transaction.get_recipient() not in processed_usernames\
and transaction.get_recipient() not in usernames_to_process\
and transaction.get_recipient() not in usernames_to_skip:
usernames_to_process.append(transaction.get_recipient())
transactions_chain.append_transaction(transaction)
return transactions_chain
def total_recieved(username:str) -> float:
to_return = 0.0
transactions = get_transactions(username)
for transaction in transactions:
if transaction.get_recipient() == username:
to_return += transaction.amount
return to_return
def total_sent(username:str) -> float:
to_return = 0.0
transactions = get_transactions(username)
for transaction in transactions:
if transaction.get_sender() == username:
to_return += transaction.amount
return to_return
def determine_main_account(transactions:TrasnsactionsChain,
suspicious_accounts:list):
'''returns list with deterined main accounts'''
to_return = []
max_amount_transactions = 0
max_amount_duco = 0
for sus in suspicious_accounts:
suspicious_transactions = transactions.search_transactions_by_recipient(sus)
suspicious_amount = transactions.total_recieved()
if len(to_return) > 0:
if (len(suspicious_transactions) > max_amount_transactions\
and suspicious_amount > max_amount_duco)\
or (len(suspicious_transactions) == max_amount_transactions\
and suspicious_amount > max_amount_duco)\
or (len(suspicious_transactions) > max_amount_transactions\
and suspicious_amount == max_amount_duco):
to_return = [sus]
max_amount_transactions = len(suspicious_transactions)
max_amount_duco = suspicious_amount
elif len(suspicious_transactions) == max_amount_transactions\
and suspicious_amount == max_amount_duco:
to_return.append(sus)
else:
to_return.append(sus)
max_amount_transactions = len(suspicious_transactions)
return to_return
def detect_suspicious_accounts(username:str,
white_list=[],
transactions:list=None,
**kwargs) -> tuple:
'''returns tuple(sus_accounts:list, main_accounts:list)'''
if transactions == None:
transactions = trace_transactions(username,white_list,kwargs)
processed_usernames = {}
sus_usernames = {username:True}
sus_accounts = transactions.search_one_way_senders()
for sus in sus_accounts:
if sus not in sus_usernames\
and sus not in white_list:
sus_usernames[sus] = True
sus_accounts = transactions.search_one_way_recipients()
for sus in sus_accounts:
if sus not in sus_usernames\
and sus not in white_list:
sus_usernames[sus] = True
processed_usernames[username] = True
for transaction in transactions:
if transaction.get_sender() not in processed_usernames:
sus_accounts = transactions.search_one_way_senders(transaction.get_sender())
for sus in sus_accounts:
if sus not in sus_usernames\
and sus not in white_list:
sus_usernames[sus] = True
sus_accounts = transactions.search_one_way_recipients(transaction.get_sender())
for sus in sus_accounts:
if sus not in sus_usernames\
and sus not in white_list:
sus_usernames[sus] = True
processed_usernames[transaction.get_sender()] = True
if transaction.get_recipient() not in processed_usernames:
sus_accounts = transactions.search_one_way_senders(transaction.get_recipient())
for sus in sus_accounts:
if sus not in sus_usernames\
and sus not in white_list:
sus_usernames[sus] = True
processed_usernames[transaction.get_recipient()] = True
sus_usernames = list(sus_usernames.keys())
main_account = determine_main_account(transactions,sus_usernames)
return sus_usernames, main_account
if __name__ == '__main__':
username = 'dianaT23s'
transactions = trace_transactions(username,
white_list=STANDART_WHITE_LIST,
max_bunch=2)
file = open('output.txt','w',encoding='utf-8')
file.write(str(transactions))
file.close()
print('Senders:')
print(transactions.get_top_senders(username))
print()
print('Recievers:')
print(transactions.get_top_recipients(username))
#print(total_recieved(username))
#print(total_sent(username))
#print(transactions.search_one_way_senders())
sus = detect_suspicious_accounts(username,
white_list=STANDART_WHITE_LIST,
transactions=transactions,
max_bunch=2)
print("SUS accounts:",sus[0])
print("Possible master accounts:",sus[1])
graph = transactions.create_graph()
rout = graph.find_shortest_sending_rout(username,'romiduX82i')
print('Found correlation rout:')
print(" -> ".join(rout))
input()