apps/common/request_passphrase: code style

pull/25/head
Jan Pochyla 6 years ago
parent 375402db67
commit 031d20b25f

@ -1,45 +1,50 @@
from trezor import res, ui, wire 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 from apps.common.cache import get_state
async def request_passphrase(ctx): async def request_passphrase_entry(ctx):
from trezor.ui.text import Text
from trezor.ui.entry_select import EntrySelector
ui.display.clear() ui.display.clear()
text = Text( text = Text(
'Enter passphrase', ui.ICON_RESET, 'Enter passphrase', ui.ICON_RESET,
'Where to enter your', 'passphrase?') 'Where to enter your', 'passphrase?')
entry = EntrySelector(text) text.render()
entry_type = await entry
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 return await EntrySelector(text)
from trezor.messages.PassphraseRequest import PassphraseRequest
from trezor.messages.wire_types import PassphraseAck, Cancel
ui.display.clear()
pass_req = PassphraseRequest() async def request_passphrase(ctx):
on_device = await request_passphrase_entry(ctx) == DEVICE
if on_device: if not on_device:
pass_req.on_device = True ui.display.clear()
else:
from trezor.ui.text import Text
text = Text( text = Text(
'Passphrase entry', ui.ICON_RESET, 'Passphrase entry', ui.ICON_RESET,
'Please, type passphrase', 'on connected host.') 'Please, type passphrase', 'on connected host.')
text.render() text.render()
ack = await ctx.call(pass_req, PassphraseAck, Cancel) req = PassphraseRequest(on_device=on_device)
if ack.MESSAGE_WIRE_TYPE == Cancel: 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') raise wire.FailureError(ActionCancelled, 'Passphrase cancelled')
if on_device: if on_device:
if ack.passphrase is not None: if ack.passphrase is not None:
raise wire.FailureError(ProcessError, 'Passphrase provided when it should not be') raise wire.FailureError(ProcessError, 'Passphrase provided when it should not be')
from trezor.ui.passphrase import PassphraseKeyboard, CANCELLED
passphrase = await PassphraseKeyboard('Enter passphrase') passphrase = await PassphraseKeyboard('Enter passphrase')
if passphrase == CANCELLED: if passphrase == CANCELLED:
raise wire.FailureError(ActionCancelled, 'Passphrase cancelled') raise wire.FailureError(ActionCancelled, 'Passphrase cancelled')
@ -56,8 +61,6 @@ async def request_passphrase(ctx):
async def protect_by_passphrase(ctx): async def protect_by_passphrase(ctx):
from apps.common import storage
if storage.has_passphrase(): if storage.has_passphrase():
return await request_passphrase(ctx) return await request_passphrase(ctx)
else: else:

@ -4,8 +4,8 @@ from trezor import ui
from trezor.ui import Widget from trezor.ui import Widget
from trezor.ui.button import Button, BTN_CLICKED from trezor.ui.button import Button, BTN_CLICKED
_DEVICE = const(0) DEVICE = const(0)
_HOST = const(1) HOST = const(1)
class EntrySelector(Widget): class EntrySelector(Widget):
@ -21,9 +21,9 @@ class EntrySelector(Widget):
def touch(self, event, pos): def touch(self, event, pos):
if self.device.touch(event, pos) == BTN_CLICKED: if self.device.touch(event, pos) == BTN_CLICKED:
return _DEVICE return DEVICE
if self.host.touch(event, pos) == BTN_CLICKED: if self.host.touch(event, pos) == BTN_CLICKED:
return _HOST return HOST
async def __iter__(self): async def __iter__(self):
return await loop.wait(super().__iter__(), self.content) return await loop.wait(super().__iter__(), self.content)

Loading…
Cancel
Save