src/apps: use require_confirm where possible, introduce require_hold_to_confirm

This commit is contained in:
Pavol Rusnak 2018-02-28 20:20:39 +01:00
parent 4f98f02ff9
commit 1269a0239d
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D
8 changed files with 28 additions and 30 deletions

View File

@ -44,3 +44,9 @@ async def require_confirm(*args, **kwargs):
confirmed = await confirm(*args, **kwargs) confirmed = await confirm(*args, **kwargs)
if not confirmed: if not confirmed:
raise wire.FailureError(FailureType.ActionCancelled, 'Cancelled') raise wire.FailureError(FailureType.ActionCancelled, 'Cancelled')
async def require_hold_to_confirm(*args, **kwargs):
confirmed = await hold_to_confirm(*args, **kwargs)
if not confirmed:
raise wire.FailureError(FailureType.ActionCancelled, 'Cancelled')

View File

@ -8,7 +8,7 @@ from . import networks
from .get_address import _ethereum_address_hex from .get_address import _ethereum_address_hex
async def confirm_tx(ctx, to, value, chain_id, token=None): async def require_confirm_tx(ctx, to, value, chain_id, token=None):
if to: if to:
str_to = _ethereum_address_hex(to) str_to = _ethereum_address_hex(to)
else: else:
@ -18,10 +18,10 @@ async def confirm_tx(ctx, to, value, chain_id, token=None):
ui.NORMAL, 'to', ui.NORMAL, 'to',
ui.MONO, *split_address(str_to), ui.MONO, *split_address(str_to),
icon_color=ui.GREEN) icon_color=ui.GREEN)
return await confirm(ctx, content, ButtonRequestType.SignTx) # we use SignTx, not ConfirmOutput, for compatibility with T1 await require_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): async def require_confirm_fee(ctx, spending, gas_price, gas_limit, chain_id, token=None):
content = Text('Confirm transaction', ui.ICON_SEND, content = Text('Confirm transaction', ui.ICON_SEND,
ui.BOLD, format_ethereum_amount(spending, token, chain_id), ui.BOLD, format_ethereum_amount(spending, token, chain_id),
ui.NORMAL, 'Gas:', ui.NORMAL, 'Gas:',
@ -29,14 +29,14 @@ async def confirm_fee(ctx, spending, gas_price, gas_limit, chain_id, token=None)
ui.NORMAL, 'Limit:', ui.NORMAL, 'Limit:',
ui.BOLD, format_ethereum_amount(gas_limit, token, chain_id), ui.BOLD, format_ethereum_amount(gas_limit, token, chain_id),
icon_color=ui.GREEN) icon_color=ui.GREEN)
return await hold_to_confirm(ctx, content, ButtonRequestType.SignTx) await require_hold_to_confirm(ctx, content, ButtonRequestType.SignTx)
def split_data(data): def split_data(data):
return chunks(data, 18) return chunks(data, 18)
async def confirm_data(ctx, data, data_total): async def require_confirm_data(ctx, data, data_total):
str_data = hexlify(data[:36]).decode() str_data = hexlify(data[:36]).decode()
if data_total > 36: if data_total > 36:
str_data = str_data[:-2] + '..' str_data = str_data[:-2] + '..'
@ -44,7 +44,7 @@ async def confirm_data(ctx, data, data_total):
ui.BOLD, 'Size: %d bytes' % data_total, ui.BOLD, 'Size: %d bytes' % data_total,
ui.MONO, *split_data(str_data), ui.MONO, *split_data(str_data),
icon_color=ui.GREEN) icon_color=ui.GREEN)
return await confirm(ctx, content, ButtonRequestType.SignTx) # we use SignTx, not ConfirmOutput, for compatibility with T1 await require_confirm(ctx, content, ButtonRequestType.SignTx) # we use SignTx, not ConfirmOutput, for compatibility with T1
def split_address(address): def split_address(address):

View File

@ -3,8 +3,8 @@ 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
from apps.ethereum import tokens, layout from apps.ethereum.layout import require_confirm_tx, require_confirm_data, require_confirm_fee
# maximum supported chain id # maximum supported chain id
MAX_CHAIN_ID = 2147483630 MAX_CHAIN_ID = 2147483630
@ -28,22 +28,14 @@ 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:
confirmed = await layout.confirm_tx(ctx, msg.to, msg.value, msg.chain_id) await require_confirm_tx(ctx, msg.to, msg.value, msg.chain_id)
if not confirmed:
raise wire.FailureError(FailureType.ActionCancelled, 'Cancelled')
else: else:
confirmed = await layout.confirm_tx(ctx, msg.data_initial_chunk[16:36], msg.data_initial_chunk[36:68], msg.chain_id, token) await require_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:
confirmed = await layout.confirm_data(ctx, msg.data_initial_chunk, data_total) await require_confirm_data(ctx, msg.data_initial_chunk, data_total)
if not confirmed:
raise wire.FailureError(FailureType.ActionCancelled, 'Cancelled')
confirmed = await layout.confirm_fee(ctx, msg.value, msg.gas_price, msg.gas_limit, msg.chain_id, token) await require_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

View File

@ -14,7 +14,7 @@ from apps.common.request_pin import request_pin, PinCancelled
async def change_pin(ctx, msg): async def change_pin(ctx, msg):
# confirm that user wants to change the pin # confirm that user wants to change the pin
await confirm_change_pin(ctx, msg) await require_confirm_change_pin(ctx, msg)
# get current pin, return failure if invalid # get current pin, return failure if invalid
if config.has_pin(): if config.has_pin():
@ -40,7 +40,7 @@ async def change_pin(ctx, msg):
return Failure(code=FailureType.PinInvalid, message='PIN invalid') return Failure(code=FailureType.PinInvalid, message='PIN invalid')
def confirm_change_pin(ctx, msg): def require_confirm_change_pin(ctx, msg):
has_pin = config.has_pin() has_pin = config.has_pin()
if msg.remove and has_pin: # removing pin if msg.remove and has_pin: # removing pin

View File

@ -3,12 +3,12 @@ from trezor.messages import ButtonRequestType
from trezor.messages.Success import Success from trezor.messages.Success import Success
from trezor.ui.text import Text from trezor.ui.text import Text
from apps.common import storage from apps.common import storage
from apps.common.confirm import hold_to_confirm from apps.common.confirm import require_hold_to_confirm
async def wipe_device(ctx, msg): async def wipe_device(ctx, msg):
await hold_to_confirm(ctx, Text( await require_hold_to_confirm(ctx, Text(
'Wipe device', 'Wipe device',
ui.ICON_WIPE, ui.ICON_WIPE,
ui.NORMAL, 'Do you really want to', 'wipe the device?', ui.NORMAL, 'Do you really want to', 'wipe the device?',

View File

@ -15,7 +15,7 @@ async def sign_identity(ctx, msg):
identity = serialize_identity(msg.identity) identity = serialize_identity(msg.identity)
await confirm_sign_identity(ctx, identity, msg.challenge_visual) await require_confirm_sign_identity(ctx, identity, msg.challenge_visual)
address_n = get_identity_path(identity, msg.identity.index or 0) address_n = get_identity_path(identity, msg.identity.index or 0)
node = await seed.derive_node(ctx, address_n, msg.ecdsa_curve_name) node = await seed.derive_node(ctx, address_n, msg.ecdsa_curve_name)
@ -43,7 +43,7 @@ async def sign_identity(ctx, msg):
return SignedIdentity(address=address, public_key=pubkey, signature=signature) return SignedIdentity(address=address, public_key=pubkey, signature=signature)
async def confirm_sign_identity(ctx, identity, challenge_visual): async def require_confirm_sign_identity(ctx, identity, challenge_visual):
lines = chunks(identity, 18) lines = chunks(identity, 18)
content = Text('Sign identity', ui.ICON_DEFAULT, content = Text('Sign identity', ui.ICON_DEFAULT,
challenge_visual, challenge_visual,

View File

@ -18,7 +18,7 @@ async def sign_message(ctx, msg):
script_type = msg.script_type or 0 script_type = msg.script_type or 0
coin = coins.by_name(coin_name) coin = coins.by_name(coin_name)
await confirm_sign_message(ctx, message) await require_confirm_sign_message(ctx, message)
node = await seed.derive_node(ctx, address_n) node = await seed.derive_node(ctx, address_n)
seckey = node.private_key() seckey = node.private_key()
@ -39,7 +39,7 @@ async def sign_message(ctx, msg):
return MessageSignature(address=address, signature=signature) return MessageSignature(address=address, signature=signature)
async def confirm_sign_message(ctx, message): async def require_confirm_sign_message(ctx, message):
message = split_message(message) message = split_message(message)
content = Text('Sign message', ui.ICON_DEFAULT, max_lines=5, *message) content = Text('Sign message', ui.ICON_DEFAULT, max_lines=5, *message)
await require_confirm(ctx, content) await require_confirm(ctx, content)

View File

@ -50,12 +50,12 @@ async def verify_message(ctx, msg):
if addr != address: if addr != address:
raise wire.FailureError(ProcessError, 'Invalid signature') raise wire.FailureError(ProcessError, 'Invalid signature')
await confirm_verify_message(ctx, address, message) await require_confirm_verify_message(ctx, address, message)
return Success(message='Message verified') return Success(message='Message verified')
async def confirm_verify_message(ctx, address, message): async def require_confirm_verify_message(ctx, address, message):
lines = _split_address(address) lines = _split_address(address)
content = Text('Confirm address', ui.ICON_DEFAULT, ui.MONO, *lines) content = Text('Confirm address', ui.ICON_DEFAULT, ui.MONO, *lines)
await require_confirm(ctx, content) await require_confirm(ctx, content)