diff --git a/src/apps/common/request_passphrase.py b/src/apps/common/request_passphrase.py index 54e1939d9..f5f20b8d2 100644 --- a/src/apps/common/request_passphrase.py +++ b/src/apps/common/request_passphrase.py @@ -1,45 +1,50 @@ from trezor import res, ui, wire +from trezor.messages import ButtonRequestType, wire_types +from trezor.messages.ButtonRequest import ButtonRequest +from trezor.messages.FailureType import ActionCancelled, ProcessError +from trezor.messages.PassphraseRequest import PassphraseRequest +from trezor.ui.entry_select import DEVICE, HOST, EntrySelector +from trezor.ui.passphrase import CANCELLED, PassphraseKeyboard +from trezor.ui.text import Text +from apps.common import storage from apps.common.cache import get_state -async def request_passphrase(ctx): - from trezor.ui.text import Text - from trezor.ui.entry_select import EntrySelector - +async def request_passphrase_entry(ctx): ui.display.clear() text = Text( 'Enter passphrase', ui.ICON_RESET, 'Where to enter your', 'passphrase?') - entry = EntrySelector(text) - entry_type = await entry + text.render() - on_device = (entry_type == 0) + ack = await ctx.call( + ButtonRequest(code=ButtonRequestType.Other), + wire_types.ButtonAck, + wire_types.Cancel) + if ack.MESSAGE_WIRE_TYPE == wire_types.Cancel: + raise wire.FailureError(ActionCancelled, 'Passphrase cancelled') - from trezor.messages.FailureType import ActionCancelled, ProcessError - from trezor.messages.PassphraseRequest import PassphraseRequest - from trezor.messages.wire_types import PassphraseAck, Cancel + return await EntrySelector(text) - ui.display.clear() - pass_req = PassphraseRequest() +async def request_passphrase(ctx): + on_device = await request_passphrase_entry(ctx) == DEVICE - if on_device: - pass_req.on_device = True - else: - from trezor.ui.text import Text + if not on_device: + ui.display.clear() text = Text( 'Passphrase entry', ui.ICON_RESET, 'Please, type passphrase', 'on connected host.') text.render() - ack = await ctx.call(pass_req, PassphraseAck, Cancel) - if ack.MESSAGE_WIRE_TYPE == Cancel: + req = PassphraseRequest(on_device=on_device) + ack = await ctx.call(req, wire_types.PassphraseAck, wire_types.Cancel) + if ack.MESSAGE_WIRE_TYPE == wire_types.Cancel: raise wire.FailureError(ActionCancelled, 'Passphrase cancelled') if on_device: if ack.passphrase is not None: raise wire.FailureError(ProcessError, 'Passphrase provided when it should not be') - from trezor.ui.passphrase import PassphraseKeyboard, CANCELLED passphrase = await PassphraseKeyboard('Enter passphrase') if passphrase == CANCELLED: raise wire.FailureError(ActionCancelled, 'Passphrase cancelled') @@ -56,8 +61,6 @@ async def request_passphrase(ctx): async def protect_by_passphrase(ctx): - from apps.common import storage - if storage.has_passphrase(): return await request_passphrase(ctx) else: diff --git a/src/trezor/ui/entry_select.py b/src/trezor/ui/entry_select.py index 1eee99e37..3624bf30b 100644 --- a/src/trezor/ui/entry_select.py +++ b/src/trezor/ui/entry_select.py @@ -4,8 +4,8 @@ from trezor import ui from trezor.ui import Widget from trezor.ui.button import Button, BTN_CLICKED -_DEVICE = const(0) -_HOST = const(1) +DEVICE = const(0) +HOST = const(1) class EntrySelector(Widget): @@ -21,9 +21,9 @@ class EntrySelector(Widget): def touch(self, event, pos): if self.device.touch(event, pos) == BTN_CLICKED: - return _DEVICE + return DEVICE if self.host.touch(event, pos) == BTN_CLICKED: - return _HOST + return HOST async def __iter__(self): return await loop.wait(super().__iter__(), self.content)