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

68 lines
2.5 KiB
Python
Raw Normal View History

2018-01-04 13:08:21 +00:00
from apps.common.confirm import *
from trezor import ui
from trezor.utils import chunks, format_amount
2017-12-27 11:48:05 +00:00
from trezor.messages import ButtonRequestType
from trezor.ui.text import Text
2018-01-04 13:08:21 +00:00
from ubinascii import hexlify
2017-12-27 11:48:05 +00:00
from . import networks
2018-02-28 01:09:10 +00:00
from .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:
str_to = _ethereum_address_hex(to)
else:
str_to = 'new contract?'
content = Text('Confirm sending', ui.ICON_SEND,
2018-05-02 14:48:10 +00:00
ui.BOLD, format_ethereum_amount(value, token, chain_id, tx_type),
2017-12-27 11:48:05 +00:00
ui.NORMAL, 'to',
2018-02-28 01:09:10 +00:00
ui.MONO, *split_address(str_to),
icon_color=ui.GREEN)
await require_confirm(ctx, content, ButtonRequestType.SignTx) # we use SignTx, not ConfirmOutput, for compatibility with T1
2017-12-27 11:48:05 +00:00
2018-05-02 14:48:10 +00:00
async def require_confirm_fee(ctx, spending, gas_price, gas_limit, chain_id, token=None, tx_type=None):
2018-02-28 01:09:10 +00:00
content = Text('Confirm transaction', ui.ICON_SEND,
2018-05-02 14:48:10 +00:00
ui.BOLD, format_ethereum_amount(spending, token, chain_id, tx_type),
ui.NORMAL, 'Gas price:',
2018-05-02 14:48:10 +00:00
ui.BOLD, format_ethereum_amount(gas_price, None, chain_id, tx_type),
ui.NORMAL, 'Maximum fee:',
2018-05-02 15:51:57 +00:00
ui.BOLD, format_ethereum_amount(gas_price * gas_limit, None, chain_id, tx_type),
2018-02-28 01:09:10 +00:00
icon_color=ui.GREEN)
await require_hold_to_confirm(ctx, content, 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-02-28 01:09:10 +00:00
str_data = hexlify(data[:36]).decode()
if data_total > 36:
str_data = str_data[:-2] + '..'
content = Text('Confirm data', ui.ICON_SEND,
ui.BOLD, 'Size: %d bytes' % data_total,
ui.MONO, *split_data(str_data),
icon_color=ui.GREEN)
await require_confirm(ctx, content, ButtonRequestType.SignTx) # we use SignTx, not ConfirmOutput, for compatibility with T1
2017-12-27 11:48:05 +00:00
def split_address(address):
return chunks(address, 17)
2018-05-02 14:48:10 +00:00
def format_ethereum_amount(value, token, chain_id, tx_type=None):
value = int.from_bytes(value, 'big')
2017-12-27 11:48:05 +00:00
if token:
suffix = token[2]
decimals = token[3]
2017-12-27 11:48:05 +00:00
else:
2018-05-02 14:48:10 +00:00
suffix = networks.suffix_by_chain_id(chain_id, tx_type)
2018-02-28 01:09:10 +00:00
decimals = 18
if value <= 1e9:
2018-02-28 01:09:10 +00:00
suffix = 'Wei ' + suffix
decimals = 0
2017-12-27 11:48:05 +00:00
return '%s %s' % (format_amount(value, decimals), suffix)