From 01832d5ae9c5914ce49276ea31bdf90c8279ff43 Mon Sep 17 00:00:00 2001 From: matejcik Date: Mon, 25 May 2020 12:49:16 +0200 Subject: [PATCH] core: call close_others() in place of ButtonRequest this makes sense, really: close_others() requests UI exclusivity, and that is something that generally happens at the same places we emit a ButtonRequest --- core/src/apps/common/__init__.py | 16 ++++++++++++++++ core/src/apps/common/confirm.py | 11 ++++++----- core/src/apps/common/passphrase.py | 6 +++--- core/src/apps/common/request_pin.py | 7 +++---- core/src/apps/debug/__init__.py | 4 ++-- .../apps/management/recovery_device/layout.py | 7 +++---- core/src/apps/monero/layout/common.py | 6 +++--- 7 files changed, 36 insertions(+), 21 deletions(-) diff --git a/core/src/apps/common/__init__.py b/core/src/apps/common/__init__.py index 7be160a22..925fa42b0 100644 --- a/core/src/apps/common/__init__.py +++ b/core/src/apps/common/__init__.py @@ -1,3 +1,19 @@ from micropython import const +from trezor import workflow +from trezor.messages import ButtonRequestType +from trezor.messages.ButtonAck import ButtonAck +from trezor.messages.ButtonRequest import ButtonRequest + +if False: + from trezor import wire + from trezor.messages.ButtonRequest import EnumTypeButtonRequestType + HARDENED = const(0x80000000) + + +async def button_request( + ctx: wire.GenericContext, code: EnumTypeButtonRequestType = ButtonRequestType.Other +) -> None: + workflow.close_others() + await ctx.call(ButtonRequest(code=code), ButtonAck) diff --git a/core/src/apps/common/confirm.py b/core/src/apps/common/confirm.py index 6e3a2f4b1..8696b50f3 100644 --- a/core/src/apps/common/confirm.py +++ b/core/src/apps/common/confirm.py @@ -1,12 +1,13 @@ from trezor import wire from trezor.messages import ButtonRequestType -from trezor.messages.ButtonAck import ButtonAck -from trezor.messages.ButtonRequest import ButtonRequest from trezor.ui.confirm import CONFIRMED, INFO, Confirm, HoldToConfirm, InfoConfirm +from . import button_request + if __debug__: from trezor.ui.scroll import Paginated + if False: from typing import Any, Callable, Optional from trezor import ui @@ -25,7 +26,7 @@ async def confirm( cancel_style: ButtonStyleType = Confirm.DEFAULT_CANCEL_STYLE, major_confirm: bool = False, ) -> bool: - await ctx.call(ButtonRequest(code=code), ButtonAck) + await button_request(ctx, code=code) if content.__class__.__name__ == "Paginated": # The following works because asserts are omitted in non-debug builds. @@ -61,7 +62,7 @@ async def info_confirm( info: ButtonContent = InfoConfirm.DEFAULT_INFO, info_style: ButtonStyleType = InfoConfirm.DEFAULT_INFO_STYLE, ) -> bool: - await ctx.call(ButtonRequest(code=code), ButtonAck) + await button_request(ctx, code=code) dialog = InfoConfirm( content, confirm, confirm_style, cancel, cancel_style, info, info_style @@ -86,7 +87,7 @@ async def hold_to_confirm( loader_style: LoaderStyleType = HoldToConfirm.DEFAULT_LOADER_STYLE, cancel: bool = True, ) -> bool: - await ctx.call(ButtonRequest(code=code), ButtonAck) + await button_request(ctx, code=code) if content.__class__.__name__ == "Paginated": # The following works because asserts are omitted in non-debug builds. diff --git a/core/src/apps/common/passphrase.py b/core/src/apps/common/passphrase.py index a15bbe48a..d6d2422a6 100644 --- a/core/src/apps/common/passphrase.py +++ b/core/src/apps/common/passphrase.py @@ -3,14 +3,14 @@ from micropython import const import storage.device from trezor import wire, workflow from trezor.messages import ButtonRequestType -from trezor.messages.ButtonAck import ButtonAck -from trezor.messages.ButtonRequest import ButtonRequest from trezor.messages.PassphraseAck import PassphraseAck from trezor.messages.PassphraseRequest import PassphraseRequest from trezor.ui import ICON_CONFIG, draw_simple from trezor.ui.passphrase import CANCELLED, PassphraseKeyboard from trezor.ui.text import Text +from . import button_request + _MAX_PASSPHRASE_LEN = const(50) @@ -56,7 +56,7 @@ async def _request_on_host(ctx: wire.Context) -> str: async def _request_on_device(ctx: wire.Context) -> str: - await ctx.call(ButtonRequest(code=ButtonRequestType.PassphraseEntry), ButtonAck) + await button_request(ctx, code=ButtonRequestType.PassphraseEntry) keyboard = PassphraseKeyboard("Enter passphrase", _MAX_PASSPHRASE_LEN) passphrase = await ctx.wait(keyboard) diff --git a/core/src/apps/common/request_pin.py b/core/src/apps/common/request_pin.py index 65828d562..89c287795 100644 --- a/core/src/apps/common/request_pin.py +++ b/core/src/apps/common/request_pin.py @@ -3,14 +3,13 @@ import utime import storage.sd_salt from trezor import config, ui, wire from trezor.messages import ButtonRequestType -from trezor.messages.ButtonAck import ButtonAck -from trezor.messages.ButtonRequest import ButtonRequest from trezor.pin import pin_to_int from trezor.ui.pin import CANCELLED, PinDialog from trezor.ui.popup import Popup from trezor.ui.text import Text -from apps.common.sdcard import SdCardUnavailable, request_sd_salt +from . import button_request +from .sdcard import SdCardUnavailable, request_sd_salt if False: from typing import Any, NoReturn, Optional, Tuple @@ -30,7 +29,7 @@ async def request_pin( attempts_remaining: int = None, allow_cancel: bool = True, ) -> str: - await ctx.call(ButtonRequest(code=ButtonRequestType.PinEntry), ButtonAck) + await button_request(ctx, code=ButtonRequestType.PinEntry) if attempts_remaining is None: subprompt = None diff --git a/core/src/apps/debug/__init__.py b/core/src/apps/debug/__init__.py index 545ae579f..dc835a66e 100644 --- a/core/src/apps/debug/__init__.py +++ b/core/src/apps/debug/__init__.py @@ -79,12 +79,12 @@ if __debug__: content = await layout_change_chan.take() await ctx.write(DebugLinkLayout(lines=content)) - async def dispatch_DebugLinkWatchLayout( # type: ignore + async def dispatch_DebugLinkWatchLayout( ctx: wire.Context, msg: DebugLinkWatchLayout ) -> Success: global watch_layout_changes layout_change_chan.putters.clear() - watch_layout_changes = msg.watch + watch_layout_changes = bool(msg.watch) log.debug(__name__, "Watch layout changes: {}".format(watch_layout_changes)) return Success() diff --git a/core/src/apps/management/recovery_device/layout.py b/core/src/apps/management/recovery_device/layout.py index 4468775ce..bbd03e45b 100644 --- a/core/src/apps/management/recovery_device/layout.py +++ b/core/src/apps/management/recovery_device/layout.py @@ -2,12 +2,11 @@ import storage.recovery from trezor import strings, ui, wire from trezor.crypto.slip39 import MAX_SHARE_COUNT from trezor.messages import ButtonRequestType -from trezor.messages.ButtonAck import ButtonAck -from trezor.messages.ButtonRequest import ButtonRequest from trezor.ui.scroll import Paginated from trezor.ui.text import Text from trezor.ui.word_select import WordSelector +from apps.common import button_request from apps.common.confirm import confirm, info_confirm, require_confirm from apps.common.layout import show_success, show_warning from apps.management import backup_types @@ -34,7 +33,7 @@ async def confirm_abort(ctx: wire.GenericContext, dry_run: bool = False) -> bool async def request_word_count(ctx: wire.GenericContext, dry_run: bool) -> int: - await ctx.call(ButtonRequest(code=ButtonRequestType.MnemonicWordCount), ButtonAck) + await button_request(ctx, code=ButtonRequestType.MnemonicWordCount) if dry_run: text = Text("Seed check", ui.ICON_RECOVERY) @@ -52,7 +51,7 @@ async def request_word_count(ctx: wire.GenericContext, dry_run: bool) -> int: async def request_mnemonic( ctx: wire.GenericContext, word_count: int, backup_type: Optional[EnumTypeBackupType] ) -> Optional[str]: - await ctx.call(ButtonRequest(code=ButtonRequestType.MnemonicInput), ButtonAck) + await button_request(ctx, code=ButtonRequestType.MnemonicInput) words = [] # type: List[str] for i in range(word_count): diff --git a/core/src/apps/monero/layout/common.py b/core/src/apps/monero/layout/common.py index b36cc00d6..b5818a555 100644 --- a/core/src/apps/monero/layout/common.py +++ b/core/src/apps/monero/layout/common.py @@ -1,9 +1,9 @@ from trezor import strings, ui, utils from trezor.messages import ButtonRequestType -from trezor.messages.ButtonAck import ButtonAck -from trezor.messages.ButtonRequest import ButtonRequest from trezor.ui.text import Text +from apps.common import button_request + async def naive_pagination( ctx, lines, title, icon=ui.ICON_RESET, icon_color=ui.ORANGE, per_page=5 @@ -25,7 +25,7 @@ async def naive_pagination( paginated = PaginatedWithButtons(pages, one_by_one=True) while True: - await ctx.call(ButtonRequest(code=ButtonRequestType.SignTx), ButtonAck) + await button_request(ctx, code=ButtonRequestType.SignTx) result = await ctx.wait(paginated) if result is CONFIRMED: return True