From ad6976e01fbb3167e59a6f7b40da0ae0df448881 Mon Sep 17 00:00:00 2001 From: matejcik Date: Fri, 16 Jul 2021 15:07:50 +0200 Subject: [PATCH] refactor(core): use confirm_blob instead of confirm_hex in apps --- core/src/apps/bitcoin/get_ownership_proof.py | 10 +-- core/src/apps/bitcoin/sign_tx/layout.py | 5 +- core/src/apps/cardano/layout.py | 4 + core/src/apps/ethereum/layout.py | 27 ++++--- .../apps/management/reset_device/layout.py | 10 +-- core/src/apps/misc/get_ecdh_session_key.py | 7 +- core/src/apps/monero/layout.py | 8 +- core/src/apps/nem/multisig/layout.py | 9 +-- core/src/apps/nem/transfer/layout.py | 16 ++-- core/src/apps/tezos/layout.py | 77 +++++++++++-------- 10 files changed, 87 insertions(+), 86 deletions(-) diff --git a/core/src/apps/bitcoin/get_ownership_proof.py b/core/src/apps/bitcoin/get_ownership_proof.py index a10e4d5bdb..c25387b9ea 100644 --- a/core/src/apps/bitcoin/get_ownership_proof.py +++ b/core/src/apps/bitcoin/get_ownership_proof.py @@ -1,8 +1,6 @@ -from ubinascii import hexlify - from trezor import ui, wire from trezor.messages import GetOwnershipProof, OwnershipProof -from trezor.ui.layouts import confirm_action, confirm_hex +from trezor.ui.layouts import confirm_action, confirm_blob from apps.common.paths import validate_path @@ -70,16 +68,14 @@ async def get_ownership_proof( description="Do you want to create a proof of ownership?", ) else: - await confirm_hex( + await confirm_blob( ctx, "confirm_ownership_proof", title="Proof of ownership", description="Do you want to create a proof of ownership for:", - data=hexlify(msg.commitment_data).decode(), + data=msg.commitment_data, icon=ui.ICON_CONFIG, icon_color=ui.ORANGE_ICON, - truncate=True, # commitment data, probably should show all - truncate_middle=True, ) ownership_proof, signature = generate_proof( diff --git a/core/src/apps/bitcoin/sign_tx/layout.py b/core/src/apps/bitcoin/sign_tx/layout.py index af5fc78e2f..77206ccf27 100644 --- a/core/src/apps/bitcoin/sign_tx/layout.py +++ b/core/src/apps/bitcoin/sign_tx/layout.py @@ -50,13 +50,12 @@ async def confirm_output( ) else: # generic OP_RETURN - layout = layouts.confirm_hex( + layout = layouts.confirm_blob( ctx, "op_return", title="OP_RETURN", - data=hexlify(data).decode(), + data=data, br_code=ButtonRequestType.ConfirmOutput, - truncate=True, # 80 bytes - not truncated 2 screens max ) else: assert output.address is not None diff --git a/core/src/apps/cardano/layout.py b/core/src/apps/cardano/layout.py index 69f37f5a7f..e6b76b054a 100644 --- a/core/src/apps/cardano/layout.py +++ b/core/src/apps/cardano/layout.py @@ -469,6 +469,10 @@ async def show_warning_address_foreign_staking_key( staking_account_path: list[int], staking_key_hash: bytes | None, ) -> None: + # TODO: confirm_properties not appropriate here + # instead, presumably, this should be a flow: + # 1. show_warning: Mismatch! continue? + # 2. confirm_blob(mismatched_value) props: list[PropertyType] = [ ( "Stake rights associated with this address do not match your account %s:" diff --git a/core/src/apps/ethereum/layout.py b/core/src/apps/ethereum/layout.py index e2effe0c5d..87f73f5a42 100644 --- a/core/src/apps/ethereum/layout.py +++ b/core/src/apps/ethereum/layout.py @@ -3,7 +3,12 @@ from ubinascii import hexlify from trezor import ui from trezor.enums import ButtonRequestType from trezor.strings import format_amount -from trezor.ui.layouts import confirm_hex, confirm_output, confirm_total_ethereum +from trezor.ui.layouts import ( + confirm_address, + confirm_blob, + confirm_output, + confirm_total_ethereum, +) from . import networks, tokens from .address import address_from_bytes @@ -37,30 +42,24 @@ async def require_confirm_fee( async def require_confirm_unknown_token(ctx, address_bytes): contract_address_hex = "0x" + hexlify(address_bytes).decode() - await confirm_hex( + await confirm_address( ctx, - "confirm_unknown", - title="Unknown token", - data=contract_address_hex, - truncate=True, + "Unknown token", + contract_address_hex, description="Contract:", - color_description=ui.GREY, + br_type="unknown_token", icon_color=ui.ORANGE, br_code=ButtonRequestType.SignTx, ) async def require_confirm_data(ctx, data, data_total): - data_str = hexlify(data[:36]).decode() - if data_total > 36: - data_str = data_str[:-2] + ".." - await confirm_hex( + await confirm_blob( ctx, "confirm_data", title="Confirm data", - data=data_str, - truncate=True, - subtitle="Size: %d bytes" % data_total, + description="Size: %d bytes" % data_total, + data=data, br_code=ButtonRequestType.SignTx, ) diff --git a/core/src/apps/management/reset_device/layout.py b/core/src/apps/management/reset_device/layout.py index e6547ff131..9ce3981cca 100644 --- a/core/src/apps/management/reset_device/layout.py +++ b/core/src/apps/management/reset_device/layout.py @@ -1,5 +1,3 @@ -import ubinascii - from trezor import ui, utils from trezor.crypto import random from trezor.enums import BackupType, ButtonRequestType @@ -9,7 +7,7 @@ from trezor.ui.components.tt.info import InfoConfirm from trezor.ui.components.tt.num_input import NumInput from trezor.ui.components.tt.scroll import Paginated from trezor.ui.components.tt.text import Text -from trezor.ui.layouts import confirm_action, confirm_hex, show_success, show_warning +from trezor.ui.layouts import confirm_action, confirm_blob, show_success, show_warning from apps.common.confirm import confirm, require_hold_to_confirm @@ -21,16 +19,14 @@ if __debug__: async def show_internal_entropy(ctx, entropy: bytes): - await confirm_hex( + await confirm_blob( ctx, "entropy", "Internal entropy", - data=ubinascii.hexlify(entropy).decode(), + data=entropy, icon=ui.ICON_RESET, icon_color=ui.ORANGE_ICON, - width=16, br_code=ButtonRequestType.ResetDevice, - truncate=True, # 32 bytes always fits ) diff --git a/core/src/apps/misc/get_ecdh_session_key.py b/core/src/apps/misc/get_ecdh_session_key.py index 8091712fb3..be6dbfb613 100644 --- a/core/src/apps/misc/get_ecdh_session_key.py +++ b/core/src/apps/misc/get_ecdh_session_key.py @@ -3,7 +3,7 @@ from ustruct import pack, unpack from trezor import ui, wire from trezor.crypto.hashlib import sha256 from trezor.messages import ECDHSessionKey -from trezor.ui.layouts import confirm_hex +from trezor.ui.layouts import confirm_address from apps.common import HARDENED from apps.common.keychain import get_keychain @@ -46,14 +46,13 @@ async def require_confirm_ecdh_session_key( ctx: wire.Context, identity: IdentityType ) -> None: proto = identity.proto.upper() if identity.proto else "identity" - await confirm_hex( + await confirm_address( ctx, - "ecdh_session_key", "Decrypt %s" % proto, serialize_identity_without_proto(identity), + description=None, icon=ui.ICON_DEFAULT, icon_color=ui.ORANGE_ICON, - truncate=True, # uri without protocol, probably should show entire ) diff --git a/core/src/apps/monero/layout.py b/core/src/apps/monero/layout.py index f3a80c5805..bf245e91c4 100644 --- a/core/src/apps/monero/layout.py +++ b/core/src/apps/monero/layout.py @@ -1,10 +1,8 @@ -from ubinascii import hexlify - from trezor import strings, ui from trezor.enums import ButtonRequestType from trezor.ui.layouts import ( confirm_action, - confirm_hex, + confirm_blob, confirm_metadata, confirm_output, ) @@ -137,11 +135,11 @@ async def _require_confirm_output( async def _require_confirm_payment_id(ctx, payment_id: bytes): - await confirm_hex( + await confirm_blob( ctx, "confirm_payment_id", title="Payment ID", - data=hexlify(payment_id).decode(), + data=payment_id, br_code=ButtonRequestType.SignTx, ) diff --git a/core/src/apps/nem/multisig/layout.py b/core/src/apps/nem/multisig/layout.py index bced5f3866..b45fa1a7a3 100644 --- a/core/src/apps/nem/multisig/layout.py +++ b/core/src/apps/nem/multisig/layout.py @@ -2,8 +2,7 @@ from trezor import ui from trezor.crypto import nem from trezor.enums import ButtonRequestType, NEMModificationType from trezor.messages import NEMAggregateModification, NEMSignTx, NEMTransactionCommon -from trezor.ui.constants import MONO_ADDR_PER_LINE -from trezor.ui.layouts import confirm_hex +from trezor.ui.layouts import confirm_address from ..layout import require_confirm_fee, require_confirm_final, require_confirm_text @@ -42,14 +41,12 @@ async def ask_aggregate_modification( async def _require_confirm_address(ctx, action: str, address: str): - await confirm_hex( + await confirm_address( ctx, br_type="confirm_multisig", title="Confirm address", description=action, - data=address, + address=address, br_code=ButtonRequestType.ConfirmOutput, icon=ui.ICON_SEND, - width=MONO_ADDR_PER_LINE, - truncate=True, ) diff --git a/core/src/apps/nem/transfer/layout.py b/core/src/apps/nem/transfer/layout.py index 5faa37fae5..ba276bdfc7 100644 --- a/core/src/apps/nem/transfer/layout.py +++ b/core/src/apps/nem/transfer/layout.py @@ -7,7 +7,12 @@ from trezor.messages import ( NEMTransfer, ) from trezor.strings import format_amount -from trezor.ui.layouts import confirm_action, confirm_output, confirm_properties +from trezor.ui.layouts import ( + confirm_action, + confirm_output, + confirm_properties, + confirm_text, +) from ..helpers import ( NEM_LEVY_PERCENTILE_DIVISOR_ABSOLUTE, @@ -145,13 +150,12 @@ async def _require_confirm_payload(ctx, payload: bytearray, encrypt=False): payload = bytes(payload).decode() subtitle = "Encrypted:" if encrypt else "Unencrypted:" - await confirm_properties( + await confirm_text( ctx, "confirm_payload", title="Confirm payload", - props=[ - (None, subtitle), - (payload, None), - ], + description=subtitle, + data=payload, icon_color=ui.GREEN if encrypt else ui.RED, + br_code=ButtonRequestType.ConfirmOutput, ) diff --git a/core/src/apps/tezos/layout.py b/core/src/apps/tezos/layout.py index c25409c45f..a1ca1f4b2a 100644 --- a/core/src/apps/tezos/layout.py +++ b/core/src/apps/tezos/layout.py @@ -2,10 +2,10 @@ from trezor import ui from trezor.enums import ButtonRequestType from trezor.strings import format_amount from trezor.ui.layouts import ( - confirm_hex, + confirm_address, confirm_metadata, confirm_output, - confirm_proposals_tezos, + confirm_properties, confirm_total, ) @@ -35,41 +35,38 @@ async def require_confirm_fee(ctx, value, fee): async def require_confirm_origination(ctx, address): - await confirm_hex( + await confirm_address( ctx, - "confirm_origination", title="Confirm origination", + address=address, description="Address:", - data=address, - width=18, - truncate=True, + br_type="confirm_origination", icon_color=ui.ORANGE, br_code=ButtonRequestType.SignTx, ) async def require_confirm_origination_fee(ctx, balance, fee): - await confirm_total( + await confirm_properties( ctx, title="Confirm origination", - total_amount=format_tezos_amount(balance), - total_label="Balance:\n", - fee_amount=format_tezos_amount(fee), - fee_label="\nFee:\n", + props=( + ("Balance:", format_tezos_amount(balance)), + ("Fee:", format_tezos_amount(fee)), + ), icon_color=ui.ORANGE, br_type="confirm_origination_final", + hold=True, ) async def require_confirm_delegation_baker(ctx, baker): - await confirm_hex( + await confirm_address( ctx, - "confirm_delegation", title="Confirm delegation", + address=baker, description="Baker address:", - data=baker, - width=18, - truncate=True, + br_type="confirm_delegation", icon_color=ui.BLUE, br_code=ButtonRequestType.SignTx, ) @@ -90,14 +87,14 @@ async def require_confirm_set_delegate(ctx, fee): async def require_confirm_register_delegate(ctx, address, fee): - await confirm_hex( + await confirm_properties( ctx, "confirm_register_delegate", title="Register delegate", - subtitle="Fee: " + format_tezos_amount(fee), - description="Address:", - data=address, - width=18, + props=( + ("Fee:", format_tezos_amount(fee)), + ("Address:", address), + ), icon_color=ui.BLUE, br_code=ButtonRequestType.SignTx, ) @@ -109,32 +106,44 @@ def format_tezos_amount(value): async def require_confirm_ballot(ctx, proposal, ballot): - await confirm_hex( + await confirm_properties( ctx, "confirm_ballot", title="Submit ballot", - subtitle="Ballot: {}\nProposal:".format(ballot), - data=proposal, - width=17, - truncate=True, + props=( + ("Ballot:", ballot), + ("Proposal:", proposal), + ), icon_color=ui.PURPLE, br_code=ButtonRequestType.SignTx, ) async def require_confirm_proposals(ctx, proposals): - await confirm_proposals_tezos(ctx, proposals) + if len(proposals) > 1: + title = "Submit proposals" + else: + title = "Submit proposal" + + await confirm_properties( + ctx, + "confirm_proposals", + title=title, + props=[ + ("Proposal " + str(i), proposal) for i, proposal in enumerate(proposals, 1) + ], + icon_color=ui.PURPLE, + br_code=ButtonRequestType.SignTx, + ) async def require_confirm_delegation_manager_withdraw(ctx, address): - await confirm_hex( + await confirm_address( ctx, - "confirm_undelegation", title="Remove delegation", - subtitle="Delegator:", - data=address, - width=18, - truncate=True, + address=address, + description="Delegator:", + br_type="confirm_undelegation", icon=ui.ICON_RECEIVE, icon_color=ui.RED, br_code=ButtonRequestType.SignTx,