forked from Foundation-Devices/passport-firmware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactions.py
2146 lines (1696 loc) · 92.3 KB
/
actions.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
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# SPDX-FileCopyrightText: 2020 Foundation Devices, Inc. <[email protected]>
# SPDX-License-Identifier: GPL-3.0-or-later
#
# SPDX-FileCopyrightText: 2018 Coinkite, Inc. <coldcardwallet.com>
# SPDX-License-Identifier: GPL-3.0-only
#
# (c) Copyright 2018 by Coinkite Inc. This file is part of Coldcard <coldcardwallet.com>
# and is covered by GPLv3 license found in COPYING.
#
# actions.py
#
# Every function here is called directly by a menu item. They should all be async.
#
import version
import gc
from files import CardMissingError, CardSlot
from uasyncio import sleep_ms
import common
from common import settings, system, noise, dis
from utils import (UXStateMachine, imported, pretty_short_delay, xfp2str, to_str,
get_accounts, run_chooser, make_account_name_num, needs_microsd,
is_all_zero, bytes_to_hex_str, split_to_lines, is_valid_btc_address,
do_address_verify, run_chooser)
from wallets.utils import (get_addr_type_from_deriv_path, get_addr_type_from_address,
get_deriv_path_from_addr_type_and_acct)
from ux import (the_ux, ux_confirm, ux_enter_text, ux_scan_qr_code, ux_shutdown,
ux_show_story, ux_show_story_sequence, ux_show_text_as_ur, ux_show_word_list)
from se_commands import *
from data_codecs.qr_type import QRType
import trezorcrypto
from seed_check_ux import SeedCheckUX
async def about_info(*a):
from common import system
from display import FontTiny
from utils import swab32
while True:
serial = system.get_serial_number()
my_xfp = settings.get('xfp', 0)
xpub = settings.get('xpub', None)
msg = '''
Master Fingerprint:
{xfp}
Reversed Fingerprint:
{rev_xfp}
Master XPUB:
{xpub}
Serial Number:
{serial}'''.format(xfp=xfp2str(my_xfp) if my_xfp else '<No Seed Yet>',
rev_xfp=xfp2str(swab32(my_xfp)) if my_xfp else '<No Seed Yet>',
xpub=xpub if xpub != None else '<No Seed Yet>',
serial=serial)
result = await ux_show_story(msg, center=True, center_vertically=True, font=FontTiny, right_btn='REGULATORY')
if result == 'y':
await regulatory_info()
else:
return
async def regulatory_info():
from display import FontTiny
msg = """\
Passport
Foundation Devices
6 Liberty Square #6018
Boston, MA 02109 USA"""
await ux_show_story(msg, title='Regulatory', center=True, font=FontTiny, overlay=(None, 303 - 34 - 72, 'fcc_ce_logos'))
# async def account_info(*a):
# # show the XPUB, and other useful information
# import common
# import stash
# from display import FontTiny
#
# xfp = settings.get('xfp', 0)
# if xfp == None:
# xfp = '<Unknown>'
# else:
# xfp = xfp2str(xfp)
#
# # Can only get these values if the derivation path is known
# xpub = '<Unknown>'
# path = '<Unknown>'
# if common.active_account.deriv_path:
# with stash.SensitiveValues() as sv:
# path = common.active_account.deriv_path
# node = sv.derive_path(path)
# xpub = sv.chain.serialize_public(node, common.active_account.addr_type)
# print('account_info(): xpub={}'.format(xpub))
#
# msg = '''
# Account Number:
# {acct_num}
#
# Derivation Path:
# {path}
#
# Account Fingerprint:
# {xfp}
#
# Account XPUB:
# {xpub}'''.format(acct_num=common.active_account.acct_num,
# path=path,
# xfp=xfp,
# xpub=xpub)
#
# await ux_show_story(
# msg,
# title=common.active_account.name,
# center=True,
# font=FontTiny)
async def rename_account(menu, label, item):
from utils import account_exists, do_rename_account
from constants import MAX_ACCOUNT_NAME_LEN
account = common.active_account
original_name = account.get('name')
new_name = original_name
while True:
new_name = await ux_enter_text('Rename', label="Enter account name", initial_text=new_name,
right_btn='RENAME', max_length=MAX_ACCOUNT_NAME_LEN)
if new_name == None or new_name == original_name:
# User selected BACK or selected RENAME with the same exact name
return
# See if an account with this name already exists
if account_exists(new_name):
await ux_show_story('An account with the name "{}" already exists. Please choose a different name.'.format(new_name),
title='Duplicate', center=True, center_vertically=True, right_btn='EDIT')
continue
# Get the accounts and replace the name and save it
await do_rename_account(account.get('acct_num'), new_name)
# Pop so we skip over the sub-menu for the account
the_ux.pop()
return
async def delete_account(menu, label, item):
from utils import do_delete_account, make_account_name_num
from ux import the_ux
account = common.active_account
# Confirm the deletion
name_num = make_account_name_num(account.get('name'), account.get('acct_num'))
if not await ux_confirm('Are you sure you want to delete this account?\n\n{}'.format(name_num)):
return
await do_delete_account(account.get('acct_num'))
# Pop so we skip over the sub-menu for the account we just deleted
the_ux.pop()
class VerifyAddressUX(UXStateMachine):
def __init__(self):
# States
self.SELECT_ACCOUNT = 1
self.SELECT_SIG_TYPE = 2
self.VERIFY_ADDRESS = 3
# print('VerifyAddressUX init')
super().__init__(self.SELECT_ACCOUNT)
self.acct_num = None
self.sig_type = None
self.multisig_wallet = None
# Account chooser
def account_chooser(self):
choices = []
values = []
accounts = get_accounts()
accounts.sort(key=lambda a: a.get('acct_num', 0))
for acct in accounts:
acct_num = acct.get('acct_num')
account_name_num = make_account_name_num(acct.get('name'), acct_num)
choices.append(account_name_num)
values.append(acct_num)
def select_account(index, text):
self.acct_num = values[index]
return 0, choices, select_account
# Select the sig type and if multisig, the specific multisig wallet
def sig_type_chooser(self):
from multisig import MultisigWallet
choices = ['Single-sig']
values = ['single-sig']
num_multisigs = MultisigWallet.get_count()
for ms_idx in range(num_multisigs):
ms = MultisigWallet.get_by_idx(ms_idx)
choices.append('%d/%d: %s' % (ms.M ,ms.N, ms.name))
values.append(ms)
def select_sig_type(index, text):
if index == 0:
self.sig_type = 'single-sig'
self.multisig_wallet = None
else:
self.sig_type = 'multisig'
self.multisig_wallet = values[index]
return 0, choices, select_sig_type
async def show(self):
while True:
# print('show: state={}'.format(self.state))
if self.state == self.SELECT_ACCOUNT:
self.acct_num = None
accounts = get_accounts()
if len(accounts) == 1:
self.acct_num = 0
self.goto(self.SELECT_SIG_TYPE, save_curr=False) # Don't save this since we're skipping this state
continue
await run_chooser(self.account_chooser, 'Account', show_checks=False)
if self.acct_num == None:
return
self.goto(self.SELECT_SIG_TYPE)
elif self.state == self.SELECT_SIG_TYPE:
# Multisig only possible for account 0, so skip this if not account 0
if self.acct_num > 0:
self.sig_type = 'single-sig'
self.goto(self.VERIFY_ADDRESS, save_curr=False) # Don't save this since we're skipping this state
continue
# Choose a wallet from the available list
multisigs = settings.get('multisig', [])
if len(multisigs) == 0:
self.sig_type = 'single-sig'
else:
await run_chooser(self.sig_type_chooser, 'Type', show_checks=False)
if self.sig_type == None:
if not self.goto_prev():
# Nothing to return back to, so we must have skipped one or more steps...we're done
return
continue
# print('self.sig_type={}'.format(self.sig_type))
self.goto(self.VERIFY_ADDRESS)
elif self.state == self.VERIFY_ADDRESS:
# Scan the address to be verified - should be a normal QR code
system.turbo(True);
address = await ux_scan_qr_code('Verify Address')
system.turbo(False)
if address == None:
return
address, is_valid_btc = await is_valid_btc_address(address)
if is_valid_btc == False:
if not self.goto_prev():
return
continue
# Get the address type from the address
is_multisig = self.sig_type == 'multisig'
# print('address={} acct_num={} is_multisig={}'.format(address, self.acct_num, is_multisig))
addr_type = get_addr_type_from_address(address, is_multisig)
deriv_path = get_deriv_path_from_addr_type_and_acct(addr_type, self.acct_num, is_multisig)
result = await do_address_verify(self.acct_num, address, addr_type, deriv_path, self.multisig_wallet)
if not result:
if not self.goto_prev():
return
else:
# User asked to stop searching
return
async def verify_address(*a):
verify_address_ux = VerifyAddressUX()
await verify_address_ux.show()
async def update_firmware(*a):
# Upgrade via microSD card
# - search for a particular file
# - verify it lightly
# - erase serial flash
# - copy it over (slow)
# - reboot into bootloader, which finishes install
from common import sf, dis
from constants import FW_HEADER_SIZE, FW_ACTUAL_HEADER_SIZE, FW_MAX_SIZE
import trezorcrypto
# Don't show any files that are pubkeys
def is_valid_firmware(filename, header):
if filename.endswith('-pub.bin'):
return False
result = system.validate_firmware_header(header)
return result[0]
fn = await file_picker(
'On the next screen, select the firmware file you want to install.',
suffix='.bin',
title='Select File',
taster=is_valid_firmware,
bite_size=FW_HEADER_SIZE)
# print('\nselected fn = {}\n'.format(fn))
if not fn:
return
failed = None
system.turbo(True)
with CardSlot() as card:
with open(fn, 'rb') as fp:
import os
offset = 0
s = os.stat(fn)
size = s[6]
if size < FW_HEADER_SIZE:
await ux_show_story('Firmware file is too small.', title='Error', left_btn='BACK', right_btn='OK', center=True, center_vertically=True)
return
if size > FW_MAX_SIZE:
await ux_show_story('Firmware file is too large.', title='Error', left_btn='BACK', right_btn='OK', center=True, center_vertically=True)
return
# Read the header
header = fp.read(FW_HEADER_SIZE)
if len(header) != FW_HEADER_SIZE:
system.turbo(False)
await ux_show_story('Firmware file is too small, and the system misreported its size.', title='Error', left_btn='BACK', right_btn='OK', center=True, center_vertically=True)
return
# Validate the header
is_valid, version, error_msg, is_user_signed = system.validate_firmware_header(header)
if not is_valid:
system.turbo(False)
await ux_show_story('Firmware header is invalid.\n\n{}'.format(error_msg), title='Error', left_btn='BACK', right_btn='OK', center=True, center_vertically=True)
return
if is_user_signed:
pubkey_result, pubkey = read_user_firmware_pubkey()
if not pubkey_result or is_all_zero(pubkey):
system.turbo(False)
await ux_show_story('Install a Developer PubKey before loading non-Foundation firmware.\n\n', title='Error', left_btn='BACK', right_btn='OK', center=True, center_vertically=True)
return
system.turbo(False)
# Give the user a chance to confirm/back out
if not await ux_confirm('Please make sure your Passport is backed up before proceeding.\n\n' +
'Are you sure you want to update the firmware?\n\nNew Version:\n{}'.format(version),
title='Update', scroll_label='MORE'):
return
if not await ux_confirm('Do not remove the batteries or shutdown Passport during the firmware update.\n\nWe recommend using fresh batteries.',
title='Reminder', negative_btn='CANCEL', positive_btn='OK'):
return
# Start the update
system.turbo(True)
# copy binary into serial flash
fp.seek(offset)
# Calculate the update request hash so that the booloader knows this was requested by the user, not
# injected into SPI flash by some external attacker.
# Hash the firmware header
header_hash = bytearray(32)
# Only hash the bytes that contain the passport_firmware_header_t to match what's hashed in the bootloader
firmware_header = header[0:FW_ACTUAL_HEADER_SIZE]
system.sha256(firmware_header, header_hash)
system.sha256(header_hash, header_hash) # Double sha
# Get the device hash
device_hash = bytearray(32)
system.get_device_hash(device_hash)
# Combine them
s = trezorcrypto.sha256()
s.update(header_hash)
s.update(device_hash)
# Result
update_hash = s.digest()
# Erase first page
sf.sector_erase(0)
while sf.is_busy():
await sleep_ms(10)
buf = bytearray(256) # must be flash page size
buf[0:32] = update_hash # Copy into the buf we'll use to write to SPI flash
sf.write(0, buf) # Need to write the entire page of 256 bytes
# Start one page in so that we can use the first page for storing a hash.
# The hash combines the firmware hash with the device hash.
pos = 256
update_display = 0
while pos <= size + 256:
# print('pos = {}'.format(pos))
# Update progress bar every 50 flash pages
if update_display % 50 == 0:
dis.splash(message='Preparing Update...', progress=(pos-256)/size)
update_display += 1
here = fp.readinto(buf)
if not here:
break
if pos % 4096 == 0:
# erase here
sf.sector_erase(pos)
while sf.is_busy():
await sleep_ms(10)
sf.write(pos, buf)
# full page write: 0.6 to 3ms
while sf.is_busy():
await sleep_ms(1)
pos += here
if failed:
system.turbo(False)
await ux_show_story(failed, title='Sorry!')
return
# Save an entry to the settings indicating that we are doing an update
(curr_version, _, _, _) = system.get_software_info()
settings.set('update', '{}->{}'.format(curr_version, version)) # old_version->new_version
await settings.save()
# NOTE: We intentionally stay in turbo mode here as we reboot to keep the final splash display fast.
# Bootloader will go back to top speed anyway.
# continue process...
# print("RESTARTING!")
# Show final progress bar at 100% and change message
dis.splash(message='Restarting...', progress=1) # TODO: Make 0-100 to be consistent with progress bar
system.turbo(False)
await sleep_ms(1000)
import machine
machine.reset()
async def reset_self(*a):
import machine
machine.soft_reset()
# NOT REACHED
async def initial_pin_setup(*a):
from common import pa, dis, loop
# TODO: Move the messaging into EnterInitialPinUX state machine
# First time they select a PIN
while 1:
# ch = await ux_show_story('''\
# Passport uses a PIN from 6 to 12 digits long.
#
# There is an additional security feature that you can use. After entering 2 or more digits of your PIN, press and hold the VALIDATE \
# button and Passport will show you two Security Words unique to your device and PIN prefix.
#
# Remember these two words, and remember how many digits you entered before checking them. When logging in, you can \
# repeat this process and you should see the same words.
#
# If you see different words, then either:
#
# 1. You entered a different number of digits
#
# 2. You entered the wrong first digits of your PIN
#
# 3. Your Passport has been tampered with''', title='PIN Info', scroll_label='MORE')
# if ch == 'y':
while 1:
ch = await ux_show_story('''\
Now it's time to set your 6-12 digit PIN.
There is no way to recover a lost PIN or reset Passport.
Please record your PIN somewhere safe.''', title='Set PIN', scroll_label='MORE')
if ch != 'y':
break
# Enter the PIN
from login_ux import EnterInitialPinUX
new_pin_ux = EnterInitialPinUX()
await new_pin_ux.show()
pin = new_pin_ux.pin
# print('pin = {}'.format(pin))
if pin is None:
continue
# New pin is being saved
dis.fullscreen("Saving PIN...")
system.show_busy_bar()
try:
assert pa.is_blank()
pa.change(new_pin=pin)
# check it? kinda, but also get object into normal "logged in" state
pa.setup(pin)
ok = pa.login()
assert ok
except Exception as e:
print("Exception: {}".format(e))
finally:
system.hide_busy_bar()
return
async def login_countdown(minutes):
# show a countdown, which may need to
# run for multiple **days**
from common import dis
from display import FontSmall
sec = minutes * 60
while sec:
dis.clear()
y = 0
dis.text(None, y, 'Login countdown in', font=FontSmall)
y += 14
dis.text(None, y, 'effect. Must wait:', font=FontSmall)
y += 14
y += 5
dis.text(None, y, pretty_short_delay(sec), font=FontSmall)
dis.show()
dis.busy_bar(1)
await sleep_ms(1000)
sec -= 1
dis.busy_bar(0)
async def block_until_login(*a):
#
# Force user to enter a valid PIN.
#
from login_ux import LoginUX
from common import pa, loop, dis
# print('pa.is_successful() = {}'.format(pa.is_successful()))
while not pa.is_successful():
login_ux = LoginUX()
# try:
await login_ux.show()
# except Exception as e:
# print('ERROR when logging in: {}'.format(e))
# # not allowed!
# pass
# print('!!!!LOGGED IN!!!!')
system.turbo(False)
async def create_new_seed(*a):
from ubinascii import hexlify as b2a_hex
import seed
system.show_busy_bar()
wallet_seed_bytes = await seed.create_new_wallet_seed()
# print('wallet_seed_bytes = {}'.format(b2a_hex(wallet_seed_bytes)))
mnemonic_str = trezorcrypto.bip39.from_data(wallet_seed_bytes)
# print('mnemonic = {}'.format(mnemonic_str))
mnemonic_words = mnemonic_str.split(' ')
# Save the wallet so we can work with it (needs to be saved for backup to work)
await seed.save_wallet_seed(wallet_seed_bytes)
# Update xpub/xfp in settings after creating new wallet
import stash
with stash.SensitiveValues() as sv:
sv.capture_xpub()
system.hide_busy_bar()
while True:
ch = await ux_show_story('''Now let's create a backup of your seed. We recommend backing up Passport to the two included microSD cards.
Experienced users can always view and record the 24-word seed in the Advanced settings menu.''', title='Backup')
if ch == 'x':
if await ux_confirm("Are you sure you want to cancel the backup?\n\nWithout a microSD backup or the seed phrase, you won't be able to recover your funds."):
# Go back to the outer loop and show the selection again
break
# Ensure microSD card is inserted before continuing
try:
with CardSlot() as card:
# TODO: Call the export.make_complete_backup() directly and have it return True/False to indicate if the backup completed
await make_microsd_backup()
break
except CardMissingError:
ch = await needs_microsd()
if ch == 'x':
continue
await goto_top_menu()
async def restore_wallet_from_seed(menu, label, item):
result = await ux_show_story('''On the next screen you'll be able to restore your seed using predictive text input.
If you'd like to enter "car" for example, type 2-2-7 and select "car" from the dropdown.''', title='Restore Seed')
if result == 'x':
return
fake_it = False
if fake_it:
pass
else:
from seed_entry_ux import SeedEntryUX
seed_phrase_entry = SeedEntryUX(seed_len=item.arg)
await seed_phrase_entry.show()
if not seed_phrase_entry.is_seed_valid:
return
# Seed is valid, so go ahead and convert the mnemonic to seed bits and save it
mnemonic = ' '.join(seed_phrase_entry.words)
# print('mnemonic = {}'.format(mnemonic))
await handle_seed_data_format(mnemonic)
async def handle_seed_data_format(mnemonic):
import seed
from foundation import bip39
from common import dis, pa
from ubinascii import hexlify as b2a_hex
entropy = bytearray(33) # Includes and extra byte for the checksum bits
# Don't let them import seed if there is already a wallet.
if not pa.is_secret_blank():
await ux_show_story('''Unable to import seed phrase because this Passport is alread configured with a seed.
First use Advanced > Erase Passport to remove the current seed.''', right_btn='OK')
return False
bip = bip39()
len = bip.mnemonic_to_bits(mnemonic, entropy)
if len == 264: # 24 words x 11 bits each
trim_pos = 32
elif len == 198: # 18 words x 11 bits each
trim_pos = 24
elif len == 132: # 12 words x 11 bits each
trim_pos = 16
entropy = entropy[:trim_pos] # Trim off the excess (including checksum bits)
# print('entropy = {}'.format(b2a_hex(entropy)))
# Entropy is now the right length - SecretStash.encode() adds a marker byte to indicate length of the secret
# so we can decode it correctly.
await seed.save_wallet_seed(entropy)
# print('Seed was imported successfully!')
# Update xpub/xfp in settings after creating new wallet
import stash
with stash.SensitiveValues() as sv:
sv.capture_xpub()
# Show post-creation message
dis.fullscreen('Successfully Imported!')
await sleep_ms(1000)
await goto_top_menu()
return True
async def handle_sign_message_format(data):
from common import dis
from public_constants import AF_CLASSIC
from auth import sign_msg
if data != None:
try:
parts = data.split(b'\n')
if len(parts) != 2:
await ux_show_story('Invalid message format.', title='Error', right_btn='DONE')
return False
# print('parts={}'.format(parts))
msg_to_sign = parts[0]
deriv_path_to_sign = parts[1]
# print('handle_sign_message_format signing msg: {} deriv_path: {}'.format(msg_to_sign, deriv_path_to_sign))
dis.fullscreen('Analyzing...')
system.show_busy_bar()
# TODO: determine addr_type dynamically instead of hard coded passing AF_CLASSIC
await sign_msg(msg_to_sign, deriv_path_to_sign, AF_CLASSIC)
except Exception as e:
await ux_show_story('Error signing message:\n\n{}'.format(e), title='Error', right_btn='DONE')
return False
finally:
system.hide_busy_bar()
return False
async def erase_wallet(menu, label, item):
# Erase the seed words, and private key from this wallet!
# This is super dangerous for the customer's money.
import seed
from common import pa
if not await ux_confirm('Are you sure you want to erase this Passport? All funds will be lost if not backed up.'):
return
if not await ux_confirm('Without a proper backup, this action will cause you to lose all funds associated with this device.\n\n' +
'Please confirm that you understand these risks.', scroll_label='MORE', negative_btn='BACK', positive_btn='CONFIRM'):
return
await seed.erase_wallet(item.arg)
# NOT REACHED -- reset happens
async def view_seed_words(*a):
import stash
from common import dis
if not await ux_confirm(
'The next screen will show your seed words and, if defined, your passphrase.\n\n' +
'Anyone who knows these words can control your funds.\n\n' +
'Do you want to display this sensitive information?', scroll_label='MORE', center=False):
return
dis.fullscreen('Retrieving Seed...')
system.show_busy_bar()
try:
with stash.SensitiveValues() as sv:
assert sv.mode == 'words' # protected by menu item predicate
words = trezorcrypto.bip39.from_data(sv.raw).split(' ')
msg = 'Seed words (%d):\n' % len(words)
msg += '\n'.join('%2d: %s' % (i+1, w) for i, w in enumerate(words))
pw = stash.bip39_passphrase
if pw:
msg += '\n\nPassphrase:\n%s' % stash.bip39_passphrase
system.hide_busy_bar()
ch = await ux_show_story(msg, sensitive=True, right_btn='VERIFY')
if ch == 'y':
seed_check = SeedCheckUX(seed_words=words, title='Verify Seed')
await seed_check.show()
return
stash.blank_object(msg)
except Exception as e:
print('Exception: {}'.format(e))
system.hide_busy_bar()
# Unable to read seed!
await ux_show_story('Unable to retrieve seed.')
async def start_login_sequence():
# Boot up login sequence here.
#
from common import pa
while not pa.is_successful():
# always get a PIN and login first
await block_until_login()
async def goto_top_menu(*a):
# Start/restart menu system
from menu import MenuSystem
from flow import MainMenu, NoSeedMenu
from common import pa
# print('pa.is_secret_blank()={}'.format(pa.is_secret_blank()))
if pa.is_secret_blank():
m = MenuSystem(NoSeedMenu)
else:
m = MenuSystem(MainMenu)
the_ux.reset(m)
return m
SENSITIVE_NOT_SECRET = '''
The file created is sensitive in terms of privacy, but should not \
compromise your funds directly.'''
PICK_ACCOUNT = '''\n\nPress 1 to enter a non-zero account number.'''
async def export_summary(*A):
# save addresses, and some other public details into a file
if not await ux_confirm('''\
Saves a text file to microSD with a summary of the *public* details \
of your wallet. For example, this gives the XPUB (extended public key) \
that you will need to import other wallet software to track balance.''' + SENSITIVE_NOT_SECRET,
title='Export', negative_btn='BACK', positive_btn='CONTINUE'):
return
# pick a semi-random file name, save it.
with imported('export') as exp:
await exp.make_summary_file()
def electrum_export_story(background=False):
# saves memory being in a function
return ('''\
This saves a skeleton Electrum wallet file onto the microSD card. \
You can then open that file in Electrum without ever connecting this Passport to a computer.\n
'''
+ (background or 'Choose an address type for the wallet on the next screen.'+PICK_ACCOUNT)
+ SENSITIVE_NOT_SECRET)
# async def electrum_skeleton(*a):
# # save xpub, and some other public details into a file: NOT MULTISIG
#
# ch = await ux_show_story(electrum_export_story())
#
# account_num = 0
# if ch == '1':
# account_num = await ux_enter_number('Account Number:', 9999)
# elif ch != 'y':
# return
#
# # pick segwit or classic derivation+such
# from public_constants import AF_CLASSIC, AF_P2WPKH, AF_P2WPKH_P2SH
# from menu import MenuSystem, MenuItem
#
# # Ordering and terminology from similar screen in Electrum. I prefer
# # 'classic' instead of 'legacy' personally.
# rv = []
#
# rv.append(MenuItem("Legacy (P2PKH)", f=electrum_skeleton_step2,
# arg=(AF_CLASSIC, account_num)))
# rv.append(MenuItem("P2SH-Segwit", f=electrum_skeleton_step2,
# arg=(AF_P2WPKH_P2SH, account_num)))
# rv.append(MenuItem("Native Segwit", f=electrum_skeleton_step2,
# arg=(AF_P2WPKH, account_num)))
#
# return MenuSystem(rv, title="Electrum")
# async def bitcoin_core_skeleton(*A):
# # save output descriptors into a file
# # - user has no choice, it's going to be bech32 with m/84'/{coin_type}'/0' path
#
# ch = await ux_show_story('''\
# This saves a command onto the microSD card that includes the public keys. \
# You can then run that command in Bitcoin Core without ever connecting this Passport to a computer.\
# ''' + PICK_ACCOUNT + SENSITIVE_NOT_SECRET)
#
# account_num = 0
# if ch == '1':
# account_num = await ux_enter_number('Account Number:', 9999)
# elif ch != 'y':
# return
#
# # no choices to be made, just do it.
# with imported('export') as exp:
# dis.fullscreen('Generating...')
# body = exp.make_bitcoin_core_wallet(account_num)
# await write_text_file('bitcoin-core.txt', body, 'Bitcoin Core')
# async def electrum_skeleton_step2(_1, _2, item):
# # pick a semi-random file name, render and save it.
# with imported('export') as exp:
# addr_fmt, account_num = item.arg
# await exp.make_json_wallet('Electrum wallet', lambda: exp.generate_electrum_wallet(addr_fmt, account_num))
# async def generic_skeleton(*a):
# # like the Multisig export, make a single JSON file with
# # basically all useful XPUB's in it.
#
# if await ux_show_story('''\
# Saves JSON file onto MicroSD card, with XPUB values that are needed to watch typical \
# single-signer UTXO associated with this Coldcard.''' + SENSITIVE_NOT_SECRET) != 'y':
# return
#
# account_num = await ux_enter_number('Account Number:', 9999)
#
# # no choices to be made, just do it.
# import export
# await export.make_json_wallet('Generic Export',
# lambda: export.generate_generic_export(account_num),
# 'coldcard-export.json')
# async def wasabi_skeleton(*A):
# # save xpub, and some other public details into a file
# # - user has no choice, it's going to be bech32 with m/84'/0'/0' path
#
# if await ux_show_story('''\
# This saves a skeleton Wasabi wallet file onto the microSD card. \
# You can then open that file in Wasabi without ever connecting this Passport to a computer.\
# ''' + SENSITIVE_NOT_SECRET) != 'y':
# return
#
# # no choices to be made, just do it.
# with imported('export') as exp:
# await exp.make_json_wallet('Wasabi wallet', lambda: exp.generate_wasabi_wallet(), 'new-wasabi.json')
async def make_microsd_backup(*A):
# save everything, using a password, into single encrypted file, typically on SD
with imported('export') as exp:
await exp.make_complete_backup()
async def verify_microsd_backup(*A):
# check most recent backup is "good"
# read 7z header, and measure checksums
with imported('export') as exp:
fn = await file_picker('Select the backup to verify.',
suffix='.7z', max_size=exp.MAX_BACKUP_FILE_SIZE, folder_path='/sd/backups')
if fn:
# do a limited CRC-check over encrypted file
await exp.verify_backup_file(fn)
EMPTY_RESTORE_MSG = '''\
Before restoring from a backup, you must erase this Passport. Make sure your device is backed up.
Navigate to Advanced > Erase Passport.'''
FULL_PARTIAL_MSG = '''A wallet seed already exists.
Do you want to perform a FULL restore or a PARTIAL restore of accounts only?'''
async def restore_microsd_backup(*A):
from common import pa
partial_restore = False
if not pa.is_secret_blank():
await ux_show_story(EMPTY_RESTORE_MSG)
return
# if not pa.is_secret_blank():
# result = await ux_show_story(FULL_PARTIAL_MSG, left_btn='FULL', right_btn='PARTIAL')
# if result == 'x':
# await ux_show_story(EMPTY_RESTORE_MSG)
# return
# else:
# partial_restore = True
# TODO: Insert step here to pick a backups-* folder when we add the XFP to the folder name
# Choose a backup file -- must be in 7z format
fn = await file_picker('Select the backup to restore and then enter the six-word password.',
suffix='.7z', max_size=10000, folder_path='/sd/backups')
if fn:
with imported('export') as exp:
await exp.restore_complete(fn, partial_restore)
async def format_sd_card(*A):
if not await ux_confirm('Erase and reformat the microSD card.', negative_btn='BACK', positive_btn='FORMAT'):