From 88c5466288270ed56a98662d9ca8055b7c40a148 Mon Sep 17 00:00:00 2001 From: obrusvit Date: Wed, 30 Oct 2024 15:47:53 +0100 Subject: [PATCH] refactor(core): move confirm_firmware_update --- core/embed/rust/src/ui/api/firmware_upy.rs | 23 ++++++++++ .../embed/rust/src/ui/model_mercury/layout.rs | 24 ---------- .../src/ui/model_mercury/ui_features_fw.rs | 9 ++++ core/embed/rust/src/ui/model_tr/layout.rs | 45 ------------------- .../rust/src/ui/model_tr/ui_features_fw.rs | 32 ++++++++++++- core/embed/rust/src/ui/model_tt/layout.rs | 38 ---------------- .../rust/src/ui/model_tt/ui_features_fw.rs | 27 +++++++++-- core/embed/rust/src/ui/ui_features_fw.rs | 5 +++ core/mocks/generated/trezorui2.pyi | 27 ----------- core/mocks/generated/trezorui_api.pyi | 9 ++++ .../src/trezor/ui/layouts/mercury/__init__.py | 2 +- core/src/trezor/ui/layouts/tr/__init__.py | 2 +- core/src/trezor/ui/layouts/tt/__init__.py | 2 +- 13 files changed, 104 insertions(+), 141 deletions(-) diff --git a/core/embed/rust/src/ui/api/firmware_upy.rs b/core/embed/rust/src/ui/api/firmware_upy.rs index 11d262f076..d4f35e60a3 100644 --- a/core/embed/rust/src/ui/api/firmware_upy.rs +++ b/core/embed/rust/src/ui/api/firmware_upy.rs @@ -69,6 +69,21 @@ extern "C" fn new_confirm_action(n_args: usize, args: *const Obj, kwargs: *mut M }; unsafe { util::try_with_args_and_kwargs(n_args, args, kwargs, block) } } +// TODO: there was `no_mangle` attribute in TT, should we apply it? +extern "C" fn new_confirm_firmware_update( + n_args: usize, + args: *const Obj, + kwargs: *mut Map, +) -> Obj { + let block = move |_args: &[Obj], kwargs: &Map| { + let description: TString = kwargs.get(Qstr::MP_QSTR_description)?.try_into()?; + let fingerprint: TString = kwargs.get(Qstr::MP_QSTR_fingerprint)?.try_into()?; + + let layout = ModelUI::confirm_firmware_update(description, fingerprint)?; + 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| { @@ -286,6 +301,14 @@ pub static mp_module_trezorui_api: Module = obj_module! { /// """Confirm action.""" Qstr::MP_QSTR_confirm_action => obj_fn_kw!(0, new_confirm_action).as_obj(), + /// def confirm_firmware_update( + /// *, + /// description: str, + /// fingerprint: str, + /// ) -> LayoutObj[UiResult]: + /// """Ask whether to update firmware, optionally show fingerprint.""" + Qstr::MP_QSTR_confirm_firmware_update => obj_fn_kw!(0, new_confirm_firmware_update).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 73408b8e71..caea1aa07f 100644 --- a/core/embed/rust/src/ui/model_mercury/layout.rs +++ b/core/embed/rust/src/ui/model_mercury/layout.rs @@ -363,22 +363,6 @@ extern "C" fn new_confirm_address(n_args: usize, args: *const Obj, kwargs: *mut unsafe { util::try_with_args_and_kwargs(n_args, args, kwargs, block) } } -extern "C" fn new_confirm_firmware_update( - n_args: usize, - args: *const Obj, - kwargs: *mut Map, -) -> Obj { - let block = move |_args: &[Obj], kwargs: &Map| { - let description: TString = kwargs.get(Qstr::MP_QSTR_description)?.try_into()?; - let fingerprint: TString = kwargs.get(Qstr::MP_QSTR_fingerprint)?.try_into()?; - - let flow = - flow::confirm_firmware_update::new_confirm_firmware_update(description, fingerprint)?; - Ok(LayoutObj::new_root(flow)?.into()) - }; - unsafe { util::try_with_args_and_kwargs(n_args, args, kwargs, block) } -} - extern "C" fn new_confirm_properties(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()?; @@ -1697,14 +1681,6 @@ pub static mp_module_trezorui2: Module = obj_module! { /// """Homescreen for locked device.""" Qstr::MP_QSTR_show_lockscreen => obj_fn_kw!(0, new_show_lockscreen).as_obj(), - /// def confirm_firmware_update( - /// *, - /// description: str, - /// fingerprint: str, - /// ) -> LayoutObj[UiResult]: - /// """Ask whether to update firmware, optionally show fingerprint.""" - Qstr::MP_QSTR_confirm_firmware_update => obj_fn_kw!(0, new_confirm_firmware_update).as_obj(), - /// def tutorial() -> LayoutObj[UiResult]: /// """Show user how to interact with the device.""" Qstr::MP_QSTR_tutorial => obj_fn_0!(new_show_tutorial).as_obj(), 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 94cc2f9fff..ce2140a1a2 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 @@ -54,6 +54,15 @@ impl UIFeaturesFirmware for ModelMercuryFeatures { Ok(flow) } + fn confirm_firmware_update( + description: TString<'static>, + fingerprint: TString<'static>, + ) -> Result { + let flow = + flow::confirm_firmware_update::new_confirm_firmware_update(description, fingerprint)?; + 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 88d2b34fc6..f970552ef4 100644 --- a/core/embed/rust/src/ui/model_tr/layout.rs +++ b/core/embed/rust/src/ui/model_tr/layout.rs @@ -1423,43 +1423,6 @@ extern "C" fn new_show_lockscreen(n_args: usize, args: *const Obj, kwargs: *mut unsafe { util::try_with_args_and_kwargs(n_args, args, kwargs, block) } } -extern "C" fn new_confirm_firmware_update( - n_args: usize, - args: *const Obj, - kwargs: *mut Map, -) -> Obj { - use super::component::bl_confirm::Confirm; - let block = move |_args: &[Obj], kwargs: &Map| { - let description: TString = kwargs.get(Qstr::MP_QSTR_description)?.try_into()?; - let fingerprint: TString = kwargs.get(Qstr::MP_QSTR_fingerprint)?.try_into()?; - - let title = TR::firmware_update__title; - let message = Label::left_aligned(description, theme::TEXT_NORMAL).vertically_centered(); - let fingerprint = Label::left_aligned( - fingerprint, - theme::TEXT_NORMAL.with_line_breaking(LineBreaking::BreakWordsNoHyphen), - ) - .vertically_centered(); - - let obj = LayoutObj::new( - Confirm::new( - theme::BG, - title.into(), - message, - None, - TR::buttons__install.as_tstring(), - false, - ) - .with_info_screen( - TR::firmware_update__title_fingerprint.as_tstring(), - fingerprint, - ), - )?; - Ok(obj.into()) - }; - unsafe { util::try_with_args_and_kwargs(n_args, args, kwargs, block) } -} - extern "C" fn new_show_wait_text(message: Obj) -> Obj { let block = || { let message: TString<'static> = message.try_into()?; @@ -1798,14 +1761,6 @@ pub static mp_module_trezorui2: Module = obj_module! { /// """Homescreen for locked device.""" Qstr::MP_QSTR_show_lockscreen => obj_fn_kw!(0, new_show_lockscreen).as_obj(), - /// def confirm_firmware_update( - /// *, - /// description: str, - /// fingerprint: str, - /// ) -> LayoutObj[UiResult]: - /// """Ask whether to update firmware, optionally show fingerprint. Shared with bootloader.""" - Qstr::MP_QSTR_confirm_firmware_update => obj_fn_kw!(0, new_confirm_firmware_update).as_obj(), - /// def show_wait_text(message: str, /) -> None: /// """Show single-line text in the middle of the screen.""" Qstr::MP_QSTR_show_wait_text => obj_fn_1!(new_show_wait_text).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 a8cf038d1c..14614e2792 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 @@ -8,7 +8,7 @@ use crate::{ ui::{ component::{ text::paragraphs::{Paragraph, ParagraphSource, ParagraphVecShort, Paragraphs, VecExt}, - Component, ComponentExt, Paginate, Timeout, + Component, ComponentExt, Label, LineBreaking, Paginate, Timeout, }, layout::{ obj::{LayoutMaybeTrace, LayoutObj, RootComponent}, @@ -67,6 +67,36 @@ impl UIFeaturesFirmware for ModelTRFeatures { ) } + fn confirm_firmware_update( + description: TString<'static>, + fingerprint: TString<'static>, + ) -> Result { + use super::component::bl_confirm::Confirm; + let title = TR::firmware_update__title; + let message = Label::left_aligned(description, theme::TEXT_NORMAL).vertically_centered(); + let fingerprint = Label::left_aligned( + fingerprint, + theme::TEXT_NORMAL.with_line_breaking(LineBreaking::BreakWordsNoHyphen), + ) + .vertically_centered(); + + let layout = RootComponent::new( + Confirm::new( + theme::BG, + title.into(), + message, + None, + TR::buttons__install.as_tstring(), + false, + ) + .with_info_screen( + TR::firmware_update__title_fingerprint.as_tstring(), + fingerprint, + ), + ); + Ok(layout) + } + 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 29aabe66a4..6f76ca851e 100644 --- a/core/embed/rust/src/ui/model_tt/layout.rs +++ b/core/embed/rust/src/ui/model_tt/layout.rs @@ -1432,36 +1432,6 @@ extern "C" fn new_show_lockscreen(n_args: usize, args: *const Obj, kwargs: *mut unsafe { util::try_with_args_and_kwargs(n_args, args, kwargs, block) } } -#[no_mangle] -extern "C" fn new_confirm_firmware_update( - n_args: usize, - args: *const Obj, - kwargs: *mut Map, -) -> Obj { - use super::component::bl_confirm::{Confirm, ConfirmTitle}; - let block = move |_args: &[Obj], kwargs: &Map| { - let description: TString = kwargs.get(Qstr::MP_QSTR_description)?.try_into()?; - let fingerprint: TString = kwargs.get(Qstr::MP_QSTR_fingerprint)?.try_into()?; - - let title_str = TR::firmware_update__title.into(); - let title = Label::left_aligned(title_str, theme::TEXT_BOLD).vertically_centered(); - let msg = Label::left_aligned(description, theme::TEXT_NORMAL); - - let left = Button::with_text(TR::buttons__cancel.into()).styled(theme::button_default()); - let right = Button::with_text(TR::buttons__install.into()).styled(theme::button_confirm()); - - let obj = LayoutObj::new( - Confirm::new(theme::BG, left, right, ConfirmTitle::Text(title), msg).with_info( - TR::firmware_update__title_fingerprint.into(), - fingerprint, - theme::button_moreinfo(), - ), - )?; - Ok(obj.into()) - }; - unsafe { util::try_with_args_and_kwargs(n_args, args, kwargs, block) } -} - extern "C" fn new_show_wait_text(message: Obj) -> Obj { let block = || { let message: TString<'static> = message.try_into()?; @@ -1813,14 +1783,6 @@ pub static mp_module_trezorui2: Module = obj_module! { /// """Homescreen for locked device.""" Qstr::MP_QSTR_show_lockscreen => obj_fn_kw!(0, new_show_lockscreen).as_obj(), - /// def confirm_firmware_update( - /// *, - /// description: str, - /// fingerprint: str, - /// ) -> LayoutObj[UiResult]: - /// """Ask whether to update firmware, optionally show fingerprint. Shared with bootloader.""" - Qstr::MP_QSTR_confirm_firmware_update => obj_fn_kw!(0, new_confirm_firmware_update).as_obj(), - /// def show_wait_text(message: str, /) -> LayoutObj[None]: /// """Show single-line text in the middle of the screen.""" Qstr::MP_QSTR_show_wait_text => obj_fn_1!(new_show_wait_text).as_obj(), 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 36760c35e7..16ddee65a2 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 @@ -6,9 +6,7 @@ use crate::{ translations::TR, ui::{ component::{ - image::BlendedImage, - text::paragraphs::{Paragraph, ParagraphSource, ParagraphVecShort, Paragraphs, VecExt}, - ComponentExt, Empty, Timeout, + image::BlendedImage, text::paragraphs::{Paragraph, ParagraphSource, ParagraphVecShort, Paragraphs, VecExt}, ComponentExt, Empty, Label, Timeout }, layout::{ obj::{LayoutMaybeTrace, LayoutObj, RootComponent}, @@ -69,6 +67,29 @@ impl UIFeaturesFirmware for ModelTTFeatures { Ok(layout) } + fn confirm_firmware_update( + description: TString<'static>, + fingerprint: TString<'static>, + ) -> Result { + use super::component::bl_confirm::{Confirm, ConfirmTitle}; + + let title_str = TR::firmware_update__title.into(); + let title = Label::left_aligned(title_str, theme::TEXT_BOLD).vertically_centered(); + let msg = Label::left_aligned(description, theme::TEXT_NORMAL); + + let left = Button::with_text(TR::buttons__cancel.into()).styled(theme::button_default()); + let right = Button::with_text(TR::buttons__install.into()).styled(theme::button_confirm()); + + let layout = RootComponent::new( + Confirm::new(theme::BG, left, right, ConfirmTitle::Text(title), msg).with_info( + TR::firmware_update__title_fingerprint.into(), + fingerprint, + theme::button_moreinfo(), + ), + ); + 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 a24a3729f1..8782142cbf 100644 --- a/core/embed/rust/src/ui/ui_features_fw.rs +++ b/core/embed/rust/src/ui/ui_features_fw.rs @@ -20,6 +20,11 @@ pub trait UIFeaturesFirmware { prompt_title: Option>, ) -> Result; + fn confirm_firmware_update( + description: TString<'static>, + fingerprint: TString<'static>, + ) -> 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 117d5f9295..2c17a95e5b 100644 --- a/core/mocks/generated/trezorui2.pyi +++ b/core/mocks/generated/trezorui2.pyi @@ -364,15 +364,6 @@ def show_lockscreen( """Homescreen for locked device.""" -# rust/src/ui/model_mercury/layout.rs -def confirm_firmware_update( - *, - description: str, - fingerprint: str, -) -> LayoutObj[UiResult]: - """Ask whether to update firmware, optionally show fingerprint.""" - - # rust/src/ui/model_mercury/layout.rs def tutorial() -> LayoutObj[UiResult]: """Show user how to interact with the device.""" @@ -808,15 +799,6 @@ def show_lockscreen( """Homescreen for locked device.""" -# rust/src/ui/model_tr/layout.rs -def confirm_firmware_update( - *, - description: str, - fingerprint: str, -) -> LayoutObj[UiResult]: - """Ask whether to update firmware, optionally show fingerprint. Shared with bootloader.""" - - # rust/src/ui/model_tr/layout.rs def show_wait_text(message: str, /) -> None: """Show single-line text in the middle of the screen.""" @@ -1190,15 +1172,6 @@ def show_lockscreen( """Homescreen for locked device.""" -# rust/src/ui/model_tt/layout.rs -def confirm_firmware_update( - *, - description: str, - fingerprint: str, -) -> LayoutObj[UiResult]: - """Ask whether to update firmware, optionally show fingerprint. Shared with bootloader.""" - - # rust/src/ui/model_tt/layout.rs def show_wait_text(message: str, /) -> LayoutObj[None]: """Show single-line text in the middle of the screen.""" diff --git a/core/mocks/generated/trezorui_api.pyi b/core/mocks/generated/trezorui_api.pyi index a0febfc6ba..c0340dd7ab 100644 --- a/core/mocks/generated/trezorui_api.pyi +++ b/core/mocks/generated/trezorui_api.pyi @@ -96,6 +96,15 @@ def confirm_action( """Confirm action.""" +# rust/src/ui/api/firmware_upy.rs +def confirm_firmware_update( + *, + description: str, + fingerprint: str, +) -> LayoutObj[UiResult]: + """Ask whether to update firmware, optionally show fingerprint.""" + + # 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 1738eb1747..73b442e80b 100644 --- a/core/src/trezor/ui/layouts/mercury/__init__.py +++ b/core/src/trezor/ui/layouts/mercury/__init__.py @@ -1183,7 +1183,7 @@ def confirm_set_new_pin( def confirm_firmware_update(description: str, fingerprint: str) -> Awaitable[None]: return raise_if_not_confirmed( - trezorui2.confirm_firmware_update( + trezorui_api.confirm_firmware_update( description=description, fingerprint=fingerprint ), "firmware_update", diff --git a/core/src/trezor/ui/layouts/tr/__init__.py b/core/src/trezor/ui/layouts/tr/__init__.py index ddccc17f77..4a8d51623e 100644 --- a/core/src/trezor/ui/layouts/tr/__init__.py +++ b/core/src/trezor/ui/layouts/tr/__init__.py @@ -1291,7 +1291,7 @@ async def confirm_set_new_pin( def confirm_firmware_update(description: str, fingerprint: str) -> Awaitable[None]: return raise_if_not_confirmed( - trezorui2.confirm_firmware_update( + trezorui_api.confirm_firmware_update( description=description, fingerprint=fingerprint ), "firmware_update", diff --git a/core/src/trezor/ui/layouts/tt/__init__.py b/core/src/trezor/ui/layouts/tt/__init__.py index 5cbc8f30ee..bcd9e7cb27 100644 --- a/core/src/trezor/ui/layouts/tt/__init__.py +++ b/core/src/trezor/ui/layouts/tt/__init__.py @@ -1252,7 +1252,7 @@ def confirm_set_new_pin( def confirm_firmware_update(description: str, fingerprint: str) -> Awaitable[None]: return raise_if_not_confirmed( - trezorui2.confirm_firmware_update( + trezorui_api.confirm_firmware_update( description=description, fingerprint=fingerprint ), "firmware_update",