From da4442bbd72569a4614fa26909d749c78141f6e3 Mon Sep 17 00:00:00 2001 From: matejcik Date: Tue, 22 Jun 2021 15:14:15 +0200 Subject: [PATCH] feat(core): auto-hexlify in confirm_properties --- core/src/apps/cardano/layout.py | 10 ++++------ core/src/apps/eos/actions/layout.py | 4 +--- core/src/trezor/ui/layouts/tt.py | 29 ++++++++++++++++++++++++----- 3 files changed, 29 insertions(+), 14 deletions(-) diff --git a/core/src/apps/cardano/layout.py b/core/src/apps/cardano/layout.py index 1ac1add299..9ed33777e7 100644 --- a/core/src/apps/cardano/layout.py +++ b/core/src/apps/cardano/layout.py @@ -1,5 +1,3 @@ -from ubinascii import hexlify - from trezor import ui from trezor.enums import ButtonRequestType, CardanoAddressType, CardanoCertificateType from trezor.strings import format_amount @@ -197,7 +195,7 @@ async def show_warning_tx_staking_key_hash( ) -> None: props = [ ("Change address staking rights do not match the current account.\n\n", None), - ("Staking key hash:", hexlify(staking_key_hash).decode()), + ("Staking key hash:", staking_key_hash), ("Change amount:", format_coin_amount(amount)), ] @@ -365,7 +363,7 @@ async def confirm_stake_pool_metadata( title="Confirm transaction", props=[ ("Pool metadata url:", metadata.url), - ("Pool metadata hash:", hexlify(metadata.hash).decode()), + ("Pool metadata hash:", metadata.hash), ], br_code=ButtonRequestType.Other, ) @@ -458,7 +456,7 @@ async def show_auxiliary_data_hash( ctx, "confirm_auxiliary_data", title="Confirm transaction", - props=[("Auxiliary data hash:", hexlify(auxiliary_data_hash).decode())], + props=[("Auxiliary data hash:", auxiliary_data_hash)], br_code=ButtonRequestType.Other, ) @@ -486,7 +484,7 @@ async def show_warning_address_foreign_staking_key( ) else: assert staking_key_hash is not None # _validate_base_address_staking_info - props.append(("Staking key:", hexlify(staking_key_hash).decode())) + props.append(("Staking key:", staking_key_hash)) props.append(("Continue?", None)) await confirm_properties( diff --git a/core/src/apps/eos/actions/layout.py b/core/src/apps/eos/actions/layout.py index edce6fa89d..4cbfeb2483 100644 --- a/core/src/apps/eos/actions/layout.py +++ b/core/src/apps/eos/actions/layout.py @@ -1,5 +1,3 @@ -from ubinascii import hexlify - from trezor import ui from trezor.enums import ButtonRequestType from trezor.ui.layouts import confirm_properties @@ -290,7 +288,7 @@ async def confirm_action_unknown( props=[ ("Contract:", helpers.eos_name_to_string(action.account)), ("Action Name:", helpers.eos_name_to_string(action.name)), - ("Checksum:", hexlify(checksum).decode("ascii")), + ("Checksum:", checksum), ], icon=ui.ICON_WIPE, icon_color=ui.RED, diff --git a/core/src/trezor/ui/layouts/tt.py b/core/src/trezor/ui/layouts/tt.py index 52779b4e8a..b26a16d3ca 100644 --- a/core/src/trezor/ui/layouts/tt.py +++ b/core/src/trezor/ui/layouts/tt.py @@ -1,5 +1,7 @@ from micropython import const +from ubinascii import hexlify + from trezor import ui, wire from trezor.enums import ButtonRequestType from trezor.ui.container import Container @@ -11,7 +13,13 @@ from ..components.common import break_path_to_lines from ..components.common.confirm import is_confirmed, raise_if_cancelled from ..components.tt.button import ButtonCancel, ButtonDefault from ..components.tt.confirm import Confirm, HoldToConfirm -from ..components.tt.scroll import Paginated, paginate_paragraphs, paginate_text, PAGINATED_LINE_WIDTH, PAGEBREAK +from ..components.tt.scroll import ( + Paginated, + paginate_paragraphs, + paginate_text, + PAGINATED_LINE_WIDTH, + PAGEBREAK, +) from ..components.tt.text import Span, Text from ..constants.tt import ( MONO_ADDR_PER_LINE, @@ -36,7 +44,7 @@ if False: ) ExceptionType = Union[BaseException, Type[BaseException]] - PropertyType = Tuple[Optional[str], Optional[str]] + PropertyType = Tuple[Optional[str], Union[str, bytes, None]] __all__ = ( @@ -597,8 +605,14 @@ async def confirm_properties( for key, val in props: span.reset(key or "", 0, ui.NORMAL, line_width=PAGINATED_LINE_WIDTH) key_lines = span.count_lines() - span.reset(val or "", 0, ui.BOLD, line_width=PAGINATED_LINE_WIDTH) - val_lines = span.count_lines() + + if isinstance(val, str): + span.reset(val, 0, ui.BOLD, line_width=PAGINATED_LINE_WIDTH) + val_lines = span.count_lines() + elif isinstance(val, bytes): + val_lines = (len(val) * 2 + MONO_HEX_PER_LINE - 1) // MONO_HEX_PER_LINE + else: + val_lines = 0 remaining_lines = TEXT_MAX_LINES - used_lines used_lines = (used_lines + key_lines + val_lines) % TEXT_MAX_LINES @@ -627,7 +641,12 @@ async def confirm_properties( if key: para.append((ui.NORMAL, key)) - if val: + if isinstance(val, bytes): + para.extend( + (ui.MONO, line) + for line in chunks(hexlify(val).decode(), MONO_HEX_PER_LINE - 2) + ) + elif isinstance(val, str): para.append((ui.BOLD, val)) content = paginate_paragraphs( para, title, icon, icon_color, confirm=HoldToConfirm if hold else Confirm