1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-18 05:28:40 +00:00
trezor-firmware/src/apps/common/request_pin.py

63 lines
1.7 KiB
Python
Raw Normal View History

2018-02-27 15:35:21 +00:00
from trezor import res, ui
from trezor.messages import PinMatrixRequestType
from trezor.ui.confirm import CONFIRMED, ConfirmDialog
from trezor.ui.pin import PinMatrix
2017-10-24 11:58:40 +00:00
class PinCancelled(Exception):
pass
2017-10-03 09:43:56 +00:00
@ui.layout
async def request_pin(code: int = None, cancellable: bool = True) -> str:
2016-06-09 14:28:34 +00:00
def onchange():
c = dialog.cancel
if matrix.pin:
back = res.load(ui.ICON_BACK)
if c.content is not back:
c.normal_style = ui.BTN_CLEAR['normal']
c.content = back
c.taint()
c.render()
else:
lock = res.load(ui.ICON_LOCK)
if c.content is not lock and cancellable:
c.normal_style = ui.BTN_CANCEL['normal']
c.content = lock
c.taint()
c.render()
2018-02-27 15:35:21 +00:00
label = _get_label(code)
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)
matrix.onchange()
if not cancellable:
dialog.cancel.content = ''
dialog.cancel.disable()
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:
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):
if code is None:
code = PinMatrixRequestType.Current
if code == PinMatrixRequestType.NewFirst:
label = 'Enter new PIN'
elif code == PinMatrixRequestType.NewSecond:
2018-02-26 18:02:09 +00:00
label = 'Re-enter new PIN'
else: # PinMatrixRequestType.Current
2018-02-26 18:02:09 +00:00
label = 'Enter your PIN'
2017-10-24 11:58:40 +00:00
return label