2018-06-05 18:21:31 +00:00
|
|
|
from ubinascii import hexlify
|
|
|
|
|
2018-02-27 02:05:15 +00:00
|
|
|
from trezor import ui
|
2021-03-23 12:35:27 +00:00
|
|
|
from trezor.enums import ButtonRequestType
|
2019-08-27 14:08:56 +00:00
|
|
|
from trezor.strings import format_amount
|
2021-06-05 00:15:25 +00:00
|
|
|
from trezor.ui.layouts import (
|
|
|
|
confirm_address,
|
|
|
|
confirm_amount,
|
|
|
|
confirm_blob,
|
|
|
|
confirm_output,
|
|
|
|
)
|
2021-03-25 11:32:10 +00:00
|
|
|
from trezor.ui.layouts.tt.altcoin import confirm_total_ethereum
|
2020-08-11 13:55:05 +00:00
|
|
|
|
|
|
|
from . import networks, tokens
|
|
|
|
from .address import address_from_bytes
|
2017-12-27 11:48:05 +00:00
|
|
|
|
|
|
|
|
2019-01-29 14:35:09 +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?"
|
2021-02-25 15:21:06 +00:00
|
|
|
await confirm_output(
|
|
|
|
ctx,
|
|
|
|
address=to_str,
|
|
|
|
amount=format_ethereum_amount(value, token, chain_id, tx_type),
|
|
|
|
font_amount=ui.BOLD,
|
|
|
|
color_to=ui.GREY,
|
|
|
|
br_code=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
|
|
|
|
):
|
2021-02-25 15:21:06 +00:00
|
|
|
await confirm_total_ethereum(
|
|
|
|
ctx,
|
|
|
|
format_ethereum_amount(spending, token, chain_id, tx_type),
|
|
|
|
format_ethereum_amount(gas_price, None, chain_id, tx_type),
|
|
|
|
format_ethereum_amount(gas_price * gas_limit, None, chain_id, tx_type),
|
|
|
|
)
|
2017-12-27 11:48:05 +00:00
|
|
|
|
|
|
|
|
2021-06-05 00:15:25 +00:00
|
|
|
async def require_confirm_eip1559_fee(
|
|
|
|
ctx, max_priority_fee, max_gas_fee, gas_limit, chain_id
|
|
|
|
):
|
|
|
|
await confirm_amount(
|
|
|
|
ctx,
|
|
|
|
title="Confirm fee",
|
|
|
|
description="Maximum fee per gas",
|
|
|
|
amount=format_ethereum_amount(max_gas_fee, None, chain_id),
|
|
|
|
)
|
|
|
|
await confirm_amount(
|
|
|
|
ctx,
|
|
|
|
title="Confirm fee",
|
|
|
|
description="Priority fee per gas",
|
|
|
|
amount=format_ethereum_amount(max_priority_fee, None, chain_id),
|
|
|
|
)
|
|
|
|
await confirm_amount(
|
|
|
|
ctx,
|
|
|
|
title="Confirm fee",
|
|
|
|
description="Maximum fee",
|
|
|
|
amount=format_ethereum_amount(max_gas_fee * gas_limit, None, chain_id),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-06-24 07:24:14 +00:00
|
|
|
async def require_confirm_unknown_token(ctx, address_bytes):
|
|
|
|
contract_address_hex = "0x" + hexlify(address_bytes).decode()
|
2021-07-16 13:07:50 +00:00
|
|
|
await confirm_address(
|
2021-02-25 15:21:06 +00:00
|
|
|
ctx,
|
2021-07-16 13:07:50 +00:00
|
|
|
"Unknown token",
|
|
|
|
contract_address_hex,
|
2021-02-25 15:21:06 +00:00
|
|
|
description="Contract:",
|
2021-07-16 13:07:50 +00:00
|
|
|
br_type="unknown_token",
|
2021-02-25 15:21:06 +00:00
|
|
|
icon_color=ui.ORANGE,
|
|
|
|
br_code=ButtonRequestType.SignTx,
|
|
|
|
)
|
2018-02-28 01:09:10 +00:00
|
|
|
|
|
|
|
|
2018-02-28 19:20:39 +00:00
|
|
|
async def require_confirm_data(ctx, data, data_total):
|
2021-07-16 13:07:50 +00:00
|
|
|
await confirm_blob(
|
2021-02-25 15:21:06 +00:00
|
|
|
ctx,
|
|
|
|
"confirm_data",
|
|
|
|
title="Confirm data",
|
2021-07-16 13:07:50 +00:00
|
|
|
description="Size: %d bytes" % data_total,
|
|
|
|
data=data,
|
2021-02-25 15:21:06 +00:00
|
|
|
br_code=ButtonRequestType.SignTx,
|
|
|
|
)
|
2017-12-27 11:48:05 +00:00
|
|
|
|
|
|
|
|
2018-05-03 09:03:37 +00:00
|
|
|
def format_ethereum_amount(value: int, token, chain_id: int, tx_type=None):
|
2020-06-24 07:24:14 +00:00
|
|
|
if token is tokens.UNKNOWN_TOKEN:
|
|
|
|
suffix = "Wei UNKN"
|
|
|
|
decimals = 0
|
|
|
|
elif token:
|
2018-02-28 01:36:43 +00:00
|
|
|
suffix = token[2]
|
|
|
|
decimals = token[3]
|
2017-12-27 11:48:05 +00:00
|
|
|
else:
|
2018-06-11 16:03:38 +00:00
|
|
|
suffix = networks.shortcut_by_chain_id(chain_id, tx_type)
|
2018-02-28 01:09:10 +00:00
|
|
|
decimals = 18
|
|
|
|
|
2019-05-16 10:50:23 +00:00
|
|
|
# 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)
|