From bdcd81e531114ee0cb255ecafe440a99d97622c7 Mon Sep 17 00:00:00 2001 From: obrusvit Date: Wed, 27 Nov 2024 17:54:13 +0100 Subject: [PATCH] WIP - refactoring confirm_total of TT --- core/embed/rust/librust_qstr.h | 5 + core/embed/rust/src/ui/model_tt/layout.rs | 52 +++++++--- core/mocks/generated/trezorui2.pyi | 13 ++- core/src/trezor/ui/layouts/tt/__init__.py | 114 ++++++++++++++-------- 4 files changed, 129 insertions(+), 55 deletions(-) diff --git a/core/embed/rust/librust_qstr.h b/core/embed/rust/librust_qstr.h index 91ab96a61f..923eb7e0ab 100644 --- a/core/embed/rust/librust_qstr.h +++ b/core/embed/rust/librust_qstr.h @@ -37,6 +37,7 @@ static void _librust_qstrs(void) { MP_QSTR___dict__; MP_QSTR___name__; MP_QSTR_account; + MP_QSTR_account_info; MP_QSTR_account_items; MP_QSTR_account_items_title; MP_QSTR_account_label; @@ -72,6 +73,7 @@ static void _librust_qstrs(void) { MP_QSTR_altcoin_tx_summary; MP_QSTR_amount; MP_QSTR_amount_change; + MP_QSTR_amount_label; MP_QSTR_amount_new; MP_QSTR_amount_title; MP_QSTR_amount_value; @@ -241,7 +243,10 @@ static void _librust_qstrs(void) { MP_QSTR_experimental_mode__only_for_dev; MP_QSTR_experimental_mode__title; MP_QSTR_extra; + MP_QSTR_extra_info; + MP_QSTR_fee; MP_QSTR_fee_amount; + MP_QSTR_fee_info; MP_QSTR_fee_items; MP_QSTR_fee_label; MP_QSTR_fee_rate_amount; diff --git a/core/embed/rust/src/ui/model_tt/layout.rs b/core/embed/rust/src/ui/model_tt/layout.rs index 6e0330f485..c781a2360a 100644 --- a/core/embed/rust/src/ui/model_tt/layout.rs +++ b/core/embed/rust/src/ui/model_tt/layout.rs @@ -319,18 +319,39 @@ impl ComponentMsgObj for super::component::bl_confirm::Confirm<'_> { extern "C" fn new_confirm_total(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 items: Obj = kwargs.get(Qstr::MP_QSTR_items)?; - let info_button: bool = kwargs.get_or(Qstr::MP_QSTR_info_button, false)?; + let amount: TString = kwargs.get(Qstr::MP_QSTR_amount)?.try_into()?; + let amount_label: TString = kwargs.get(Qstr::MP_QSTR_amount_label)?.try_into()?; + let fee: TString = kwargs.get(Qstr::MP_QSTR_fee)?.try_into()?; + let fee_label: TString = kwargs.get(Qstr::MP_QSTR_fee_label)?.try_into()?; + let title: Option = kwargs + .get(Qstr::MP_QSTR_title) + .unwrap_or_else(|_| Obj::const_none()) + .try_into_option()?; + let fee_info: Option = kwargs + .get(Qstr::MP_QSTR_fee_info) + .unwrap_or_else(|_| Obj::const_none()) + .try_into_option()?; + let account_info: Option = kwargs + .get(Qstr::MP_QSTR_account_info) + .unwrap_or_else(|_| Obj::const_none()) + .try_into_option()?; + let extra_info: Option = kwargs + .get(Qstr::MP_QSTR_extra_info) + .unwrap_or_else(|_| Obj::const_none()) + .try_into_option()?; let cancel_arrow: bool = kwargs.get_or(Qstr::MP_QSTR_cancel_arrow, false)?; - let mut paragraphs = ParagraphVecShort::new(); + // TODO: default title here? + let title = title.unwrap_or(TR::words__title_summary.into()); + let info_button: bool = + fee_info.is_some() || account_info.is_some() || extra_info.is_some(); + let mut paragraphs = ParagraphVecShort::from_iter([ + Paragraph::new(&theme::TEXT_NORMAL, amount_label).no_break(), + Paragraph::new(&theme::TEXT_MONO, amount), + Paragraph::new(&theme::TEXT_NORMAL, fee_label).no_break(), + Paragraph::new(&theme::TEXT_MONO, fee), + ]); - for pair in IterBuf::new().try_iterate(items)? { - let [label, value]: [TString; 2] = util::iter_into_array(pair)?; - paragraphs.add(Paragraph::new(&theme::TEXT_NORMAL, label).no_break()); - paragraphs.add(Paragraph::new(&theme::TEXT_MONO, value)); - } let mut page = ButtonPage::new(paragraphs.into_paragraphs(), theme::BG).with_hold()?; if cancel_arrow { page = page.with_cancel_arrow() @@ -356,10 +377,15 @@ pub static mp_module_trezorui2: Module = obj_module! { /// def confirm_total( /// *, - /// title: str, - /// items: Iterable[tuple[str, str]], - /// info_button: bool = False, - /// cancel_arrow: bool = False, + /// amount: str, + /// amount_label: str, + /// fee: str, + /// fee_label: str, + /// title: str | None = None, + /// fee_info: Iterable[tuple[str, str]] | None = None, + /// account_info: Iterable[tuple[str, str]] | None = None, + /// extra_info: Iterable[tuple[str, str]] | None = None, + /// cancel_arrow: bool = False, # unused on mercury /// ) -> LayoutObj[UiResult]: /// """Transaction summary. Always hold to confirm.""" Qstr::MP_QSTR_confirm_total => obj_fn_kw!(0, new_confirm_total).as_obj(), diff --git a/core/mocks/generated/trezorui2.pyi b/core/mocks/generated/trezorui2.pyi index 5827d06a6d..99cc975f6e 100644 --- a/core/mocks/generated/trezorui2.pyi +++ b/core/mocks/generated/trezorui2.pyi @@ -171,9 +171,14 @@ from trezorui_api import * # rust/src/ui/model_tt/layout.rs def confirm_total( *, - title: str, - items: Iterable[tuple[str, str]], - info_button: bool = False, - cancel_arrow: bool = False, + amount: str, + amount_label: str, + fee: str, + fee_label: str, + title: str | None = None, + fee_info: Iterable[tuple[str, str]] | None = None, + account_info: Iterable[tuple[str, str]] | None = None, + extra_info: Iterable[tuple[str, str]] | None = None, + cancel_arrow: bool = False, # unused on mercury ) -> LayoutObj[UiResult]: """Transaction summary. Always hold to confirm.""" diff --git a/core/src/trezor/ui/layouts/tt/__init__.py b/core/src/trezor/ui/layouts/tt/__init__.py index 3374142f08..305d3c7a38 100644 --- a/core/src/trezor/ui/layouts/tt/__init__.py +++ b/core/src/trezor/ui/layouts/tt/__init__.py @@ -719,43 +719,67 @@ def confirm_total( total_label = total_label or f"{TR.send__total_amount}:" # def_arg fee_label = fee_label or TR.send__including_fee # def_arg - items = [ - (total_label, total_amount), - (fee_label, fee_amount), - ] - info_items = [] + account_info_items = [] + fee_info_items = [] + extra_info_items = [] if source_account: - info_items.append((TR.confirm_total__sending_from_account, source_account)) + account_info_items.append( + (TR.confirm_total__sending_from_account, source_account) + ) if fee_rate_amount: - info_items.append((f"{TR.confirm_total__fee_rate}:", fee_rate_amount)) + fee_info_items.append((f"{TR.confirm_total__fee_rate}:", fee_rate_amount)) return _confirm_summary( - items, - TR.words__title_summary, - info_items=info_items, + total_amount, + total_label, + fee_amount, + fee_label, + title=title, + fee_info_items=fee_info_items, + account_info_items=account_info_items, + extra_info_items=extra_info_items, br_name=br_name, br_code=br_code, ) def _confirm_summary( - items: Iterable[tuple[str, str]], + amount: str, + amount_label: str, + fee: str, + fee_label: str, title: str | None = None, - info_items: Iterable[tuple[str, str]] | None = None, - info_title: str | None = None, + fee_info_items: Iterable[tuple[str, str]] | None = None, + account_info_items: Iterable[tuple[str, str]] | None = None, + extra_info_items: Iterable[tuple[str, str]] | None = None, + extra_info_title: str | None = None, br_name: str = "confirm_total", br_code: ButtonRequestType = ButtonRequestType.SignTx, ) -> Awaitable[None]: title = title or TR.words__title_summary # def_arg total_layout = trezorui2.confirm_total( + amount=amount, + amount_label=amount_label, + fee=fee, + fee_label=fee_label, title=title, - items=items, - info_button=bool(info_items), + fee_info=fee_info_items, + account_info=account_info_items, + extra_info=extra_info_items, + cancel_arrow=True, ) - info_items = info_items or [] + + # TODO: use `_info` params directly in this^ layout instead of using `with_info` + info_items = [] + if fee_info_items: + info_items.extend(fee_info_items) + if account_info_items: + info_items.extend(account_info_items) + if extra_info_items: + info_items.extend(extra_info_items) info_layout = trezorui_api.show_info_with_cancel( - title=info_title if info_title else TR.words__title_information, + title=extra_info_title if extra_info_title else TR.words__title_information, items=info_items, ) return with_info(total_layout, info_layout, br_name, br_code) @@ -782,13 +806,14 @@ if not utils.BITCOIN_ONLY: br_code: ButtonRequestType = ButtonRequestType.SignTx, chunkify: bool = False, ) -> None: + # NOTE: fee_info used so that info button is shown total_layout = trezorui2.confirm_total( + amount=total_amount, + amount_label=f"{TR.words__amount}:", + fee=maximum_fee, + fee_label=f"{TR.send__maximum_fee}:", title=TR.words__title_summary, - items=[ - (f"{TR.words__amount}:", total_amount), - (f"{TR.send__maximum_fee}:", maximum_fee), - ], - info_button=True, + fee_info=fee_info_items, cancel_arrow=True, ) info_layout = trezorui_api.show_info_with_cancel( @@ -850,17 +875,23 @@ if not utils.BITCOIN_ONLY: # confirmation if verb == TR.ethereum__staking_claim: - items = ((f"{TR.send__maximum_fee}:", maximum_fee),) + amount = "" + amount_label = "" + fee_label = f"{TR.send__maximum_fee}:" + fee = maximum_fee else: - items = ( - (f"{TR.words__amount}:", total_amount), - (f"{TR.send__maximum_fee}:", maximum_fee), - ) + amount_label = f"{TR.words__amount}:" + amount = total_amount + fee_label = f"{TR.send__maximum_fee}:" + fee = maximum_fee await _confirm_summary( - items, # items + amount, + amount_label, + fee, + fee_label, title=title, - info_title=TR.confirm_total__title_fee, - info_items=[(f"{k}:", v) for (k, v) in info_items], + extra_info_items=[(f"{k}:", v) for (k, v) in info_items], + extra_info_title=TR.confirm_total__title_fee, br_name=br_name, br_code=br_code, ) @@ -879,8 +910,11 @@ if not utils.BITCOIN_ONLY: ) # def_arg fee_title = fee_title or TR.words__fee # def_arg return _confirm_summary( - ((amount_title, amount), (fee_title, fee)), - info_items=items, + amount, + amount_title, + fee, + fee_title, + extra_info_items=items, br_name=br_name, br_code=br_code, ) @@ -893,8 +927,11 @@ if not utils.BITCOIN_ONLY: amount_title = f"{TR.send__total_amount}:" fee_title = TR.send__including_fee return _confirm_summary( - ((amount_title, amount), (fee_title, fee)), - info_items=items, + amount, + amount_title, + fee, + fee_title, + extra_info_items=items, br_name="confirm_cardano_tx", br_code=ButtonRequestType.SignTx, ) @@ -902,12 +939,13 @@ if not utils.BITCOIN_ONLY: def confirm_joint_total(spending_amount: str, total_amount: str) -> Awaitable[None]: return raise_if_not_confirmed( + # FIXME: arguments for amount/fee are misused here trezorui2.confirm_total( + spending_amount, + TR.send__you_are_contributing, + total_amount, + TR.send__to_the_total_amount, title=TR.send__title_joint_transaction, - items=[ - (TR.send__you_are_contributing, spending_amount), - (TR.send__to_the_total_amount, total_amount), - ], ), "confirm_joint_total", ButtonRequestType.SignTx,