2018-02-28 17:25:04 +00:00
|
|
|
from trezor import ui, wire
|
2018-02-20 14:19:47 +00:00
|
|
|
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
|
2018-02-28 23:07:45 +00:00
|
|
|
from trezor.messages.PassphraseStateRequest import PassphraseStateRequest
|
2018-02-27 02:05:15 +00:00
|
|
|
from trezor.ui.entry_select import DEVICE, EntrySelector
|
2018-02-20 14:19:47 +00:00
|
|
|
from trezor.ui.passphrase import CANCELLED, PassphraseKeyboard
|
|
|
|
from trezor.ui.text import Text
|
|
|
|
from apps.common import storage
|
2018-02-24 17:58:02 +00:00
|
|
|
from apps.common.cache import get_state
|
2016-10-20 13:04:54 +00:00
|
|
|
|
2018-02-14 22:53:10 +00:00
|
|
|
|
2018-02-26 17:40:44 +00:00
|
|
|
@ui.layout
|
2018-02-20 14:19:47 +00:00
|
|
|
async def request_passphrase_entry(ctx):
|
2018-02-14 18:10:42 +00:00
|
|
|
text = Text(
|
2018-02-27 22:48:51 +00:00
|
|
|
'Enter passphrase', ui.ICON_CONFIG,
|
2018-02-14 18:10:42 +00:00
|
|
|
'Where to enter your', 'passphrase?')
|
2018-02-20 14:19:47 +00:00
|
|
|
text.render()
|
2018-02-05 19:26:13 +00:00
|
|
|
|
2018-02-20 14:19:47 +00:00
|
|
|
ack = await ctx.call(
|
2018-02-27 11:07:42 +00:00
|
|
|
ButtonRequest(code=ButtonRequestType.PassphraseType),
|
2018-02-20 14:19:47 +00:00
|
|
|
wire_types.ButtonAck,
|
|
|
|
wire_types.Cancel)
|
|
|
|
if ack.MESSAGE_WIRE_TYPE == wire_types.Cancel:
|
|
|
|
raise wire.FailureError(ActionCancelled, 'Passphrase cancelled')
|
2018-02-05 19:26:13 +00:00
|
|
|
|
2018-02-28 16:16:33 +00:00
|
|
|
selector = EntrySelector(text)
|
|
|
|
return await ctx.wait(selector)
|
2018-02-05 19:26:13 +00:00
|
|
|
|
|
|
|
|
2018-02-26 17:40:44 +00:00
|
|
|
@ui.layout
|
|
|
|
async def request_passphrase_ack(ctx, on_device):
|
2018-02-20 14:19:47 +00:00
|
|
|
if not on_device:
|
2018-02-14 18:10:42 +00:00
|
|
|
text = Text(
|
2018-02-27 22:48:51 +00:00
|
|
|
'Passphrase entry', ui.ICON_CONFIG,
|
2018-02-14 18:10:42 +00:00
|
|
|
'Please, type passphrase', 'on connected host.')
|
|
|
|
text.render()
|
2016-10-20 13:04:54 +00:00
|
|
|
|
2018-02-20 14:19:47 +00:00
|
|
|
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:
|
2018-02-14 18:10:42 +00:00
|
|
|
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')
|
2018-02-28 16:16:33 +00:00
|
|
|
keyboard = PassphraseKeyboard('Enter passphrase')
|
|
|
|
passphrase = await ctx.wait(keyboard)
|
2018-02-14 18:10:42 +00:00
|
|
|
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
|
|
|
|
|
2018-02-28 23:07:45 +00:00
|
|
|
req = PassphraseStateRequest(state=get_state(state=ack.state, passphrase=passphrase))
|
|
|
|
ack = await ctx.call(req, wire_types.PassphraseStateAck, wire_types.Cancel)
|
|
|
|
|
|
|
|
return passphrase
|
2017-01-24 13:10:36 +00:00
|
|
|
|
2018-02-26 17:40:44 +00:00
|
|
|
|
|
|
|
async def request_passphrase(ctx):
|
|
|
|
on_device = await request_passphrase_entry(ctx) == DEVICE
|
2018-02-28 23:07:45 +00:00
|
|
|
passphrase = await request_passphrase_ack(ctx, on_device)
|
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-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 ''
|