Skip to content

Commit

Permalink
refactor(core/ui): raise exception on dialog cancel by default
Browse files Browse the repository at this point in the history
  • Loading branch information
mmilata committed Mar 10, 2021
1 parent 42221ca commit 28ec3a8
Show file tree
Hide file tree
Showing 40 changed files with 546 additions and 628 deletions.
6 changes: 2 additions & 4 deletions core/src/apps/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,10 @@ async def handle_EndSession(ctx: wire.Context, msg: EndSession) -> Success:

async def handle_Ping(ctx: wire.Context, msg: Ping) -> Success:
if msg.button_protection:
from trezor.ui.layouts import require, confirm_action
from trezor.ui.layouts import confirm_action
from trezor.messages.ButtonRequestType import ProtectCall

await require(
confirm_action(ctx, "ping", "Confirm", "ping", br_code=ProtectCall)
)
await confirm_action(ctx, "ping", "Confirm", "ping", br_code=ProtectCall)
return Success(message=msg.message)


Expand Down
4 changes: 2 additions & 2 deletions core/src/apps/binance/get_public_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from trezor.messages.BinanceGetPublicKey import BinanceGetPublicKey
from trezor.messages.BinancePublicKey import BinancePublicKey
from trezor.ui.layouts import require, show_pubkey
from trezor.ui.layouts import show_pubkey

from apps.common import paths
from apps.common.keychain import Keychain, auto_keychain
Expand All @@ -15,6 +15,6 @@ async def get_public_key(ctx, msg: BinanceGetPublicKey, keychain: Keychain):
pubkey = node.public_key()

if msg.show_display:
await require(show_pubkey(ctx, hexlify(pubkey).decode()))
await show_pubkey(ctx, hexlify(pubkey).decode())

return BinancePublicKey(public_key=pubkey)
30 changes: 13 additions & 17 deletions core/src/apps/bitcoin/authorize_coinjoin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from trezor.messages.AuthorizeCoinJoin import AuthorizeCoinJoin
from trezor.messages.Success import Success
from trezor.strings import format_amount
from trezor.ui.layouts import confirm_action, confirm_coinjoin, require
from trezor.ui.layouts import confirm_action, confirm_coinjoin

from apps.base import set_authorization
from apps.common.paths import validate_path
Expand Down Expand Up @@ -45,29 +45,25 @@ async def authorize_coinjoin(ctx: wire.Context, msg: AuthorizeCoinJoin) -> Succe
),
)

await require(
confirm_action(
ctx,
"coinjoin_coordinator",
title="Authorize CoinJoin",
description="Do you really want to take part in a CoinJoin transaction at:\n{}",
description_param=msg.coordinator,
description_param_font=ui.MONO,
icon=ui.ICON_RECOVERY,
)
await confirm_action(
ctx,
"coinjoin_coordinator",
title="Authorize CoinJoin",
description="Do you really want to take part in a CoinJoin transaction at:\n{}",
description_param=msg.coordinator,
description_param_font=ui.MONO,
icon=ui.ICON_RECOVERY,
)

fee_per_anonymity = None
if msg.fee_per_anonymity is not None:
fee_per_anonymity = format_amount(
msg.fee_per_anonymity, FEE_PER_ANONYMITY_DECIMALS
)
await require(
confirm_coinjoin(
ctx,
fee_per_anonymity,
format_coin_amount(msg.max_total_fee, coin, msg.amount_unit),
)
await confirm_coinjoin(
ctx,
fee_per_anonymity,
format_coin_amount(msg.max_total_fee, coin, msg.amount_unit),
)

set_authorization(CoinJoinAuthorization(msg, keychain, coin))
Expand Down
36 changes: 16 additions & 20 deletions core/src/apps/bitcoin/get_ownership_proof.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from trezor import ui, wire
from trezor.messages.GetOwnershipProof import GetOwnershipProof
from trezor.messages.OwnershipProof import OwnershipProof
from trezor.ui.layouts import confirm_action, confirm_hex, require
from trezor.ui.layouts import confirm_action, confirm_hex

from apps.common.paths import validate_path

Expand Down Expand Up @@ -65,27 +65,23 @@ async def get_ownership_proof(
# In order to set the "user confirmation" bit in the proof, the user must actually confirm.
if msg.user_confirmation and not authorization:
if not msg.commitment_data:
await require(
confirm_action(
ctx,
"confirm_ownership_proof",
title="Proof of ownership",
description="Do you want to create a proof of ownership?",
)
await confirm_action(
ctx,
"confirm_ownership_proof",
title="Proof of ownership",
description="Do you want to create a proof of ownership?",
)
else:
await require(
confirm_hex(
ctx,
"confirm_ownership_proof",
title="Proof of ownership",
description="Do you want to create a proof of ownership for:",
data=hexlify(msg.commitment_data).decode(),
icon=ui.ICON_CONFIG,
icon_color=ui.ORANGE_ICON,
truncate=True,
truncate_middle=True,
)
await confirm_hex(
ctx,
"confirm_ownership_proof",
title="Proof of ownership",
description="Do you want to create a proof of ownership for:",
data=hexlify(msg.commitment_data).decode(),
icon=ui.ICON_CONFIG,
icon_color=ui.ORANGE_ICON,
truncate=True,
truncate_middle=True,
)

ownership_proof, signature = generate_proof(
Expand Down
4 changes: 2 additions & 2 deletions core/src/apps/bitcoin/get_public_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from trezor.messages import InputScriptType
from trezor.messages.HDNodeType import HDNodeType
from trezor.messages.PublicKey import PublicKey
from trezor.ui.layouts import require, show_xpub
from trezor.ui.layouts import show_xpub

from apps.common import coins, paths
from apps.common.keychain import get_keychain
Expand Down Expand Up @@ -59,7 +59,7 @@ async def get_public_key(ctx: wire.Context, msg: GetPublicKey) -> PublicKey:
)

if msg.show_display:
await require(show_xpub(ctx, node_xpub, "XPUB", "Cancel"))
await show_xpub(ctx, node_xpub, "XPUB", "Cancel")

return PublicKey(
node=node_type,
Expand Down
4 changes: 2 additions & 2 deletions core/src/apps/bitcoin/sign_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from trezor.crypto.curve import secp256k1
from trezor.messages.InputScriptType import SPENDADDRESS, SPENDP2SHWITNESS, SPENDWITNESS
from trezor.messages.MessageSignature import MessageSignature
from trezor.ui.layouts import confirm_signverify, require
from trezor.ui.layouts import confirm_signverify

from apps.common.paths import validate_path
from apps.common.signverify import decode_message, message_digest
Expand All @@ -26,7 +26,7 @@ async def sign_message(
script_type = msg.script_type or 0

await validate_path(ctx, keychain, address_n)
await require(confirm_signverify(ctx, coin.coin_shortcut, decode_message(message)))
await confirm_signverify(ctx, coin.coin_shortcut, decode_message(message))

node = keychain.derive(address_n)
seckey = node.private_key()
Expand Down
93 changes: 39 additions & 54 deletions core/src/apps/bitcoin/sign_tx/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from trezor.messages import AmountUnit, ButtonRequestType, OutputScriptType
from trezor.strings import format_amount
from trezor.ui import layouts
from trezor.ui.layouts import require

from .. import addresses
from . import omni
Expand Down Expand Up @@ -71,16 +70,14 @@ async def confirm_output(
ctx, address_short, format_coin_amount(output.amount, coin, amount_unit)
)

await require(layout)
await layout


async def confirm_replacement(ctx: wire.Context, description: str, txid: bytes) -> None:
await require(
layouts.confirm_replacement(
ctx,
description,
hexlify(txid).decode(),
)
await layouts.confirm_replacement(
ctx,
description,
hexlify(txid).decode(),
)


Expand All @@ -91,13 +88,11 @@ async def confirm_modify_fee(
coin: CoinInfo,
amount_unit: EnumTypeAmountUnit,
) -> None:
await require(
layouts.confirm_modify_fee(
ctx,
user_fee_change,
format_coin_amount(abs(user_fee_change), coin, amount_unit),
format_coin_amount(total_fee_new, coin, amount_unit),
)
await layouts.confirm_modify_fee(
ctx,
user_fee_change,
format_coin_amount(abs(user_fee_change), coin, amount_unit),
format_coin_amount(total_fee_new, coin, amount_unit),
)


Expand All @@ -108,12 +103,10 @@ async def confirm_joint_total(
coin: CoinInfo,
amount_unit: EnumTypeAmountUnit,
) -> None:
await require(
layouts.confirm_joint_total(
ctx,
spending_amount=format_coin_amount(spending, coin, amount_unit),
total_amount=format_coin_amount(total, coin, amount_unit),
),
await layouts.confirm_joint_total(
ctx,
spending_amount=format_coin_amount(spending, coin, amount_unit),
total_amount=format_coin_amount(total, coin, amount_unit),
)


Expand All @@ -124,43 +117,37 @@ async def confirm_total(
coin: CoinInfo,
amount_unit: EnumTypeAmountUnit,
) -> None:
await require(
layouts.confirm_total(
ctx,
total_amount=format_coin_amount(spending, coin, amount_unit),
fee_amount=format_coin_amount(fee, coin, amount_unit),
),
await layouts.confirm_total(
ctx,
total_amount=format_coin_amount(spending, coin, amount_unit),
fee_amount=format_coin_amount(fee, coin, amount_unit),
)


async def confirm_feeoverthreshold(
ctx: wire.Context, fee: int, coin: CoinInfo, amount_unit: EnumTypeAmountUnit
) -> None:
fee_amount = format_coin_amount(fee, coin, amount_unit)
await require(
layouts.confirm_metadata(
ctx,
"fee_over_threshold",
"High fee",
"The fee of\n{}is unexpectedly high.",
fee_amount,
ButtonRequestType.FeeOverThreshold,
)
await layouts.confirm_metadata(
ctx,
"fee_over_threshold",
"High fee",
"The fee of\n{}is unexpectedly high.",
fee_amount,
ButtonRequestType.FeeOverThreshold,
)


async def confirm_change_count_over_threshold(
ctx: wire.Context, change_count: int
) -> None:
await require(
layouts.confirm_metadata(
ctx,
"change_count_over_threshold",
"Warning",
"There are {}\nchange-outputs.\n",
str(change_count),
ButtonRequestType.SignTx,
)
await layouts.confirm_metadata(
ctx,
"change_count_over_threshold",
"Warning",
"There are {}\nchange-outputs.\n",
str(change_count),
ButtonRequestType.SignTx,
)


Expand All @@ -180,13 +167,11 @@ async def confirm_nondefault_locktime(
text = "Locktime for this\ntransaction is set to\ntimestamp:\n{}"
param = str(lock_time)

await require(
layouts.confirm_metadata(
ctx,
"nondefault_locktime",
title,
text,
param,
br_code=ButtonRequestType.SignTx,
)
await layouts.confirm_metadata(
ctx,
"nondefault_locktime",
title,
text,
param,
br_code=ButtonRequestType.SignTx,
)
14 changes: 6 additions & 8 deletions core/src/apps/bitcoin/verify_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from trezor.crypto.curve import secp256k1
from trezor.messages.InputScriptType import SPENDADDRESS, SPENDP2SHWITNESS, SPENDWITNESS
from trezor.messages.Success import Success
from trezor.ui.layouts import confirm_signverify, require
from trezor.ui.layouts import confirm_signverify

from apps.common import coins
from apps.common.signverify import decode_message, message_digest
Expand Down Expand Up @@ -63,13 +63,11 @@ async def verify_message(ctx: wire.Context, msg: VerifyMessage) -> Success:
if addr != address:
raise wire.ProcessError("Invalid signature")

await require(
confirm_signverify(
ctx,
coin.coin_shortcut,
decode_message(message),
address=address_short(coin, address),
)
await confirm_signverify(
ctx,
coin.coin_shortcut,
decode_message(message),
address=address_short(coin, address),
)

return Success(message="Message verified")
4 changes: 2 additions & 2 deletions core/src/apps/cardano/get_public_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from trezor import log, wire
from trezor.messages.CardanoPublicKey import CardanoPublicKey
from trezor.messages.HDNodeType import HDNodeType
from trezor.ui.layouts import require, show_pubkey
from trezor.ui.layouts import show_pubkey

from apps.common import paths
from apps.common.seed import remove_ed25519_prefix
Expand Down Expand Up @@ -36,7 +36,7 @@ async def get_public_key(
raise wire.ProcessError("Deriving public key failed")

if msg.show_display:
await require(show_pubkey(ctx, hexlify(key.node.public_key).decode()))
await show_pubkey(ctx, hexlify(key.node.public_key).decode())
return key


Expand Down
4 changes: 2 additions & 2 deletions core/src/apps/common/paths.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from trezor.ui.constants import MONO_CHARS_PER_LINE
from trezor.ui.layouts import confirm_path_warning, require
from trezor.ui.layouts import confirm_path_warning

from . import HARDENED
from .layout import address_n_to_str
Expand Down Expand Up @@ -259,7 +259,7 @@ async def validate_path(


async def show_path_warning(ctx: wire.Context, path: Bip32Path) -> None:
await require(confirm_path_warning(ctx, address_n_to_str(path)))
await confirm_path_warning(ctx, address_n_to_str(path))


def is_hardened(i: int) -> bool:
Expand Down
Loading

0 comments on commit 28ec3a8

Please sign in to comment.