-
Notifications
You must be signed in to change notification settings - Fork 1
/
wallet.py
368 lines (273 loc) · 11.5 KB
/
wallet.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
import asyncio
import websockets
import random
import json
from aioconsole import ainput
import os
import traceback
import logging
from Crypto.PublicKey import ECC
from Crypto.Hash import SHA256
from Crypto.Hash import BLAKE2b
from Crypto.Signature import DSS
from nacl.signing import SigningKey
import base64
import zlib
try:
print(os.environ["debug"])
logging.basicConfig(level=logging.DEBUG)
except KeyError:
logging.basicConfig(level=logging.INFO)
websocketPool = {}
from Crypto.PublicKey import RSA
from Crypto.Cipher import AES, PKCS1_OAEP
if os.name == "nt":
storageDir = f"{os.getenv('APPDATA')}/MurraxCoin/Wallet/"
else:
storageDir = os.path.expanduser("~/.MurraxCoin/Wallet/")
os.makedirs(f"{storageDir}keys", exist_ok=True)
if len(os.listdir(storageDir + "keys")) == 0: # If first boot
keyName = input("New account name: ")
seed = os.urandom(32)
privateKey = SigningKey(seed)
f = open(f"{storageDir}keys/{keyName}", "wb+")
f.write(seed)
f.close()
def printAccounts():
print("Accounts")
print("========")
count = 0
for key in os.listdir(f"{storageDir}keys"):
count += 1
f = open(f"{storageDir}keys/{key}", "rb")
privateKey = SigningKey(f.read())
f.close()
publicKey = privateKey.verify_key
addressChecksum = zlib.adler32(publicKey.encode()).to_bytes(4, byteorder="big")
addressChecksum = base64.b32encode(addressChecksum).decode("utf-8").replace("=", "").lower()
address = base64.b32encode(publicKey.encode()).decode("utf-8").replace("=", "").lower()
publicKeyStr = f"mxc_{address}{addressChecksum}"
print(f"{count}. | {publicKeyStr} | {key}")
printAccounts()
keyNum = input("Choose an account (by number), or '+' to add a new one: ")
while keyNum == "+":
account = input("New account name: ")
seed = os.urandom(32)
privateKey = SigningKey(seed)
f = open(f"{storageDir}keys/{account}", "wb+")
f.write(seed)
f.close()
printAccounts()
keyNum = input("Choose an account (by number), or '+' to add a new one: ")
keyNum = int(keyNum)
privateFile = os.listdir(f"{storageDir}keys")[keyNum-1]
f = open(f"{storageDir}keys/{privateFile}", "rb")
privateKey = SigningKey(f.read())
f.close()
publicKey = privateKey.verify_key
addressChecksum = zlib.adler32(publicKey.encode()).to_bytes(4, byteorder="big")
addressChecksum = base64.b32encode(addressChecksum).decode("utf-8").replace("=", "").lower()
address = base64.b32encode(publicKey.encode()).decode("utf-8").replace("=", "").lower()
publicKeyStr = f"mxc_{address}{addressChecksum}"
print(f"Selected {privateFile}")
print(f"Your address is {publicKeyStr}")
try:
f = open(f"{storageDir}handshake_key.pem", "rb")
handshakeKey = RSA.import_key(f.read())
f.close()
except FileNotFoundError:
handshakeKey = RSA.generate(2048)
toWrite = handshakeKey.export_key(pkcs=8)
f = open(f"{storageDir}handshake_key.pem", "wb+")
f.write(toWrite)
f.close()
del toWrite
handshakePublicKey = handshakeKey.publickey()
handshakePublicKeyStr = handshakePublicKey.export_key()
handshakeCipher = PKCS1_OAEP.new(handshakeKey)
class websocketSecure:
def __init__(self, url):
self.url = url
async def initiateConnection(self):
self.websocket = await websockets.connect(self.url)
await self.websocket.send(handshakePublicKeyStr)
handshakeData = await self.websocket.recv()
handshakeData = json.loads(handshakeData)
sessionKey = base64.b64decode(handshakeData["sessionKey"].encode('utf-8'))
#sessionKey = bytes.fromhex(handshakeData["sessionKey"])
self.sessionKey = handshakeCipher.decrypt(sessionKey)
@classmethod
async def connect(cls, url):
self = websocketSecure(url)
await asyncio.wait({self.initiateConnection()})
for i in range(200):
try:
self.sessionKey
return self
except:
await asyncio.sleep(0.1)
raise TimeoutError
async def recv(self):
data = await self.websocket.recv()
ciphertext, tag, nonce = data.split("|||")
ciphertext, tag, nonce = base64.b64decode(ciphertext.encode("utf-8")), base64.b64decode(tag), base64.b64decode(nonce)
cipher = AES.new(self.sessionKey, AES.MODE_GCM, nonce)
plaintext = cipher.decrypt_and_verify(ciphertext, tag)
plaintext = plaintext.decode("utf-8")
return plaintext
async def send(self, plaintext):
cipher = AES.new(self.sessionKey, AES.MODE_GCM)
ciphertext, tag = cipher.encrypt_and_digest(plaintext.encode("utf-8"))
await self.websocket.send(base64.b64encode(ciphertext).decode("utf-8") + "|||" + base64.b64encode(tag).decode("utf-8") + "|||" + base64.b64encode(cipher.nonce).decode("utf-8"))
async def close(self):
await self.websocket.close()
async def genSignature(data, privateKey):
data = json.dumps(data).encode()
signature = privateKey.sign(data).signature
signature = hex(int.from_bytes(signature, "little"))
return signature
doBackgroundCheck = True
websocket = None
async def receive(sendAmount, block):
global websocket
resp = await wsRequest(f'{{"type": "balance", "address": "{publicKeyStr}"}}')
resp = json.loads(resp)
if resp["type"] != "rejection":
balance = float(resp["balance"])
blockType = "receive"
response = await wsRequest(f'{{"type": "getPrevious", "address": "{publicKeyStr}"}}')
previous = json.loads(response)["link"]
else:
balance = 0
blockType = "open"
previous = "0" * 20
response = await wsRequest(json.dumps({"type": "getRepresentative", "address": publicKeyStr}))
representative = json.loads(response)["representative"]
block = {"type": f"{blockType}", "previous": f"{previous}", "address": f"{publicKeyStr}",
"link": f"{block}", "balance": balance + float(sendAmount), "representative": representative}
hasher = BLAKE2b.new(digest_bits=512)
blockID = hasher.update(json.dumps(block).encode("utf-8")).hexdigest()
block["id"] = blockID
signature = await genSignature(block, privateKey)
block = {**block, **{"signature": signature}}
resp = await wsRequest(json.dumps(block))
resp = json.loads(resp)
if resp["type"] == "confirm":
receiveAmount = sendAmount
newBalance = block["balance"]
print(f"\nReceived MXC: {receiveAmount}")
print(f"New Balance: {newBalance}")
print("Send or Delegate? (s/d)")
else:
print("\nFailed to receive MXC!")
print(resp)
async def sendAlert(data):
data = json.loads(data)
await receive(data["sendAmount"], data["link"])
async def websocketPoolLoop():
global websocketPool
while True:
await asyncio.sleep(0.03)
try:
resp = await asyncio.wait_for(websocket.recv(), 0.5)
if prevRequest == "":
if json.loads(resp)["type"] == "sendAlert":
asyncio.create_task(sendAlert(resp))
else:
print("Unknown Alert")
print(resp)
continue
else:
websocketPool[prevRequest][1] = resp
prevRequest = ""
except ValueError:
traceback.print_exc()
except:
pass
if len(websocketPool.keys()) > 0:
poolKeys = list(websocketPool.keys())
if websocketPool[poolKeys[0]][1] == "":
await websocket.send(websocketPool[poolKeys[0]][0])
prevRequest = poolKeys[0]
websocketPool[poolKeys[0]][1] = 0
async def wsRequest(request):
global websocketPool
requestID = random.randint(0, 99999999999999)
websocketPool[requestID] = [request, ""]
while True:
await asyncio.sleep(0.1)
if websocketPool[requestID][1] != "" and websocketPool[requestID][1] != 0:
resp = websocketPool[requestID][1]
websocketPool.pop(requestID)
return resp
async def ping():
resp = await wsRequest('{"type": "ping"}')
return resp
async def main():
global websocket
uri = "ws://murraxcoin.murraygrov.es:6969"
websocket = await websocketSecure.connect(uri)
logging.debug(f"Connected to node at {uri}")
asyncio.create_task(websocketPoolLoop())
await ping()
logging.debug("Node responded to ping")
resp = await wsRequest(f'{{"type": "balance", "address": "{publicKeyStr}"}}')
resp = json.loads(resp)
if resp["type"] != "rejection":
balance = float(resp["balance"])
else:
balance = 0
print(f"Balance: {balance}")
req = {"type": "pendingSend", "address": publicKeyStr}
resp = await wsRequest(json.dumps(req))
resp = json.loads(resp)
if resp["link"] != "":
await receive(resp["sendAmount"], resp["link"])
req = {"type": "watchForSends", "address": publicKeyStr}
await wsRequest(json.dumps(req))
while True:
action = await ainput("Send or Delegate? (s/d) ")
if action == "s":
sendAddress = await ainput("Send Address: ")
toSend = await ainput("Amount to send: ")
toSend = int(toSend)
resp = await wsRequest(f'{{"type": "balance", "address": "{publicKeyStr}"}}')
resp = json.loads(resp)
if resp["type"] != "rejection":
balance = float(resp["balance"])
else:
balance = 0
newBalance = balance - toSend
response = await wsRequest(f'{{"type": "getPrevious", "address": "{publicKeyStr}"}}')
previous = json.loads(response)["link"]
response = await wsRequest(json.dumps({"type": "getRepresentative", "address": publicKeyStr}))
representative = json.loads(response)["representative"]
data = {"type": "send", "address": f"{publicKeyStr}", "link": f"{sendAddress}", "balance": f"{newBalance}", "previous": previous, "representative": representative}
hasher = BLAKE2b.new(digest_bits=512)
blockID = hasher.update(json.dumps(data).encode("utf-8")).hexdigest()
data["id"] = blockID
signature = await genSignature(data, privateKey)
data = {**data, **{"signature": f"{signature}"}}
resp = await wsRequest(json.dumps(data))
if json.loads(resp)["type"] == "confirm":
print("MXC send initiated!")
else:
print("MXC send failed to initiate, please see error below:")
print(resp)
elif action == "d":
delegateAddress = await ainput("Delegate Address: ")
response = await wsRequest(f'{{"type": "getPrevious", "address": "{publicKeyStr}"}}')
previous = json.loads(response)["link"]
data = {"type": "change", "address": publicKeyStr, "balance": balance, "representative": delegateAddress, "previous": previous}
hasher = BLAKE2b.new(digest_bits=512)
blockID = hasher.update(json.dumps(data).encode("utf-8")).hexdigest()
data["id"] = blockID
signature = await genSignature(data, privateKey)
data["signature"] = signature
resp = await wsRequest(json.dumps(data))
if json.loads(resp)["type"] == "confirm":
print("Delegation change initiated!")
else:
print("MXC delegation change failed to initiate, please see error below:")
print(resp)
asyncio.get_event_loop().run_until_complete(main())