1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-01-13 00:40:58 +00:00
trezor-firmware/src/apps/ethereum/layout.py

72 lines
2.4 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.ethereum import networks, tokens
from apps.ethereum.get_address import _ethereum_address_hex
2017-12-27 11:48:05 +00:00
2018-05-02 14:48:10 +00:00
async def require_confirm_tx(ctx, to, value, chain_id, token=None, tx_type=None):
2018-02-28 01:09:10 +00:00
if to:
2018-07-02 13:19:04 +00:00
to_str = _ethereum_address_hex(to, 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?"
text = Text("Confirm sending", ui.ICON_SEND, icon_color=ui.GREEN)
2018-07-02 13:19:04 +00:00
text.bold(format_ethereum_amount(value, token, chain_id, tx_type))
2018-07-03 14:20:58 +00:00
text.normal("to")
2018-07-02 13:19:04 +00:00
text.mono(*split_address(to_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
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
):
text = Text("Confirm transaction", ui.ICON_SEND, icon_color=ui.GREEN)
2018-07-02 13:19:04 +00:00
text.bold(format_ethereum_amount(spending, token, chain_id, tx_type))
2018-07-03 14:20:58 +00:00
text.normal("Gas price:")
2018-07-02 13:19:04 +00:00
text.bold(format_ethereum_amount(gas_price, None, chain_id, tx_type))
2018-07-03 14:20:58 +00:00
text.normal("Maximum fee:")
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] + ".."
text = Text("Confirm data", ui.ICON_SEND, icon_color=ui.GREEN)
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 split_address(address):
return chunks(address, 17)
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
if value <= 1e9:
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)