1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-01-27 15:51:02 +00:00
trezor-firmware/src/apps/ethereum/layout.py

56 lines
2.1 KiB
Python
Raw Normal View History

2018-01-04 13:08:21 +00:00
from apps.common.confirm import *
2017-12-27 11:48:05 +00:00
from trezor import wire, ui
from trezor.utils import chunks
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
async def confirm_tx(ctx, to, value, chain_id, token=None): # TODO: wording
str_to = '0x' + hexlify(to).decode() # TODO: use ethereum address format
2017-12-27 11:48:05 +00:00
content = Text('Confirm transaction', ui.ICON_RESET,
ui.BOLD, format_amount(value, token, chain_id),
ui.NORMAL, 'to',
2018-01-04 13:08:21 +00:00
ui.MONO, *split_address(str_to))
return await confirm(ctx, content, ButtonRequestType.SignTx) # we use SignTx, not ConfirmOutput, for compatibility with T1
2017-12-27 11:48:05 +00:00
async def confirm_fee(ctx, spending, gas_price, gas_limit, chain_id, token=None): # TODO: wording
2018-01-04 13:08:21 +00:00
content = Text('Confirm fee', ui.ICON_RESET,
2017-12-27 11:48:05 +00:00
'Sending: %s' % format_amount(spending, token, chain_id),
2018-01-04 13:08:21 +00:00
'Gas: %s' % format_amount(gas_price, token, chain_id),
'Limit: %s' % format_amount(gas_limit, token, chain_id))
2017-12-27 11:48:05 +00:00
return await hold_to_confirm(ctx, content, ButtonRequestType.SignTx)
async def confirm_data(ctx, data, data_total): # TODO: wording
2018-01-04 13:08:21 +00:00
str_data = hexlify(data[:8]).decode() + '..'
content = Text('Confirm data:', ui.ICON_RESET,
ui.MONO, str_data,
'Total: ', str(data_total) + 'B')
return await 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)
def format_amount(value, token, chain_id):
2018-01-04 13:08:21 +00:00
value = int.from_bytes(value, 'big')
2017-12-27 11:48:05 +00:00
if token:
suffix = token.ticker
decimals = token.decimals
elif value < 1e18:
suffix = 'Wei'
decimals = 0
else:
decimals = 18
suffix = networks.suffix_by_chain_id(chain_id)
d = pow(10, decimals)
value = ('%d.%0*d' % (value // d , decimals, value % d)).rstrip('0')
if value.endswith('.'):
value += '0'
return '%s %s' % (value, suffix)