From 0e36849c36466f7d1bfd12abd1b1faa400bcc363 Mon Sep 17 00:00:00 2001 From: obrusvit Date: Mon, 25 Nov 2024 07:05:04 +0100 Subject: [PATCH] refactor(core): move prompt_backup - model_r `confirm_backup` and mercury `flow_prompt_backup` merged into `prompt_backup` - not implemented for model_t --- core/embed/rust/librust_qstr.h | 3 +- core/embed/rust/src/ui/api/firmware_upy.rs | 14 ++++++- .../embed/rust/src/ui/model_mercury/layout.rs | 13 ------- .../src/ui/model_mercury/ui_features_fw.rs | 5 +++ core/embed/rust/src/ui/model_tr/layout.rs | 37 ------------------- .../rust/src/ui/model_tr/ui_features_fw.rs | 30 +++++++++++++++ .../rust/src/ui/model_tt/ui_features_fw.rs | 6 +++ core/embed/rust/src/ui/ui_features_fw.rs | 2 + core/mocks/generated/trezorui2.pyi | 10 ----- core/mocks/generated/trezorui_api.pyi | 5 +++ .../src/trezor/ui/layouts/mercury/__init__.py | 2 +- core/src/trezor/ui/layouts/tr/__init__.py | 2 +- 12 files changed, 64 insertions(+), 65 deletions(-) diff --git a/core/embed/rust/librust_qstr.h b/core/embed/rust/librust_qstr.h index 0ccd95cbd1..91ab96a61f 100644 --- a/core/embed/rust/librust_qstr.h +++ b/core/embed/rust/librust_qstr.h @@ -192,7 +192,6 @@ static void _librust_qstrs(void) { MP_QSTR_coinjoin_authorized; MP_QSTR_confirm_action; MP_QSTR_confirm_address; - MP_QSTR_confirm_backup; MP_QSTR_confirm_blob; MP_QSTR_confirm_blob_intro; MP_QSTR_confirm_coinjoin; @@ -255,7 +254,6 @@ static void _librust_qstrs(void) { MP_QSTR_flow_confirm_set_new_pin; MP_QSTR_flow_confirm_summary; MP_QSTR_flow_get_address; - MP_QSTR_flow_prompt_backup; MP_QSTR_flow_warning_hi_prio; MP_QSTR_get_language; MP_QSTR_get_transition_out; @@ -426,6 +424,7 @@ static void _librust_qstrs(void) { MP_QSTR_progress__x_seconds_left_template; MP_QSTR_progress_event; MP_QSTR_prompt; + MP_QSTR_prompt_backup; MP_QSTR_prompt_screen; MP_QSTR_prompt_title; MP_QSTR_qr_title; diff --git a/core/embed/rust/src/ui/api/firmware_upy.rs b/core/embed/rust/src/ui/api/firmware_upy.rs index 7ef729c12b..75a4d6e00c 100644 --- a/core/embed/rust/src/ui/api/firmware_upy.rs +++ b/core/embed/rust/src/ui/api/firmware_upy.rs @@ -4,7 +4,7 @@ use crate::{ gc::Gc, iter::IterBuf, list::List, - macros::{obj_fn_1, obj_fn_kw, obj_module}, + macros::{obj_fn_0, obj_fn_1, obj_fn_kw, obj_module}, map::Map, module::Module, obj::Obj, @@ -382,6 +382,14 @@ extern "C" fn new_continue_recovery_homepage( unsafe { util::try_with_args_and_kwargs(n_args, args, kwargs, block) } } +extern "C" fn new_prompt_backup() -> Obj { + let block = || { + let layout = ModelUI::prompt_backup()?; + Ok(LayoutObj::new_root(layout)?.into()) + }; + unsafe { util::try_or_raise(block) } +} + extern "C" fn new_request_bip39(n_args: usize, args: *const Obj, kwargs: *mut Map) -> Obj { let block = move |_args: &[Obj], kwargs: &Map| { let prompt: TString = kwargs.get(Qstr::MP_QSTR_prompt)?.try_into()?; @@ -1069,6 +1077,10 @@ pub static mp_module_trezorui_api: Module = obj_module! { /// """Device recovery homescreen.""" Qstr::MP_QSTR_continue_recovery_homepage => obj_fn_kw!(0, new_continue_recovery_homepage).as_obj(), + /// def prompt_backup() -> LayoutObj[UiResult]: + /// """Strongly recommend user to do a backup.""" + Qstr::MP_QSTR_prompt_backup => obj_fn_0!(new_prompt_backup).as_obj(), + /// def request_bip39( /// *, /// prompt: str, diff --git a/core/embed/rust/src/ui/model_mercury/layout.rs b/core/embed/rust/src/ui/model_mercury/layout.rs index 0b41dd512a..502361f215 100644 --- a/core/embed/rust/src/ui/model_mercury/layout.rs +++ b/core/embed/rust/src/ui/model_mercury/layout.rs @@ -534,15 +534,6 @@ extern "C" fn new_get_address(n_args: usize, args: *const Obj, kwargs: *mut Map) unsafe { util::try_with_args_and_kwargs(n_args, args, kwargs, block) } } -extern "C" fn new_prompt_backup() -> Obj { - let block = || { - let flow = flow::prompt_backup::new_prompt_backup()?; - let obj = LayoutObj::new_root(flow)?; - Ok(obj.into()) - }; - unsafe { util::try_or_raise(block) } -} - extern "C" fn new_warning_hi_prio(n_args: usize, args: *const Obj, kwargs: *mut Map) -> Obj { let block = move |_args: &[Obj], kwargs: &Map| { let title: TString = kwargs.get(Qstr::MP_QSTR_title)?.try_into()?; @@ -610,10 +601,6 @@ pub static mp_module_trezorui2: Module = obj_module! { /// """Transaction summary. Always hold to confirm.""" Qstr::MP_QSTR_confirm_total => obj_fn_kw!(0, new_confirm_total).as_obj(), - /// def flow_prompt_backup() -> LayoutObj[UiResult]: - /// """Prompt a user to create backup with an option to skip.""" - Qstr::MP_QSTR_flow_prompt_backup => obj_fn_0!(new_prompt_backup).as_obj(), - /// def flow_get_address( /// *, /// address: str | bytes, diff --git a/core/embed/rust/src/ui/model_mercury/ui_features_fw.rs b/core/embed/rust/src/ui/model_mercury/ui_features_fw.rs index 699eb4d740..329391002b 100644 --- a/core/embed/rust/src/ui/model_mercury/ui_features_fw.rs +++ b/core/embed/rust/src/ui/model_mercury/ui_features_fw.rs @@ -427,6 +427,11 @@ impl UIFeaturesFirmware for ModelMercuryFeatures { LayoutObj::new_root(flow) } + fn prompt_backup() -> Result { + let flow = flow::prompt_backup::new_prompt_backup()?; + Ok(flow) + } + fn request_bip39( prompt: TString<'static>, prefill_word: TString<'static>, diff --git a/core/embed/rust/src/ui/model_tr/layout.rs b/core/embed/rust/src/ui/model_tr/layout.rs index a093be6b8a..ed9c469b65 100644 --- a/core/embed/rust/src/ui/model_tr/layout.rs +++ b/core/embed/rust/src/ui/model_tr/layout.rs @@ -270,39 +270,6 @@ fn content_in_button_page( Ok(obj.into()) } -extern "C" fn new_confirm_backup(n_args: usize, args: *const Obj, kwargs: *mut Map) -> Obj { - let block = move |_args: &[Obj], _kwargs: &Map| { - let get_page = move |page_index| match page_index { - 0 => { - let btn_layout = ButtonLayout::text_none_arrow_wide(TR::buttons__skip.into()); - let btn_actions = ButtonActions::cancel_none_next(); - let ops = OpTextLayout::new(theme::TEXT_NORMAL) - .text_normal(TR::backup__new_wallet_created) - .newline() - .text_normal(TR::backup__it_should_be_backed_up_now); - let formatted = FormattedText::new(ops).vertically_centered(); - Page::new(btn_layout, btn_actions, formatted) - .with_title(TR::words__title_success.into()) - } - 1 => { - let btn_layout = ButtonLayout::up_arrow_none_text(TR::buttons__back_up.into()); - let btn_actions = ButtonActions::prev_none_confirm(); - let ops = - OpTextLayout::new(theme::TEXT_NORMAL).text_normal(TR::backup__recover_anytime); - let formatted = FormattedText::new(ops).vertically_centered(); - Page::new(btn_layout, btn_actions, formatted) - .with_title(TR::backup__title_backup_wallet.into()) - } - _ => unreachable!(), - }; - let pages = FlowPages::new(get_page, 2); - - let obj = LayoutObj::new(Flow::new(pages))?; - Ok(obj.into()) - }; - unsafe { util::try_with_args_and_kwargs(n_args, args, kwargs, block) } -} - extern "C" fn new_show_address_details(n_args: usize, args: *const Obj, kwargs: *mut Map) -> Obj { let block = move |_args: &[Obj], kwargs: &Map| { let address: TString = kwargs.get(Qstr::MP_QSTR_address)?.try_into()?; @@ -624,10 +591,6 @@ pub static mp_module_trezorui2: Module = obj_module! { /// Qstr::MP_QSTR___name__ => Qstr::MP_QSTR_trezorui2.to_obj(), - /// def confirm_backup() -> LayoutObj[UiResult]: - /// """Strongly recommend user to do backup.""" - Qstr::MP_QSTR_confirm_backup => obj_fn_kw!(0, new_confirm_backup).as_obj(), - /// def show_address_details( /// *, /// address: str, diff --git a/core/embed/rust/src/ui/model_tr/ui_features_fw.rs b/core/embed/rust/src/ui/model_tr/ui_features_fw.rs index 7148f8de0a..b664722b79 100644 --- a/core/embed/rust/src/ui/model_tr/ui_features_fw.rs +++ b/core/embed/rust/src/ui/model_tr/ui_features_fw.rs @@ -543,6 +543,36 @@ impl UIFeaturesFirmware for ModelTRFeatures { LayoutObj::new_root(layout) } + fn prompt_backup() -> Result { + let get_page = move |page_index| match page_index { + 0 => { + let btn_layout = ButtonLayout::text_none_arrow_wide(TR::buttons__skip.into()); + let btn_actions = ButtonActions::cancel_none_next(); + let ops = OpTextLayout::new(theme::TEXT_NORMAL) + .text_normal(TR::backup__new_wallet_created) + .newline() + .text_normal(TR::backup__it_should_be_backed_up_now); + let formatted = FormattedText::new(ops).vertically_centered(); + Page::new(btn_layout, btn_actions, formatted) + .with_title(TR::words__title_success.into()) + } + 1 => { + let btn_layout = ButtonLayout::up_arrow_none_text(TR::buttons__back_up.into()); + let btn_actions = ButtonActions::prev_none_confirm(); + let ops = + OpTextLayout::new(theme::TEXT_NORMAL).text_normal(TR::backup__recover_anytime); + let formatted = FormattedText::new(ops).vertically_centered(); + Page::new(btn_layout, btn_actions, formatted) + .with_title(TR::backup__title_backup_wallet.into()) + } + _ => unreachable!(), + }; + let pages = FlowPages::new(get_page, 2); + + let layout = RootComponent::new(Flow::new(pages)); + Ok(layout) + } + fn request_bip39( prompt: TString<'static>, prefill_word: TString<'static>, diff --git a/core/embed/rust/src/ui/model_tt/ui_features_fw.rs b/core/embed/rust/src/ui/model_tt/ui_features_fw.rs index 234733e6dc..aaed6ad8db 100644 --- a/core/embed/rust/src/ui/model_tt/ui_features_fw.rs +++ b/core/embed/rust/src/ui/model_tt/ui_features_fw.rs @@ -487,6 +487,12 @@ impl UIFeaturesFirmware for ModelTTFeatures { } } + fn prompt_backup() -> Result { + Err::, Error>(Error::ValueError( + c"prompt_backup not implemented", + )) + } + fn request_bip39( prompt: TString<'static>, prefill_word: TString<'static>, diff --git a/core/embed/rust/src/ui/ui_features_fw.rs b/core/embed/rust/src/ui/ui_features_fw.rs index 676ca8a981..0f1316a901 100644 --- a/core/embed/rust/src/ui/ui_features_fw.rs +++ b/core/embed/rust/src/ui/ui_features_fw.rs @@ -136,6 +136,8 @@ pub trait UIFeaturesFirmware { fn check_homescreen_format(image: BinaryData, accept_toif: bool) -> bool; + fn prompt_backup() -> Result; + fn request_bip39( prompt: TString<'static>, prefill_word: TString<'static>, diff --git a/core/mocks/generated/trezorui2.pyi b/core/mocks/generated/trezorui2.pyi index 69ef7445f8..8187ddaab5 100644 --- a/core/mocks/generated/trezorui2.pyi +++ b/core/mocks/generated/trezorui2.pyi @@ -48,11 +48,6 @@ def confirm_total( """Transaction summary. Always hold to confirm.""" -# rust/src/ui/model_mercury/layout.rs -def flow_prompt_backup() -> LayoutObj[UiResult]: - """Prompt a user to create backup with an option to skip.""" - - # rust/src/ui/model_mercury/layout.rs def flow_get_address( *, @@ -126,11 +121,6 @@ from trezor import utils from trezorui_api import * -# rust/src/ui/model_tr/layout.rs -def confirm_backup() -> LayoutObj[UiResult]: - """Strongly recommend user to do backup.""" - - # rust/src/ui/model_tr/layout.rs def show_address_details( *, diff --git a/core/mocks/generated/trezorui_api.pyi b/core/mocks/generated/trezorui_api.pyi index abe9c96b1c..cdb44db2e8 100644 --- a/core/mocks/generated/trezorui_api.pyi +++ b/core/mocks/generated/trezorui_api.pyi @@ -265,6 +265,11 @@ def continue_recovery_homepage( """Device recovery homescreen.""" +# rust/src/ui/api/firmware_upy.rs +def prompt_backup() -> LayoutObj[UiResult]: + """Strongly recommend user to do a backup.""" + + # rust/src/ui/api/firmware_upy.rs def request_bip39( *, diff --git a/core/src/trezor/ui/layouts/mercury/__init__.py b/core/src/trezor/ui/layouts/mercury/__init__.py index 2fb11689a8..a75f62fd3e 100644 --- a/core/src/trezor/ui/layouts/mercury/__init__.py +++ b/core/src/trezor/ui/layouts/mercury/__init__.py @@ -105,7 +105,7 @@ async def show_wallet_created_success() -> None: async def prompt_backup() -> bool: result = await interact( - trezorui2.flow_prompt_backup(), + trezorui_api.prompt_backup(), "backup_device", ButtonRequestType.ResetDevice, raise_on_cancel=None, diff --git a/core/src/trezor/ui/layouts/tr/__init__.py b/core/src/trezor/ui/layouts/tr/__init__.py index 71413e48bd..c355c3b792 100644 --- a/core/src/trezor/ui/layouts/tr/__init__.py +++ b/core/src/trezor/ui/layouts/tr/__init__.py @@ -128,7 +128,7 @@ async def prompt_backup() -> bool: br_code = ButtonRequestType.ResetDevice result = await interact( - trezorui2.confirm_backup(), + trezorui_api.prompt_backup(), br_name, br_code, raise_on_cancel=None,