trezor-core/src/apps/common/request_passphrase.py

60 lines
1.9 KiB
Python
Raw Normal View History

from trezor import res, ui, wire
2016-10-20 06:04:54 -07:00
async def request_passphrase(ctx):
from trezor.ui.text import Text
from trezor.ui.entry_select import EntrySelector
ui.display.clear()
text = Text(
'Enter passphrase', ui.ICON_RESET,
'Where to enter your', 'passphrase?')
entry = EntrySelector(text)
entry_type = await entry
on_device = on_device=(entry_type == 0)
from trezor.messages.FailureType import ActionCancelled, ProcessError
2016-10-20 06:04:54 -07:00
from trezor.messages.PassphraseRequest import PassphraseRequest
2017-01-24 05:10:36 -08:00
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()
2016-10-20 06:04:54 -07:00
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
2017-01-24 05:10:36 -08:00
return passphrase
2017-01-24 05:10:36 -08:00
2017-08-15 06:09:09 -07:00
async def protect_by_passphrase(ctx):
2017-01-24 05:10:36 -08:00
from apps.common import storage
2017-10-24 04:58:40 -07:00
if storage.has_passphrase():
2017-08-15 06:09:09 -07:00
return await request_passphrase(ctx)
2017-01-24 05:10:36 -08:00
else:
return ''