2018-03-20 15:38:21 +00:00
|
|
|
from trezor import loop, res, ui
|
2018-02-27 15:35:21 +00:00
|
|
|
from trezor.ui.confirm import CONFIRMED, ConfirmDialog
|
|
|
|
from trezor.ui.pin import PinMatrix
|
2016-06-06 12:10:36 +00:00
|
|
|
|
2018-03-20 15:38:21 +00:00
|
|
|
if __debug__:
|
|
|
|
from apps.debug import input_signal
|
|
|
|
|
2016-11-16 13:23:05 +00:00
|
|
|
|
2017-10-24 11:58:40 +00:00
|
|
|
class PinCancelled(Exception):
|
|
|
|
pass
|
2016-06-06 12:10:36 +00:00
|
|
|
|
2017-10-03 09:43:56 +00:00
|
|
|
|
2017-10-09 13:34:10 +00:00
|
|
|
@ui.layout
|
2018-03-20 15:44:23 +00:00
|
|
|
async def request_pin(label=None, cancellable: bool=True) -> str:
|
2016-06-09 14:28:34 +00:00
|
|
|
|
2017-09-21 13:57:08 +00:00
|
|
|
def onchange():
|
|
|
|
c = dialog.cancel
|
|
|
|
if matrix.pin:
|
2018-02-20 15:50:28 +00:00
|
|
|
back = res.load(ui.ICON_BACK)
|
|
|
|
if c.content is not back:
|
|
|
|
c.normal_style = ui.BTN_CLEAR['normal']
|
|
|
|
c.content = back
|
2018-02-27 19:21:54 +00:00
|
|
|
c.enable()
|
2018-02-20 15:50:28 +00:00
|
|
|
c.taint()
|
2017-09-21 13:57:08 +00:00
|
|
|
else:
|
2018-02-20 15:50:28 +00:00
|
|
|
lock = res.load(ui.ICON_LOCK)
|
2018-02-27 19:21:54 +00:00
|
|
|
if not cancellable and c.content:
|
|
|
|
c.content = ''
|
|
|
|
c.disable()
|
|
|
|
c.taint()
|
|
|
|
elif c.content is not lock:
|
2018-02-20 15:50:28 +00:00
|
|
|
c.normal_style = ui.BTN_CANCEL['normal']
|
|
|
|
c.content = lock
|
2018-02-27 19:21:54 +00:00
|
|
|
c.enable()
|
2018-02-20 15:50:28 +00:00
|
|
|
c.taint()
|
2018-02-27 19:21:54 +00:00
|
|
|
c.render()
|
2017-09-21 13:57:08 +00:00
|
|
|
|
2018-03-20 15:44:23 +00:00
|
|
|
if label is None:
|
|
|
|
label = 'Enter your PIN'
|
|
|
|
matrix = PinMatrix(label)
|
2017-09-21 13:57:08 +00:00
|
|
|
matrix.onchange = onchange
|
2016-06-09 14:28:34 +00:00
|
|
|
dialog = ConfirmDialog(matrix)
|
2018-01-11 18:44:56 +00:00
|
|
|
dialog.cancel.area = ui.grid(12)
|
|
|
|
dialog.confirm.area = ui.grid(14)
|
2017-10-09 13:34:10 +00:00
|
|
|
matrix.onchange()
|
2017-09-21 13:57:08 +00:00
|
|
|
|
|
|
|
while True:
|
2018-03-20 15:38:21 +00:00
|
|
|
if __debug__:
|
2018-04-13 12:57:04 +00:00
|
|
|
result = await loop.spawn(dialog, input_signal)
|
2018-03-20 15:38:21 +00:00
|
|
|
if isinstance(result, str):
|
|
|
|
return result
|
|
|
|
else:
|
|
|
|
result = await dialog
|
2017-10-24 11:58:40 +00:00
|
|
|
if result == CONFIRMED:
|
|
|
|
return matrix.pin
|
2018-03-20 15:38:21 +00:00
|
|
|
elif matrix.pin: # reset
|
2017-09-21 13:57:08 +00:00
|
|
|
matrix.change('')
|
|
|
|
continue
|
2018-03-20 15:38:21 +00:00
|
|
|
else: # cancel
|
2017-10-24 11:58:40 +00:00
|
|
|
raise PinCancelled()
|