src/apps/ethereum: fix ethereum ui

This commit is contained in:
Pavol Rusnak 2018-02-28 02:09:10 +01:00
parent 0c42976e91
commit 19bae93d92
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D
2 changed files with 47 additions and 22 deletions

View File

@ -5,30 +5,45 @@ from trezor.messages import ButtonRequestType
from trezor.ui.text import Text from trezor.ui.text import Text
from ubinascii import hexlify from ubinascii import hexlify
from . import networks from . import networks
from .get_address import _ethereum_address_hex
async def confirm_tx(ctx, to, value, chain_id, token=None): # TODO: wording async def confirm_tx(ctx, to, value, chain_id, token=None):
str_to = '0x' + hexlify(to).decode() # TODO: use ethereum address format if to:
content = Text('Confirm transaction', ui.ICON_DEFAULT, str_to = _ethereum_address_hex(to)
else:
str_to = 'new contract?'
content = Text('Confirm sending', ui.ICON_SEND,
ui.BOLD, format_ethereum_amount(value, token, chain_id), ui.BOLD, format_ethereum_amount(value, token, chain_id),
ui.NORMAL, 'to', ui.NORMAL, 'to',
ui.MONO, *split_address(str_to)) ui.MONO, *split_address(str_to),
icon_color=ui.GREEN)
return await confirm(ctx, content, ButtonRequestType.SignTx) # we use SignTx, not ConfirmOutput, for compatibility with T1 return await confirm(ctx, content, ButtonRequestType.SignTx) # we use SignTx, not ConfirmOutput, for compatibility with T1
async def confirm_fee(ctx, spending, gas_price, gas_limit, chain_id, token=None): # TODO: wording async def confirm_fee(ctx, spending, gas_price, gas_limit, chain_id, token=None):
content = Text('Confirm fee', ui.ICON_DEFAULT, content = Text('Confirm transaction', ui.ICON_SEND,
'Sending: %s' % format_ethereum_amount(spending, token, chain_id), ui.BOLD, format_ethereum_amount(spending, token, chain_id),
'Gas: %s' % format_ethereum_amount(gas_price, token, chain_id), ui.NORMAL, 'Gas:',
'Limit: %s' % format_ethereum_amount(gas_limit, token, chain_id)) ui.BOLD, format_ethereum_amount(gas_price, token, chain_id),
ui.NORMAL, 'Limit:',
ui.BOLD, format_ethereum_amount(gas_limit, token, chain_id),
icon_color=ui.GREEN)
return await hold_to_confirm(ctx, content, ButtonRequestType.SignTx) return await hold_to_confirm(ctx, content, ButtonRequestType.SignTx)
async def confirm_data(ctx, data, data_total): # TODO: wording def split_data(data):
str_data = hexlify(data[:8]).decode() + '..' return chunks(data, 18)
content = Text('Confirm data:', ui.ICON_DEFAULT,
ui.MONO, str_data,
'Total: ', str(data_total) + 'B') async def confirm_data(ctx, data, data_total):
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)
return await confirm(ctx, content, ButtonRequestType.SignTx) # we use SignTx, not ConfirmOutput, for compatibility with T1 return await confirm(ctx, content, ButtonRequestType.SignTx) # we use SignTx, not ConfirmOutput, for compatibility with T1
@ -41,11 +56,12 @@ def format_ethereum_amount(value, token, chain_id):
if token: if token:
suffix = token['symbol'] suffix = token['symbol']
decimals = token['decimal'] decimals = token['decimal']
elif value < 1e18:
suffix = 'Wei'
decimals = 0
else: else:
decimals = 18
suffix = networks.suffix_by_chain_id(chain_id) suffix = networks.suffix_by_chain_id(chain_id)
decimals = 18
if value < 1e18:
suffix = 'Wei ' + suffix
decimals = 0
return '%s %s' % (format_amount(value, decimals), suffix) return '%s %s' % (format_amount(value, decimals), suffix)

View File

@ -3,6 +3,7 @@ from trezor.messages.EthereumTxRequest import EthereumTxRequest
from trezor.messages import FailureType from trezor.messages import FailureType
from trezor.utils import HashWriter from trezor.utils import HashWriter
from trezor.crypto import rlp from trezor.crypto import rlp
from trezor import wire
from apps.ethereum import tokens, layout from apps.ethereum import tokens, layout
# maximum supported chain id # maximum supported chain id
@ -27,14 +28,22 @@ async def ethereum_sign_tx(ctx, msg):
token = tokens.token_by_chain_address(msg.chain_id, msg.to) token = tokens.token_by_chain_address(msg.chain_id, msg.to)
if token is None: if token is None:
await layout.confirm_tx(ctx, msg.to, msg.value, msg.chain_id) confirmed = await layout.confirm_tx(ctx, msg.to, msg.value, msg.chain_id)
if not confirmed:
raise wire.FailureError(FailureType.ActionCancelled, 'Cancelled')
else: else:
await layout.confirm_tx(ctx, msg.data_initial_chunk[16:36], msg.data_initial_chunk[36:68], msg.chain_id, token) confirmed = await layout.confirm_tx(ctx, msg.data_initial_chunk[16:36], msg.data_initial_chunk[36:68], msg.chain_id, token)
if not confirmed:
raise wire.FailureError(FailureType.ActionCancelled, 'Cancelled')
if token is None and msg.data_length > 0: if token is None and msg.data_length > 0:
await layout.confirm_data(ctx, msg.data_initial_chunk, data_total) confirmed = await layout.confirm_data(ctx, msg.data_initial_chunk, data_total)
if not confirmed:
raise wire.FailureError(FailureType.ActionCancelled, 'Cancelled')
await layout.confirm_fee(ctx, msg.value, msg.gas_price, msg.gas_limit, msg.chain_id, token) confirmed = await layout.confirm_fee(ctx, msg.value, msg.gas_price, msg.gas_limit, msg.chain_id, token)
if not confirmed:
raise wire.FailureError(FailureType.ActionCancelled, 'Cancelled')
data = bytearray() data = bytearray()
data += msg.data_initial_chunk data += msg.data_initial_chunk