forked from hclivess/Bismuth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexchange.py
162 lines (128 loc) · 3.98 KB
/
exchange.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
import socks, connections, time, sys, json
from Crypto.PublicKey import RSA
#define private key
key = RSA.importKey(open('/privkey.der').read())
private_key_readable = str(key.exportKey().decode("utf-8")) #private key must be decoded
exchange_address = "MAIN_EXCHANGE_ADDRESS"
#print ('Number of arguments:', len(sys.argv), 'arguments.')
#print ('Argument List:', str(sys.argv))
try:
command = sys.argv[1]
except:
pass
try:
arg1 = sys.argv[2]
except:
pass
try:
arg2 = sys.argv[3]
except:
pass
try:
arg3 = sys.argv[4]
except:
pass
s = socks.socksocket()
s.connect(("127.0.0.1", 5658))
def getbalance(socket):
#get balance
connections.send(s, "balanceget", 10)
connections.send(s, exchange_address, 10)
balance_ledger = connections.receive(s, 10)
result = {
u"balance": balance_ledger[0]
}
print (json.dumps(result, indent=2))
#get balance
def getinfo(socket):
#get last block
connections.send(s, "blocklast", 10)
block_result = connections.receive(s, 10)
connections.send(s, "statusget", 10)
status_result = connections.receive(s, 10)
connections.send(s, "balanceget", 10)
connections.send(s, exchange_address, 10)
balance_result = connections.receive(s, 10)
connections.send(s, "diffget", 10)
diff_result = connections.receive(s, 10)
result = {
u"version": status_result[6],
u"protocolversion": 0,
u"walletversion": 0,
u"balance": balance_result[0],
u"blocks": block_result[0],
u"timeoffset": 0,
u"connections": status_result[0],
u"difficulty": diff_result[0],
u"errors": "",
u"consensus": status_result[5]
}
print (json.dumps(result, indent=2))
#get last hash
def gettransactions(socket):
#get all txs for an address
connections.send(s, "addlist", 10)
connections.send(s, exchange_address, 10)
address_tx_list = connections.receive(s, 10)
connections.send(s, "blocklast", 10)
block_result = connections.receive(s, 10)
connections.send(s, "statusget", 10)
status_result = connections.receive(s, 10)
result = []
if status_result[5] > 10:
for row in address_tx_list:
result.append({
u"address": row[11],
u"category": (row[3] == exchange_address) and "receive" or "send",
u"amount": row[4],
u"confirmations": (block_result[0] - row[0]),
u"blockhash": row[0],
u"blockindex": row[0],
u"blocktime": int(float(row[1])),
u"txid": row[5][:64],
u"time": int(float(row[1])),
u"timereceived": int(float(row[1]))
})
print(json.dumps(result, indent=2))
#get all txs for an address
def sendtransaction(socket, arg1, arg2, arg3):
#generate transaction
connections.send(s, "txsend", 10)
remote_tx_timestamp = '%.2f' % time.time()
remote_tx_privkey = private_key_readable
remote_tx_recipient = arg1
remote_tx_amount = arg2
remote_tx_keep = '0'
remote_tx_openfield = arg3
connections.send(s, (str(remote_tx_timestamp), str(remote_tx_privkey), str(remote_tx_recipient), str(remote_tx_amount), str(remote_tx_keep), str(remote_tx_openfield)), 10)
tx_id = connections.receive(s, 10)
result = {
u"txid": tx_id[:64]
}
print(json.dumps(result, indent=2))
#generate transaction
def validateaddress(socket, arg1):
#validateAddress
connections.send(s, "addvalidate", 10)
connections.send(s, arg1, 10)
validate_result = connections.receive(s, 10)
if validate_result == "invalid":
print (json.dumps({ u"isvalid": False }, indent=2))
else:
print (json.dumps({ u"isvalid": True }, indent=2))
#validateAddress
if command == "getbalance":
getbalance(s)
elif command == "getinfo":
getinfo(s)
elif command == "gettransactions":
gettransactions(s)
elif command == "sendtransaction":
try:
arg3
except:
arg3=""
sendtransaction(s, arg1, arg2, arg3)
elif command == "validateaddress":
validateaddress(s, arg1)
s.close()