refactor(core): convert apps.ethereum to layouts

pull/1679/head
Martin Milata 4 years ago committed by matejcik
parent b7cab90e3c
commit 6c926ad82e

@ -3,11 +3,7 @@ from ubinascii import hexlify
from trezor import ui from trezor import ui
from trezor.enums import ButtonRequestType from trezor.enums import ButtonRequestType
from trezor.strings import format_amount from trezor.strings import format_amount
from trezor.ui.components.tt.text import Text from trezor.ui.layouts import confirm_hex, confirm_output, confirm_total_ethereum
from trezor.utils import chunks
from apps.common.confirm import require_confirm, require_hold_to_confirm
from apps.common.layout import split_address
from . import networks, tokens from . import networks, tokens
from .address import address_from_bytes from .address import address_from_bytes
@ -18,49 +14,55 @@ async def require_confirm_tx(ctx, to_bytes, value, chain_id, token=None, tx_type
to_str = address_from_bytes(to_bytes, networks.by_chain_id(chain_id)) to_str = address_from_bytes(to_bytes, networks.by_chain_id(chain_id))
else: else:
to_str = "new contract?" to_str = "new contract?"
text = Text("Confirm sending", ui.ICON_SEND, ui.GREEN, new_lines=False) await confirm_output(
text.bold(format_ethereum_amount(value, token, chain_id, tx_type)) ctx,
text.normal(ui.GREY, " to ", ui.FG) address=to_str,
for to_line in split_address(to_str): amount=format_ethereum_amount(value, token, chain_id, tx_type),
text.br() font_amount=ui.BOLD,
text.mono(to_line) color_to=ui.GREY,
# we use SignTx, not ConfirmOutput, for compatibility with T1 br_code=ButtonRequestType.SignTx,
await require_confirm(ctx, text, ButtonRequestType.SignTx) )
async def require_confirm_fee( async def require_confirm_fee(
ctx, spending, gas_price, gas_limit, chain_id, token=None, tx_type=None ctx, spending, gas_price, gas_limit, chain_id, token=None, tx_type=None
): ):
text = Text("Confirm transaction", ui.ICON_SEND, ui.GREEN, new_lines=False) await confirm_total_ethereum(
text.bold(format_ethereum_amount(spending, token, chain_id, tx_type)) ctx,
text.normal(" ", ui.GREY, "Gas price:", ui.FG) format_ethereum_amount(spending, token, chain_id, tx_type),
text.bold(format_ethereum_amount(gas_price, None, chain_id, tx_type)) format_ethereum_amount(gas_price, None, chain_id, tx_type),
text.normal(" ", ui.GREY, "Maximum fee:", ui.FG) format_ethereum_amount(gas_price * gas_limit, None, chain_id, tx_type),
text.bold(format_ethereum_amount(gas_price * gas_limit, None, chain_id, tx_type)) )
await require_hold_to_confirm(ctx, text, ButtonRequestType.SignTx)
async def require_confirm_unknown_token(ctx, address_bytes): async def require_confirm_unknown_token(ctx, address_bytes):
text = Text("Unknown token", ui.ICON_SEND, ui.ORANGE, new_lines=False)
text.normal(ui.GREY, "Contract:", ui.FG)
contract_address_hex = "0x" + hexlify(address_bytes).decode() contract_address_hex = "0x" + hexlify(address_bytes).decode()
text.mono(*split_data(contract_address_hex)) await confirm_hex(
await require_confirm(ctx, text, ButtonRequestType.SignTx) ctx,
"confirm_unknown",
title="Unknown token",
def split_data(data): data=contract_address_hex,
return chunks(data, 18) truncate=True,
description="Contract:",
color_description=ui.GREY,
icon_color=ui.ORANGE,
br_code=ButtonRequestType.SignTx,
)
async def require_confirm_data(ctx, data, data_total): async def require_confirm_data(ctx, data, data_total):
data_str = hexlify(data[:36]).decode() data_str = hexlify(data[:36]).decode()
if data_total > 36: if data_total > 36:
data_str = data_str[:-2] + ".." data_str = data_str[:-2] + ".."
text = Text("Confirm data", ui.ICON_SEND, ui.GREEN) await confirm_hex(
text.bold("Size: %d bytes" % data_total) ctx,
text.mono(*split_data(data_str)) "confirm_data",
# we use SignTx, not ConfirmOutput, for compatibility with T1 title="Confirm data",
await require_confirm(ctx, text, ButtonRequestType.SignTx) data=data_str,
truncate=True,
subtitle="Size: %d bytes" % data_total,
br_code=ButtonRequestType.SignTx,
)
def format_ethereum_amount(value: int, token, chain_id: int, tx_type=None): def format_ethereum_amount(value: int, token, chain_id: int, tx_type=None):

@ -53,6 +53,7 @@ __all__ = (
"confirm_decred_sstx_submission", "confirm_decred_sstx_submission",
"confirm_hex", "confirm_hex",
"confirm_total", "confirm_total",
"confirm_total_ethereum",
"confirm_joint_total", "confirm_joint_total",
"confirm_metadata", "confirm_metadata",
"confirm_replacement", "confirm_replacement",
@ -215,7 +216,7 @@ def _truncate_hex(
width: int = MONO_HEX_PER_LINE, width: int = MONO_HEX_PER_LINE,
middle: bool = False, middle: bool = False,
) -> Iterator[str]: ) -> Iterator[str]:
if len(hex_data) >= width * lines: if len(hex_data) > width * lines:
if middle: if middle:
hex_data = ( hex_data = (
hex_data[: lines * width // 2 - 1] hex_data[: lines * width // 2 - 1]
@ -438,12 +439,15 @@ async def confirm_output(
ctx: wire.GenericContext, ctx: wire.GenericContext,
address: str, address: str,
amount: str, amount: str,
font_amount: int = ui.NORMAL, # TODO cleanup @ redesign
color_to: int = ui.FG, # TODO cleanup @ redesign
br_code: ButtonRequestType = ButtonRequestType.ConfirmOutput,
) -> None: ) -> None:
text = Text("Confirm sending", ui.ICON_SEND, ui.GREEN, new_lines=False) text = Text("Confirm sending", ui.ICON_SEND, ui.GREEN, new_lines=False)
text.normal(amount + " to\n") text.content = [font_amount, amount, ui.NORMAL, color_to, " to\n", ui.FG]
text.mono(*_split_address(address)) text.mono(*_split_address(address))
await raise_if_cancelled( await raise_if_cancelled(
interact(ctx, Confirm(text), "confirm_output", ButtonRequestType.ConfirmOutput) interact(ctx, Confirm(text), "confirm_output", br_code)
) )
@ -475,14 +479,18 @@ async def confirm_hex(
br_code: ButtonRequestType = ButtonRequestType.Other, br_code: ButtonRequestType = ButtonRequestType.Other,
icon: str = ui.ICON_SEND, # TODO cleanup @ redesign icon: str = ui.ICON_SEND, # TODO cleanup @ redesign
icon_color: int = ui.GREEN, # TODO cleanup @ redesign icon_color: int = ui.GREEN, # TODO cleanup @ redesign
font_description: int = ui.NORMAL, # TODO cleanup @ redesign
color_description: int = ui.FG, # TODO cleanup @ redesign
width: int = MONO_HEX_PER_LINE, width: int = MONO_HEX_PER_LINE,
truncate_middle: bool = False, truncate_middle: bool = False,
) -> None: ) -> None:
text = Text(title, icon, icon_color, new_lines=False) text = Text(title, icon, icon_color, new_lines=False)
description_lines = 0 description_lines = 0
if description is not None: if description is not None:
description_lines = Span(description, 0, ui.NORMAL).count_lines() description_lines = Span(description, 0, font_description).count_lines()
text.normal(description) text.content.extend(
(font_description, color_description, description, ui.FG)
)
text.br() text.br()
text.mono( text.mono(
*_truncate_hex( *_truncate_hex(
@ -509,6 +517,21 @@ async def confirm_total(
) )
# TODO cleanup @ redesign
async def confirm_total_ethereum(
ctx: wire.GenericContext, total_amount: str, gas_price: str, fee_max: str
) -> None:
text = Text("Confirm transaction", ui.ICON_SEND, ui.GREEN, new_lines=False)
text.bold(total_amount)
text.normal(" ", ui.GREY, "Gas price:", ui.FG)
text.bold(gas_price)
text.normal(" ", ui.GREY, "Maximum fee:", ui.FG)
text.bold(fee_max)
await raise_if_cancelled(
interact(ctx, HoldToConfirm(text), "confirm_total", ButtonRequestType.SignTx)
)
async def confirm_joint_total( async def confirm_joint_total(
ctx: wire.GenericContext, spending_amount: str, total_amount: str ctx: wire.GenericContext, spending_amount: str, total_amount: str
) -> None: ) -> None:

Loading…
Cancel
Save