2016-06-09 14:28:34 +00:00
|
|
|
from trezor import ui
|
2016-06-06 12:10:36 +00:00
|
|
|
from trezor import wire
|
2016-09-21 12:24:12 +00:00
|
|
|
from trezor.utils import unimport
|
2016-06-06 12:10:36 +00:00
|
|
|
|
2016-11-23 13:51:39 +00:00
|
|
|
if __debug__:
|
|
|
|
matrix = None
|
2016-11-16 13:23:05 +00:00
|
|
|
|
2016-06-06 12:10:36 +00:00
|
|
|
|
2016-09-25 14:00:21 +00:00
|
|
|
@unimport
|
2016-11-16 13:23:05 +00:00
|
|
|
async def request_pin_on_display(session_id: int, code: int=None) -> str:
|
2016-09-25 14:00:21 +00:00
|
|
|
from trezor.messages.ButtonRequest import ButtonRequest
|
|
|
|
from trezor.messages.ButtonRequestType import ProtectCall
|
|
|
|
from trezor.messages.FailureType import PinCancelled
|
|
|
|
from trezor.messages.wire_types import ButtonAck
|
2016-06-06 12:10:36 +00:00
|
|
|
from trezor.ui.confirm import ConfirmDialog, CONFIRMED
|
2016-09-25 14:00:21 +00:00
|
|
|
from trezor.ui.pin import PinMatrix
|
2016-06-09 14:28:34 +00:00
|
|
|
|
2016-11-23 13:51:39 +00:00
|
|
|
if __debug__:
|
|
|
|
global matrix
|
2016-11-16 13:23:05 +00:00
|
|
|
|
|
|
|
_, label = _get_code_and_label(code)
|
|
|
|
|
2016-09-25 14:00:21 +00:00
|
|
|
await wire.reply_message(session_id,
|
|
|
|
ButtonRequest(code=ProtectCall),
|
|
|
|
ButtonAck)
|
2016-06-09 14:28:34 +00:00
|
|
|
|
2016-10-06 10:31:03 +00:00
|
|
|
ui.display.clear()
|
2016-11-16 13:23:05 +00:00
|
|
|
matrix = PinMatrix(label)
|
2016-06-09 14:28:34 +00:00
|
|
|
dialog = ConfirmDialog(matrix)
|
2016-11-16 13:23:05 +00:00
|
|
|
pin = matrix.pin
|
|
|
|
matrix = None
|
|
|
|
|
2016-09-25 14:00:21 +00:00
|
|
|
if await dialog != CONFIRMED:
|
|
|
|
raise wire.FailureError(PinCancelled, 'PIN cancelled')
|
2016-11-16 13:23:05 +00:00
|
|
|
return pin
|
2016-06-09 14:28:34 +00:00
|
|
|
|
2016-11-16 13:23:05 +00:00
|
|
|
|
|
|
|
@unimport
|
|
|
|
async def request_pin_on_client(session_id: int, code: int=None) -> str:
|
|
|
|
from trezor.messages.FailureType import PinCancelled
|
|
|
|
from trezor.messages.PinMatrixRequest import PinMatrixRequest
|
|
|
|
from trezor.messages.wire_types import PinMatrixAck, Cancel
|
|
|
|
from trezor.ui.pin import PinMatrix
|
|
|
|
|
2016-11-23 13:51:39 +00:00
|
|
|
if __debug__:
|
|
|
|
global matrix
|
2016-11-16 13:23:05 +00:00
|
|
|
|
|
|
|
code, label = _get_code_and_label(code)
|
|
|
|
|
|
|
|
ui.display.clear()
|
|
|
|
matrix = PinMatrix(label)
|
|
|
|
matrix.render()
|
|
|
|
|
|
|
|
ack = await wire.reply_message(session_id,
|
|
|
|
PinMatrixRequest(code=code),
|
|
|
|
PinMatrixAck, Cancel)
|
|
|
|
digits = matrix.digits
|
|
|
|
matrix = None
|
|
|
|
|
|
|
|
if ack.WIRE_TYPE == Cancel:
|
|
|
|
raise wire.FailureError(PinCancelled, 'PIN cancelled')
|
|
|
|
return _decode_pin(ack.pin, digits)
|
|
|
|
|
|
|
|
|
|
|
|
request_pin = request_pin_on_client
|
2016-06-09 14:28:34 +00:00
|
|
|
|
|
|
|
|
2016-09-25 14:00:21 +00:00
|
|
|
@unimport
|
2016-11-16 13:23:05 +00:00
|
|
|
async def request_pin_twice(session_id: int) -> str:
|
2016-06-09 14:28:34 +00:00
|
|
|
from trezor.messages.FailureType import PinInvalid
|
2016-11-16 13:23:05 +00:00
|
|
|
from trezor.messages import PinMatrixRequestType
|
2016-06-09 14:28:34 +00:00
|
|
|
|
2016-11-16 13:23:05 +00:00
|
|
|
pin_first = await request_pin(session_id, PinMatrixRequestType.NewFirst)
|
|
|
|
pin_again = await request_pin(session_id, PinMatrixRequestType.NewSecond)
|
2016-09-25 14:00:21 +00:00
|
|
|
if pin_first != pin_again:
|
|
|
|
raise wire.FailureError(PinInvalid, 'PIN invalid')
|
2016-06-06 12:10:36 +00:00
|
|
|
|
2016-09-25 14:00:21 +00:00
|
|
|
return pin_first
|
2016-11-16 13:23:05 +00:00
|
|
|
|
|
|
|
|
2016-11-23 13:51:39 +00:00
|
|
|
async def protect_by_pin(session_id: int):
|
|
|
|
from . import storage
|
|
|
|
|
|
|
|
while storage.is_locked():
|
|
|
|
pin = await request_pin(session_id)
|
|
|
|
storage.unlock(pin, _render_pin_failure)
|
|
|
|
|
|
|
|
|
|
|
|
def _render_pin_failure(sleep_ms: int):
|
|
|
|
ui.display.clear()
|
|
|
|
ui.display.text_center(240, 240, 'Sleeping for %d seconds' % sleep_ms / 1000,
|
|
|
|
ui.BOLD, ui.RED, ui.BLACK)
|
|
|
|
|
|
|
|
|
2016-11-16 13:23:05 +00:00
|
|
|
def _get_code_and_label(code: int) -> str:
|
|
|
|
from trezor.messages import PinMatrixRequestType
|
|
|
|
if code is None:
|
|
|
|
code = PinMatrixRequestType.Current
|
|
|
|
if code == PinMatrixRequestType.NewFirst:
|
|
|
|
label = 'Enter new PIN'
|
|
|
|
elif code == PinMatrixRequestType.NewSecond:
|
|
|
|
label = 'Enter new PIN again'
|
|
|
|
else: # PinMatrixRequestType.Current
|
|
|
|
label = 'Enter PIN'
|
|
|
|
return code, label
|
|
|
|
|
|
|
|
|
|
|
|
def _decode_pin(pin: str, secret: list) -> str:
|
|
|
|
return ''.join([str(secret[int(d) - 1]) for d in pin])
|