mirror of
https://github.com/trezor/trezor-firmware.git
synced 2024-11-18 05:28:40 +00:00
core: make verify_user_pin accept a Context argument
This commit is contained in:
parent
8ca7ffc3b8
commit
32fcc4ad9c
@ -1,4 +1,5 @@
|
||||
from trezor import config, loop, ui, wire
|
||||
import storage.sd_salt
|
||||
from trezor import config, ui, wire
|
||||
from trezor.messages import ButtonRequestType
|
||||
from trezor.messages.ButtonAck import ButtonAck
|
||||
from trezor.messages.ButtonRequest import ButtonRequest
|
||||
@ -17,10 +18,13 @@ if __debug__:
|
||||
|
||||
|
||||
async def request_pin(
|
||||
ctx: wire.GenericContext,
|
||||
prompt: str = "Enter your PIN",
|
||||
attempts_remaining: int = None,
|
||||
allow_cancel: bool = True,
|
||||
) -> str:
|
||||
await ctx.call(ButtonRequest(code=ButtonRequestType.Other), ButtonAck)
|
||||
|
||||
if attempts_remaining is None:
|
||||
subprompt = None
|
||||
elif attempts_remaining == 1:
|
||||
@ -32,26 +36,19 @@ async def request_pin(
|
||||
|
||||
while True:
|
||||
if __debug__:
|
||||
pin = await loop.race(dialog, input_signal())
|
||||
pin = await ctx.wait(dialog, input_signal())
|
||||
else:
|
||||
pin = await dialog
|
||||
pin = await ctx.wait(dialog)
|
||||
if pin is CANCELLED:
|
||||
raise wire.PinCancelled
|
||||
assert isinstance(pin, str)
|
||||
return pin
|
||||
|
||||
|
||||
async def request_pin_ack(ctx: wire.Context, *args: Any, **kwargs: Any) -> str:
|
||||
await ctx.call(ButtonRequest(code=ButtonRequestType.Other), ButtonAck)
|
||||
pin = await ctx.wait(request_pin(*args, **kwargs))
|
||||
assert isinstance(pin, str)
|
||||
return pin
|
||||
|
||||
|
||||
async def request_pin_confirm(ctx: wire.Context, *args: Any, **kwargs: Any) -> str:
|
||||
while True:
|
||||
pin1 = await request_pin_ack(ctx, "Enter new PIN", *args, **kwargs)
|
||||
pin2 = await request_pin_ack(ctx, "Re-enter new PIN", *args, **kwargs)
|
||||
pin1 = await request_pin(ctx, "Enter new PIN", *args, **kwargs)
|
||||
pin2 = await request_pin(ctx, "Re-enter new PIN", *args, **kwargs)
|
||||
if pin1 == pin2:
|
||||
return pin1
|
||||
await pin_mismatch()
|
||||
@ -70,7 +67,7 @@ async def request_pin_and_sd_salt(
|
||||
ctx: wire.Context, prompt: str = "Enter your PIN", allow_cancel: bool = True
|
||||
) -> Tuple[str, Optional[bytearray]]:
|
||||
if config.has_pin():
|
||||
pin = await request_pin_ack(ctx, prompt, config.get_pin_rem(), allow_cancel)
|
||||
pin = await request_pin(ctx, prompt, config.get_pin_rem(), allow_cancel)
|
||||
config.ensure_not_wipe_code(pin_to_int(pin))
|
||||
else:
|
||||
pin = ""
|
||||
@ -81,16 +78,19 @@ async def request_pin_and_sd_salt(
|
||||
|
||||
|
||||
async def verify_user_pin(
|
||||
prompt: str = "Enter your PIN", allow_cancel: bool = True, retry: bool = True
|
||||
ctx: wire.GenericContext = wire.DUMMY_CONTEXT,
|
||||
prompt: str = "Enter your PIN",
|
||||
allow_cancel: bool = True,
|
||||
retry: bool = True,
|
||||
) -> None:
|
||||
if config.has_pin():
|
||||
pin = await request_pin(prompt, config.get_pin_rem(), allow_cancel)
|
||||
pin = await request_pin(ctx, prompt, config.get_pin_rem(), allow_cancel)
|
||||
config.ensure_not_wipe_code(pin_to_int(pin))
|
||||
else:
|
||||
pin = ""
|
||||
|
||||
try:
|
||||
salt = await request_sd_salt()
|
||||
salt = await request_sd_salt(ctx)
|
||||
except SdCardUnavailable:
|
||||
raise wire.PinCancelled("SD salt is unavailable")
|
||||
if config.unlock(pin_to_int(pin), salt):
|
||||
@ -100,7 +100,7 @@ async def verify_user_pin(
|
||||
|
||||
while retry:
|
||||
pin = await request_pin(
|
||||
"Wrong PIN, enter again", config.get_pin_rem(), allow_cancel
|
||||
ctx, "Wrong PIN, enter again", config.get_pin_rem(), allow_cancel
|
||||
)
|
||||
if config.unlock(pin_to_int(pin), salt):
|
||||
return
|
||||
|
@ -9,7 +9,7 @@ from apps.common.confirm import require_confirm
|
||||
from apps.common.layout import show_success
|
||||
from apps.common.request_pin import (
|
||||
error_pin_invalid,
|
||||
request_pin_ack,
|
||||
request_pin,
|
||||
request_pin_and_sd_salt,
|
||||
)
|
||||
|
||||
@ -85,12 +85,12 @@ def _require_confirm_action(
|
||||
|
||||
async def _request_wipe_code_confirm(ctx: wire.Context, pin: str) -> str:
|
||||
while True:
|
||||
code1 = await request_pin_ack(ctx, "Enter new wipe code")
|
||||
code1 = await request_pin(ctx, "Enter new wipe code")
|
||||
if code1 == pin:
|
||||
await _wipe_code_invalid()
|
||||
continue
|
||||
|
||||
code2 = await request_pin_ack(ctx, "Re-enter new wipe code")
|
||||
code2 = await request_pin(ctx, "Re-enter new wipe code")
|
||||
if code1 == code2:
|
||||
return code1
|
||||
await _wipe_code_mismatch()
|
||||
|
@ -11,7 +11,7 @@ from apps.common.confirm import require_confirm
|
||||
from apps.common.layout import show_success
|
||||
from apps.common.request_pin import (
|
||||
error_pin_invalid,
|
||||
request_pin_ack,
|
||||
request_pin,
|
||||
request_pin_and_sd_salt,
|
||||
)
|
||||
from apps.common.sdcard import ensure_sdcard, sd_problem_dialog
|
||||
@ -66,7 +66,7 @@ async def sd_protect_enable(ctx: wire.Context, msg: SdProtect) -> Success:
|
||||
|
||||
# Get the current PIN.
|
||||
if config.has_pin():
|
||||
pin = pin_to_int(await request_pin_ack(ctx, "Enter PIN", config.get_pin_rem()))
|
||||
pin = pin_to_int(await request_pin(ctx, "Enter PIN", config.get_pin_rem()))
|
||||
else:
|
||||
pin = pin_to_int("")
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user