1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-02-23 13:02:03 +00:00
trezor-firmware/src/apps/common/request_pin.py

56 lines
1.4 KiB
Python
Raw Normal View History

2017-10-24 11:58:40 +00:00
from trezor import res
from trezor import ui
2017-10-24 11:58:40 +00:00
class PinCancelled(Exception):
pass
2017-10-03 09:43:56 +00:00
@ui.layout
2017-10-24 11:58:40 +00:00
async def request_pin(code: int = None) -> str:
from trezor.ui.confirm import ConfirmDialog, CONFIRMED
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
def onchange():
c = dialog.cancel
if matrix.pin:
2017-10-24 11:58:40 +00:00
c.content = res.load(ui.ICON_CLEAR)
else:
2017-10-24 11:58:40 +00:00
c.content = res.load(ui.ICON_LOCK)
c.taint()
c.render()
ui.display.clear()
matrix = PinMatrix(label, with_zero=True)
matrix.onchange = onchange
2016-06-09 14:28:34 +00:00
dialog = ConfirmDialog(matrix)
dialog.cancel.area = (0, 240 - 48, 80, 48)
dialog.confirm.area = (240 - 80, 240 - 48, 80, 48)
matrix.onchange()
while True:
2017-10-24 11:58:40 +00:00
result = await dialog
2017-10-24 11:58:40 +00:00
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):
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 PIN again'
else: # PinMatrixRequestType.Current
label = 'Enter PIN'
2017-10-24 11:58:40 +00:00
return label