1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-18 13:38:12 +00:00

pin: simplify label code

This commit is contained in:
Jan Pochyla 2018-03-20 16:44:23 +01:00
parent 01bc12ec27
commit 6c9c563323
3 changed files with 21 additions and 42 deletions

View File

@ -1,5 +1,4 @@
from trezor import loop, res, ui from trezor import loop, res, ui
from trezor.messages import PinMatrixRequestType
from trezor.ui.confirm import CONFIRMED, ConfirmDialog from trezor.ui.confirm import CONFIRMED, ConfirmDialog
from trezor.ui.pin import PinMatrix from trezor.ui.pin import PinMatrix
@ -12,7 +11,7 @@ class PinCancelled(Exception):
@ui.layout @ui.layout
async def request_pin(code=None, cancellable: bool=True) -> str: async def request_pin(label=None, cancellable: bool=True) -> str:
def onchange(): def onchange():
c = dialog.cancel c = dialog.cancel
@ -36,8 +35,9 @@ async def request_pin(code=None, cancellable: bool=True) -> str:
c.taint() c.taint()
c.render() c.render()
label = _get_label(code) if label is None:
matrix = PinMatrix(label, with_zero=True) label = 'Enter your PIN'
matrix = PinMatrix(label)
matrix.onchange = onchange matrix.onchange = onchange
dialog = ConfirmDialog(matrix) dialog = ConfirmDialog(matrix)
dialog.cancel.area = ui.grid(12) dialog.cancel.area = ui.grid(12)
@ -58,17 +58,3 @@ async def request_pin(code=None, cancellable: bool=True) -> str:
continue continue
else: # cancel else: # cancel
raise PinCancelled() raise PinCancelled()
def _get_label(code):
if isinstance(code, str):
return code
if code is None:
code = PinMatrixRequestType.Current
if code == PinMatrixRequestType.NewFirst:
label = 'Enter new PIN'
elif code == PinMatrixRequestType.NewSecond:
label = 'Re-enter new PIN'
else: # PinMatrixRequestType.Current
label = 'Enter your PIN'
return label

View File

@ -1,14 +1,13 @@
from trezor import config, loop, ui, wire from trezor import config, loop, ui, wire
from trezor.messages import FailureType, PinMatrixRequestType from trezor.messages import FailureType, wire_types
from trezor.messages.ButtonRequest import ButtonRequest from trezor.messages.ButtonRequest import ButtonRequest
from trezor.messages.ButtonRequestType import Other from trezor.messages.ButtonRequestType import Other
from trezor.messages.Failure import Failure from trezor.messages.Failure import Failure
from trezor.messages.Success import Success from trezor.messages.Success import Success
from trezor.messages import wire_types
from trezor.pin import pin_to_int, show_pin_timeout from trezor.pin import pin_to_int, show_pin_timeout
from trezor.ui.text import Text from trezor.ui.text import Text
from apps.common.confirm import require_confirm from apps.common.confirm import require_confirm
from apps.common.request_pin import request_pin, PinCancelled from apps.common.request_pin import PinCancelled, request_pin
async def change_pin(ctx, msg): async def change_pin(ctx, msg):
@ -18,7 +17,7 @@ async def change_pin(ctx, msg):
# get current pin, return failure if invalid # get current pin, return failure if invalid
if config.has_pin(): if config.has_pin():
curpin = await request_pin_ack(ctx, PinMatrixRequestType.Current) curpin = await request_pin_ack(ctx)
if not config.check_pin(pin_to_int(curpin), show_pin_timeout): if not config.check_pin(pin_to_int(curpin), show_pin_timeout):
return Failure(code=FailureType.PinInvalid, message='PIN invalid') return Failure(code=FailureType.PinInvalid, message='PIN invalid')
else: else:
@ -62,26 +61,23 @@ def require_confirm_change_pin(ctx, msg):
'set new PIN?')) 'set new PIN?'))
async def request_pin_ack(ctx, *args, **kwargs):
# TODO: send PinMatrixRequest here, with specific code?
await ctx.call(ButtonRequest(code=Other), wire_types.ButtonAck)
try:
return await ctx.wait(request_pin(*args, **kwargs))
except PinCancelled:
raise wire.FailureError(FailureType.ActionCancelled, 'Cancelled')
async def request_pin_confirm(ctx, *args, **kwargs): async def request_pin_confirm(ctx, *args, **kwargs):
while True: while True:
pin1 = await request_pin_ack( pin1 = await request_pin_ack(ctx, 'Enter new PIN', *args, **kwargs)
ctx, code=PinMatrixRequestType.NewFirst, *args, **kwargs) pin2 = await request_pin_ack(ctx, 'Re-enter new PIN', *args, **kwargs)
pin2 = await request_pin_ack(
ctx, code=PinMatrixRequestType.NewSecond, *args, **kwargs)
if pin1 == pin2: if pin1 == pin2:
return pin1 return pin1
await pin_mismatch() await pin_mismatch()
async def request_pin_ack(ctx, *args, **kwargs):
try:
await ctx.call(ButtonRequest(code=Other), wire_types.ButtonAck)
return await ctx.wait(request_pin(*args, **kwargs))
except PinCancelled:
raise wire.FailureError(FailureType.ActionCancelled, 'Cancelled')
@ui.layout @ui.layout
async def pin_mismatch(): async def pin_mismatch():
text = Text( text = Text(

View File

@ -11,22 +11,19 @@ def digit_area(i):
return ui.grid(i + 3) # skip the first line return ui.grid(i + 3) # skip the first line
def generate_digits(with_zero): def generate_digits():
if with_zero: digits = list(range(0, 10)) # 0-9
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='', maxlength=9, with_zero=False): def __init__(self, label, pin='', maxlength=9):
self.label = label self.label = label
self.pin = pin self.pin = pin
self.maxlength = maxlength self.maxlength = maxlength
self.digits = generate_digits(with_zero) self.digits = generate_digits()
# 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)