Skip to content

Commit

Permalink
src/apps/common: refactor address_n_to_str into apps.common.layout
Browse files Browse the repository at this point in the history
  • Loading branch information
prusnak committed Oct 10, 2018
1 parent cc25069 commit addbdd8
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 36 deletions.
22 changes: 0 additions & 22 deletions src/apps/cardano/address.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from micropython import const

from trezor import wire
from trezor.crypto import base58, crc, hashlib

Expand Down Expand Up @@ -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
5 changes: 3 additions & 2 deletions src/apps/cardano/sign_tx.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
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


Expand Down Expand Up @@ -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,
Expand Down
22 changes: 17 additions & 5 deletions src/apps/common/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
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))
Expand All @@ -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])
2 changes: 1 addition & 1 deletion src/apps/ethereum/get_address.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/apps/lisk/get_address.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion src/apps/nem/get_address.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/apps/ripple/get_address.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/apps/stellar/get_address.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/apps/tezos/get_address.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/apps/wallet/get_address.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit addbdd8

Please sign in to comment.