2018-02-05 19:26:13 +00:00
|
|
|
from trezor import res, ui, wire
|
2016-10-20 13:04:54 +00:00
|
|
|
|
2018-02-14 22:53:10 +00:00
|
|
|
|
2018-02-14 18:10:42 +00:00
|
|
|
async def request_passphrase(ctx):
|
|
|
|
from trezor.ui.text import Text
|
|
|
|
from trezor.ui.entry_select import EntrySelector
|
2018-02-05 19:26:13 +00:00
|
|
|
|
|
|
|
ui.display.clear()
|
2018-02-14 18:10:42 +00:00
|
|
|
text = Text(
|
|
|
|
'Enter passphrase', ui.ICON_RESET,
|
|
|
|
'Where to enter your', 'passphrase?')
|
|
|
|
entry = EntrySelector(text)
|
|
|
|
entry_type = await entry
|
2018-02-05 19:26:13 +00:00
|
|
|
|
2018-02-14 22:53:10 +00:00
|
|
|
on_device = (entry_type == 0)
|
2018-02-05 19:26:13 +00:00
|
|
|
|
2018-02-14 18:10:42 +00:00
|
|
|
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
|
2018-02-05 19:26:13 +00:00
|
|
|
|
|
|
|
ui.display.clear()
|
|
|
|
|
2018-02-14 18:10:42 +00:00
|
|
|
pass_req = PassphraseRequest()
|
2018-02-05 19:26:13 +00:00
|
|
|
|
2018-02-14 18:10:42 +00:00
|
|
|
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
|
|
|
|
2018-02-14 18:10:42 +00:00
|
|
|
ack = await ctx.call(pass_req, PassphraseAck, Cancel)
|
|
|
|
if ack.MESSAGE_WIRE_TYPE == Cancel:
|
|
|
|
raise wire.FailureError(ActionCancelled, 'Passphrase cancelled')
|
2018-01-29 18:26:42 +00:00
|
|
|
|
2018-02-14 18:10:42 +00:00
|
|
|
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')
|
2018-01-29 18:26:42 +00:00
|
|
|
else:
|
2018-02-14 18:10:42 +00:00
|
|
|
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 13:10:36 +00:00
|
|
|
|
2018-02-14 18:10:42 +00:00
|
|
|
return passphrase
|
2017-01-24 13:10:36 +00:00
|
|
|
|
2018-02-14 22:53:10 +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 ''
|