1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-27 16:48:09 +00:00
trezor-firmware/src/apps/common/request_passphrase.py

65 lines
2.0 KiB
Python
Raw Normal View History

from trezor import res, ui, wire
from apps.common.cache import get_state
2016-10-20 13:04:54 +00: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 = (entry_type == 0)
from trezor.messages.FailureType import ActionCancelled, ProcessError
2016-10-20 13:04:54 +00:00
from trezor.messages.PassphraseRequest import PassphraseRequest
2017-01-24 13:10:36 +00: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 13:04:54 +00: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
if ack.state is not None:
if ack.state != get_state(salt=ack.state[:32], passphrase=passphrase):
raise wire.FailureError(ProcessError, 'Passphrase mismatch')
2017-01-24 13:10:36 +00:00
return passphrase
2017-01-24 13:10:36 +00:00
2017-08-15 13:09:09 +00:00
async def protect_by_passphrase(ctx):
2017-01-24 13:10:36 +00:00
from apps.common import storage
2017-10-24 11:58:40 +00:00
if storage.has_passphrase():
2017-08-15 13:09:09 +00:00
return await request_passphrase(ctx)
2017-01-24 13:10:36 +00:00
else:
return ''