From ebf6ef16667f1fd78f20663dc0b470634ccb3710 Mon Sep 17 00:00:00 2001 From: Martin Milata Date: Wed, 3 Mar 2021 16:13:17 +0100 Subject: [PATCH] refactor(core): convert apps.tezos to layouts --- core/src/apps/tezos/layout.py | 187 +++++++++++++++++++------------ core/src/trezor/ui/layouts/tt.py | 48 ++++++-- 2 files changed, 154 insertions(+), 81 deletions(-) diff --git a/core/src/apps/tezos/layout.py b/core/src/apps/tezos/layout.py index 181c6c68d..c25409c45 100644 --- a/core/src/apps/tezos/layout.py +++ b/core/src/apps/tezos/layout.py @@ -1,76 +1,106 @@ from trezor import ui from trezor.enums import ButtonRequestType from trezor.strings import format_amount -from trezor.ui.components.tt.scroll import Paginated -from trezor.ui.components.tt.text import Text -from trezor.utils import chunks - -from apps.common.confirm import require_confirm, require_hold_to_confirm +from trezor.ui.layouts import ( + confirm_hex, + confirm_metadata, + confirm_output, + confirm_proposals_tezos, + confirm_total, +) from .helpers import TEZOS_AMOUNT_DECIMALS async def require_confirm_tx(ctx, to, value): - text = Text("Confirm sending", ui.ICON_SEND, ui.GREEN) - text.bold(format_tezos_amount(value)) - text.normal("to") - text.mono(*split_address(to)) - await require_confirm(ctx, text, ButtonRequestType.SignTx) + await confirm_output( + ctx, + to, + format_tezos_amount(value), + font_amount=ui.BOLD, + to_str="\nto\n", + width=18, + br_code=ButtonRequestType.SignTx, + ) async def require_confirm_fee(ctx, value, fee): - text = Text("Confirm transaction", ui.ICON_SEND, ui.GREEN) - text.normal("Amount:") - text.bold(format_tezos_amount(value)) - text.normal("Fee:") - text.bold(format_tezos_amount(fee)) - await require_hold_to_confirm(ctx, text, ButtonRequestType.SignTx) + await confirm_total( + ctx, + total_amount=format_tezos_amount(value), + total_label="Amount:\n", + fee_amount=format_tezos_amount(fee), + fee_label="\nFee:\n", + ) async def require_confirm_origination(ctx, address): - text = Text("Confirm origination", ui.ICON_SEND, ui.ORANGE) - text.normal("Address:") - text.mono(*split_address(address)) - await require_confirm(ctx, text, ButtonRequestType.SignTx) + await confirm_hex( + ctx, + "confirm_origination", + title="Confirm origination", + description="Address:", + data=address, + width=18, + truncate=True, + icon_color=ui.ORANGE, + br_code=ButtonRequestType.SignTx, + ) async def require_confirm_origination_fee(ctx, balance, fee): - text = Text("Confirm origination", ui.ICON_SEND, ui.ORANGE) - text.normal("Balance:") - text.bold(format_tezos_amount(balance)) - text.normal("Fee:") - text.bold(format_tezos_amount(fee)) - await require_hold_to_confirm(ctx, text, ButtonRequestType.SignTx) + await confirm_total( + ctx, + title="Confirm origination", + total_amount=format_tezos_amount(balance), + total_label="Balance:\n", + fee_amount=format_tezos_amount(fee), + fee_label="\nFee:\n", + icon_color=ui.ORANGE, + br_type="confirm_origination_final", + ) async def require_confirm_delegation_baker(ctx, baker): - text = Text("Confirm delegation", ui.ICON_SEND, ui.BLUE) - text.normal("Baker address:") - text.mono(*split_address(baker)) - await require_confirm(ctx, text, ButtonRequestType.SignTx) + await confirm_hex( + ctx, + "confirm_delegation", + title="Confirm delegation", + description="Baker address:", + data=baker, + width=18, + truncate=True, + icon_color=ui.BLUE, + br_code=ButtonRequestType.SignTx, + ) async def require_confirm_set_delegate(ctx, fee): - text = Text("Confirm delegation", ui.ICON_SEND, ui.BLUE) - text.normal("Fee:") - text.bold(format_tezos_amount(fee)) - await require_hold_to_confirm(ctx, text, ButtonRequestType.SignTx) + await confirm_metadata( + ctx, + "confirm_delegation_final", + title="Confirm delegation", + content="Fee:\n{}", + param=format_tezos_amount(fee), + hold=True, + hide_continue=True, + icon_color=ui.BLUE, + br_code=ButtonRequestType.SignTx, + ) async def require_confirm_register_delegate(ctx, address, fee): - text = Text("Register delegate", ui.ICON_SEND, ui.BLUE) - text.bold("Fee: " + format_tezos_amount(fee)) - text.normal("Address:") - text.mono(*split_address(address)) - await require_hold_to_confirm(ctx, text, ButtonRequestType.SignTx) - - -def split_address(address): - return chunks(address, 18) - - -def split_proposal(proposal): - return chunks(proposal, 17) + await confirm_hex( + ctx, + "confirm_register_delegate", + title="Register delegate", + subtitle="Fee: " + format_tezos_amount(fee), + description="Address:", + data=address, + width=18, + icon_color=ui.BLUE, + br_code=ButtonRequestType.SignTx, + ) def format_tezos_amount(value): @@ -79,39 +109,48 @@ def format_tezos_amount(value): async def require_confirm_ballot(ctx, proposal, ballot): - text = Text("Submit ballot", ui.ICON_SEND, icon_color=ui.PURPLE) - text.bold("Ballot: {}".format(ballot)) - text.bold("Proposal:") - text.mono(*split_proposal(proposal)) - await require_confirm(ctx, text, ButtonRequestType.SignTx) + await confirm_hex( + ctx, + "confirm_ballot", + title="Submit ballot", + subtitle="Ballot: {}\nProposal:".format(ballot), + data=proposal, + width=17, + truncate=True, + icon_color=ui.PURPLE, + br_code=ButtonRequestType.SignTx, + ) async def require_confirm_proposals(ctx, proposals): - if len(proposals) > 1: - title = "Submit proposals" - else: - title = "Submit proposal" - - pages = [] - for page, proposal in enumerate(proposals): - text = Text(title, ui.ICON_SEND, icon_color=ui.PURPLE) - text.bold("Proposal {}: ".format(page + 1)) - text.mono(*split_proposal(proposal)) - pages.append(text) - paginated = Paginated(pages) - - await require_confirm(ctx, paginated, ButtonRequestType.SignTx) + await confirm_proposals_tezos(ctx, proposals) async def require_confirm_delegation_manager_withdraw(ctx, address): - text = Text("Remove delegation", ui.ICON_RECEIVE, icon_color=ui.RED) - text.bold("Delegator:") - text.mono(*split_address(address)) - await require_confirm(ctx, text, ButtonRequestType.SignTx) + await confirm_hex( + ctx, + "confirm_undelegation", + title="Remove delegation", + subtitle="Delegator:", + data=address, + width=18, + truncate=True, + icon=ui.ICON_RECEIVE, + icon_color=ui.RED, + br_code=ButtonRequestType.SignTx, + ) async def require_confirm_manager_remove_delegate(ctx, fee): - text = Text("Remove delegation", ui.ICON_RECEIVE, ui.RED) - text.normal("Fee:") - text.bold(format_tezos_amount(fee)) - await require_hold_to_confirm(ctx, text, ButtonRequestType.SignTx) + await confirm_metadata( + ctx, + "confirm_undelegation_final", + title="Remove delegation", + content="Fee:\n{}", + param=format_tezos_amount(fee), + hold=True, + hide_continue=True, + icon=ui.ICON_RECEIVE, + icon_color=ui.RED, + br_code=ButtonRequestType.SignTx, + ) diff --git a/core/src/trezor/ui/layouts/tt.py b/core/src/trezor/ui/layouts/tt.py index 7d63c3298..fa82f7c1d 100644 --- a/core/src/trezor/ui/layouts/tt.py +++ b/core/src/trezor/ui/layouts/tt.py @@ -62,6 +62,7 @@ __all__ = ( "confirm_modify_fee", "confirm_coinjoin", "confirm_timebounds_stellar", + "confirm_proposals_tezos", ) @@ -451,6 +452,7 @@ async def confirm_output( amount: str, font_amount: int = ui.NORMAL, # TODO cleanup @ redesign color_to: int = ui.FG, # TODO cleanup @ redesign + to_str: str = " to\n", # TODO cleanup @ redesign width: int = MONO_ADDR_PER_LINE, width_paginated: int = MONO_ADDR_PER_LINE - 1, br_code: ButtonRequestType = ButtonRequestType.ConfirmOutput, @@ -462,7 +464,7 @@ async def confirm_output( content: ui.Layout = paginate_paragraphs(para, title, ui.ICON_SEND, ui.GREEN) else: text = Text(title, ui.ICON_SEND, ui.GREEN, new_lines=False) - text.content = [font_amount, amount, ui.NORMAL, color_to, " to\n", ui.FG] + text.content = [font_amount, amount, ui.NORMAL, color_to, to_str, ui.FG] text.mono(*chunks_intersperse(address, width)) content = Confirm(text) @@ -547,15 +549,22 @@ async def confirm_hex( async def confirm_total( - ctx: wire.GenericContext, total_amount: str, fee_amount: str + ctx: wire.GenericContext, + total_amount: str, + fee_amount: str, + title: str = "Confirm transaction", + total_label: str = "Total amount:\n", + fee_label: str = "\nincluding fee:\n", + icon_color: int = ui.GREEN, + br_type: str = "confirm_total", ) -> None: - text = Text("Confirm transaction", ui.ICON_SEND, ui.GREEN, new_lines=False) - text.normal("Total amount:\n") + text = Text(title, ui.ICON_SEND, icon_color, new_lines=False) + text.normal(total_label) text.bold(total_amount) - text.normal("\nincluding fee:\n") + text.normal(fee_label) text.bold(fee_amount) await raise_if_cancelled( - interact(ctx, HoldToConfirm(text), "confirm_total", ButtonRequestType.SignTx) + interact(ctx, HoldToConfirm(text), br_type, ButtonRequestType.SignTx) ) @@ -616,8 +625,9 @@ async def confirm_metadata( hide_continue: bool = False, hold: bool = False, icon: str = ui.ICON_SEND, # TODO cleanup @ redesign + icon_color: int = ui.GREEN, # TODO cleanup @ redesign ) -> None: - text = Text(title, icon, ui.GREEN, new_lines=False) + text = Text(title, icon, icon_color, new_lines=False) text.format_parametrized(content, param if param is not None else "") if not hide_continue: @@ -775,3 +785,27 @@ async def confirm_timebounds_stellar( ctx, Confirm(text), "confirm_timebounds", ButtonRequestType.ConfirmOutput ) ) + + +# TODO cleanup @ redesign +async def confirm_proposals_tezos( + ctx: wire.GenericContext, proposals: Sequence[str] +) -> None: + if len(proposals) > 1: + title = "Submit proposals" + else: + title = "Submit proposal" + + pages: list[ui.Component] = [] + for page, proposal in enumerate(proposals): + text = Text(title, ui.ICON_SEND, icon_color=ui.PURPLE, new_lines=False) + text.bold("Proposal {}:\n".format(page + 1)) + text.mono(*chunks_intersperse(proposal, 17)) + pages.append(text) + + pages[-1] = Confirm(pages[-1]) + paginated = Paginated(pages) + + await raise_if_cancelled( + interact(ctx, paginated, "confirm_proposals", ButtonRequestType.SignTx) + )