From d4fa752709303150bbd8df475c0dcfc0c125fa60 Mon Sep 17 00:00:00 2001 From: obrusvit Date: Fri, 1 Nov 2024 21:03:39 +0100 Subject: [PATCH] refactor(core): move confirm_reset to UiFeatures - unify param to just one bool `recovery` - title and button is determined in Rust based on the argument --- core/embed/rust/librust_qstr.h | 1 - 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 | 26 -------------- .../rust/src/ui/model_tr/ui_features_fw.rs | 27 ++++++++++++++- core/embed/rust/src/ui/model_tt/layout.rs | 34 ------------------- .../rust/src/ui/model_tt/ui_features_fw.rs | 31 +++++++++++++++++ core/embed/rust/src/ui/ui_features_fw.rs | 2 ++ core/mocks/generated/trezorui2.pyi | 23 ------------- core/mocks/generated/trezorui_api.pyi | 5 +++ .../management/recovery_device/__init__.py | 2 +- .../apps/management/reset_device/__init__.py | 2 +- .../src/trezor/ui/layouts/mercury/__init__.py | 6 ++-- core/src/trezor/ui/layouts/tr/__init__.py | 11 +----- core/src/trezor/ui/layouts/tt/__init__.py | 12 ++----- 16 files changed, 92 insertions(+), 123 deletions(-) diff --git a/core/embed/rust/librust_qstr.h b/core/embed/rust/librust_qstr.h index f72e2a42e2..3f2ca5aa90 100644 --- a/core/embed/rust/librust_qstr.h +++ b/core/embed/rust/librust_qstr.h @@ -251,7 +251,6 @@ static void _librust_qstrs(void) { MP_QSTR_firmware_update__title_fingerprint; MP_QSTR_first_screen; MP_QSTR_flow_confirm_output; - MP_QSTR_flow_confirm_reset; MP_QSTR_flow_confirm_set_new_pin; MP_QSTR_flow_confirm_summary; MP_QSTR_flow_continue_recovery; diff --git a/core/embed/rust/src/ui/api/firmware_upy.rs b/core/embed/rust/src/ui/api/firmware_upy.rs index 10b07e4933..ef6b271c6d 100644 --- a/core/embed/rust/src/ui/api/firmware_upy.rs +++ b/core/embed/rust/src/ui/api/firmware_upy.rs @@ -110,6 +110,16 @@ extern "C" fn new_confirm_homescreen(n_args: usize, args: *const Obj, kwargs: *m unsafe { util::try_with_args_and_kwargs(n_args, args, kwargs, block) } } +extern "C" fn new_confirm_reset_device(n_args: usize, args: *const Obj, kwargs: *mut Map) -> Obj { + let block = move |_args: &[Obj], kwargs: &Map| { + let recovery: bool = kwargs.get(Qstr::MP_QSTR_recovery)?.try_into()?; + + let layout = ModelUI::confirm_reset_device(recovery)?; + Ok(LayoutObj::new_root(layout)?.into()) + }; + unsafe { util::try_with_args_and_kwargs(n_args, args, kwargs, 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()?; @@ -494,6 +504,10 @@ pub static mp_module_trezorui_api: Module = obj_module! { /// """Confirm homescreen.""" Qstr::MP_QSTR_confirm_homescreen => obj_fn_kw!(0, new_confirm_homescreen).as_obj(), + /// def confirm_reset_device(recovery: bool) -> LayoutObj[UiResult]: + /// """Confirm TOS before creating wallet creation or wallet recovery.""" + Qstr::MP_QSTR_confirm_reset_device => obj_fn_kw!(0, new_confirm_reset_device).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 887e9a2bf3..bdabc4da3b 100644 --- a/core/embed/rust/src/ui/model_mercury/layout.rs +++ b/core/embed/rust/src/ui/model_mercury/layout.rs @@ -389,16 +389,6 @@ extern "C" fn new_confirm_properties(n_args: usize, args: *const Obj, kwargs: *m unsafe { util::try_with_args_and_kwargs(n_args, args, kwargs, block) } } -extern "C" fn new_confirm_reset(n_args: usize, args: *const Obj, kwargs: *mut Map) -> Obj { - let block = move |_args: &[Obj], kwargs: &Map| { - let recovery: bool = kwargs.get(Qstr::MP_QSTR_recovery)?.try_into()?; - - let flow = flow::confirm_reset::new_confirm_reset(recovery)?; - Ok(LayoutObj::new_root(flow)?.into()) - }; - unsafe { util::try_with_args_and_kwargs(n_args, args, kwargs, block) } -} - extern "C" fn new_confirm_set_new_pin(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()?; @@ -1147,10 +1137,6 @@ pub static mp_module_trezorui2: Module = obj_module! { /// the value is to be rendered as binary with monospace font, False otherwise.""" Qstr::MP_QSTR_confirm_properties => obj_fn_kw!(0, new_confirm_properties).as_obj(), - /// def flow_confirm_reset(recovery: bool) -> LayoutObj[UiResult]: - /// """Confirm TOS before creating wallet creation or wallet recovery.""" - Qstr::MP_QSTR_flow_confirm_reset => obj_fn_kw!(0, new_confirm_reset).as_obj(), - // TODO: supply more arguments for Wipe code setting when figma done /// def flow_confirm_set_new_pin( /// *, 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 df1de7083d..88797c4e00 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 @@ -107,6 +107,11 @@ impl UIFeaturesFirmware for ModelMercuryFeatures { Ok(flow) } + fn confirm_reset_device(recovery: bool) -> Result { + let flow = flow::confirm_reset::new_confirm_reset(recovery)?; + Ok(flow) + } + fn check_homescreen_format(image: BinaryData, __accept_toif: bool) -> bool { super::component::check_homescreen_format(image) } diff --git a/core/embed/rust/src/ui/model_tr/layout.rs b/core/embed/rust/src/ui/model_tr/layout.rs index d5f9c629ed..8d0c97612c 100644 --- a/core/embed/rust/src/ui/model_tr/layout.rs +++ b/core/embed/rust/src/ui/model_tr/layout.rs @@ -372,24 +372,6 @@ extern "C" fn new_confirm_properties(n_args: usize, args: *const Obj, kwargs: *m unsafe { util::try_with_args_and_kwargs(n_args, args, kwargs, block) } } -extern "C" fn new_confirm_reset_device(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()?; - let button: TString<'static> = kwargs.get(Qstr::MP_QSTR_button)?.try_into()?; - - let ops = OpTextLayout::new(theme::TEXT_NORMAL) - .text_normal(TR::reset__by_continuing) - .next_page() - .text_normal(TR::reset__more_info_at) - .newline() - .text_bold(TR::reset__tos_link); - let formatted = FormattedText::new(ops).vertically_centered(); - - content_in_button_page(title, formatted, button, Some("".into()), false) - }; - unsafe { util::try_with_args_and_kwargs(n_args, args, kwargs, block) } -} - 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 { @@ -1296,14 +1278,6 @@ pub static mp_module_trezorui2: Module = obj_module! { /// This only concerns the text style, you need to decode the value to UTF-8 in python.""" Qstr::MP_QSTR_confirm_properties => obj_fn_kw!(0, new_confirm_properties).as_obj(), - /// def confirm_reset_device( - /// *, - /// title: str, - /// button: str, - /// ) -> LayoutObj[UiResult]: - /// """Confirm TOS before device setup.""" - Qstr::MP_QSTR_confirm_reset_device => obj_fn_kw!(0, new_confirm_reset_device).as_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(), 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 8f6a528364..a8c485035e 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 @@ -30,7 +30,9 @@ use crate::{ use super::{ component::{ - ButtonDetails, ButtonPage, CoinJoinProgress, ConfirmHomescreen, Flow, FlowPages, Frame, Homescreen, Lockscreen, NumberInput, PassphraseEntry, PinEntry, Progress, ScrollableFrame, SimpleChoice, WordlistEntry, WordlistType + ButtonDetails, ButtonPage, CoinJoinProgress, ConfirmHomescreen, Flow, FlowPages, Frame, + Homescreen, Lockscreen, NumberInput, PassphraseEntry, PinEntry, Progress, ScrollableFrame, + SimpleChoice, WordlistEntry, WordlistType, }, theme, ModelTRFeatures, }; @@ -114,6 +116,29 @@ impl UIFeaturesFirmware for ModelTRFeatures { Ok(layout) } + fn confirm_reset_device(recovery: bool) -> Result { + let (title, button) = if recovery { + ( + TR::recovery__title_recover.into(), + TR::reset__button_recover.into(), + ) + } else { + ( + TR::reset__title_create_wallet.into(), + TR::reset__button_create.into(), + ) + }; + let ops = OpTextLayout::new(theme::TEXT_NORMAL) + .text_normal(TR::reset__by_continuing) + .next_page() + .text_normal(TR::reset__more_info_at) + .newline() + .text_bold(TR::reset__tos_link); + let formatted = FormattedText::new(ops).vertically_centered(); + + content_in_button_page(title, formatted, button, Some("".into()), false) + } + fn check_homescreen_format(image: BinaryData, _accept_toif: bool) -> bool { super::component::check_homescreen_format(image) } diff --git a/core/embed/rust/src/ui/model_tt/layout.rs b/core/embed/rust/src/ui/model_tt/layout.rs index 7b2483d8a8..6b47aef134 100644 --- a/core/embed/rust/src/ui/model_tt/layout.rs +++ b/core/embed/rust/src/ui/model_tt/layout.rs @@ -565,32 +565,6 @@ extern "C" fn new_confirm_properties(n_args: usize, args: *const Obj, kwargs: *m unsafe { util::try_with_args_and_kwargs(n_args, args, kwargs, block) } } -extern "C" fn new_confirm_reset_device(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()?; - let button: TString = kwargs.get(Qstr::MP_QSTR_button)?.try_into()?; - - let par_array: [Paragraph<'static>; 3] = [ - Paragraph::new(&theme::TEXT_NORMAL, TR::reset__by_continuing).with_bottom_padding(17), /* simulating a carriage return */ - Paragraph::new(&theme::TEXT_NORMAL, TR::reset__more_info_at), - Paragraph::new(&theme::TEXT_DEMIBOLD, TR::reset__tos_link), - ]; - let paragraphs = Paragraphs::new(par_array); - let buttons = Button::cancel_confirm( - Button::with_icon(theme::ICON_CANCEL), - Button::with_text(button).styled(theme::button_confirm()), - true, - ); - let obj = LayoutObj::new(Frame::left_aligned( - theme::label_title(), - title, - Dialog::new(paragraphs, buttons), - ))?; - 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 qr_title: TString<'static> = kwargs.get(Qstr::MP_QSTR_qr_title)?.try_into()?; @@ -1252,14 +1226,6 @@ pub static mp_module_trezorui2: Module = obj_module! { /// the value is to be rendered as binary with monospace font, False otherwise.""" Qstr::MP_QSTR_confirm_properties => obj_fn_kw!(0, new_confirm_properties).as_obj(), - /// def confirm_reset_device( - /// *, - /// title: str, - /// button: str, - /// ) -> LayoutObj[UiResult]: - /// """Confirm TOS before device setup.""" - Qstr::MP_QSTR_confirm_reset_device => obj_fn_kw!(0, new_confirm_reset_device).as_obj(), - /// def show_address_details( /// *, /// qr_title: str, 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 bd1cf6885a..e653c0db1b 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 @@ -122,6 +122,37 @@ impl UIFeaturesFirmware for ModelTTFeatures { Ok(layout) } + fn confirm_reset_device(recovery: bool) -> Result { + let (title, button) = if recovery { + ( + TR::recovery__title_recover.into(), + TR::reset__button_recover.into(), + ) + } else { + ( + TR::reset__title_create_wallet.into(), + TR::reset__button_create.into(), + ) + }; + let par_array: [Paragraph<'static>; 3] = [ + Paragraph::new(&theme::TEXT_NORMAL, TR::reset__by_continuing).with_bottom_padding(17), /* simulating a carriage return */ + Paragraph::new(&theme::TEXT_NORMAL, TR::reset__more_info_at), + Paragraph::new(&theme::TEXT_DEMIBOLD, TR::reset__tos_link), + ]; + let paragraphs = Paragraphs::new(par_array); + let buttons = Button::cancel_confirm( + Button::with_icon(theme::ICON_CANCEL), + Button::with_text(button).styled(theme::button_confirm()), + true, + ); + let layout = RootComponent::new(Frame::left_aligned( + theme::label_title(), + title, + Dialog::new(paragraphs, buttons), + )); + Ok(layout) + } + fn check_homescreen_format(image: BinaryData, accept_toif: bool) -> bool { super::component::check_homescreen_format(image, false) } diff --git a/core/embed/rust/src/ui/ui_features_fw.rs b/core/embed/rust/src/ui/ui_features_fw.rs index 60d52f30c4..a15a3d8f37 100644 --- a/core/embed/rust/src/ui/ui_features_fw.rs +++ b/core/embed/rust/src/ui/ui_features_fw.rs @@ -30,6 +30,8 @@ pub trait UIFeaturesFirmware { fingerprint: TString<'static>, ) -> Result; + fn confirm_reset_device(recovery: bool) -> Result; + fn check_homescreen_format(image: BinaryData, accept_toif: bool) -> bool; fn request_bip39( diff --git a/core/mocks/generated/trezorui2.pyi b/core/mocks/generated/trezorui2.pyi index d626a6e76d..5338ad35a0 100644 --- a/core/mocks/generated/trezorui2.pyi +++ b/core/mocks/generated/trezorui2.pyi @@ -60,11 +60,6 @@ def confirm_properties( the value is to be rendered as binary with monospace font, False otherwise.""" -# rust/src/ui/model_mercury/layout.rs -def flow_confirm_reset(recovery: bool) -> LayoutObj[UiResult]: - """Confirm TOS before creating wallet creation or wallet recovery.""" - - # rust/src/ui/model_mercury/layout.rs def flow_confirm_set_new_pin( *, @@ -391,15 +386,6 @@ def confirm_properties( This only concerns the text style, you need to decode the value to UTF-8 in python.""" -# rust/src/ui/model_tr/layout.rs -def confirm_reset_device( - *, - title: str, - button: str, -) -> LayoutObj[UiResult]: - """Confirm TOS before device setup.""" - - # rust/src/ui/model_tr/layout.rs def confirm_backup() -> LayoutObj[UiResult]: """Strongly recommend user to do backup.""" @@ -700,15 +686,6 @@ def confirm_properties( the value is to be rendered as binary with monospace font, False otherwise.""" -# rust/src/ui/model_tt/layout.rs -def confirm_reset_device( - *, - title: str, - button: str, -) -> LayoutObj[UiResult]: - """Confirm TOS before device setup.""" - - # rust/src/ui/model_tt/layout.rs def show_address_details( *, diff --git a/core/mocks/generated/trezorui_api.pyi b/core/mocks/generated/trezorui_api.pyi index 4b36e08e8c..ec2c2f5e01 100644 --- a/core/mocks/generated/trezorui_api.pyi +++ b/core/mocks/generated/trezorui_api.pyi @@ -114,6 +114,11 @@ def confirm_homescreen( """Confirm homescreen.""" +# rust/src/ui/api/firmware_upy.rs +def confirm_reset_device(recovery: bool) -> LayoutObj[UiResult]: + """Confirm TOS before creating wallet creation or wallet recovery.""" + + # rust/src/ui/api/firmware_upy.rs def request_bip39( *, diff --git a/core/src/apps/management/recovery_device/__init__.py b/core/src/apps/management/recovery_device/__init__.py index 08eb02b412..10ca5f6377 100644 --- a/core/src/apps/management/recovery_device/__init__.py +++ b/core/src/apps/management/recovery_device/__init__.py @@ -67,7 +67,7 @@ async def recovery_device(msg: RecoveryDevice) -> Success: return await recovery_process() if recovery_type == RecoveryType.NormalRecovery: - await confirm_reset_device(TR.recovery__title_recover, recovery=True) + await confirm_reset_device(recovery=True) # wipe storage to make sure the device is in a clear state storage.reset() diff --git a/core/src/apps/management/reset_device/__init__.py b/core/src/apps/management/reset_device/__init__.py index 4e38bfcd8c..f7863675c7 100644 --- a/core/src/apps/management/reset_device/__init__.py +++ b/core/src/apps/management/reset_device/__init__.py @@ -55,7 +55,7 @@ async def reset_device(msg: ResetDevice) -> Success: _validate_reset_device(msg) # make sure user knows they're setting up a new wallet - await confirm_reset_device(TR.reset__title_create_wallet) + await confirm_reset_device() # Rendering empty loader so users do not feel a freezing screen render_empty_loader(config.StorageMessage.PROCESSING_MSG) diff --git a/core/src/trezor/ui/layouts/mercury/__init__.py b/core/src/trezor/ui/layouts/mercury/__init__.py index 55b6fed606..ca553afd21 100644 --- a/core/src/trezor/ui/layouts/mercury/__init__.py +++ b/core/src/trezor/ui/layouts/mercury/__init__.py @@ -89,8 +89,10 @@ def confirm_single( ) -def confirm_reset_device(_title: str, recovery: bool = False) -> Awaitable[None]: - return raise_if_not_confirmed(trezorui2.flow_confirm_reset(recovery=recovery), None) +def confirm_reset_device(recovery: bool = False) -> Awaitable[None]: + return raise_if_not_confirmed( + trezorui_api.confirm_reset_device(recovery=recovery), None + ) async def show_wallet_created_success() -> None: diff --git a/core/src/trezor/ui/layouts/tr/__init__.py b/core/src/trezor/ui/layouts/tr/__init__.py index 618e0169d6..d539197bd3 100644 --- a/core/src/trezor/ui/layouts/tr/__init__.py +++ b/core/src/trezor/ui/layouts/tr/__init__.py @@ -109,19 +109,10 @@ def confirm_single( def confirm_reset_device( - title: str, recovery: bool = False, ) -> Awaitable[None]: - if recovery: - button = TR.reset__button_recover - else: - button = TR.reset__button_create - return raise_if_not_confirmed( - trezorui2.confirm_reset_device( - title=title, - button=button, - ), + trezorui_api.confirm_reset_device(recovery=recovery), "recover_device" if recovery else "setup_device", ButtonRequestType.ProtectCall if recovery else ButtonRequestType.ResetDevice, ) diff --git a/core/src/trezor/ui/layouts/tt/__init__.py b/core/src/trezor/ui/layouts/tt/__init__.py index b6b757889b..bdbcb11772 100644 --- a/core/src/trezor/ui/layouts/tt/__init__.py +++ b/core/src/trezor/ui/layouts/tt/__init__.py @@ -84,17 +84,9 @@ def confirm_single( ) -def confirm_reset_device(title: str, recovery: bool = False) -> Awaitable[None]: - if recovery: - button = TR.reset__button_recover - else: - button = TR.reset__button_create - +def confirm_reset_device(recovery: bool = False) -> Awaitable[None]: return raise_if_not_confirmed( - trezorui2.confirm_reset_device( - title=title, - button=button, - ), + trezorui_api.confirm_reset_device(recovery=recovery), "recover_device" if recovery else "setup_device", (ButtonRequestType.ProtectCall if recovery else ButtonRequestType.ResetDevice), )