How to use the trezor.loop function in trezor

To help you get started, we’ve selected a few trezor examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github trezor / trezor-firmware / core / src / trezor / ui / passphrase.py View on Github external
async def handle_input(self) -> None:
        touch = loop.wait(io.TOUCH)
        timeout = loop.sleep(1000 * 1000 * 1)
        race_touch = loop.race(touch)
        race_timeout = loop.race(touch, timeout)

        while True:
            if self.pending_button is not None:
                race = race_timeout
            else:
                race = race_touch
            result = await race

            if touch in race.finished:
                event, x, y = result
                self.dispatch(event, x, y)
            else:
                self.on_timeout()
github trezor / trezor-core / src / trezor / wire / codec_v1.py View on Github external
async def awrite(self, buf):
        """
        Encode and write every byte from `buf`.  Does not need to be called in
        case message has zero length.  Raises `EOFError` if the length of `buf`
        exceeds the remaining message length.
        """
        if self.size < len(buf):
            raise EOFError

        write = loop.wait(self.iface.iface_num() | io.POLL_WRITE)
        nwritten = 0
        while nwritten < len(buf):
            # copy as much as possible to report buffer
            nbytes = utils.memcpy(self.data, self.ofs, buf, nwritten, len(buf))
            nwritten += nbytes
            self.ofs += nbytes
            self.size -= nbytes

            if self.ofs == _REP_LEN:
                # we are at the end of the report, flush it
                while True:
                    await write
                    n = self.iface.write(self.data)
                    if n == len(self.data):
                        break
                self.ofs = _REP_CONT_DATA
github trezor / trezor-firmware / src / apps / homescreen / homescreen.py View on Github external
async def layout_homescreen():
    while True:
        await loop.wait(swipe_to_rotate(), display_homescreen())
github trezor / trezor-core / src / trezor / ui / mnemonic.py View on Github external
async def edit_loop(self):
        timeout = loop.sleep(1000 * 1000 * 1)
        touch = loop.wait(io.TOUCH)
        wait_timeout = loop.spawn(touch, timeout)
        wait_touch = loop.spawn(touch)
        content = None

        self.back.taint()
        self.input.taint()

        while content is None:
            self.render()
            if self.pbutton is not None:
                wait = wait_timeout
            else:
                wait = wait_touch
            result = await wait
            if touch in wait.finished:
github trezor / trezor-firmware / core / src / trezor / ui / swipe.py View on Github external
def __iter__(self) -> loop.Task:  # type: ignore
        try:
            touch = loop.wait(io.TOUCH)
            while True:
                event, x, y = yield touch
                self.dispatch(event, x, y)
        except ui.Result as result:
            return result.value
github trezor / trezor-firmware / core / src / trezor / ui / confirm.py View on Github external
async def handle_paging(self) -> None:
        from trezor.ui.swipe import SWIPE_HORIZONTAL, SWIPE_LEFT, SWIPE_RIGHT, Swipe

        if self.pageable.is_first():
            directions = SWIPE_LEFT
        elif self.pageable.is_last():
            directions = SWIPE_RIGHT
        else:
            directions = SWIPE_HORIZONTAL

        if __debug__:
            swipe = await loop.race(Swipe(directions), swipe_signal())
        else:
            swipe = await Swipe(directions)

        if swipe == SWIPE_LEFT:
            self.pageable.next()
        else:
            self.pageable.prev()

        self.content.repaint = True
        if self.confirm is not None:
            self.confirm.repaint = True
        if self.cancel is not None:
            self.cancel.repaint = True
github trezor / trezor-core / src / trezor / ui / __init__.py View on Github external
display = Display()

# in debug mode, display an indicator in top right corner
if __debug__:

    def debug_display_refresh():
        display.bar(Display.WIDTH - 8, 0, 8, 8, 0xF800)
        display.refresh()
        if utils.SAVE_SCREEN:
            display.save("refresh")

    loop.after_step_hook = debug_display_refresh

# in both debug and production, emulator needs to draw the screen explicitly
elif utils.EMULATOR:
    loop.after_step_hook = display.refresh

# re-export constants from modtrezorui
NORMAL = Display.FONT_NORMAL
BOLD = Display.FONT_BOLD
MONO = Display.FONT_MONO
MONO_BOLD = Display.FONT_MONO_BOLD
SIZE = Display.FONT_SIZE
WIDTH = Display.WIDTH
HEIGHT = Display.HEIGHT


def lerpi(a: int, b: int, t: float) -> int:
    return int(a + t * (b - a))


def rgb(r: int, g: int, b: int) -> int:
github trezor / trezor-firmware / core / src / apps / webauthn / fido2.py View on Github external
if bcnt > _MAX_U2FHID_MSG_PAYLOAD_LEN:
            # invalid payload length, abort current msg
            if __debug__:
                log.warning(__name__, "_MAX_U2FHID_MSG_PAYLOAD_LEN")
            await send_cmd(cmd_error(ifrm.cid, _ERR_INVALID_LEN), iface)
            return None

        if datalen < bcnt:
            databuf = bytearray(bcnt)
            utils.memcpy(databuf, 0, data, 0, bcnt)
            data = databuf
        else:
            data = data[:bcnt]

        while datalen < bcnt:
            buf = await loop.race(read, loop.sleep(_CTAP_HID_TIMEOUT_MS * 1000))
            if not isinstance(buf, (bytes, bytearray)):
                await send_cmd(cmd_error(ifrm.cid, _ERR_MSG_TIMEOUT), iface)
                return None

            cfrm = overlay_struct(buf, desc_cont)

            if cfrm.seq == _CMD_INIT:
                # _CMD_INIT frame, cancels current channel
                break

            if cfrm.cid != ifrm.cid:
                # cont frame for a different channel, reply with BUSY and abort
                if __debug__:
                    log.warning(__name__, "_ERR_CHANNEL_BUSY")
                await send_cmd(cmd_error(cfrm.cid, _ERR_CHANNEL_BUSY), iface)
                continue
github trezor / trezor-firmware / core / src / apps / debug / __init__.py View on Github external
halt("debug mode inactive")

if __debug__:
    from trezor import config, io, log, loop, ui, utils, wire
    from trezor.messages import MessageType, DebugSwipeDirection
    from trezor.messages.DebugLinkLayout import DebugLinkLayout

    if False:
        from typing import List, Optional
        from trezor.messages.DebugLinkDecision import DebugLinkDecision
        from trezor.messages.DebugLinkGetState import DebugLinkGetState
        from trezor.messages.DebugLinkState import DebugLinkState

    reset_internal_entropy = None  # type: Optional[bytes]
    reset_current_words = loop.chan()
    reset_word_index = loop.chan()

    confirm_chan = loop.chan()
    swipe_chan = loop.chan()
    input_chan = loop.chan()
    confirm_signal = confirm_chan.take
    swipe_signal = swipe_chan.take
    input_signal = input_chan.take

    debuglink_decision_chan = loop.chan()

    layout_change_chan = loop.chan()
    current_content = None  # type: Optional[List[str]]

    def notify_layout_change(layout: ui.Layout) -> None:
        global current_content
github trezor / trezor-firmware / src / lib_linux / transport_pipe.py View on Github external
global read_fd, write_fd, poll

    filename_read = filename + '.to'
    filename_write = filename + '.from'

    os.mkfifo(filename_read, 0o600)
    os.mkfifo(filename_write, 0o600)

    write_fd = os.open(filename_write, os.O_RDWR, 0o600)
    read_fd = os.open(filename_read, os.O_RDWR, 0o600)

    poll = uselect.poll()
    poll.register(read_fd, uselect.POLLIN)

    # Setup polling
    loop.call_soon(watch_read())