apps/common/request_pin: add support for 0

pull/25/head
Jan Pochyla 7 years ago
parent f8d961f839
commit 3379c94598

@ -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)
result = await dialog dialog.confirm.area = (240 - 80, 240 - 48, 80, 48)
pin = matrix.pin
matrix = None while True:
res = await dialog
if result != CONFIRMED: pin = matrix.pin
raise wire.FailureError(PinCancelled, 'PIN cancelled')
if res == CONFIRMED:
return pin matrix = None
return pin
elif res != CONFIRMED and pin:
matrix.change('')
continue
else:
matrix = None
raise wire.FailureError(PinCancelled, 'PIN cancelled')
@unimport @unimport

@ -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()

Loading…
Cancel
Save