chore(core): decrease TT_v2 UI size by 390 bytes

pull/2633/head
grdddj 2 years ago committed by matejcik
parent bd7513f2df
commit 107c222631

@ -17,11 +17,11 @@ if TYPE_CHECKING:
display = Display() display = Display()
# re-export constants from modtrezorui # re-export constants from modtrezorui
NORMAL = Display.FONT_NORMAL NORMAL: int = Display.FONT_NORMAL
BOLD = Display.FONT_BOLD BOLD: int = Display.FONT_BOLD
MONO = Display.FONT_MONO MONO: int = Display.FONT_MONO
WIDTH = Display.WIDTH WIDTH: int = Display.WIDTH
HEIGHT = Display.HEIGHT HEIGHT: int = Display.HEIGHT
# viewport margins # viewport margins
VIEWX = const(6) VIEWX = const(6)

@ -1,8 +1,8 @@
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
from ubinascii import hexlify
from trezor import io, log, loop, ui, wire, workflow from trezor import io, log, loop, ui
from trezor.enums import ButtonRequestType from trezor.enums import ButtonRequestType
from trezor.wire import ActionCancelled
import trezorui2 import trezorui2
@ -11,11 +11,18 @@ from ..common import button_request, interact
if TYPE_CHECKING: if TYPE_CHECKING:
from typing import Any, Awaitable, Iterable, NoReturn, Sequence, TypeVar from typing import Any, Awaitable, Iterable, NoReturn, Sequence, TypeVar
from trezor.wire import GenericContext, Context
from ..common import PropertyType, ExceptionType from ..common import PropertyType, ExceptionType
T = TypeVar("T") T = TypeVar("T")
CONFIRMED = trezorui2.CONFIRMED # global_import_cache
CANCELLED = trezorui2.CANCELLED # global_import_cache
INFO = trezorui2.INFO # global_import_cache
BR_TYPE_OTHER = ButtonRequestType.Other # global_import_cache
class _RustLayout(ui.Layout): class _RustLayout(ui.Layout):
# pylint: disable=super-init-not-called # pylint: disable=super-init-not-called
def __init__(self, layout: Any, is_backup: bool = False): def __init__(self, layout: Any, is_backup: bool = False):
@ -132,6 +139,8 @@ class _RustLayout(ui.Layout):
ui.backlight_fade(self.BACKLIGHT_LEVEL) ui.backlight_fade(self.BACKLIGHT_LEVEL)
def handle_input_and_rendering(self) -> loop.Task: # type: ignore [awaitable-is-generator] def handle_input_and_rendering(self) -> loop.Task: # type: ignore [awaitable-is-generator]
from trezor import workflow
touch = loop.wait(io.TOUCH) touch = loop.wait(io.TOUCH)
self._before_render() self._before_render()
# self.layout.bounds() # self.layout.bounds()
@ -160,7 +169,7 @@ class _RustLayout(ui.Layout):
return self.layout.page_count() return self.layout.page_count()
async def raise_if_not_confirmed(a: Awaitable[T], exc: Any = wire.ActionCancelled) -> T: async def raise_if_not_confirmed(a: Awaitable[T], exc: Any = ActionCancelled) -> T:
result = await a result = await a
if result is not trezorui2.CONFIRMED: if result is not trezorui2.CONFIRMED:
raise exc raise exc
@ -168,7 +177,7 @@ async def raise_if_not_confirmed(a: Awaitable[T], exc: Any = wire.ActionCancelle
async def confirm_action( async def confirm_action(
ctx: wire.GenericContext, ctx: GenericContext,
br_type: str, br_type: str,
title: str, title: str,
action: str | None = None, action: str | None = None,
@ -183,8 +192,8 @@ async def confirm_action(
icon_color: int | None = None, icon_color: int | None = None,
reverse: bool = False, reverse: bool = False,
larger_vspace: bool = False, larger_vspace: bool = False,
exc: ExceptionType = wire.ActionCancelled, exc: ExceptionType = ActionCancelled,
br_code: ButtonRequestType = ButtonRequestType.Other, br_code: ButtonRequestType = BR_TYPE_OTHER,
) -> None: ) -> None:
if isinstance(verb, bytes) or isinstance(verb_cancel, bytes): if isinstance(verb, bytes) or isinstance(verb_cancel, bytes):
raise NotImplementedError raise NotImplementedError
@ -220,7 +229,7 @@ async def confirm_action(
async def confirm_reset_device( async def confirm_reset_device(
ctx: wire.GenericContext, prompt: str, recovery: bool = False ctx: GenericContext, prompt: str, recovery: bool = False
) -> None: ) -> None:
if recovery: if recovery:
title = "RECOVERY MODE" title = "RECOVERY MODE"
@ -245,7 +254,7 @@ async def confirm_reset_device(
# TODO cleanup @ redesign # TODO cleanup @ redesign
async def confirm_backup(ctx: wire.GenericContext) -> bool: async def confirm_backup(ctx: GenericContext) -> bool:
result = await interact( result = await interact(
ctx, ctx,
_RustLayout( _RustLayout(
@ -260,7 +269,7 @@ async def confirm_backup(ctx: wire.GenericContext) -> bool:
"backup_device", "backup_device",
ButtonRequestType.ResetDevice, ButtonRequestType.ResetDevice,
) )
if result is trezorui2.CONFIRMED: if result is CONFIRMED:
return True return True
result = await interact( result = await interact(
@ -277,11 +286,11 @@ async def confirm_backup(ctx: wire.GenericContext) -> bool:
"backup_device", "backup_device",
ButtonRequestType.ResetDevice, ButtonRequestType.ResetDevice,
) )
return result is trezorui2.CONFIRMED return result is CONFIRMED
async def confirm_path_warning( async def confirm_path_warning(
ctx: wire.GenericContext, path: str, path_type: str = "Path" ctx: GenericContext, path: str, path_type: str = "Path"
) -> None: ) -> None:
await raise_if_not_confirmed( await raise_if_not_confirmed(
interact( interact(
@ -309,9 +318,7 @@ def _show_xpub(xpub: str, title: str, cancel: str) -> ui.Layout:
return content return content
async def show_xpub( async def show_xpub(ctx: GenericContext, xpub: str, title: str, cancel: str) -> None:
ctx: wire.GenericContext, xpub: str, title: str, cancel: str
) -> None:
await raise_if_not_confirmed( await raise_if_not_confirmed(
interact( interact(
ctx, ctx,
@ -323,7 +330,7 @@ async def show_xpub(
async def show_address( async def show_address(
ctx: wire.GenericContext, ctx: GenericContext,
address: str, address: str,
*, *,
address_qr: str | None = None, address_qr: str | None = None,
@ -351,7 +358,7 @@ async def show_address(
"show_address", "show_address",
ButtonRequestType.Address, ButtonRequestType.Address,
) )
if result is trezorui2.CONFIRMED: if result is CONFIRMED:
break break
result = await interact( result = await interact(
@ -367,7 +374,7 @@ async def show_address(
"show_qr", "show_qr",
ButtonRequestType.Address, ButtonRequestType.Address,
) )
if result is trezorui2.CONFIRMED: if result is CONFIRMED:
break break
if is_multisig: if is_multisig:
@ -381,12 +388,12 @@ async def show_address(
"show_xpub", "show_xpub",
ButtonRequestType.PublicKey, ButtonRequestType.PublicKey,
) )
if result is trezorui2.CONFIRMED: if result is CONFIRMED:
return return
def show_pubkey( def show_pubkey(
ctx: wire.Context, pubkey: str, title: str = "Confirm public key" ctx: Context, pubkey: str, title: str = "Confirm public key"
) -> Awaitable[None]: ) -> Awaitable[None]:
return confirm_blob( return confirm_blob(
ctx, ctx,
@ -399,14 +406,14 @@ def show_pubkey(
async def show_error_and_raise( async def show_error_and_raise(
ctx: wire.GenericContext, ctx: GenericContext,
br_type: str, br_type: str,
content: str, content: str,
header: str = "Error", header: str = "Error",
subheader: str | None = None, subheader: str | None = None,
button: str = "CLOSE", button: str = "CLOSE",
red: bool = False, red: bool = False,
exc: ExceptionType = wire.ActionCancelled, exc: ExceptionType = ActionCancelled,
) -> NoReturn: ) -> NoReturn:
await interact( await interact(
ctx, ctx,
@ -419,13 +426,13 @@ async def show_error_and_raise(
) )
), ),
br_type, br_type,
ButtonRequestType.Other, BR_TYPE_OTHER,
) )
raise exc raise exc
async def show_warning( async def show_warning(
ctx: wire.GenericContext, ctx: GenericContext,
br_type: str, br_type: str,
content: str, content: str,
header: str = "Warning", header: str = "Warning",
@ -453,7 +460,7 @@ async def show_warning(
async def show_success( async def show_success(
ctx: wire.GenericContext, ctx: GenericContext,
br_type: str, br_type: str,
content: str, content: str,
subheader: str | None = None, subheader: str | None = None,
@ -477,7 +484,7 @@ async def show_success(
async def confirm_output( async def confirm_output(
ctx: wire.GenericContext, ctx: GenericContext,
address: str, address: str,
amount: str, amount: str,
font_amount: int = ui.NORMAL, # TODO cleanup @ redesign font_amount: int = ui.NORMAL, # TODO cleanup @ redesign
@ -517,7 +524,7 @@ async def confirm_output(
async def confirm_payment_request( async def confirm_payment_request(
ctx: wire.GenericContext, ctx: GenericContext,
recipient_name: str, recipient_name: str,
amount: str, amount: str,
memos: list[str], memos: list[str],
@ -537,21 +544,21 @@ async def confirm_payment_request(
"confirm_payment_request", "confirm_payment_request",
ButtonRequestType.ConfirmOutput, ButtonRequestType.ConfirmOutput,
) )
if result is trezorui2.CONFIRMED: if result is CONFIRMED:
return confirm.CONFIRMED return confirm.CONFIRMED
elif result is trezorui2.INFO: elif result is INFO:
return confirm.INFO return confirm.INFO
else: else:
raise wire.ActionCancelled raise ActionCancelled
async def should_show_more( async def should_show_more(
ctx: wire.GenericContext, ctx: GenericContext,
title: str, title: str,
para: Iterable[tuple[int, str]], para: Iterable[tuple[int, str]],
button_text: str = "Show all", button_text: str = "Show all",
br_type: str = "should_show_more", br_type: str = "should_show_more",
br_code: ButtonRequestType = ButtonRequestType.Other, br_code: ButtonRequestType = BR_TYPE_OTHER,
icon: str = ui.ICON_DEFAULT, icon: str = ui.ICON_DEFAULT,
icon_color: int = ui.ORANGE_ICON, icon_color: int = ui.ORANGE_ICON,
confirm: str | bytes | None = None, confirm: str | bytes | None = None,
@ -583,27 +590,29 @@ async def should_show_more(
br_code, br_code,
) )
if result is trezorui2.CONFIRMED: if result is CONFIRMED:
return False return False
elif result is trezorui2.INFO: elif result is INFO:
return True return True
else: else:
assert result is trezorui2.CANCELLED assert result is CANCELLED
raise wire.ActionCancelled raise ActionCancelled
async def confirm_blob( async def confirm_blob(
ctx: wire.GenericContext, ctx: GenericContext,
br_type: str, br_type: str,
title: str, title: str,
data: bytes | str, data: bytes | str,
description: str | None = None, description: str | None = None,
hold: bool = False, hold: bool = False,
br_code: ButtonRequestType = ButtonRequestType.Other, br_code: ButtonRequestType = BR_TYPE_OTHER,
icon: str = ui.ICON_SEND, # TODO cleanup @ redesign icon: str = ui.ICON_SEND, # TODO cleanup @ redesign
icon_color: int = ui.GREEN, # TODO cleanup @ redesign icon_color: int = ui.GREEN, # TODO cleanup @ redesign
ask_pagination: bool = False, ask_pagination: bool = False,
) -> None: ) -> None:
from ubinascii import hexlify
if isinstance(data, bytes): if isinstance(data, bytes):
data = hexlify(data).decode() data = hexlify(data).decode()
@ -626,12 +635,12 @@ async def confirm_blob(
def confirm_address( def confirm_address(
ctx: wire.GenericContext, ctx: GenericContext,
title: str, title: str,
address: str, address: str,
description: str | None = "Address:", description: str | None = "Address:",
br_type: str = "confirm_address", br_type: str = "confirm_address",
br_code: ButtonRequestType = ButtonRequestType.Other, br_code: ButtonRequestType = BR_TYPE_OTHER,
icon: str = ui.ICON_SEND, # TODO cleanup @ redesign icon: str = ui.ICON_SEND, # TODO cleanup @ redesign
icon_color: int = ui.GREEN, # TODO cleanup @ redesign icon_color: int = ui.GREEN, # TODO cleanup @ redesign
) -> Awaitable[None]: ) -> Awaitable[None]:
@ -647,12 +656,12 @@ def confirm_address(
async def confirm_text( async def confirm_text(
ctx: wire.GenericContext, ctx: GenericContext,
br_type: str, br_type: str,
title: str, title: str,
data: str, data: str,
description: str | None = None, description: str | None = None,
br_code: ButtonRequestType = ButtonRequestType.Other, br_code: ButtonRequestType = BR_TYPE_OTHER,
icon: str = ui.ICON_SEND, # TODO cleanup @ redesign icon: str = ui.ICON_SEND, # TODO cleanup @ redesign
icon_color: int = ui.GREEN, # TODO cleanup @ redesign icon_color: int = ui.GREEN, # TODO cleanup @ redesign
) -> None: ) -> None:
@ -668,12 +677,12 @@ async def confirm_text(
def confirm_amount( def confirm_amount(
ctx: wire.GenericContext, ctx: GenericContext,
title: str, title: str,
amount: str, amount: str,
description: str = "Amount:", description: str = "Amount:",
br_type: str = "confirm_amount", br_type: str = "confirm_amount",
br_code: ButtonRequestType = ButtonRequestType.Other, br_code: ButtonRequestType = BR_TYPE_OTHER,
icon: str = ui.ICON_SEND, # TODO cleanup @ redesign icon: str = ui.ICON_SEND, # TODO cleanup @ redesign
icon_color: int = ui.GREEN, # TODO cleanup @ redesign icon_color: int = ui.GREEN, # TODO cleanup @ redesign
) -> Awaitable[None]: ) -> Awaitable[None]:
@ -689,7 +698,7 @@ def confirm_amount(
def confirm_value( def confirm_value(
ctx: wire.GenericContext, ctx: GenericContext,
title: str, title: str,
value: str, value: str,
description: str, description: str,
@ -723,7 +732,7 @@ def confirm_value(
async def confirm_properties( async def confirm_properties(
ctx: wire.GenericContext, ctx: GenericContext,
br_type: str, br_type: str,
title: str, title: str,
props: Iterable[PropertyType], props: Iterable[PropertyType],
@ -733,6 +742,8 @@ async def confirm_properties(
br_code: ButtonRequestType = ButtonRequestType.ConfirmOutput, br_code: ButtonRequestType = ButtonRequestType.ConfirmOutput,
) -> None: ) -> None:
def handle_bytes(prop): def handle_bytes(prop):
from ubinascii import hexlify
if isinstance(prop[1], bytes): if isinstance(prop[1], bytes):
return (prop[0], hexlify(prop[1]).decode(), True) return (prop[0], hexlify(prop[1]).decode(), True)
else: else:
@ -751,11 +762,11 @@ async def confirm_properties(
br_code, br_code,
) )
if result is not trezorui2.CONFIRMED: if result is not trezorui2.CONFIRMED:
raise wire.ActionCancelled raise ActionCancelled
async def confirm_total( async def confirm_total(
ctx: wire.GenericContext, ctx: GenericContext,
total_amount: str, total_amount: str,
fee_amount: str, fee_amount: str,
fee_rate_amount: str | None = None, fee_rate_amount: str | None = None,
@ -788,7 +799,7 @@ async def confirm_total(
async def confirm_joint_total( async def confirm_joint_total(
ctx: wire.GenericContext, spending_amount: str, total_amount: str ctx: GenericContext, spending_amount: str, total_amount: str
) -> None: ) -> None:
await raise_if_not_confirmed( await raise_if_not_confirmed(
@ -807,7 +818,7 @@ async def confirm_joint_total(
async def confirm_metadata( async def confirm_metadata(
ctx: wire.GenericContext, ctx: GenericContext,
br_type: str, br_type: str,
title: str, title: str,
content: str, content: str,
@ -856,9 +867,7 @@ async def confirm_metadata(
) )
async def confirm_replacement( async def confirm_replacement(ctx: GenericContext, description: str, txid: str) -> None:
ctx: wire.GenericContext, description: str, txid: str
) -> None:
await confirm_blob( await confirm_blob(
ctx, ctx,
title=description.upper(), title=description.upper(),
@ -870,7 +879,7 @@ async def confirm_replacement(
async def confirm_modify_output( async def confirm_modify_output(
ctx: wire.GenericContext, ctx: GenericContext,
address: str, address: str,
sign: int, sign: int,
amount_change: str, amount_change: str,
@ -894,7 +903,7 @@ async def confirm_modify_output(
async def confirm_modify_fee( async def confirm_modify_fee(
ctx: wire.GenericContext, ctx: GenericContext,
sign: int, sign: int,
user_fee_change: str, user_fee_change: str,
total_fee_new: str, total_fee_new: str,
@ -917,7 +926,7 @@ async def confirm_modify_fee(
async def confirm_coinjoin( async def confirm_coinjoin(
ctx: wire.GenericContext, max_rounds: int, max_fee_per_vbyte: str ctx: GenericContext, max_rounds: int, max_fee_per_vbyte: str
) -> None: ) -> None:
await raise_if_not_confirmed( await raise_if_not_confirmed(
interact( interact(
@ -940,7 +949,7 @@ def show_coinjoin() -> None:
# TODO cleanup @ redesign # TODO cleanup @ redesign
async def confirm_sign_identity( async def confirm_sign_identity(
ctx: wire.GenericContext, proto: str, identity: str, challenge_visual: str | None ctx: GenericContext, proto: str, identity: str, challenge_visual: str | None
) -> None: ) -> None:
await confirm_blob( await confirm_blob(
ctx, ctx,
@ -953,7 +962,7 @@ async def confirm_sign_identity(
async def confirm_signverify( async def confirm_signverify(
ctx: wire.GenericContext, coin: str, message: str, address: str, verify: bool ctx: GenericContext, coin: str, message: str, address: str, verify: bool
) -> None: ) -> None:
if verify: if verify:
title = f"VERIFY {coin} MESSAGE" title = f"VERIFY {coin} MESSAGE"
@ -964,19 +973,19 @@ async def confirm_signverify(
await confirm_blob( await confirm_blob(
ctx, ctx,
title=title, br_type,
data=address, title,
description="Confirm address:", address,
br_type=br_type, "Confirm address:",
br_code=ButtonRequestType.Other, br_code=ButtonRequestType.Other,
) )
await confirm_blob( await confirm_blob(
ctx, ctx,
title=title, br_type,
data=message, title,
description="Confirm message:", message,
br_type=br_type, "Confirm message:",
br_code=ButtonRequestType.Other, br_code=ButtonRequestType.Other,
) )
@ -1004,7 +1013,7 @@ def draw_simple_text(title: str, description: str = "") -> None:
log.error(__name__, "draw_simple_text not implemented") log.error(__name__, "draw_simple_text not implemented")
async def request_passphrase_on_device(ctx: wire.GenericContext, max_len: int) -> str: async def request_passphrase_on_device(ctx: GenericContext, max_len: int) -> str:
await button_request( await button_request(
ctx, "passphrase_device", code=ButtonRequestType.PassphraseEntry ctx, "passphrase_device", code=ButtonRequestType.PassphraseEntry
) )
@ -1013,19 +1022,21 @@ async def request_passphrase_on_device(ctx: wire.GenericContext, max_len: int) -
trezorui2.request_passphrase(prompt="Enter passphrase", max_len=max_len) trezorui2.request_passphrase(prompt="Enter passphrase", max_len=max_len)
) )
result = await ctx.wait(keyboard) result = await ctx.wait(keyboard)
if result is trezorui2.CANCELLED: if result is CANCELLED:
raise wire.ActionCancelled("Passphrase entry cancelled") raise ActionCancelled("Passphrase entry cancelled")
assert isinstance(result, str) assert isinstance(result, str)
return result return result
async def request_pin_on_device( async def request_pin_on_device(
ctx: wire.GenericContext, ctx: GenericContext,
prompt: str, prompt: str,
attempts_remaining: int | None, attempts_remaining: int | None,
allow_cancel: bool, allow_cancel: bool,
) -> str: ) -> str:
from trezor.wire import PinCancelled
await button_request(ctx, "pin_device", code=ButtonRequestType.PinEntry) await button_request(ctx, "pin_device", code=ButtonRequestType.PinEntry)
warning = "Wrong PIN" if "Wrong" in prompt else None warning = "Wrong PIN" if "Wrong" in prompt else None
@ -1049,7 +1060,7 @@ async def request_pin_on_device(
) )
while True: while True:
result = await ctx.wait(dialog) result = await ctx.wait(dialog)
if result is trezorui2.CANCELLED: if result is CANCELLED:
raise wire.PinCancelled raise PinCancelled
assert isinstance(result, str) assert isinstance(result, str)
return result return result

@ -1,6 +1,5 @@
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
from trezor import wire
from trezor.enums import ButtonRequestType from trezor.enums import ButtonRequestType
from trezor.ui.layouts import ( from trezor.ui.layouts import (
confirm_amount, confirm_amount,
@ -11,12 +10,13 @@ from trezor.ui.layouts import (
if TYPE_CHECKING: if TYPE_CHECKING:
from typing import Sequence from typing import Sequence
from trezor.wire import GenericContext
pass pass
async def confirm_total_ethereum( async def confirm_total_ethereum(
ctx: wire.GenericContext, total_amount: str, gas_price: str, fee_max: str ctx: GenericContext, total_amount: str, gas_price: str, fee_max: str
) -> None: ) -> None:
await confirm_amount( await confirm_amount(
ctx, ctx,
@ -34,7 +34,7 @@ async def confirm_total_ethereum(
async def confirm_total_ripple( async def confirm_total_ripple(
ctx: wire.GenericContext, ctx: GenericContext,
address: str, address: str,
amount: str, amount: str,
) -> None: ) -> None:
@ -42,7 +42,7 @@ async def confirm_total_ripple(
async def confirm_transfer_binance( async def confirm_transfer_binance(
ctx: wire.GenericContext, inputs_outputs: Sequence[tuple[str, str, str]] ctx: GenericContext, inputs_outputs: Sequence[tuple[str, str, str]]
) -> None: ) -> None:
for title, amount, address in inputs_outputs: for title, amount, address in inputs_outputs:
await confirm_blob( await confirm_blob(
@ -55,7 +55,7 @@ async def confirm_transfer_binance(
async def confirm_decred_sstx_submission( async def confirm_decred_sstx_submission(
ctx: wire.GenericContext, ctx: GenericContext,
address: str, address: str,
amount: str, amount: str,
) -> None: ) -> None:

@ -1,22 +1,24 @@
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
from trezor import strings, wire
from trezor.crypto.slip39 import MAX_SHARE_COUNT
from trezor.enums import ButtonRequestType from trezor.enums import ButtonRequestType
from trezor.wire import ActionCancelled
import trezorui2 import trezorui2
from ..common import button_request, interact from ..common import interact
from . import _RustLayout from . import _RustLayout
if TYPE_CHECKING: if TYPE_CHECKING:
from typing import Iterable, Callable, Any from typing import Iterable, Callable, Any
from trezor.wire import GenericContext
pass
CONFIRMED = trezorui2.CONFIRMED # global_import_cache
INFO = trezorui2.INFO # global_import_cache
async def _is_confirmed_info( async def _is_confirmed_info(
ctx: wire.GenericContext, ctx: GenericContext,
dialog: _RustLayout, dialog: _RustLayout,
info_func: Callable, info_func: Callable,
) -> bool: ) -> bool:
@ -26,10 +28,10 @@ async def _is_confirmed_info(
if result is trezorui2.INFO: if result is trezorui2.INFO:
await info_func(ctx) await info_func(ctx)
else: else:
return result is trezorui2.CONFIRMED return result is CONFIRMED
async def request_word_count(ctx: wire.GenericContext, dry_run: bool) -> int: async def request_word_count(ctx: GenericContext, dry_run: bool) -> int:
selector = _RustLayout(trezorui2.select_word_count(dry_run=dry_run)) selector = _RustLayout(trezorui2.select_word_count(dry_run=dry_run))
count = await interact( count = await interact(
ctx, selector, "word_count", ButtonRequestType.MnemonicWordCount ctx, selector, "word_count", ButtonRequestType.MnemonicWordCount
@ -38,7 +40,7 @@ async def request_word_count(ctx: wire.GenericContext, dry_run: bool) -> int:
async def request_word( async def request_word(
ctx: wire.GenericContext, word_index: int, word_count: int, is_slip39: bool ctx: GenericContext, word_index: int, word_count: int, is_slip39: bool
) -> str: ) -> str:
if is_slip39: if is_slip39:
keyboard: Any = _RustLayout( keyboard: Any = _RustLayout(
@ -58,11 +60,14 @@ async def request_word(
async def show_remaining_shares( async def show_remaining_shares(
ctx: wire.GenericContext, ctx: GenericContext,
groups: Iterable[tuple[int, tuple[str, ...]]], # remaining + list 3 words groups: Iterable[tuple[int, tuple[str, ...]]], # remaining + list 3 words
shares_remaining: list[int], shares_remaining: list[int],
group_threshold: int, group_threshold: int,
) -> None: ) -> None:
from trezor import strings
from trezor.crypto.slip39 import MAX_SHARE_COUNT
pages: list[tuple[str, str]] = [] pages: list[tuple[str, str]] = []
for remaining, group in groups: for remaining, group in groups:
if 0 < remaining < MAX_SHARE_COUNT: if 0 < remaining < MAX_SHARE_COUNT:
@ -87,12 +92,12 @@ async def show_remaining_shares(
"show_shares", "show_shares",
ButtonRequestType.Other, ButtonRequestType.Other,
) )
if result is not trezorui2.CONFIRMED: if result is not CONFIRMED:
raise wire.ActionCancelled raise ActionCancelled
async def show_group_share_success( async def show_group_share_success(
ctx: wire.GenericContext, share_index: int, group_index: int ctx: GenericContext, share_index: int, group_index: int
) -> None: ) -> None:
result = await interact( result = await interact(
ctx, ctx,
@ -109,18 +114,20 @@ async def show_group_share_success(
"share_success", "share_success",
ButtonRequestType.Other, ButtonRequestType.Other,
) )
if result is not trezorui2.CONFIRMED: if result is not CONFIRMED:
raise wire.ActionCancelled raise ActionCancelled
async def continue_recovery( async def continue_recovery(
ctx: wire.GenericContext, ctx: GenericContext,
button_label: str, button_label: str,
text: str, text: str,
subtext: str | None, subtext: str | None,
info_func: Callable | None, info_func: Callable | None,
dry_run: bool, dry_run: bool,
) -> bool: ) -> bool:
from ..common import button_request
title = text title = text
if subtext: if subtext:
title += "\n" title += "\n"
@ -156,4 +163,4 @@ async def continue_recovery(
"recovery", "recovery",
ButtonRequestType.RecoveryHomepage, ButtonRequestType.RecoveryHomepage,
) )
return result is trezorui2.CONFIRMED return result is CONFIRMED

@ -1,7 +1,7 @@
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
from trezor import wire from trezor.enums import ButtonRequestType
from trezor.enums import BackupType, ButtonRequestType from trezor.wire import ActionCancelled
import trezorui2 import trezorui2
@ -10,8 +10,11 @@ from . import _RustLayout
if TYPE_CHECKING: if TYPE_CHECKING:
from typing import Callable, Sequence, List from typing import Callable, Sequence, List
from trezor.enums import BackupType
from trezor.wire import GenericContext
pass
CONFIRMED = trezorui2.CONFIRMED # global_import_cache
def _split_share_into_pages(share_words: Sequence[str], per_page: int = 4) -> List[str]: def _split_share_into_pages(share_words: Sequence[str], per_page: int = 4) -> List[str]:
@ -34,7 +37,7 @@ def _split_share_into_pages(share_words: Sequence[str], per_page: int = 4) -> Li
async def show_share_words( async def show_share_words(
ctx: wire.GenericContext, ctx: GenericContext,
share_words: Sequence[str], share_words: Sequence[str],
share_index: int | None = None, share_index: int | None = None,
group_index: int | None = None, group_index: int | None = None,
@ -58,8 +61,8 @@ async def show_share_words(
# "confirm_backup_words", # "confirm_backup_words",
# ButtonRequestType.ResetDevice, # ButtonRequestType.ResetDevice,
# ) # )
# if result != trezorui2.CONFIRMED: # if result != CONFIRMED:
# raise wire.ActionCancelled # raise ActionCancelled
pages = _split_share_into_pages(share_words) pages = _split_share_into_pages(share_words)
@ -75,12 +78,12 @@ async def show_share_words(
"backup_words", "backup_words",
ButtonRequestType.ResetDevice, ButtonRequestType.ResetDevice,
) )
if result != trezorui2.CONFIRMED: if result != CONFIRMED:
raise wire.ActionCancelled raise ActionCancelled
async def select_word( async def select_word(
ctx: wire.GenericContext, ctx: GenericContext,
words: Sequence[str], words: Sequence[str],
share_index: int | None, share_index: int | None,
checked_index: int, checked_index: int,
@ -111,17 +114,25 @@ async def select_word(
async def slip39_show_checklist( async def slip39_show_checklist(
ctx: wire.GenericContext, step: int, backup_type: BackupType ctx: GenericContext, step: int, backup_type: BackupType
) -> None: ) -> None:
items = [] from trezor.enums import BackupType
if backup_type is BackupType.Slip39_Basic:
items.append("Set number of shares") assert backup_type in (BackupType.Slip39_Basic, BackupType.Slip39_Advanced)
items.append("Set threshold")
items.append("Write down and check all recovery shares") items = (
elif backup_type is BackupType.Slip39_Advanced: (
items.append("Set number of groups") "Set number of shares",
items.append("Set group threshold") "Set threshold",
items.append("Set size and threshold for each group") "Write down and check all recovery shares",
)
if backup_type == BackupType.Slip39_Basic
else (
"Set number of groups",
"Set number of shares",
"Set size and threshold for each group",
)
)
result = await interact( result = await interact(
ctx, ctx,
@ -136,12 +147,12 @@ async def slip39_show_checklist(
"slip39_checklist", "slip39_checklist",
ButtonRequestType.ResetDevice, ButtonRequestType.ResetDevice,
) )
if result != trezorui2.CONFIRMED: if result != CONFIRMED:
raise wire.ActionCancelled raise ActionCancelled
async def _prompt_number( async def _prompt_number(
ctx: wire.GenericContext, ctx: GenericContext,
title: str, title: str,
description: Callable[[int], str], description: Callable[[int], str],
info: Callable[[int], str], info: Callable[[int], str],
@ -174,7 +185,7 @@ async def _prompt_number(
result = (result, count) result = (result, count)
status, value = result status, value = result
if status == trezorui2.CONFIRMED: if status == CONFIRMED:
assert isinstance(value, int) assert isinstance(value, int)
return value return value
@ -189,7 +200,7 @@ async def _prompt_number(
async def slip39_prompt_threshold( async def slip39_prompt_threshold(
ctx: wire.GenericContext, num_of_shares: int, group_id: int | None = None ctx: GenericContext, num_of_shares: int, group_id: int | None = None
) -> int: ) -> int:
count = num_of_shares // 2 + 1 count = num_of_shares // 2 + 1
# min value of share threshold is 2 unless the number of shares is 1 # min value of share threshold is 2 unless the number of shares is 1
@ -197,7 +208,7 @@ async def slip39_prompt_threshold(
min_count = min(2, num_of_shares) min_count = min(2, num_of_shares)
max_count = num_of_shares max_count = num_of_shares
def description(count: int): def description(count: int) -> str:
if group_id is None: if group_id is None:
if count == 1: if count == 1:
return "For recovery you need 1 share." return "For recovery you need 1 share."
@ -208,7 +219,7 @@ async def slip39_prompt_threshold(
else: else:
return f"The required number of shares to form Group {group_id + 1}." return f"The required number of shares to form Group {group_id + 1}."
def info(count: int): def info(count: int) -> str:
text = "The threshold sets the number of shares " text = "The threshold sets the number of shares "
if group_id is None: if group_id is None:
text += "needed to recover your wallet. " text += "needed to recover your wallet. "
@ -233,18 +244,18 @@ async def slip39_prompt_threshold(
return await _prompt_number( return await _prompt_number(
ctx, ctx,
title="SET THRESHOLD", "SET THRESHOLD",
description=description, description,
info=info, info,
count=count, count,
min_count=min_count, min_count,
max_count=max_count, max_count,
br_name="slip39_threshold", "slip39_threshold",
) )
async def slip39_prompt_number_of_shares( async def slip39_prompt_number_of_shares(
ctx: wire.GenericContext, group_id: int | None = None ctx: GenericContext, group_id: int | None = None
) -> int: ) -> int:
count = 5 count = 5
min_count = 1 min_count = 1
@ -266,17 +277,17 @@ async def slip39_prompt_number_of_shares(
return await _prompt_number( return await _prompt_number(
ctx, ctx,
title="SET NUMBER OF SHARES", "SET NUMBER OF SHARES",
description=description, description,
info=lambda i: info, lambda i: info,
count=count, count,
min_count=min_count, min_count,
max_count=max_count, max_count,
br_name="slip39_shares", "slip39_shares",
) )
async def slip39_advanced_prompt_number_of_groups(ctx: wire.GenericContext) -> int: async def slip39_advanced_prompt_number_of_groups(ctx: GenericContext) -> int:
count = 5 count = 5
min_count = 2 min_count = 2
max_count = 16 max_count = 16
@ -285,18 +296,18 @@ async def slip39_advanced_prompt_number_of_groups(ctx: wire.GenericContext) -> i
return await _prompt_number( return await _prompt_number(
ctx, ctx,
title="SET NUMBER OF GROUPS", "SET NUMBER OF GROUPS",
description=lambda i: description, lambda i: description,
info=lambda i: info, lambda i: info,
count=count, count,
min_count=min_count, min_count,
max_count=max_count, max_count,
br_name="slip39_groups", "slip39_groups",
) )
async def slip39_advanced_prompt_group_threshold( async def slip39_advanced_prompt_group_threshold(
ctx: wire.GenericContext, num_of_groups: int ctx: GenericContext, num_of_groups: int
) -> int: ) -> int:
count = num_of_groups // 2 + 1 count = num_of_groups // 2 + 1
min_count = 1 min_count = 1
@ -306,17 +317,17 @@ async def slip39_advanced_prompt_group_threshold(
return await _prompt_number( return await _prompt_number(
ctx, ctx,
title="SET GROUP THRESHOLD", "SET GROUP THRESHOLD",
description=lambda i: description, lambda i: description,
info=lambda i: info, lambda i: info,
count=count, count,
min_count=min_count, min_count,
max_count=max_count, max_count,
br_name="slip39_group_threshold", "slip39_group_threshold",
) )
async def show_warning_backup(ctx: wire.GenericContext, slip39: bool) -> None: async def show_warning_backup(ctx: GenericContext, slip39: bool) -> None:
if slip39: if slip39:
description = ( description = (
"Never make a digital copy of your shares and never upload them online." "Never make a digital copy of your shares and never upload them online."
@ -337,5 +348,5 @@ async def show_warning_backup(ctx: wire.GenericContext, slip39: bool) -> None:
"backup_warning", "backup_warning",
ButtonRequestType.ResetDevice, ButtonRequestType.ResetDevice,
) )
if result != trezorui2.CONFIRMED: if result != CONFIRMED:
raise wire.ActionCancelled raise ActionCancelled

@ -1,15 +1,15 @@
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
from trezor import wire
from ...components.common.webauthn import ConfirmInfo from ...components.common.webauthn import ConfirmInfo
if TYPE_CHECKING: if TYPE_CHECKING:
from trezor.wire import GenericContext
Pageable = object Pageable = object
async def confirm_webauthn( async def confirm_webauthn(
ctx: wire.GenericContext | None, ctx: GenericContext | None,
info: ConfirmInfo, info: ConfirmInfo,
pageable: Pageable | None = None, pageable: Pageable | None = None,
) -> bool: ) -> bool:

Loading…
Cancel
Save