mirror of
https://github.com/trezor/trezor-firmware.git
synced 2024-12-22 22:38:08 +00:00
apps/common/request_passphrase: code style
This commit is contained in:
parent
375402db67
commit
031d20b25f
@ -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)
|
||||
|
||||
|
||||
async def request_passphrase(ctx):
|
||||
on_device = await request_passphrase_entry(ctx) == DEVICE
|
||||
|
||||
if not on_device:
|
||||
ui.display.clear()
|
||||
|
||||
pass_req = PassphraseRequest()
|
||||
|
||||
if on_device:
|
||||
pass_req.on_device = True
|
||||
else:
|
||||
from trezor.ui.text import Text
|
||||
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:
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user