-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaleo.py
497 lines (439 loc) · 12.7 KB
/
aleo.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
import subprocess
import env
from http_utils import get_request
import json
from aws_utils import (
dynamodb_scan,
dynamodb_delete,
dynamodb_update,
dynamodb_get,
)
import asyncio
from project_utils import utc_now_ms, ascync_run, record_to_amount
import json
import re
from fake_useragent import UserAgent
async def create_account():
aleo_account_new_stdout = await ascync_run(
[f"{env.CARGO_BIN_DIR_PATH}snarkos", "account", "new"]
)
stdout_lines = aleo_account_new_stdout.split("\n")
private_key = stdout_lines[1].split(" ")[-1]
view_key = stdout_lines[2].split(" ")[-1]
address = stdout_lines[3].split(" ")[-1]
return private_key, view_key, address
async def get_height():
height_endpoint = f"{env.ALEO_API}/testnet3/latest/height"
return int(await get_request(height_endpoint))
async def get_block_transactions(height):
endpoint = f"{env.ALEO_API}/testnet3/block/{height}/transactions"
return await get_request(endpoint)
async def scan_records(
view_key, endpoint=env.ALEO_API, start=0, end=None, last=None
):
if last is not None:
period_args = ["--last", str(last)]
else:
if end is None:
end = await get_height()
period_args = ["--start", str(start)]
period_args += ["--end", str(end)]
stdout = await ascync_run(
[
f"{env.CARGO_BIN_DIR_PATH}snarkos",
"developer",
"scan",
"--endpoint",
endpoint,
"--view-key",
view_key,
*period_args,
]
)
stdout_lines = stdout.split("\n")
if stdout_lines[3] == "No records found":
return []
records_string = "".join(stdout_lines[5:-2])
record_strings = json.loads(records_string)
return [record_string.replace(" ", "") for record_string in record_strings]
async def transfer_credits(
private_key, receiver_address, amount, record, fee_record, priority_fee=0
):
fee_record = f'"{fee_record}"'
record = f'"{record}"'
stdout = await ascync_run(
[
f"{env.CARGO_BIN_DIR_PATH}snarkos",
"developer",
"execute",
env.ALEO_CREDITS_PROGRAM_ID,
"transfer_private",
record,
receiver_address,
f"{amount}u64",
"--query",
env.ALEO_API,
"--private-key",
private_key,
"--broadcast",
env.ALEO_BROADCAST_ENDPOINT,
"--fee",
priority_fee,
"--record",
fee_record,
]
)
tx_ids = re.findall(r"at1[a-z0-9]{58}", stdout)
if not tx_ids:
raise Exception(stdout)
tx_id = tx_ids[0]
return tx_id
async def split_credit(
private_key,
record,
amount,
):
encoded_amount = f"{amount}u64"
record = f'"{record}"'
stdout = await ascync_run(
[
f"{env.CARGO_BIN_DIR_PATH}snarkos",
"developer",
"execute",
env.ALEO_CREDITS_PROGRAM_ID,
"split",
record,
encoded_amount,
"--query",
env.ALEO_API,
"--private-key",
private_key,
"--broadcast",
env.ALEO_BROADCAST_ENDPOINT,
]
)
tx_ids = re.findall(r"at1[a-z0-9]{58}", stdout)
if not tx_ids:
raise Exception(stdout)
tx_id = tx_ids[0]
return tx_id
async def join_credits(
private_key,
record1,
record2,
fee_record,
priority_fee=0,
):
record1 = f'"{record1}"'
record2 = f'"{record2}"'
fee_record = f'"{fee_record}"'
stdout = await ascync_run(
[
f"{env.CARGO_BIN_DIR_PATH}snarkos",
"developer",
"execute",
env.ALEO_CREDITS_PROGRAM_ID,
"join",
record1,
record2,
"--query",
env.ALEO_API,
"--private-key",
private_key,
"--broadcast",
env.ALEO_BROADCAST_ENDPOINT,
"--fee",
priority_fee,
"--record",
fee_record,
]
)
tx_ids = re.findall(r"at1[a-z0-9]{58}", stdout)
if not tx_ids:
raise Exception(stdout)
tx_id = tx_ids[0]
return tx_id
async def get_transaction(tx_id):
transaction_endpoint = f"{env.ALEO_API}/testnet3/transaction/{tx_id}"
return await get_request(transaction_endpoint)
async def get_transaction_outputs(tx_id, view_key):
transaction = await get_transaction(tx_id)
fee = transaction.get("fee")
transitions = transaction["execution"]["transitions"] + (
[transaction["fee"]["transition"]] if fee else []
)
outputs = {}
for transition in transitions:
transition_id = f'{transition["program"]}/{transition["function"]}'
output_ciphers = transition["outputs"]
outputs[transition_id] = [
await decrypt_record(output_cipher["value"], view_key)
for output_cipher in output_ciphers
]
return outputs
async def decrypt_record(record, view_key):
stdout = await ascync_run(
[
f"{env.CARGO_BIN_DIR_PATH}snarkos",
"developer",
"decrypt",
"--ciphertext",
record,
"--view-key",
view_key,
]
)
record = "".join(stdout.split("\n")[1:-1]).replace(" ", "")
if not record.startswith("{"):
return ""
return "".join(stdout.split("\n")[1:-1]).replace(" ", "")
async def get_treasury_records():
treasury_records = await dynamodb_scan(env.ALEO_TREASURY_RECORDS_TABLE)
records = []
collection_record = None
for record_scanned in treasury_records:
if record_scanned.get("collection_record"):
collection_record = record_scanned["record_id"]
continue
records.append(
{
"record": record_scanned["record_id"],
"amount": record_to_amount(record_scanned["record_id"]),
}
)
records.sort(key=lambda x: x["amount"])
if len(records) < 2:
raise Exception("Not enough records in treasury.")
if not collection_record:
raise Exception("No collection record in treasury.")
biggest_record, smallest_record = records[-1], records[0]
await asyncio.gather(
dynamodb_delete(
env.ALEO_TREASURY_RECORDS_TABLE,
{"record_id": biggest_record["record"]},
),
dynamodb_delete(
env.ALEO_TREASURY_RECORDS_TABLE,
{"record_id": smallest_record["record"]},
),
dynamodb_delete(
env.ALEO_TREASURY_RECORDS_TABLE,
{"record_id": collection_record},
),
)
return (
smallest_record["record"],
biggest_record["record"],
collection_record,
)
async def get_stored_token_record(token_number):
treasury_record = await dynamodb_get(
env.PRIVACY_PRIDE_STORED_TOKEN_RECORDS_TABLE,
{"token_number": token_number},
)
if not treasury_record:
raise Exception("No corresponding token record in treasury.")
await dynamodb_delete(
env.PRIVACY_PRIDE_STORED_TOKEN_RECORDS_TABLE,
{"token_number": token_number},
)
return treasury_record["token_record"]
async def push_treasury_records(records):
await asyncio.gather(
*[
dynamodb_update(
env.ALEO_TREASURY_RECORDS_TABLE,
{"record_id": record},
{"used_already": False, "collection_record": collection},
)
for (record, collection) in records
]
)
async def transfer_pp(
private_key,
amount_record,
receiver_address,
fee_record,
priority_fee=0,
):
amount_record = f'"{amount_record}"'
fee_record = f'"{fee_record}"'
stdout = await ascync_run(
[
f"{env.CARGO_BIN_DIR_PATH}snarkos",
"developer",
"execute",
env.PRIVACY_PRIDE_PROGRAM_ID,
"transfer_private",
amount_record,
receiver_address,
"--query",
env.ALEO_API,
"--private-key",
private_key,
"--broadcast",
env.ALEO_BROADCAST_ENDPOINT,
"--fee",
priority_fee,
"--record",
fee_record,
]
)
tx_ids = re.findall(r"at1[a-z0-9]{58}", stdout)
if not tx_ids:
raise Exception(stdout)
tx_id = tx_ids[0]
return tx_id
async def transfer_token_private(
private_key,
amount_record,
receiver_address,
fee_record,
priority_fee=0,
):
amount_record = f'"{amount_record}"'
fee_record = f'"{fee_record}"'
stdout = await ascync_run(
[
f"{env.CARGO_BIN_DIR_PATH}snarkos",
"developer",
"execute",
env.ALEO_STORE_PROGRAM_ID,
"transfer_token_private",
amount_record,
receiver_address,
"--query",
env.ALEO_API,
"--private-key",
private_key,
"--broadcast",
env.ALEO_BROADCAST_ENDPOINT,
"--fee",
priority_fee,
"--record",
fee_record,
]
)
tx_ids = re.findall(r"at1[a-z0-9]{58}", stdout)
if not tx_ids:
raise Exception(stdout)
tx_id = tx_ids[0]
return tx_id
async def burn_private(
private_key,
collection_record,
amount_record,
fee_record,
priority_fee=0,
):
amount_record = f'"{amount_record}"'
collection_record = f'"{collection_record}"'
fee_record = f'"{fee_record}"'
stdout = await ascync_run(
[
f"{env.CARGO_BIN_DIR_PATH}snarkos",
"developer",
"execute",
env.ALEO_STORE_PROGRAM_ID,
"burn_private",
collection_record,
amount_record,
"--query",
env.ALEO_API,
"--private-key",
private_key,
"--broadcast",
env.ALEO_BROADCAST_ENDPOINT,
"--fee",
priority_fee,
"--record",
fee_record,
]
)
tx_ids = re.findall(r"at1[a-z0-9]{58}", stdout)
if not tx_ids:
raise Exception(stdout)
tx_id = tx_ids[0]
return tx_id
async def get_transaction_id(transition_id):
return await get_request(
f"{env.ALEO_API}/testnet3/find/transactionID/{transition_id}",
)
async def get_transaction(transaction_id):
return await get_request(
f"{env.ALEO_API}/testnet3/transaction/{transaction_id}",
)
async def get_recent_transitions(program_id):
ua = UserAgent()
header = {"User-Agent": str(ua.chrome)}
res = await get_request(
f"{env.HAMP_API}/program?id={program_id}",
headers=header,
json_output=False,
)
print(res)
transition_ids = re.findall(
r"\<a\ href\=\"\/transition\?id\=(as1[0-9a-z-A-Z]+)\"\>", res
)
return transition_ids
async def mint_private(
private_key,
collection_record,
token_number,
user_address,
metadata_uri,
fee_record,
priority_fee=0,
):
collection_record = f'"{collection_record}"'
fee_record = f'"{fee_record}"'
metadata_uri = '"{metadata_uri:' + metadata_uri + ',transferable:true}"'
stdout = await ascync_run(
[
f"{env.CARGO_BIN_DIR_PATH}snarkos",
"developer",
"execute",
env.ALEO_STORE_PROGRAM_ID,
"mint_private",
collection_record,
token_number,
user_address,
metadata_uri,
"--query",
env.ALEO_API,
"--private-key",
private_key,
"--broadcast",
env.ALEO_BROADCAST_ENDPOINT,
"--fee",
priority_fee,
"--record",
fee_record,
]
)
tx_ids = re.findall(r"at1[a-z0-9]{58}", stdout)
if not tx_ids:
raise Exception(stdout)
tx_id = tx_ids[0]
return tx_id
def encode_string(string, part_amount, bits_per_part):
part_len = bits_per_part // 8
parts_str = []
if len(string) > part_amount * bits_per_part // 8:
raise Exception("String too long to be encoded.")
for i in range(part_amount):
parts_str.append(string[i * part_len : (i + 1) * part_len])
parts_hex = [part_str.encode("utf-8").hex() for part_str in parts_str]
parts_int = [
int(part_hex, 16) if part_hex else 0 for part_hex in parts_hex
]
out_str = "{" + "".join(
[
f"part{i}:{parts_int[i]}u{bits_per_part},"
for i in range(part_amount)
]
)
out_str = out_str[:-1] + "}"
return out_str
def encode_string64(string):
return encode_string(string, 4, 128)