1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-02-10 22:52:46 +00:00
trezor-firmware/core/src/apps/ethereum/layout.py

72 lines
2.6 KiB
Python
Raw Normal View History

2018-06-05 18:21:31 +00:00
from ubinascii import hexlify
from trezor import ui
2017-12-27 11:48:05 +00:00
from trezor.messages import ButtonRequestType
from trezor.ui.text import Text
2018-07-03 14:20:26 +00:00
from trezor.utils import chunks, format_amount
2018-06-05 18:21:31 +00:00
from apps.common.confirm import require_confirm, require_hold_to_confirm
from apps.common.layout import split_address
2018-06-05 18:21:31 +00:00
from apps.ethereum import networks, tokens
from apps.ethereum.address import address_from_bytes
2017-12-27 11:48:05 +00:00
async def require_confirm_tx(ctx, to_bytes, value, chain_id, token=None, tx_type=None):
if to_bytes:
to_str = address_from_bytes(to_bytes, networks.by_chain_id(chain_id))
2018-02-28 01:09:10 +00:00
else:
2018-07-03 14:20:58 +00:00
to_str = "new contract?"
2019-05-13 13:06:34 +00:00
text = Text("Confirm sending", ui.ICON_SEND, ui.GREEN, new_lines=False)
2018-07-02 13:19:04 +00:00
text.bold(format_ethereum_amount(value, token, chain_id, tx_type))
text.normal(ui.GREY, "to", ui.FG)
for to_line in split_address(to_str):
text.br()
text.mono(to_line)
2018-07-02 13:19:04 +00:00
# we use SignTx, not ConfirmOutput, for compatibility with T1
await require_confirm(ctx, text, ButtonRequestType.SignTx)
2017-12-27 11:48:05 +00:00
2018-07-03 14:20:58 +00:00
async def require_confirm_fee(
ctx, spending, gas_price, gas_limit, chain_id, token=None, tx_type=None
):
2019-05-13 13:06:34 +00:00
text = Text("Confirm transaction", ui.ICON_SEND, ui.GREEN, new_lines=False)
2018-07-02 13:19:04 +00:00
text.bold(format_ethereum_amount(spending, token, chain_id, tx_type))
text.normal(ui.GREY, "Gas price:", ui.FG)
2018-07-02 13:19:04 +00:00
text.bold(format_ethereum_amount(gas_price, None, chain_id, tx_type))
text.normal(ui.GREY, "Maximum fee:", ui.FG)
2018-07-02 13:19:04 +00:00
text.bold(format_ethereum_amount(gas_price * gas_limit, None, chain_id, tx_type))
await require_hold_to_confirm(ctx, text, ButtonRequestType.SignTx)
2017-12-27 11:48:05 +00:00
2018-02-28 01:09:10 +00:00
def split_data(data):
return chunks(data, 18)
async def require_confirm_data(ctx, data, data_total):
2018-07-02 13:19:04 +00:00
data_str = hexlify(data[:36]).decode()
2018-02-28 01:09:10 +00:00
if data_total > 36:
2018-07-03 14:20:58 +00:00
data_str = data_str[:-2] + ".."
2019-05-13 13:06:34 +00:00
text = Text("Confirm data", ui.ICON_SEND, ui.GREEN)
2018-07-03 14:20:58 +00:00
text.bold("Size: %d bytes" % data_total)
2018-07-02 13:19:04 +00:00
text.mono(*split_data(data_str))
# we use SignTx, not ConfirmOutput, for compatibility with T1
await require_confirm(ctx, text, ButtonRequestType.SignTx)
2017-12-27 11:48:05 +00:00
def format_ethereum_amount(value: int, token, chain_id: int, tx_type=None):
2017-12-27 11:48:05 +00:00
if token:
if token is tokens.UNKNOWN_TOKEN:
2018-07-03 14:20:58 +00:00
return "Unknown token value"
suffix = token[2]
decimals = token[3]
2017-12-27 11:48:05 +00:00
else:
suffix = networks.shortcut_by_chain_id(chain_id, tx_type)
2018-02-28 01:09:10 +00:00
decimals = 18
# Don't want to display wei values for tokens with small decimal numbers
if decimals > 9 and value < 10 ** (decimals - 9):
2018-07-03 14:20:58 +00:00
suffix = "Wei " + suffix
2018-02-28 01:09:10 +00:00
decimals = 0
2017-12-27 11:48:05 +00:00
2018-07-03 14:20:58 +00:00
return "%s %s" % (format_amount(value, decimals), suffix)