def do_usb_command(cmd, args): # TESTING commands! # - only to be implemented on the simulator!! # - pleaes don't freak out, stay calm # - if you can trick us into running this, can run anything worse directly # - and we don't ship this code on the real product, only part of simulator if cmd == 'XKEY': from main import numpad try: numpad.inject(str(args, 'ascii')) except: pass return try: if cmd == 'EVAL': return b'biny' + repr(eval(str(args, 'utf8'))).encode() if cmd == 'EXEC': RV = uio.BytesIO() exec(str(args, 'utf8'), None, dict(RV=RV)) return b'biny' + RV.getvalue() except BaseException as exc: tmp = uio.StringIO() sys.print_exception(exc, tmp) return b'biny' + tmp.getvalue().encode() return b'err_Unknown SIMULATOR cmd'
async def test_7z(): # test full 7z round-trip # Altho cleartext mode is not for real, if the code is written, I must test it. from backups import write_complete_backup, restore_complete_doit from sffile import SFFile import tcc from main import settings, sf, numpad today = tcc.random.uniform(1000000) import machine machine.reset = lambda: None for chain in ['BTC', 'XTN']: for words in ([], ['abc', 'def']): settings.set('check', today) settings.set('chain', chain) ll, sha = await write_complete_backup(words, None, True) result = SFFile(0, ll).read() if words: #open('debug.7z', 'wb').write(result) assert ll > 800 assert len(sha) == 32 assert result[0:6] == b"7z\xbc\xaf'\x1c" assert tcc.sha256(result).digest() == sha assert len(set(result)) >= 240 # encrypted else: sr = str(result, 'ascii') print("Backup contents:\n" + sr) assert sr[0] == '#', result assert 'Coldcard' in sr assert len(set(sr)) < 100 # cleartext, english assert ('chain = "%s"' % chain) in result # test restore # - cant wipe flash, since the backup file is there # - cant wipe all settings becuase PIN and stuff is simulated there del settings.current['check'] with SFFile(0, ll) as fd: numpad.inject('y') # for 'success' message await restore_complete_doit(fd, words) assert settings.get('check') == today, \ (settings.get('check'), '!=', today) assert settings.get('chain') == chain, \ (settings.get('chain'), '!=', chain) today += 3 import ux ux.restore_menu()
async def usb_keypad_emu(): # Take keypresses on USB virtual serial port (when not in REPL mode) # and converts them into keypad events. Super handy for UX testing/dev. # # IMPORTANT: # - code is **not** used in real product, but left here for devs to use # - this code isn't even called; unless you add code to do so, see ../stm32/my_lib_boot2.py # from ux import the_ux from menu import MenuSystem from seed import WordNestMenu u = pyb.USB_VCP() remap = { '\r': 'y', '\x1b': 'x', '\x1b[A': '5', '\x1b[B': '8', '\x1b[C': '9', '\x1b[D': '7' } while 1: await sleep_ms(100) while u.isconnected() and u.any(): from main import numpad k = u.read(3).decode() if k in '\x04': # ^D # warm reset from machine import soft_reset soft_reset() if k == 'T': ckcc.vcp_enabled(True) print("Repl") continue if k in remap: k = remap[k] if k in '0123456789xy': numpad.inject(k) continue
async def usb_keypad_emu(): # Take keypresses on USB virtual serial port (when not in REPL mode) # and converts them into keypad events. Super handy for UX testing/dev. # # IMPORTANT: # - code is **not** used in real product, but left here for devs to use # - this code isn't even called; unless you add code to do so. # u = pyb.USB_VCP() while 1: await sleep_ms(100) while u.isconnected() and u.any(): from main import numpad k = u.read(3).decode() remap = { '\r': 'y', '\x1b': 'x', 'q': 'x', '\x1b[A': '5', '\x1b[B': '8', '\x1b[C': '9', '\x1b[D': '7' } if k in remap: k = remap[k] if k in '\x04': # ^D # warm reset from machine import soft_reset soft_reset() if k == 'U': # enter DFU #from machine import bootloader #bootloader() import callgate callgate.enter_dfu() if k in '0123456789xy': numpad.inject(k) else: print('? %r' % k)
3190206587, 'tpubD9ArfXowvGHnuECKdGXVKDMfZVGdephVWg8fWGWStH3VKHzT4ph3A4ZcgXWqFu1F5xGTfxncmrnf3sLC86dup2a8Kx7z3xQ3AgeNTQeFxPa' ], [ 1130956047, 'tpubD8NXmKsmWp3a3DXhbihAYbYLGaRNVdTnr6JoSxxfXYQcmwVtW2hv8QoDwng6JtEonmJoL3cNEwfd2cLXMpGezwZ2vL2dQ7259bueNKj9C8n' ]], { 'ch': 'XTN', 'pp': "45'" } ]] sim_defaults['fee_limit'] = -1 # start in multisig wallet from main import numpad numpad.inject('9') numpad.inject('y') numpad.inject('9') numpad.inject('5') numpad.inject('y') if '-s' in sys.argv: # MicroSD menu from main import numpad numpad.inject('4') numpad.inject('y') numpad.inject('4') numpad.inject('y') if '-a' in sys.argv: # Address Explorer
async def usb_keypad_emu(): # Take keypresses on USB virtual serial port (when not in REPL mode) # and converts them into keypad events. Super handy for UX testing/dev. # # IMPORTANT: # - code is **not** used in real product, but left here for devs to use # - this code isn't even called; unless you add code to do so, see ../stm32/my_lib_boot2.py # u = pyb.USB_VCP() while 1: await sleep_ms(100) while u.isconnected() and u.any(): from main import numpad k = u.read(3).decode() remap = { '\r': 'y', '\x1b': 'x', 'q': 'x', '\x1b[A': '5', '\x1b[B': '8', '\x1b[C': '9', '\x1b[D': '7' } if k in remap: k = remap[k] if k in '\x04': # ^D # warm reset from machine import soft_reset soft_reset() if k == 'd': numpad.debug = (numpad.debug + 1) % 3 continue if k == 'n': if numpad.disabled: numpad.start() else: numpad.stop() print("npdis = %d" % numpad.disabled) continue if k == 'r': numpad.trigger_baseline = True continue if k == 's': numpad.sensitivity = (numpad.sensitivity + 1) % 5 print("sensi = %d" % numpad.sensitivity) continue if k == 'U': # enter DFU import callgate callgate.enter_dfu() # not reached if k == 't': ckcc.vcp_enabled(True) print("Repl enabled") continue if k == 'm': import gc print("Memory: %d" % gc.mem_free()) continue if k in '0123456789xy': numpad.inject(k) else: print('? %r' % k)
# imported late, for simulator only ... go to specific sub-sub-menus (dev aid) import sys if '-m' in sys.argv: # start in multisig wallet from main import numpad numpad.inject('9') numpad.inject('y') numpad.inject('9') numpad.inject('5') numpad.inject('y') if '-s' in sys.argv: # MicroSD menu from main import numpad numpad.inject('4') numpad.inject('y') numpad.inject('4') numpad.inject('y') if '--addr' in sys.argv: # Address Explorer from main import numpad numpad.inject('4') numpad.inject('y') numpad.inject('4') numpad.inject('8') numpad.inject('8') numpad.inject('y') numpad.inject('y') numpad.inject('4') # skips warning!
async def usb_keypad_emu(): # Take keypresses on USB virtual serial port (when not in REPL mode) # and converts them into keypad events. Super handy for UX testing/dev. # # IMPORTANT: # - code is **not** used in real product, but left here for devs to use # - this code isn't even called; unless you add code to do so, see ../stm32/my_lib_boot2.py # from ux import the_ux from menu import MenuSystem from seed import WordNestMenu u = pyb.USB_VCP() remap = { '\r': 'y', '\x1b': 'x', '\x1b[A': '5', '\x1b[B': '8', '\x1b[C': '9', '\x1b[D': '7' } while 1: await sleep_ms(100) while u.isconnected() and u.any(): from main import numpad k = u.read(3).decode() if k in '\x04': # ^D # warm reset from machine import soft_reset soft_reset() if 0: if k == 'd': numpad.debug = (numpad.debug + 1) % 3 continue if k == 'n': if numpad.disabled: numpad.start() else: numpad.stop() print("npdis = %d" % numpad.disabled) continue if k == 'U': # enter DFU import callgate callgate.enter_dfu() # not reached if k == 'T': ckcc.vcp_enabled(True) print("Repl enabled") continue if k == 'M': import gc print("Memory: %d" % gc.mem_free()) continue # word menus: shortcut for first letter top = the_ux.top_of_stack() if top and isinstance(top, WordNestMenu) and len(top.items) > 6: pos = min(len(i.label) for i in top.items) if pos >= 2: for n, it in enumerate(top.items): if it.label[pos-2] == k: top.goto_idx(n) top.show() k = None break if k is None: continue if k in remap: k = remap[k] if k in '0123456789xy': numpad.inject(k) continue print('? %r' % k)