Beispiel #1
0
    async def dispatch_DebugLinkGetState(
            ctx: wire.Context,
            msg: DebugLinkGetState) -> DebugLinkState | None:
        from trezor.messages import DebugLinkState
        from apps.common import mnemonic, passphrase

        m = DebugLinkState()
        m.mnemonic_secret = mnemonic.get_secret()
        m.mnemonic_type = mnemonic.get_type()
        m.passphrase_protection = passphrase.is_enabled()
        m.reset_entropy = storage.reset_internal_entropy

        if msg.wait_layout:
            if not storage.watch_layout_changes:
                raise wire.ProcessError("Layout is not watched")
            storage.layout_watcher = LAYOUT_WATCHER_STATE
            loop.schedule(return_layout_change())
            return None
        else:
            m.layout_lines = storage.current_content

        if msg.wait_word_pos:
            m.reset_word_pos = await reset_word_index.take()
        if msg.wait_word_list:
            m.reset_word = " ".join(await reset_current_words.take())
        return m
Beispiel #2
0
def alert(count: int = 3) -> None:
    global _alert_in_progress
    if _alert_in_progress:
        return

    _alert_in_progress = True
    loop.schedule(_alert(count))
Beispiel #3
0
def setup(iface):
    '''Initialize the wire stack on passed USB interface.'''
    # session_supervisor = codec_v2.SesssionSupervisor(iface, session_handler)
    # session_supervisor.open(codec_v1.SESSION_ID)
    # loop.schedule(session_supervisor.listen())
    handler = session_handler(iface, codec_v1.SESSION_ID)
    loop.schedule(handler)
Beispiel #4
0
 async def inner(*args, **kwargs):
     await backlight_slide(BACKLIGHT_DIM, delay, step)
     slide = backlight_slide(BACKLIGHT_NORMAL, delay, step)
     try:
         loop.schedule(slide)
         return await f(*args, **kwargs)
     finally:
         loop.close(slide)
Beispiel #5
0
def startdefault(handler):
    global default
    global default_handler

    if not default:
        default_handler = handler
        default = handler()
        loop.schedule(default)
Beispiel #6
0
def startdefault(layout):
    global default
    global default_layout

    if not default:
        default_layout = layout
        default = layout()
        loop.schedule(default)
Beispiel #7
0
def startdefault(layout: Callable[[], loop.Task]) -> None:
    global default
    global default_layout

    if not default:
        default_layout = layout
        default = layout()
        loop.schedule(default)
Beispiel #8
0
def start_default(constructor: Callable[[], loop.Task]) -> None:
    """Start a default workflow, created from `constructor`."""
    global default_task
    global default_constructor

    if not default_task:
        default_constructor = constructor
        default_task = constructor()
        loop.schedule(default_task)
Beispiel #9
0
def startdefault(handler):
    global default
    global default_handler

    if not default:
        default_handler = handler
        default = handler()
        loop.schedule(default)
        ui.display.backlight(ui.BACKLIGHT_NORMAL)
        log.debug(__name__, 'startdefault')
Beispiel #10
0
    def touch(self) -> None:
        """Wake up the idle timer.

        Events that represent some form of activity (USB messages, touches, etc.) should
        call `touch()` to notify the timer of the activity. All pending callback timers
        will reset.
        """
        for callback, task in self.tasks.items():
            timeout_us = self.timeouts[callback]
            deadline = utime.ticks_add(utime.ticks_ms(), timeout_us)
            loop.schedule(task, None, deadline, reschedule=True)
Beispiel #11
0
 async def inner(*args, **kwargs):
     await backlight_slide(BACKLIGHT_DIM)
     slide = backlight_slide(BACKLIGHT_NORMAL)
     try:
         layout = f(*args, **kwargs)
         workflow.onlayoutstart(layout)
         loop.schedule(slide)
         return await layout
     finally:
         loop.close(slide)
         workflow.onlayoutclose(layout)
Beispiel #12
0
    def setup(self, action: int, checksum: bytes, app_id: bytes) -> None:
        if workflow.workflows:
            return False

        self.action = action
        self.checksum = checksum
        self.app_id = app_id

        self.confirmed = None
        self.workflow = self.confirm_workflow()
        loop.schedule(self.workflow)
        return True
def start_default(constructor: Callable[[], loop.Task]) -> None:
    """Start a default workflow, created from `constructor`."""
    global default_task
    global default_constructor

    if not default_task:
        default_constructor = constructor
        default_task = constructor()

        # Schedule the default task.  Because the task can complete on its own,
        # we need to reset the `default_task` global in a finalizer.
        loop.schedule(default_task, None, None, _finalize_default)
Beispiel #14
0
    def setup(self, action: int, checksum: bytes, app_id: bytes) -> bool:
        if workflow.tasks or self.workflow:
            # If any other workflow is running, we bail out.
            return False

        self.action = action
        self.checksum = checksum
        self.app_id = app_id

        self.confirmed = None
        self.workflow = self.confirm_workflow()
        loop.schedule(self.workflow)
        return True
Beispiel #15
0
def boot(iface: io.HID) -> None:
    wire.add(
        MessageType.WebAuthnListResidentCredentials,
        __name__,
        "list_resident_credentials",
    )
    wire.add(MessageType.WebAuthnAddResidentCredential, __name__,
             "add_resident_credential")
    wire.add(
        MessageType.WebAuthnRemoveResidentCredential,
        __name__,
        "remove_resident_credential",
    )
    loop.schedule(handle_reports(iface))
Beispiel #16
0
    async def dispatch_DebugLinkDecision(ctx: wire.Context,
                                         msg: DebugLinkDecision) -> None:
        if debuglink_decision_chan.putters:
            log.warning(__name__, "DebugLinkDecision queue is not empty")

        if msg.x is not None:
            evt_down = io.TOUCH_START, msg.x, msg.y
            evt_up = io.TOUCH_END, msg.x, msg.y
            loop.synthetic_events.append((io.TOUCH, evt_down))
            loop.synthetic_events.append((io.TOUCH, evt_up))
        else:
            debuglink_decision_chan.publish(msg)

        if msg.wait:
            loop.schedule(return_layout_change(ctx))
def boot() -> None:
    wire.add(
        MessageType.WebAuthnListResidentCredentials,
        __name__,
        "list_resident_credentials",
    )
    wire.add(MessageType.WebAuthnAddResidentCredential, __name__,
             "add_resident_credential")
    wire.add(
        MessageType.WebAuthnRemoveResidentCredential,
        __name__,
        "remove_resident_credential",
    )
    import usb

    loop.schedule(handle_reports(usb.iface_webauthn))
Beispiel #18
0
    def boot() -> None:
        workflow_handlers.register(MessageType.DebugLinkDecision,
                                   dispatch_DebugLinkDecision)  # type: ignore
        workflow_handlers.register(MessageType.DebugLinkGetState,
                                   dispatch_DebugLinkGetState)  # type: ignore
        workflow_handlers.register(MessageType.DebugLinkReseedRandom,
                                   dispatch_DebugLinkReseedRandom)
        workflow_handlers.register(MessageType.DebugLinkRecordScreen,
                                   dispatch_DebugLinkRecordScreen)
        workflow_handlers.register(MessageType.DebugLinkEraseSdCard,
                                   dispatch_DebugLinkEraseSdCard)
        workflow_handlers.register(MessageType.DebugLinkWatchLayout,
                                   dispatch_DebugLinkWatchLayout)

        loop.schedule(debuglink_decision_dispatcher())
        if storage.layout_watcher is not LAYOUT_WATCHER_NONE:
            loop.schedule(return_layout_change())
Beispiel #19
0
    def boot() -> None:
        workflow_handlers.register(
            MessageType.DebugLinkDecision, dispatch_DebugLinkDecision
        )  # type: ignore [Argument of type "(ctx: Context, msg: DebugLinkDecision) -> Coroutine[Any, Any, None]" cannot be assigned to parameter "handler" of type "Handler[Msg@register]" in function "register"]
        workflow_handlers.register(
            MessageType.DebugLinkGetState, dispatch_DebugLinkGetState
        )  # type: ignore [Argument of type "(ctx: Context, msg: DebugLinkGetState) -> Coroutine[Any, Any, DebugLinkState | None]" cannot be assigned to parameter "handler" of type "Handler[Msg@register]" in function "register"]
        workflow_handlers.register(MessageType.DebugLinkReseedRandom,
                                   dispatch_DebugLinkReseedRandom)
        workflow_handlers.register(MessageType.DebugLinkRecordScreen,
                                   dispatch_DebugLinkRecordScreen)
        workflow_handlers.register(MessageType.DebugLinkEraseSdCard,
                                   dispatch_DebugLinkEraseSdCard)
        workflow_handlers.register(MessageType.DebugLinkWatchLayout,
                                   dispatch_DebugLinkWatchLayout)

        loop.schedule(debuglink_decision_dispatcher())
        if storage.layout_watcher is not LAYOUT_WATCHER_NONE:
            loop.schedule(return_layout_change())
Beispiel #20
0
def start_default(constructor: Callable[[], loop.Task]) -> None:
    """Start a default workflow, created from `constructor`.

    If a default task is already running, nothing will happen. Use `replace_default`
    to set up a new default task for the next run.
    """
    global default_task
    global default_constructor

    if not default_task:
        default_constructor = constructor
        default_task = constructor()
        if __debug__:
            log.debug(__name__, "start default: %s", default_task)
        # Schedule the default task.  Because the task can complete on its own,
        # we need to reset the `default_task` global in a finalizer.
        loop.schedule(default_task, None, None, _finalize_default)
    else:
        if __debug__:
            log.debug(__name__, "default already started")
Beispiel #21
0
    async def dispatch_DebugLinkDecision(ctx: wire.Context,
                                         msg: DebugLinkDecision) -> None:
        from trezor import io

        if debuglink_decision_chan.putters:
            log.warning(__name__, "DebugLinkDecision queue is not empty")

        if msg.x is not None and msg.y is not None:
            evt_down = io.TOUCH_START, msg.x, msg.y
            evt_up = io.TOUCH_END, msg.x, msg.y
            loop.synthetic_events.append((io.TOUCH, evt_down))
            if msg.hold_ms is not None:
                loop.schedule(touch_hold(msg.x, msg.y, msg.hold_ms))
            else:
                loop.synthetic_events.append((io.TOUCH, evt_up))
        else:
            debuglink_decision_chan.publish(msg)

        if msg.wait:
            storage.layout_watcher = LAYOUT_WATCHER_LAYOUT
            loop.schedule(return_layout_change())
Beispiel #22
0
def setup(iface: WireInterface, use_workflow: bool = True) -> None:
    """Initialize the wire stack on passed USB interface."""
    loop.schedule(handle_session(iface, codec_v1.SESSION_ID, use_workflow))
Beispiel #23
0
                    ui.Result(
                        confirm.CONFIRMED if msg.yes_no else confirm.CANCELLED)
                )
            if msg.swipe is not None:
                if msg.swipe == DebugSwipeDirection.UP:
                    await swipe_chan.put(swipe.SWIPE_UP)
                elif msg.swipe == DebugSwipeDirection.DOWN:
                    await swipe_chan.put(swipe.SWIPE_DOWN)
                elif msg.swipe == DebugSwipeDirection.LEFT:
                    await swipe_chan.put(swipe.SWIPE_LEFT)
                elif msg.swipe == DebugSwipeDirection.RIGHT:
                    await swipe_chan.put(swipe.SWIPE_RIGHT)
            if msg.input is not None:
                await input_chan.put(msg.input)

    loop.schedule(debuglink_decision_dispatcher())

    async def return_layout_change(ctx: wire.Context) -> None:
        content = await layout_change_chan.take()
        await ctx.write(DebugLinkLayout(lines=content))

    async def dispatch_DebugLinkDecision(ctx: wire.Context,
                                         msg: DebugLinkDecision) -> None:
        if debuglink_decision_chan.putters:
            log.warning(__name__, "DebugLinkDecision queue is not empty")

        if msg.x is not None:
            evt_down = io.TOUCH_START, msg.x, msg.y
            evt_up = io.TOUCH_END, msg.x, msg.y
            loop.synthetic_events.append((io.TOUCH, evt_down))
            loop.synthetic_events.append((io.TOUCH, evt_up))
Beispiel #24
0
def setup(iface: WireInterface, is_debug_session: bool = False) -> None:
    """Initialize the wire stack on passed USB interface."""
    loop.schedule(handle_session(iface, codec_v1.SESSION_ID, is_debug_session))
Beispiel #25
0
    ui.display.text_center(ui.WIDTH // 2, 128, "Locked", ui.BOLD,
                           ui.TITLE_GREY, ui.BG)

    ui.display.text_center(ui.WIDTH // 2 + 10, 220, "Tap to unlock", ui.BOLD,
                           ui.TITLE_GREY, ui.BG)
    ui.display.icon(45, 202, res.load(ui.ICON_CLICK), ui.TITLE_GREY, ui.BG)

    ui.backlight_fade(ui.BACKLIGHT_NORMAL)

    await ui.click()


if utils.EMULATOR:
    # Ensure the emulated SD card is FAT32 formatted.
    sd = io.SDCard()
    sd.power(True)
    fs = io.FatFS()
    try:
        fs.mount()
    except OSError:
        fs.mkfs()
    else:
        fs.unmount()
    sd.power(False)

ui.display.backlight(ui.BACKLIGHT_NONE)
ui.backlight_fade(ui.BACKLIGHT_NORMAL)
config.init(show_pin_timeout)
loop.schedule(bootscreen())
loop.run()
Beispiel #26
0
def boot(iface: io.HID):
    loop.schedule(handle_reports(iface))
Beispiel #27
0
from trezor import config
from trezor.pin import pin_to_int, show_pin_timeout
from trezor import loop
from trezor import ui

from apps.common.request_pin import request_pin


async def unlock_layout():
    while True:
        if config.has_pin():
            pin = await request_pin()
        else:
            pin = ''
        if config.unlock(pin_to_int(pin), show_pin_timeout):
            return
        else:
            await unlock_failed()


async def unlock_failed():
    pass


config.init()
ui.display.backlight(ui.BACKLIGHT_DIM)
loop.schedule(unlock_layout())
loop.run()
Beispiel #28
0
 def fork(self) -> None:
     self.deadline_ms = utime.ticks_ms() + _CONFIRM_STATE_TIMEOUT_MS
     self.task = self.confirm()
     workflow.onstart(self.task)
     loop.schedule(self.task)
Beispiel #29
0
def setup(iface: WireInterface) -> None:
    """Initialize the wire stack on passed USB interface."""
    loop.schedule(session_handler(iface, codec_v1.SESSION_ID))
Beispiel #30
0
def setup(iface):
    '''Initialize the wire stack on passed USB interface.'''
    loop.schedule(session_handler(iface, codec_v1.SESSION_ID))