/
lib.py
803 lines (735 loc) · 34 KB
/
lib.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
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
import binascii
import json
import subprocess
import tempfile
import time
import os
import sys
import apsw
import tarfile
from pathlib import Path
from blockfrost import BlockFrostApi, ApiError, ApiUrls
def input_mnemonic():
data = input('Input 1st word or entire mnemonic: ')
words = data.split(" ")
if len(words) > 10:
return words
elif len(words) == 1:
count=1
while True:
count += 1
data = input(f"Word {count}: ")
words.append(data)
if data == "":
return words
class AdaWallet:
"""A single address wallet library that supports menmonics and hardware wallets"""
def __init__(self, state_dir):
self.state_dir = Path(state_dir)
self.accounts = {}
self.db = None
self.wallet_initialized = False
self.load_state()
def initialize(self, testnet, rosetta_url=""):
self.testnet = testnet
self.rosetta_url = rosetta_url
self.state_dir.mkdir(parents=True, exist_ok=True)
db_exists = os.path.exists(self.state_dir / "data.sqlite")
self.db = apsw.Connection(str((self.state_dir / "data.sqlite").resolve()))
if not db_exists:
self.initialize_db()
self.load_state()
def initialize_db(self):
cursor = self.db.cursor()
cursor.execute("create table status(hw_wallet,root_key,testnet,rosetta_url)")
cursor.execute("create table utxo(txid,tx_index,address,amount)")
cursor.execute("create table accounts(id,payment_vkey,payment_skey,stake_vkey,stake_skey,address,stake_address)")
def load_state(self):
if not os.path.exists(self.state_dir / "data.sqlite"):
return
if os.path.exists(self.state_dir / "data.sqlite") and not self.db:
self.db = apsw.Connection(str((self.state_dir / "data.sqlite").resolve()))
cursor = self.db.cursor()
row = cursor.execute("select * from status").fetchone()
if row:
self.wallet_initialized = True
self.hardware_wallet = row[0]
self.root_key = row[1]
self.testnet = row[2]
self.blockfrost = BlockFrostApi()
if row[3]:
# TODO: blockfrost URL stuff???
pass
if self.testnet:
self.magic_args = ["--testnet-magic", "2"]
else:
self.magic_args = ["--mainnet"]
rows = cursor.execute("select * from accounts")
for (index, payment_vkey, payment_skey, stake_vkey, stake_skey, address, stake_address) in rows:
self.accounts[index] = {
"payment_vkey": payment_vkey,
"payment_skey": payment_skey,
"stake_vkey": stake_vkey,
"stake_skey": stake_skey,
"address": address,
"stake_address": stake_address
}
def wipe(self, prompt=True):
if self.wallet_initialized and prompt:
resp = input(f"Are you sure you want to wipe state directory {self.state_dir}? Type YES to confirm: ")
if resp != 'YES':
print('Aborting wipe!')
exit(1)
cli_args = [
'rm',
'-rf',
self.state_dir,
]
p = subprocess.run(cli_args, capture_output=True, text=True)
if p.returncode != 0:
print(p.stderr)
raise Exception("Unknown error removing state directory")
self.wallet_initialized = False
def load_wallet_from_mnemonic(self, mnemonic):
self.initialize_root_key(mnemonic)
def initialize_hardware_wallet(self):
cursor = self.db.cursor()
cursor.execute("insert into status values(?,?,?,?)", (True, None, self.testnet, ""))
self.hardware_wallet = True
def initialize_read_only(self):
cursor = self.db.cursor()
cursor.execute("insert into status values(?,?,?,?)", (False, None, self.testnet, ""))
def create_wallet_mnemonic(self):
cli_args = [
"cardano-address",
"recovery-phrase",
"generate",
]
p = subprocess.run(cli_args, capture_output=True, text=True)
if p.returncode != 0:
print(p.stderr)
raise Exception("Unknown error generating mnemonic")
mnemonic = p.stdout.rstrip().split(" ")
self.initialize_root_key(mnemonic)
return mnemonic
def initialize_root_key(self, mnemonic):
cli_args = [
"cardano-address",
"key",
"from-recovery-phrase",
"Shelley",
]
p = subprocess.run(cli_args, input=" ".join(mnemonic), capture_output=True, text=True)
if p.returncode != 0:
print(p.stderr)
raise Exception("Unknown error converting mnemonic to root key")
root_key = p.stdout.rstrip()
cursor = self.db.cursor()
cursor.execute("insert into status values(?,?,?,?)", (False, root_key, self.testnet, ""))
def import_accounts(self, start=None, end=None, accounts_file=None):
if self.hardware_wallet:
# HW wallet bulk import with one click
self.import_accounts_hw(start, end, reload_state=False)
elif start != None and end != None:
for i in range(start, end + 1):
self.import_account(i, reload_state=False)
elif accounts_file:
with open(accounts_file) as f:
accounts = json.load(f)
for account in accounts:
self.import_account(account, reload_state=False)
self.load_state()
def import_accounts_hw(self, start, end, reload_state=True):
account_indexes = range(start, end + 1)
accounts = self.derive_account_keys_bulk_hw(account_indexes)
for account_keys in accounts:
self.write_account(account_keys)
if reload_state:
self.load_state()
def import_account(self, account, reload_state=True):
if type(account) == int:
if account not in self.accounts:
payment_vkey, payment_skey = self.derive_account_keys(account, "payment")
stake_vkey, stake_skey = self.derive_account_keys(account, "stake")
address = self.build_address(payment_vkey, stake_vkey)
stake_address = self.build_stake_address(stake_vkey)
account_keys = (account, payment_vkey, payment_skey, stake_vkey, stake_skey, address, stake_address)
self.write_account(account_keys)
elif type(account) == dict and account["index"] not in self.accounts:
account_keys = (account["index"], account["payment_vkey"], account["payment_skey"], account["stake_vkey"], account["stake_skey"], account["address"], account["stake_address"])
self.write_account(account_keys)
if reload_state:
self.load_state()
def export_accounts(self, out_file):
cursor = self.db.cursor()
accounts = []
for account_index, value in self.accounts.items():
account = value.copy()
account["index"] = account_index
account["payment_skey"] = None
account["stake_skey"] = None
accounts.append(account)
with open(out_file, 'w') as f:
f.write(json.dumps(accounts))
def write_account(self, account_keys):
cursor = self.db.cursor()
cursor.execute("insert into accounts values(?,?,?,?,?,?,?)", account_keys)
def build_address(self, payment_vkey, stake_vkey):
with tempfile.NamedTemporaryFile("w+") as payment, tempfile.NamedTemporaryFile("w+") as stake:
payment.write(payment_vkey)
payment.flush()
stake.write(stake_vkey)
stake.flush()
cli_args = [
"cardano-cli",
"address",
"build",
*self.magic_args,
"--payment-verification-key-file",
payment.name,
"--stake-verification-key-file",
stake.name
]
p = subprocess.run(cli_args, capture_output=True, text=True)
if p.returncode != 0:
print(p.stderr)
raise Exception(f"Unknown error building address")
return p.stdout.rstrip()
def build_stake_address(self, stake_vkey):
with tempfile.NamedTemporaryFile("w+") as stake:
stake.write(stake_vkey)
stake.flush()
cli_args = [
"cardano-cli",
"stake-address",
"build",
*self.magic_args,
"--stake-verification-key-file",
stake.name
]
p = subprocess.run(cli_args, capture_output=True, text=True)
if p.returncode != 0:
print(" ".join("cli_args"))
print(p.stderr)
raise Exception(f"Unknown error building stake address")
return p.stdout.rstrip()
def derive_account_keys_bulk_hw(self, account_indexes):
if self.hardware_wallet:
accounts = []
account_args = []
key_files = []
for i in account_indexes:
payment_vkey = tempfile.NamedTemporaryFile("w+")
payment_hws = tempfile.NamedTemporaryFile("w+")
stake_vkey = tempfile.NamedTemporaryFile("w+")
stake_hws = tempfile.NamedTemporaryFile("w+")
account_args.extend(["--path", f"1852H/1815H/{i}H/0/0", "--hw-signing-file", payment_hws.name, "--verification-key-file", payment_vkey.name])
account_args.extend(["--path", f"1852H/1815H/{i}H/2/0", "--hw-signing-file", stake_hws.name, "--verification-key-file", stake_vkey.name])
key_files.append((i, payment_vkey, payment_hws, stake_vkey, stake_hws))
cli_args = [
"cardano-hw-cli",
"shelley",
"address",
"key-gen",
*account_args
]
p = subprocess.run(cli_args, capture_output=True, text=True)
if p.returncode != 0:
print(p.stderr)
raise Exception(f"Unknown error extracting bulk accounts from hardware wallet")
for account, payment_vkey, payment_hws, stake_vkey, stake_hws in key_files:
if account not in self.accounts:
payment_hws_contents = payment_hws.read()
payment_vkey_contents = payment_vkey.read()
stake_hws_contents = stake_hws.read()
stake_vkey_contents = stake_vkey.read()
address = self.build_address(payment_vkey_contents, stake_vkey_contents)
stake_address = self.build_stake_address(stake_vkey_contents)
accounts.append((account, payment_vkey_contents, payment_hws_contents, stake_vkey_contents, stake_hws_contents, address, stake_address))
return accounts
def derive_account_keys(self, account, role):
if role == "stake":
role_index = 2
elif role == "payment":
role_index = 0
else:
raise Exception(f"Role {role} not supported!")
if self.hardware_wallet:
with tempfile.NamedTemporaryFile("w+") as vkey, tempfile.NamedTemporaryFile("w+") as hws:
cli_args = [
"cardano-hw-cli",
"shelley",
"address",
"key-gen",
"--path",
f"1852H/1815H/{account}H/{role_index}/0",
"--hw-signing-file",
hws.name,
"--verification-key-file",
vkey.name
]
p = subprocess.run(cli_args, capture_output=True, text=True)
if p.returncode != 0:
print(p.stderr)
raise Exception(f"Unknown error extracting account key {account}")
hws_contents = hws.read()
vkey_contents = vkey.read()
return (vkey_contents, hws_contents)
else:
with tempfile.NamedTemporaryFile("w+") as vkeyx, tempfile.NamedTemporaryFile("w+") as vkey, tempfile.NamedTemporaryFile("w+") as skey:
cli_args = [
"cardano-address",
"key",
"child",
f"1852H/1815H/{account}H/{role_index}/0",
]
p = subprocess.run(cli_args, input=self.root_key, capture_output=True, text=True)
if p.returncode != 0:
print(p.stderr)
raise Exception(f"Unknown error deriving child account key for {account}")
child_skey = p.stdout.rstrip()
cli_args = [
"cardano-cli",
"key",
"convert-cardano-address-key",
f"--shelley-{role}-key",
"--signing-key-file",
"/dev/stdin",
"--out-file",
skey.name
]
p = subprocess.run(cli_args, input=child_skey, capture_output=True, text=True)
if p.returncode != 0:
print(p.stderr)
raise Exception(f"Unknown error converting child key to CLI format for {account}")
cli_args = [
"cardano-cli",
"key",
"verification-key",
"--signing-key-file",
skey.name,
"--verification-key-file",
vkeyx.name
]
p = subprocess.run(cli_args, capture_output=True, text=True)
if p.returncode != 0:
print(p.stderr)
raise Exception(f"Unknown error converting signing key to verification extended key for {account}")
cli_args = [
"cardano-cli",
"key",
"non-extended-key",
"--extended-verification-key-file",
vkeyx.name,
"--verification-key-file",
vkey.name
]
p = subprocess.run(cli_args, capture_output=True, text=True)
if p.returncode != 0:
print(p.stderr)
raise Exception(f"Unknown error converting verification extended key to verification key for {account}")
skey_contents = skey.read()
vkey_contents = vkey.read()
return (vkey_contents, skey_contents)
def sign_tx(self, account, tx_body, out_file, stake=False):
if account not in self.accounts:
self.import_account(account)
if self.hardware_wallet:
with tempfile.NamedTemporaryFile("w+") as payment_hws, tempfile.NamedTemporaryFile("w+") as stake_hws, tempfile.NamedTemporaryFile("w+") as tx_body_formatted:
signing_args = []
payment_hws.write(self.accounts[account]["payment_skey"])
payment_hws.flush()
signing_args.extend(["--hw-signing-file", payment_hws.name])
cli_args = [
"cardano-hw-cli",
"transaction",
"transform-raw",
"--tx-body-file",
tx_body,
"--out-file",
tx_body_formatted.name
]
p = subprocess.run(cli_args, capture_output=True, text=True)
if p.returncode != 0 or not os.path.exists(tx_body_formatted.name):
print(" ".join(cli_args))
# TODO: cardano-hw-cli prints an error to stdout. Remove when fixed
print(p.stdout)
print(p.stderr)
raise Exception(f"Unknown error signing transaction with account {account}")
if stake:
stake_hws.write(self.accounts[account]["stake_skey"])
stake_hws.flush()
signing_args.extend(["--hw-signing-file", stake_hws.name])
cli_args = [
"cardano-hw-cli",
"shelley",
"transaction",
"sign",
*self.magic_args,
"--tx-body-file",
tx_body_formatted.name,
"--out-file",
out_file,
*signing_args
]
p = subprocess.run(cli_args, capture_output=True, text=True)
if p.returncode != 0 or not os.path.exists(out_file):
print(" ".join(cli_args))
# TODO: cardano-hw-cli prints an error to stdout. Remove when fixed
print(p.stdout)
print(p.stderr)
raise Exception(f"Unknown error signing transaction with account {account}")
return
elif self.accounts[account]["payment_skey"]:
with tempfile.NamedTemporaryFile("w+") as payment_skey, tempfile.NamedTemporaryFile("w+") as stake_skey:
signing_args = []
payment_skey.write(self.accounts[account]["payment_skey"])
payment_skey.flush()
signing_args.extend(["--signing-key-file", payment_skey.name])
if stake:
stake_skey.write(self.accounts[account]["stake_skey"])
stake_skey.flush()
signing_args.extend(["--signing-key-file", stake_skey.name])
cli_args = [
"cardano-cli",
"transaction",
"sign",
"--tx-body-file",
tx_body,
"--out-file",
out_file,
*signing_args
]
p = subprocess.run(cli_args, capture_output=True, text=True)
if p.returncode != 0 or not os.path.exists(out_file):
print(" ".join(cli_args))
# TODO: cardano-hw-cli prints an error to stdout. Remove when fixed
print(p.stdout)
print(p.stderr)
raise Exception(f"Unknown error signing transaction with account {account}")
return
raise Exception(f"No signing key available for account {account}")
def bulk_witness_tx(self, tx_archive, out_file, role):
with tarfile.open(name=out_file, mode='w:gz') as tarout, tarfile.open(name=tx_archive, mode='r:gz') as tarin:
for member in tarin.getmembers():
with tempfile.NamedTemporaryFile("wb+") as tx, tempfile.NamedTemporaryFile("w+") as witness:
tx_buf = tarin.extractfile(member)
tx_contents = tx_buf.read()
tx.write(tx_contents)
tx.flush()
account = int(member.name.split(".")[0])
txwitness_name = f"{account}.txwitness-{role}"
self.witness_tx(account, tx.name, witness.name, role)
tarout.add(witness.name, txwitness_name)
def bulk_sign_tx(self, tx_archive, out_file, stake=False):
with tarfile.open(name=out_file, mode='w:gz') as tarout, tarfile.open(name=tx_archive, mode='r:gz') as tarin:
for member in tarin.getmembers():
with tempfile.NamedTemporaryFile("wb+") as tx, tempfile.NamedTemporaryFile("w+") as sign:
tx_buf = tarin.extractfile(member)
tx_contents = tx_buf.read()
tx.write(tx_contents)
tx.flush()
account = int(member.name.split(".")[0])
txsigned_name = f"{account}.txsigned"
self.sign_tx(account, tx.name, sign.name, stake=stake)
tarout.add(sign.name, txsigned_name)
def witness_tx(self, account, tx_body, out_file, role):
if account not in self.accounts:
self.import_account(account)
if self.hardware_wallet:
with tempfile.NamedTemporaryFile("w+") as hws:
signing_args = []
hws.write(self.accounts[account][f"{role}_skey"])
hws.flush()
signing_args.extend(["--hw-signing-file", hws.name])
cli_args = [
"cardano-hw-cli",
"shelley",
"transaction",
"witness",
*self.magic_args,
"--tx-body-file",
tx_body,
"--out-file",
out_file,
*signing_args
]
p = subprocess.run(cli_args, capture_output=True, text=True)
if p.returncode != 0 or not os.path.exists(out_file):
print(" ".join(cli_args))
# TODO: cardano-hw-cli prints an error to stdout. Remove when fixed
print(p.stdout)
print(p.stderr)
raise Exception(f"Unknown error witnessing transaction with account {account}")
return
elif self.accounts[account][f"{role}_skey"]:
with tempfile.NamedTemporaryFile("w+") as skey:
signing_args = []
skey.write(self.accounts[account][f"{role}_skey"])
skey.flush()
signing_args.extend(["--signing-key-file", skey.name])
cli_args = [
"cardano-cli",
"transaction",
"witness",
"--tx-body-file",
tx_body,
"--out-file",
out_file,
*signing_args
]
p = subprocess.run(cli_args, capture_output=True, text=True)
if p.returncode != 0 or not os.path.exists(out_file):
print(" ".join(cli_args))
# TODO: cardano-hw-cli prints an error to stdout. Remove when fixed
print(p.stdout)
print(p.stderr)
raise Exception(f"Unknown error witnessing transaction with account {account}")
return
raise Exception(f"No signing key available for account {account} with role {role}")
def clear_utxo_table(self):
cursor = self.db.cursor()
cursor.execute("delete from utxo")
def update_utxos_for_accounts(self, reload_state=False):
self.clear_utxo_table()
cursor = self.db.cursor()
for account,details in self.accounts.items():
utxos = self.get_utxos_for_address(details["address"])
for utxo in utxos:
cursor.execute("insert into utxo values(?,?,?,?)", utxo)
if reload_state:
self.load_state()
def import_utxos_for_accounts(self, utxo_json, reload_state=False):
self.clear_utxo_table()
with open(utxo_json, 'r') as f:
utxos = json.load(f)
cursor = self.db.cursor()
for utxo_attrs in utxos:
utxo = (utxo_attrs['txid'], utxo_attrs['tx_index'], utxo_attrs['address'], utxo_attrs['amount'])
cursor.execute("insert into utxo values(?,?,?,?)", utxo)
if reload_state:
self.load_state()
def export_utxos_for_accounts(self, utxo_json):
cursor = self.db.cursor()
utxo_entries = []
rows = cursor.execute("select * from utxo")
for txid, tx_index, address, amount in rows:
utxo_entries.append({
"txid": txid,
"tx_index": tx_index,
"address": address,
"amount": amount
})
with open(utxo_json, 'w') as f:
f.write(json.dumps(utxo_entries))
def get_rewards_for_stake_address(self, stake_address):
try:
account_details = self.blockfrost.accounts(
stake_address=stake_address,
)
return { stake_address: int(account_details.withdrawable_amount) }
except ApiError as e:
print(e)
exit(1)
def get_block(self, blockid=None):
try:
if blockid:
return self.blockfrost.block(hash_or_number=blockid)
return self.blockfrost.block_latest()
except ApiError as e:
print(e)
exit(1)
def get_slot_tip(self):
block = self.get_block()
return block.slot
def fetch_utxos_address(self, address):
utxos = []
cursor = self.db.cursor()
rows = cursor.execute("select * from utxo WHERE address=?", (address,))
for row in rows:
utxos.append((row[0], row[1], int(row[3])))
return utxos
def stake_registration_tx(self, account, out_file, fee, ttl=None, sign=False, deposit=2000000):
with tempfile.NamedTemporaryFile("w+") as stake_registration_certificate, tempfile.NamedTemporaryFile("w+") as vkey:
vkey.write(self.accounts[account]["stake_vkey"])
vkey.flush()
cli_args = [
"cardano-cli",
"stake-address",
"registration-certificate",
"--stake-verification-key-file",
vkey.name,
"--out-file",
stake_registration_certificate.name
]
p = subprocess.run(cli_args, capture_output=True, text=True)
if p.returncode != 0:
print(" ".join(cli_args))
# TODO: cardano-hw-cli prints an error to stdout. Remove when fixed
print(p.stdout)
print(p.stderr)
raise Exception(f"Unknown error generating stake registration certificate for account {account}")
return self.build_tx(account, out_file, fee, certificates=[stake_registration_certificate.name], ttl=ttl, sign=sign, deposit=deposit, stake=True)
def bulk_stake_registration_tx(self, out_file, fee, ttl=None, deposit=2000000, sign=False):
if sign:
suffix="txsigned"
else:
suffix="txbody"
sum_result = (0, 0, 0, 0)
with tarfile.open(name=out_file, mode='w:gz') as tar:
for account,details in self.accounts.items():
with tempfile.NamedTemporaryFile("w+") as tx:
result = self.stake_registration_tx(int(account), tx.name, fee, ttl, sign, deposit)
tar.add(tx.name, f"{account}.{suffix}")
sum_result = (sum_result[0] + result[0], sum_result[1] + result[1], sum_result[2] + result[2], sum_result[3] + result[3])
return sum_result
def bulk_delegate_pool_tx(self, delegations_file, out_file, fee, ttl=None, sign=False):
if sign:
suffix="txsigned"
else:
suffix="txbody"
sum_result = (0, 0, 0, 0)
with open(delegations_file) as f:
delegations = json.load(f)
with open(out_file, "wb") as f2:
with tarfile.open(fileobj=f2, mode='w:gz') as tar:
for account,pool_id in delegations.items():
with tempfile.NamedTemporaryFile("w+") as tx:
result = self.delegate_pool_tx(int(account), pool_id, tx.name, fee, ttl, sign)
tar.add(tx.name, f"{account}.{suffix}")
sum_result = (sum_result[0] + result[0], sum_result[1] + result[1], sum_result[2] + result[2], sum_result[3] + result[3])
return sum_result
def bulk_drain_tx(self, send_addr, out_file, fee, ttl=None, sign=False):
if sign:
suffix="txsigned"
else:
suffix="txbody"
sum_result = (0, 0, 0, 0)
with open(out_file, "wb") as f2:
with tarfile.open(fileobj=f2, mode='w:gz') as tar:
for account in self.accounts:
with tempfile.NamedTemporaryFile("w+") as tx:
result = self.drain_tx(int(account), send_addr, tx.name, fee, ttl, sign)
if result == None:
continue
tar.add(tx.name, f"{account}.{suffix}")
sum_result = (sum_result[0] + result[0], sum_result[1] + result[1], sum_result[2] + result[2], sum_result[3] + result[3])
return sum_result
def migrate_wallet(self, accounts_file, out_file, fee, ttl=None, sign=False):
if sign:
suffix="txsigned"
else:
suffix="txbody"
with open(accounts_file) as f:
migrate_accounts = json.load(f)
sum_result = (0, 0, 0, 0)
with open(out_file, "wb") as f:
with tarfile.open(fileobj=f, mode='w:gz') as tar:
for account,details in self.accounts.items():
with tempfile.NamedTemporaryFile("w+") as tx:
account_address = details["address"]
account_utxos = self.fetch_utxos_address(account_address)
total_in = sum(i for _, _, i in account_utxos)
output_address = None
for migrate_details in migrate_accounts:
if int(account) == int(migrate_details["index"]):
output_address = migrate_details["address"]
if not output_address:
raise Exception("Error! accounts file must contain an address for every existing account to migrate")
txouts = { output_address: total_in - fee }
result = self.build_tx(account, tx.name, fee, txouts=txouts, ttl=ttl, sign=sign)
tar.add(tx.name, f"{account}.{suffix}")
sum_result = (sum_result[0] + result[0], sum_result[1] + result[1], sum_result[2] + result[2], sum_result[3] + result[3])
return sum_result
def delegate_pool_tx(self, account, pool_id, out_file, fee, ttl=None, sign=False):
with tempfile.NamedTemporaryFile("w+") as delegation_certificate, tempfile.NamedTemporaryFile("w+") as vkey:
vkey.write(self.accounts[account]["stake_vkey"])
vkey.flush()
cli_args = [
"cardano-cli",
"stake-address",
"delegation-certificate",
"--stake-verification-key-file",
vkey.name,
"--stake-pool-id",
pool_id,
"--out-file",
delegation_certificate.name
]
p = subprocess.run(cli_args, capture_output=True, text=True)
if p.returncode != 0:
print(" ".join(cli_args))
# TODO: cardano-hw-cli prints an error to stdout. Remove when fixed
print(p.stdout)
print(p.stderr)
raise Exception(f"Unknown error generating stake delegation certificate for account {account}")
result = self.build_tx(account, out_file, fee, certificates=[delegation_certificate.name], ttl=ttl, sign=sign, stake=True)
return result
def drain_tx(self, account, send_addr, out_file, fee, ttl=None, sign=False):
stake_address = self.accounts[account]["stake_address"]
withdrawals = self.get_rewards_for_stake_address(stake_address)
if withdrawals != None:
return self.build_tx(account, out_file, fee, withdrawals=withdrawals, ttl=ttl, sign=sign, stake=True, change_address=send_addr)
return None
def build_tx(self, account, out_file, fee, txouts={}, withdrawals={}, certificates=[], ttl=None, sign=False, deposit=0, stake=False, change_address=None):
account_address = self.accounts[account]["address"]
if change_address == None:
change_address = account_address
if not ttl:
ttl = self.get_slot_tip() + 5000
out_total = 0
in_total = 0
cli_args = [
"cardano-cli",
"transaction",
"build-raw",
"--ttl",
str(ttl),
"--out-file",
out_file
]
for txid, index, value in self.fetch_utxos_address(account_address):
cli_args.extend(["--tx-in", f"{txid}#{index}"])
in_total += value
for address, value in txouts.items():
cli_args.extend(["--tx-out", f"{address}+{value}"])
out_total += value
for address, value in withdrawals.items():
cli_args.extend(["--withdrawal", f"{address}+{value}"])
in_total += value
for certificate in certificates:
cli_args.extend(["--certificate", certificate])
change = in_total - out_total - fee - deposit
if change >= 1000000:
cli_args.extend(["--tx-out", f"{change_address}+{change}"])
elif change == 0:
pass
elif change < 1000000 and change > 0:
fee = change + fee
elif change < 0:
raise Exception("Error generating transaction, not enough funds")
else:
raise Exception("Error generating transaction, unknown error calculating change")
cli_args.extend(["--fee", str(fee)])
p = subprocess.run(cli_args, capture_output=True, text=True)
if p.returncode != 0:
print(p.stderr)
raise Exception("Unknown error creating bulk transaction")
if sign:
self.sign_tx(account, out_file, out_file, stake=stake)
return((in_total, out_total, fee, change))
def get_utxos_for_address(self, address):
utxos = []
try:
bf_utxos = self.blockfrost.address_utxos(address=address)
except ApiError as e:
print(e)
exit(1)
for utxo in bf_utxos:
# TODO: this is a hack assuming utxo only has lovelace if amount == 1
if len(utxo.amount) == 1:
amount = utxo.amount[0].quantity
txid = utxo.tx_hash
index = utxo.tx_index
utxos.append((txid, index, address, amount))
return utxos