-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathutils.py
107 lines (90 loc) · 3.09 KB
/
utils.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
#!/usr/bin/python3
import sys
import requests
import json
from dataclasses import dataclass
@dataclass
class RingMember:
idx: int
tx_hash: str
version = '0.1'
ringct_start = 1220516
existing_ring_members = []
#UPDATE THIS TO WATCH FOR DESIRED OUTPUTS
outputs_to_watch = [00000000, 00000000, 00000000, 00000000, 00000000]
def get_transactions(tx_hashes):
PARAMS = {'txs_hashes' : tx_hashes, 'decode_as_json' : True}
transactions = call_rpc("get_transactions", PARAMS)
if 'txs' in transactions.json():
return transactions.json()['txs']
else:
return []
def get_idx(key_offset_index, key_offsets):
idx = 0
i = 0
while i <= key_offset_index:
idx += key_offsets[i]
i += 1
return idx
def find_any_ring_members(transactions, outputs_to_watch):
decoys = []
for t in transactions:
tx = json.loads(t['as_json'])
inputs = tx['vin']
for i in inputs:
input = i['key']
key_offsets = input['key_offsets']
o = 0
for o1 in key_offsets:
idx = get_idx(o, key_offsets)
o += 1
if idx in outputs_to_watch:
decoys += [RingMember(idx, t['tx_hash'])]
return decoys
def get_block(block_hash):
PARAMS = { "hash": block_hash }
block = call_json_rpc("get_block", PARAMS).json()
return block
def get_block_by_height(block_height):
PARAMS = { "height": block_height }
block = call_json_rpc("get_block", PARAMS).json()
return block
def get_height():
PARAMS = {}
height = call_rpc("get_height", PARAMS).json()
return height['height'] - 1
def call_rpc(method_name, params):
response = requests.post("http://localhost:18081/" + method_name, json = params)
return response
def call_json_rpc(method_name, params):
PARAMS = {'jsonrpc':'2.0', 'id':'0', 'method': method_name, 'params': params, 'decode_as_json' : True}
response = requests.post("http://localhost:18081/json_rpc", json = PARAMS)
return response
def record_any_transactions(block):
tx_hashes_json = json.loads(block['result']['json'])
tx_hashes = tx_hashes_json['tx_hashes']
transactions = get_transactions(tx_hashes)
ring_members = find_any_ring_members(transactions, outputs_to_watch)
for ring_member in ring_members:
maybe_record_ring_member(ring_member)
def maybe_record_ring_member(ring_member):
global existing_ring_members
found = False
member_string = ring_member.tx_hash+":"+ring_member.idx.__str__()
if member_string in existing_ring_members:
found = True
if not found:
existing_ring_members += [member_string]
with open('decoys.txt', 'a+') as f:
f.write(ring_member.idx.__str__() + ": " + ring_member.tx_hash+"\n")
def load_previous_txs():
temp_txs = []
txs = []
with open('decoys.txt', 'r') as f:
txs = f.readlines()
for line in txs:
tx_hash = line.split(": ")[1].rstrip()
idx = line.split(": ")[0].rstrip()
temp_txs += [tx_hash+":"+idx]
return temp_txs
existing_ring_members = load_previous_txs()