mirror of
https://github.com/trezor/trezor-firmware.git
synced 2025-01-22 05:10:56 +00:00
src/apps/common: refactor passphrase request, implement new on_device field
This commit is contained in:
parent
75e87ede86
commit
3c738b62c4
@ -1,34 +1,5 @@
|
||||
from trezor import res, ui, wire
|
||||
|
||||
|
||||
async def request_passphrase_on_display(ctx):
|
||||
from trezor.messages.FailureType import ActionCancelled
|
||||
from trezor.ui.passphrase import PassphraseKeyboard, CANCELLED
|
||||
|
||||
ui.display.clear()
|
||||
passphrase = await PassphraseKeyboard('Enter passphrase')
|
||||
if passphrase == CANCELLED:
|
||||
raise wire.FailureError(ActionCancelled, 'Passphrase cancelled')
|
||||
return passphrase
|
||||
|
||||
|
||||
async def request_passphrase_on_host(ctx):
|
||||
from trezor.messages.FailureType import ActionCancelled
|
||||
from trezor.messages.PassphraseRequest import PassphraseRequest
|
||||
from trezor.messages.wire_types import PassphraseAck, Cancel
|
||||
from trezor.ui.text import Text
|
||||
|
||||
text = Text(
|
||||
'Passphrase entry', ui.ICON_RESET,
|
||||
'Please, type passphrase', 'on connected host.')
|
||||
ui.display.clear()
|
||||
text.render()
|
||||
ack = await ctx.call(PassphraseRequest(), PassphraseAck, Cancel)
|
||||
if ack.MESSAGE_WIRE_TYPE == Cancel:
|
||||
raise wire.FailureError(ActionCancelled, 'Passphrase cancelled')
|
||||
return ack.passphrase
|
||||
|
||||
|
||||
async def request_passphrase(ctx):
|
||||
from trezor.ui.text import Text
|
||||
from trezor.ui.entry_select import EntrySelector
|
||||
@ -40,11 +11,44 @@ async def request_passphrase(ctx):
|
||||
entry = EntrySelector(text)
|
||||
entry_type = await entry
|
||||
|
||||
if entry_type == 1:
|
||||
return await request_passphrase_on_host(ctx)
|
||||
else:
|
||||
return await request_passphrase_on_display(ctx)
|
||||
on_device = on_device=(entry_type == 0)
|
||||
|
||||
from trezor.messages.FailureType import ActionCancelled, ProcessError
|
||||
from trezor.messages.PassphraseRequest import PassphraseRequest
|
||||
from trezor.messages.wire_types import PassphraseAck, Cancel
|
||||
|
||||
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:
|
||||
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')
|
||||
else:
|
||||
if ack.passphrase is None:
|
||||
raise wire.FailureError(ProcessError, 'Passphrase not provided')
|
||||
passphrase = ack.passphrase
|
||||
|
||||
# TODO: process ack.state and check against the current device state, throw error if different
|
||||
|
||||
return passphrase
|
||||
|
||||
async def protect_by_passphrase(ctx):
|
||||
from apps.common import storage
|
||||
|
Loading…
Reference in New Issue
Block a user