From d3e3c87be71c89a77b9eae6e4d0ac5ca8a05a351 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 | 14 ------- .../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(+), 66 deletions(-) diff --git a/core/embed/rust/librust_qstr.h b/core/embed/rust/librust_qstr.h index e9a0877da4..671f01dbfe 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_get_language; MP_QSTR_get_transition_out; MP_QSTR_haptic_feedback__disable; @@ -425,6 +423,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 212a9bac86..d8b07ef45c 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, @@ -387,6 +387,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()?; @@ -1091,6 +1099,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 a09210d696..f62f2a64eb 100644 --- a/core/embed/rust/src/ui/model_mercury/layout.rs +++ b/core/embed/rust/src/ui/model_mercury/layout.rs @@ -534,16 +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) } -} - - #[no_mangle] pub static mp_module_trezorui2: Module = obj_module! { /// from trezor import utils @@ -594,10 +584,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 e4951621c6..e76beb92c8 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 b8dcae3193..8f3e0671b2 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 @@ -544,6 +544,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 5d1502f678..ab90c17db8 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 @@ -488,6 +488,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 26e71aa38e..af7e259214 100644 --- a/core/embed/rust/src/ui/ui_features_fw.rs +++ b/core/embed/rust/src/ui/ui_features_fw.rs @@ -137,6 +137,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 8cceba5093..4e326e6e72 100644 --- a/core/mocks/generated/trezorui2.pyi +++ b/core/mocks/generated/trezorui2.pyi @@ -47,11 +47,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( *, @@ -114,11 +109,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 9ddb10a447..33c5439886 100644 --- a/core/mocks/generated/trezorui_api.pyi +++ b/core/mocks/generated/trezorui_api.pyi @@ -266,6 +266,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 0fccca4568..fa93601388 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 5dde34021d..2dd8e54d27 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,