1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-07-09 08:08:09 +00:00

refactor(core): improve recovery_enter_share

This commit is contained in:
matejcik 2023-11-20 11:58:33 +01:00
parent 162f13c6ea
commit 82d4b5c065
8 changed files with 56 additions and 33 deletions

View File

@ -122,6 +122,7 @@ static void _librust_qstrs(void) {
MP_QSTR_show_homescreen; MP_QSTR_show_homescreen;
MP_QSTR_show_info; MP_QSTR_show_info;
MP_QSTR_show_info_with_cancel; MP_QSTR_show_info_with_cancel;
MP_QSTR_show_instructions;
MP_QSTR_show_lockscreen; MP_QSTR_show_lockscreen;
MP_QSTR_show_mismatch; MP_QSTR_show_mismatch;
MP_QSTR_show_passphrase; MP_QSTR_show_passphrase;

View File

@ -1460,11 +1460,11 @@ extern "C" fn new_confirm_recovery(n_args: usize, args: *const Obj, kwargs: *mut
let description: StrBuffer = kwargs.get(Qstr::MP_QSTR_description)?.try_into()?; let description: StrBuffer = kwargs.get(Qstr::MP_QSTR_description)?.try_into()?;
let button: StrBuffer = kwargs.get(Qstr::MP_QSTR_button)?.try_into()?; let button: StrBuffer = kwargs.get(Qstr::MP_QSTR_button)?.try_into()?;
let dry_run: bool = kwargs.get(Qstr::MP_QSTR_dry_run)?.try_into()?; let dry_run: bool = kwargs.get(Qstr::MP_QSTR_dry_run)?.try_into()?;
let show_info: bool = kwargs.get(Qstr::MP_QSTR_show_info)?.try_into()?; let show_instructions: bool = kwargs.get(Qstr::MP_QSTR_show_instructions)?.try_into()?;
let mut paragraphs = ParagraphVecShort::new(); let mut paragraphs = ParagraphVecShort::new();
paragraphs.add(Paragraph::new(&theme::TEXT_NORMAL, description)); paragraphs.add(Paragraph::new(&theme::TEXT_NORMAL, description));
if show_info { if show_instructions {
let first = "You'll only have to select the first 2-4 letters of each word."; let first = "You'll only have to select the first 2-4 letters of each word.";
let second = let second =
"Position of the cursor will change between entries for enhanced security."; "Position of the cursor will change between entries for enhanced security.";
@ -1994,7 +1994,7 @@ pub static mp_module_trezorui2: Module = obj_module! {
/// button: str, /// button: str,
/// dry_run: bool, /// dry_run: bool,
/// info_button: bool, # unused on TR /// info_button: bool, # unused on TR
/// show_info: bool, /// show_instructions: bool,
/// ) -> LayoutObj[UiResult]: /// ) -> LayoutObj[UiResult]:
/// """Device recovery homescreen.""" /// """Device recovery homescreen."""
Qstr::MP_QSTR_confirm_recovery => obj_fn_kw!(0, new_confirm_recovery).as_obj(), Qstr::MP_QSTR_confirm_recovery => obj_fn_kw!(0, new_confirm_recovery).as_obj(),

View File

@ -2043,6 +2043,7 @@ pub static mp_module_trezorui2: Module = obj_module! {
/// button: str, /// button: str,
/// dry_run: bool, /// dry_run: bool,
/// info_button: bool = False, /// info_button: bool = False,
/// show_instructions: bool = False, # unused on TT
/// ) -> LayoutObj[UiResult]: /// ) -> LayoutObj[UiResult]:
/// """Device recovery homescreen.""" /// """Device recovery homescreen."""
Qstr::MP_QSTR_confirm_recovery => obj_fn_kw!(0, new_confirm_recovery).as_obj(), Qstr::MP_QSTR_confirm_recovery => obj_fn_kw!(0, new_confirm_recovery).as_obj(),

View File

@ -371,7 +371,7 @@ def confirm_recovery(
button: str, button: str,
dry_run: bool, dry_run: bool,
info_button: bool, # unused on TR info_button: bool, # unused on TR
show_info: bool, show_instructions: bool,
) -> LayoutObj[UiResult]: ) -> LayoutObj[UiResult]:
"""Device recovery homescreen.""" """Device recovery homescreen."""
@ -883,6 +883,7 @@ def confirm_recovery(
button: str, button: str,
dry_run: bool, dry_run: bool,
info_button: bool = False, info_button: bool = False,
show_instructions: bool = False, # unused on TT
) -> LayoutObj[UiResult]: ) -> LayoutObj[UiResult]:
"""Device recovery homescreen.""" """Device recovery homescreen."""

View File

@ -188,12 +188,7 @@ async def _request_share_first_screen(word_count: int) -> None:
if remaining: if remaining:
await _request_share_next_screen() await _request_share_next_screen()
else: else:
await layout.homescreen_dialog( await layout.enter_share(word_count=word_count)
"Enter share",
"Enter any share",
f"({word_count} words)",
show_info=True,
)
else: # BIP-39 else: # BIP-39
await layout.homescreen_dialog( await layout.homescreen_dialog(
"Continue", "Continue",
@ -204,8 +199,6 @@ async def _request_share_first_screen(word_count: int) -> None:
async def _request_share_next_screen() -> None: async def _request_share_next_screen() -> None:
from trezor import strings
remaining = storage_recovery.fetch_slip39_remaining_shares() remaining = storage_recovery.fetch_slip39_remaining_shares()
group_count = storage_recovery.get_slip39_group_count() group_count = storage_recovery.get_slip39_group_count()
if not remaining: if not remaining:
@ -213,22 +206,10 @@ async def _request_share_next_screen() -> None:
raise RuntimeError raise RuntimeError
if group_count > 1: if group_count > 1:
await layout.homescreen_dialog( await layout.enter_share(info_func=_show_remaining_groups_and_shares)
"Enter",
"More shares needed",
info_func=_show_remaining_groups_and_shares,
)
else: else:
still_needed_shares = remaining[0] entered = len(storage_recovery_shares.fetch_group(0))
already_entered_shares = len(storage_recovery_shares.fetch_group(0)) await layout.enter_share(entered_remaining=(entered, remaining[0]))
overall_needed = still_needed_shares + already_entered_shares
entered = (
f"{already_entered_shares} of {overall_needed} shares entered successfully."
)
needed = strings.format_plural(
"{count} more {plural} needed.", still_needed_shares, "share"
)
await layout.homescreen_dialog("Enter share", entered, needed)
async def _show_remaining_groups_and_shares() -> None: async def _show_remaining_groups_and_shares() -> None:

View File

@ -12,7 +12,7 @@ from trezor.ui.layouts.recovery import ( # noqa: F401
from .. import backup_types from .. import backup_types
if TYPE_CHECKING: if TYPE_CHECKING:
from typing import Callable from typing import Awaitable, Callable
from trezor.enums import BackupType from trezor.enums import BackupType
@ -120,6 +120,45 @@ async def show_invalid_mnemonic(word_count: int) -> None:
) )
def enter_share(
word_count: int | None = None,
entered_remaining: tuple[int, int] | None = None,
info_func: Callable | None = None,
) -> Awaitable[None]:
from trezor import strings
show_instructions = False
if word_count is not None:
# First-time entry. Show instructions and word count.
text = "Enter any share"
subtext = f"({word_count} words)"
show_instructions = True
elif entered_remaining is not None:
# Basic Shamir. There is only one group, we report entered/remaining count.
entered, remaining = entered_remaining
total = entered + remaining
text = f"{entered} of {total} shares entered successfully."
subtext = strings.format_plural(
"{count} more {plural} needed.", remaining, "share"
)
else:
# SuperShamir. We cannot easily show entered/remaining across groups,
# the caller provided an info_func that has the details.
text = "More shares needed."
subtext = None
return homescreen_dialog(
"Enter share",
text,
subtext,
info_func,
show_instructions,
)
async def homescreen_dialog( async def homescreen_dialog(
button_label: str, button_label: str,
text: str, text: str,

View File

@ -67,7 +67,7 @@ async def continue_recovery(
subtext: str | None, subtext: str | None,
info_func: Callable | None, info_func: Callable | None,
dry_run: bool, dry_run: bool,
show_info: bool = False, show_instructions: bool = False,
) -> bool: ) -> bool:
# TODO: implement info_func? # TODO: implement info_func?
# There is very limited space on the screen # There is very limited space on the screen
@ -75,7 +75,7 @@ async def continue_recovery(
# Never showing info for dry-run, user already saw it and it is disturbing # Never showing info for dry-run, user already saw it and it is disturbing
if dry_run: if dry_run:
show_info = False show_instructions = False
if subtext: if subtext:
text += f"\n\n{subtext}" text += f"\n\n{subtext}"
@ -86,7 +86,7 @@ async def continue_recovery(
button=button_label.upper(), button=button_label.upper(),
info_button=False, info_button=False,
dry_run=dry_run, dry_run=dry_run,
show_info=show_info, # type: ignore [No parameter named "show_info"] show_instructions=show_instructions,
) )
result = await interact( result = await interact(
homepage, homepage,

View File

@ -106,9 +106,9 @@ async def continue_recovery(
subtext: str | None, subtext: str | None,
info_func: Callable | None, info_func: Callable | None,
dry_run: bool, dry_run: bool,
show_info: bool = False, # unused on TT show_instructions: bool = False,
) -> bool: ) -> bool:
if show_info: if show_instructions:
# Show this just one-time # Show this just one-time
description = "You'll only have to select the first 2-4 letters of each word." description = "You'll only have to select the first 2-4 letters of each word."
else: else: