2017-10-24 11:58:40 +00:00
|
|
|
from trezor import res
|
|
|
|
from trezor import ui
|
2016-06-06 12:10:36 +00:00
|
|
|
|
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
|
2017-10-24 11:58:40 +00:00
|
|
|
async def request_pin(code: int = None) -> str:
|
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
|
|
|
|
2017-10-24 11:58:40 +00:00
|
|
|
label = _get_label(code)
|
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-05 13:26:12 +00:00
|
|
|
c.normal_style = ui.BTN_CLEAR['normal']
|
|
|
|
c.content = res.load(ui.ICON_BACK)
|
2017-09-21 13:57:08 +00:00
|
|
|
else:
|
2018-02-05 13:26:12 +00:00
|
|
|
c.normal_style = ui.BTN_CANCEL['normal']
|
2017-10-24 11:58:40 +00:00
|
|
|
c.content = res.load(ui.ICON_LOCK)
|
2017-09-21 13:57:08 +00:00
|
|
|
c.taint()
|
|
|
|
c.render()
|
|
|
|
|
2016-10-06 10:31:03 +00:00
|
|
|
ui.display.clear()
|
2017-09-21 13:57:08 +00:00
|
|
|
matrix = PinMatrix(label, with_zero=True)
|
|
|
|
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:
|
2017-10-24 11:58:40 +00:00
|
|
|
result = await dialog
|
|
|
|
if result == CONFIRMED:
|
|
|
|
return matrix.pin
|
|
|
|
elif result != CONFIRMED and matrix.pin:
|
2017-09-21 13:57:08 +00:00
|
|
|
matrix.change('')
|
|
|
|
continue
|
|
|
|
else:
|
2017-10-24 11:58:40 +00:00
|
|
|
raise PinCancelled()
|
2017-01-17 16:43:08 +00:00
|
|
|
|
|
|
|
|
2017-10-24 11:58:40 +00:00
|
|
|
def _get_label(code: int):
|
2016-11-16 13:23:05 +00:00
|
|
|
from trezor.messages import PinMatrixRequestType
|
|
|
|
if code is None:
|
|
|
|
code = PinMatrixRequestType.Current
|
|
|
|
if code == PinMatrixRequestType.NewFirst:
|
|
|
|
label = 'Enter new PIN'
|
|
|
|
elif code == PinMatrixRequestType.NewSecond:
|
2016-12-19 10:32:08 +00:00
|
|
|
label = 'Enter PIN again'
|
2016-11-16 13:23:05 +00:00
|
|
|
else: # PinMatrixRequestType.Current
|
|
|
|
label = 'Enter PIN'
|
2017-10-24 11:58:40 +00:00
|
|
|
return label
|