From addbdd89371efdf10b5e5b13f558060557e50489 Mon Sep 17 00:00:00 2001 From: Pavol Rusnak Date: Wed, 10 Oct 2018 17:58:26 +0200 Subject: [PATCH] src/apps/common: refactor address_n_to_str into apps.common.layout --- src/apps/cardano/address.py | 22 ---------------------- src/apps/cardano/sign_tx.py | 5 +++-- src/apps/common/layout.py | 22 +++++++++++++++++----- src/apps/ethereum/get_address.py | 2 +- src/apps/lisk/get_address.py | 2 +- src/apps/nem/get_address.py | 4 +++- src/apps/ripple/get_address.py | 2 +- src/apps/stellar/get_address.py | 2 +- src/apps/tezos/get_address.py | 2 +- src/apps/wallet/get_address.py | 2 +- 10 files changed, 29 insertions(+), 36 deletions(-) diff --git a/src/apps/cardano/address.py b/src/apps/cardano/address.py index c5ca7d1ec..95daddac8 100644 --- a/src/apps/cardano/address.py +++ b/src/apps/cardano/address.py @@ -1,5 +1,3 @@ -from micropython import const - from trezor import wire from trezor.crypto import base58, crc, hashlib @@ -56,23 +54,3 @@ def derive_address_and_node(root_node, path: list): ) ) return (address, derived_node) - - -def _break_address_n_to_lines(address_n: list) -> list: - def path_item(i: int): - if i & HARDENED: - return str(i ^ HARDENED) + "'" - else: - return str(i) - - lines = [] - path_str = "m/" + "/".join([path_item(i) for i in address_n]) - - per_line = const(17) - while len(path_str) > per_line: - i = path_str[:per_line].rfind("/") - lines.append(path_str[:i]) - path_str = path_str[i:] - lines.append(path_str) - - return lines diff --git a/src/apps/cardano/sign_tx.py b/src/apps/cardano/sign_tx.py index c0abebd59..c423ea782 100644 --- a/src/apps/cardano/sign_tx.py +++ b/src/apps/cardano/sign_tx.py @@ -6,11 +6,12 @@ from trezor.messages.CardanoTxRequest import CardanoTxRequest from trezor.messages.MessageType import CardanoTxAck from trezor.ui.text import BR -from .address import _break_address_n_to_lines, derive_address_and_node +from .address import derive_address_and_node from .layout import confirm_with_pagination, progress from apps.cardano import cbor from apps.common import seed, storage +from apps.common.layout import address_n_to_str, split_address from apps.homescreen.homescreen import display_homescreen @@ -53,7 +54,7 @@ async def show_tx( for index, change in enumerate(change_derivation_paths): if not await confirm_with_pagination( ctx, - _break_address_n_to_lines(change), + split_address(address_n_to_str(change)), "Confirm change", ui.ICON_SEND, ui.GREEN, diff --git a/src/apps/common/layout.py b/src/apps/common/layout.py index c2af10df6..ed4f80467 100644 --- a/src/apps/common/layout.py +++ b/src/apps/common/layout.py @@ -8,11 +8,13 @@ from trezor.ui.qr import Qr from trezor.ui.text import Text from trezor.utils import chunks +from apps.common import HARDENED from apps.common.confirm import confirm, require_confirm -async def show_address(ctx, address: str, network: str = None): +async def show_address(ctx, address: str, address_n: list, network: str = None): text = Text("Confirm address", ui.ICON_RECEIVE, icon_color=ui.GREEN) + # TODO: print address_n via address_n_to_str(address_n) if network: text.normal("%s network" % network) text.mono(*split_address(address)) @@ -38,12 +40,22 @@ async def show_qr(ctx, address: str): ) -def split_address(address: str): - return chunks(address, 17) - - async def show_pubkey(ctx, pubkey: bytes): lines = chunks(hexlify(pubkey).decode(), 18) text = Text("Confirm public key", ui.ICON_RECEIVE, icon_color=ui.GREEN) text.mono(*lines) return await require_confirm(ctx, text, code=ButtonRequestType.PublicKey) + + +def split_address(address: str): + return chunks(address, 17) + + +def address_n_to_str(address_n: list) -> str: + def path_item(i: int): + if i & HARDENED: + return str(i ^ HARDENED) + "'" + else: + return str(i) + + return "m/" + "/".join([path_item(i) for i in address_n]) diff --git a/src/apps/ethereum/get_address.py b/src/apps/ethereum/get_address.py index 46938efc9..d91cc2597 100644 --- a/src/apps/ethereum/get_address.py +++ b/src/apps/ethereum/get_address.py @@ -24,7 +24,7 @@ async def get_address(ctx, msg): hex_addr = _ethereum_address_hex(address, network) while True: - if await show_address(ctx, hex_addr): + if await show_address(ctx, hex_addr, address_n): break if await show_qr(ctx, hex_addr): break diff --git a/src/apps/lisk/get_address.py b/src/apps/lisk/get_address.py index d0d37e690..b5b602c65 100644 --- a/src/apps/lisk/get_address.py +++ b/src/apps/lisk/get_address.py @@ -16,7 +16,7 @@ async def get_address(ctx, msg): if msg.show_display: while True: - if await show_address(ctx, address): + if await show_address(ctx, address, address_n): break if await show_qr(ctx, address): break diff --git a/src/apps/nem/get_address.py b/src/apps/nem/get_address.py index 3285b923c..56e4360af 100644 --- a/src/apps/nem/get_address.py +++ b/src/apps/nem/get_address.py @@ -14,7 +14,9 @@ async def get_address(ctx, msg): if msg.show_display: while True: - if await show_address(ctx, address, get_network_str(network)): + if await show_address( + ctx, address, msg.address_n, network=get_network_str(network) + ): break if await show_qr(ctx, address.upper()): break diff --git a/src/apps/ripple/get_address.py b/src/apps/ripple/get_address.py index ed76bcd93..4cddeeda1 100644 --- a/src/apps/ripple/get_address.py +++ b/src/apps/ripple/get_address.py @@ -14,7 +14,7 @@ async def get_address(ctx, msg: RippleGetAddress): if msg.show_display: while True: - if await show_address(ctx, address): + if await show_address(ctx, address, msg.address_n): break if await show_qr(ctx, address.upper()): break diff --git a/src/apps/stellar/get_address.py b/src/apps/stellar/get_address.py index 5369e1437..a28d5c76f 100644 --- a/src/apps/stellar/get_address.py +++ b/src/apps/stellar/get_address.py @@ -13,7 +13,7 @@ async def get_address(ctx, msg: StellarGetAddress): if msg.show_display: while True: - if await show_address(ctx, address): + if await show_address(ctx, address, msg.address_n): break if await show_qr(ctx, address.upper()): break diff --git a/src/apps/tezos/get_address.py b/src/apps/tezos/get_address.py index 77049fe41..76d05de15 100644 --- a/src/apps/tezos/get_address.py +++ b/src/apps/tezos/get_address.py @@ -20,7 +20,7 @@ async def get_address(ctx, msg): if msg.show_display: while True: - if await show_address(ctx, address): + if await show_address(ctx, address, address_n): break if await show_qr(ctx, address): break diff --git a/src/apps/wallet/get_address.py b/src/apps/wallet/get_address.py index 3825f3148..6b1c1e648 100644 --- a/src/apps/wallet/get_address.py +++ b/src/apps/wallet/get_address.py @@ -16,7 +16,7 @@ async def get_address(ctx, msg): if msg.show_display: while True: - if await show_address(ctx, address_short): + if await show_address(ctx, address_short, msg.address_n): break if await show_qr( ctx,