1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-01-09 15:00:58 +00:00

apps/common/request_pin: add support for 0

This commit is contained in:
Jan Pochyla 2017-09-21 15:57:08 +02:00
parent f8d961f839
commit 3379c94598
2 changed files with 46 additions and 30 deletions

View File

@ -23,18 +23,35 @@ async def request_pin_on_display(ctx: wire.Context, code: int=None) -> str:
await ctx.call(ButtonRequest(code=ProtectCall), await ctx.call(ButtonRequest(code=ProtectCall),
ButtonAck) ButtonAck)
def onchange():
c = dialog.cancel
if matrix.pin:
c.content = 'Clean'
else:
c.content = 'Cancel'
c.taint()
c.render()
ui.display.clear() ui.display.clear()
matrix = PinMatrix(label) matrix = PinMatrix(label, with_zero=True)
matrix.onchange = onchange
dialog = ConfirmDialog(matrix) dialog = ConfirmDialog(matrix)
dialog.cancel.area = (0, 240 - 48, 80, 48)
dialog.confirm.area = (240 - 80, 240 - 48, 80, 48)
result = await dialog while True:
pin = matrix.pin res = await dialog
matrix = None pin = matrix.pin
if result != CONFIRMED: if res == CONFIRMED:
raise wire.FailureError(PinCancelled, 'PIN cancelled') matrix = None
return pin
return pin elif res != CONFIRMED and pin:
matrix.change('')
continue
else:
matrix = None
raise wire.FailureError(PinCancelled, 'PIN cancelled')
@unimport @unimport

View File

@ -2,42 +2,43 @@ from micropython import const
from trezor import ui, res from trezor import ui, res
from trezor.crypto import random from trezor.crypto import random
from trezor.ui import display from trezor.ui import display
from trezor.ui.button import Button, BTN_CLICKED, CLEAR_BUTTON, CLEAR_BUTTON_ACTIVE from trezor.ui.button import Button, BTN_CLICKED
def digit_area(i): def digit_area(i):
width = const(80) width = const(80)
height = const(48) height = const(48)
if i == 9: # 0-position
i = 10 # display it in the middle
x = (i % 3) * width x = (i % 3) * width
y = (i // 3) * height y = (i // 3) * height
# 48px is offset of input line / -1px is due to corner bug of overlaying # 48px is offset of input line, -1px is the border size
# elements
return (x, y + 48, width - 1, height - 1) return (x, y + 48, width - 1, height - 1)
def generate_digits(): def generate_digits(with_zero):
digits = list(range(1, 10)) # 1-9 if with_zero:
digits = list(range(0, 10)) # 0-9
else:
digits = list(range(1, 10)) # 1-9
random.shuffle(digits) random.shuffle(digits)
return digits return digits
class PinMatrix(ui.Widget): class PinMatrix(ui.Widget):
def __init__(self, label, pin=''): def __init__(self, label, pin='', maxlength=9, with_zero=False):
self.label = label self.label = label
self.pin = pin self.pin = pin
self.digits = generate_digits() self.maxlength = maxlength
self.digits = generate_digits(with_zero)
# we lay out the buttons top-left to bottom-right, but the order of the # we lay out the buttons top-left to bottom-right, but the order of the
# digits is defined as bottom-left to top-right (on numpad) # digits is defined as bottom-left to top-right (on numpad)
reordered_digits = self.digits[6:] + self.digits[3:6] + self.digits[:3] reordered_digits = self.digits[6:] + self.digits[3:6] + self.digits[:3]
self.pin_buttons = [Button(digit_area(i), str(d)) self.pin_buttons = [Button(digit_area(i), str(d))
for i, d in enumerate(reordered_digits)] for i, d in enumerate(reordered_digits)]
self.onchange = None
self.clear_button = Button((240 - 35, 5, 30, 30),
res.load('trezor/res/pin_close.toig'),
normal_style=CLEAR_BUTTON,
active_style=CLEAR_BUTTON_ACTIVE)
def render(self): def render(self):
@ -49,12 +50,6 @@ class PinMatrix(ui.Widget):
# input line with a header # input line with a header
display.text_center(120, 30, header, ui.BOLD, ui.GREY, ui.BLACK) display.text_center(120, 30, header, ui.BOLD, ui.GREY, ui.BLACK)
# render clear button
if self.pin:
self.clear_button.render()
else:
display.bar(240 - 48, 0, 48, 42, ui.BLACK)
# pin matrix buttons # pin matrix buttons
for btn in self.pin_buttons: for btn in self.pin_buttons:
btn.render() btn.render()
@ -68,9 +63,13 @@ class PinMatrix(ui.Widget):
# display.bar(0, 142, 240, 2, ui.blend(ui.BLACK, ui.WHITE, 0.25)) # display.bar(0, 142, 240, 2, ui.blend(ui.BLACK, ui.WHITE, 0.25))
def touch(self, event, pos): def touch(self, event, pos):
if self.clear_button.touch(event, pos) == BTN_CLICKED:
self.pin = ''
for btn in self.pin_buttons: for btn in self.pin_buttons:
if btn.touch(event, pos) == BTN_CLICKED: if btn.touch(event, pos) == BTN_CLICKED:
if len(self.pin) < 9: if len(self.pin) < self.maxlength:
self.pin += btn.content self.change(self.pin + btn.content)
break
def change(self, pin):
self.pin = pin
if self.onchange:
self.onchange()