2018-06-05 18:21:31 +00:00
|
|
|
from ubinascii import hexlify
|
|
|
|
|
2018-02-27 02:05:15 +00:00
|
|
|
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
|
2018-08-27 10:13:38 +00:00
|
|
|
from apps.common.layout import split_address
|
2018-06-05 18:21:31 +00:00
|
|
|
from apps.ethereum import networks, tokens
|
2019-01-29 14:35:09 +00:00
|
|
|
from apps.ethereum.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?"
|
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))
|
2018-10-23 15:07:07 +00:00
|
|
|
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))
|
2018-10-23 15:07:07 +00:00
|
|
|
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))
|
2018-10-23 15:07:07 +00:00
|
|
|
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)
|
|
|
|
|
|
|
|
|
2018-02-28 19:20:39 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
2018-05-03 09:03:37 +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:
|
2018-05-10 11:35:28 +00:00
|
|
|
if token is tokens.UNKNOWN_TOKEN:
|
2018-07-03 14:20:58 +00:00
|
|
|
return "Unknown token value"
|
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)
|