1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-01-03 03:50:58 +00:00

chore(core/ui): unify UI2 layouts that raise when not confirmed

[no changelog]
This commit is contained in:
grdddj 2022-09-12 15:38:18 +02:00 committed by Martin Milata
parent 23a0a37a45
commit be93dad604

View File

@ -9,14 +9,16 @@ import trezorui2
from ..common import button_request, interact from ..common import button_request, interact
if TYPE_CHECKING: if TYPE_CHECKING:
from typing import Any, Awaitable, Iterable, NoReturn, Sequence from typing import Any, Awaitable, Iterable, NoReturn, Sequence, TypeVar
from ..common import PropertyType, ExceptionType from ..common import PropertyType, ExceptionType
T = TypeVar("T")
class _RustLayout(ui.Layout): class _RustLayout(ui.Layout):
# pylint: disable=super-init-not-called # pylint: disable=super-init-not-called
def __init__(self, layout: Any, is_backup=False): def __init__(self, layout: Any, is_backup: bool = False):
self.layout = layout self.layout = layout
self.timer = loop.Timer() self.timer = loop.Timer()
self.layout.attach_timer_fn(self.set_timer) self.layout.attach_timer_fn(self.set_timer)
@ -46,9 +48,9 @@ class _RustLayout(ui.Layout):
) )
def read_content(self) -> list[str]: def read_content(self) -> list[str]:
result = [] result: list[str] = []
def callback(*args): def callback(*args: Any) -> None:
for arg in args: for arg in args:
result.append(str(arg)) result.append(str(arg))
@ -97,7 +99,7 @@ class _RustLayout(ui.Layout):
end = ">" end = ">"
start_pos = content.index(start) start_pos = content.index(start)
end_pos = content.index(end, start_pos) end_pos = content.index(end, start_pos)
words = [] words: list[str] = []
for line in content[start_pos + len(start) : end_pos].split("\n"): for line in content[start_pos + len(start) : end_pos].split("\n"):
line = line.strip() line = line.strip()
if not line: if not line:
@ -158,6 +160,13 @@ class _RustLayout(ui.Layout):
return self.layout.page_count() return self.layout.page_count()
async def raise_if_not_confirmed(a: Awaitable[T], exc: Any = wire.ActionCancelled) -> T:
result = await a
if result is not trezorui2.CONFIRMED:
raise exc
return result
async def confirm_action( async def confirm_action(
ctx: wire.GenericContext, ctx: wire.GenericContext,
br_type: str, br_type: str,
@ -189,7 +198,8 @@ async def confirm_action(
log.error(__name__, "confirm_action description_param_font not implemented") log.error(__name__, "confirm_action description_param_font not implemented")
description = description.format(description_param) description = description.format(description_param)
result = await interact( await raise_if_not_confirmed(
interact(
ctx, ctx,
_RustLayout( _RustLayout(
trezorui2.confirm_action( trezorui2.confirm_action(
@ -204,9 +214,9 @@ async def confirm_action(
), ),
br_type, br_type,
br_code, br_code,
),
exc,
) )
if result is not trezorui2.CONFIRMED:
raise exc
async def confirm_reset_device( async def confirm_reset_device(
@ -217,7 +227,8 @@ async def confirm_reset_device(
else: else:
title = "CREATE NEW WALLET" title = "CREATE NEW WALLET"
result = await interact( await raise_if_not_confirmed(
interact(
ctx, ctx,
_RustLayout( _RustLayout(
trezorui2.confirm_reset_device( trezorui2.confirm_reset_device(
@ -226,10 +237,11 @@ async def confirm_reset_device(
) )
), ),
"recover_device" if recovery else "setup_device", "recover_device" if recovery else "setup_device",
ButtonRequestType.ProtectCall if recovery else ButtonRequestType.ResetDevice, ButtonRequestType.ProtectCall
if recovery
else ButtonRequestType.ResetDevice,
)
) )
if result is not trezorui2.CONFIRMED:
raise wire.ActionCancelled
# TODO cleanup @ redesign # TODO cleanup @ redesign
@ -271,7 +283,8 @@ async def confirm_backup(ctx: wire.GenericContext) -> bool:
async def confirm_path_warning( async def confirm_path_warning(
ctx: wire.GenericContext, path: str, path_type: str = "Path" ctx: wire.GenericContext, path: str, path_type: str = "Path"
) -> None: ) -> None:
result = await interact( await raise_if_not_confirmed(
interact(
ctx, ctx,
_RustLayout( _RustLayout(
trezorui2.show_warning( trezorui2.show_warning(
@ -282,8 +295,7 @@ async def confirm_path_warning(
"path_warning", "path_warning",
ButtonRequestType.UnknownDerivationPath, ButtonRequestType.UnknownDerivationPath,
) )
if result is not trezorui2.CONFIRMED: )
raise wire.ActionCancelled
def _show_xpub(xpub: str, title: str, cancel: str) -> ui.Layout: def _show_xpub(xpub: str, title: str, cancel: str) -> ui.Layout:
@ -300,14 +312,14 @@ def _show_xpub(xpub: str, title: str, cancel: str) -> ui.Layout:
async def show_xpub( async def show_xpub(
ctx: wire.GenericContext, xpub: str, title: str, cancel: str ctx: wire.GenericContext, xpub: str, title: str, cancel: str
) -> None: ) -> None:
result = await interact( await raise_if_not_confirmed(
interact(
ctx, ctx,
_show_xpub(xpub, title, cancel), _show_xpub(xpub, title, cancel),
"show_xpub", "show_xpub",
ButtonRequestType.PublicKey, ButtonRequestType.PublicKey,
) )
if result is not trezorui2.CONFIRMED: )
raise wire.ActionCancelled
async def show_address( async def show_address(
@ -423,7 +435,8 @@ async def show_warning(
icon: str = ui.ICON_WRONG, icon: str = ui.ICON_WRONG,
icon_color: int = ui.RED, icon_color: int = ui.RED,
) -> None: ) -> None:
result = await interact( await raise_if_not_confirmed(
interact(
ctx, ctx,
_RustLayout( _RustLayout(
trezorui2.show_warning( trezorui2.show_warning(
@ -436,8 +449,7 @@ async def show_warning(
br_type, br_type,
br_code, br_code,
) )
if result is not trezorui2.CONFIRMED: )
raise wire.ActionCancelled
async def show_success( async def show_success(
@ -447,7 +459,8 @@ async def show_success(
subheader: str | None = None, subheader: str | None = None,
button: str = "CONTINUE", button: str = "CONTINUE",
) -> None: ) -> None:
result = await interact( await raise_if_not_confirmed(
interact(
ctx, ctx,
_RustLayout( _RustLayout(
trezorui2.show_success( trezorui2.show_success(
@ -460,8 +473,7 @@ async def show_success(
br_type, br_type,
ButtonRequestType.Success, ButtonRequestType.Success,
) )
if result is not trezorui2.CONFIRMED: )
raise wire.ActionCancelled
async def confirm_output( async def confirm_output(
@ -483,7 +495,8 @@ async def confirm_output(
if title.startswith("CONFIRM "): if title.startswith("CONFIRM "):
title = title[len("CONFIRM ") :] title = title[len("CONFIRM ") :]
result = await interact( await raise_if_not_confirmed(
interact(
ctx, ctx,
_RustLayout( _RustLayout(
trezorui2.confirm_output( trezorui2.confirm_output(
@ -495,10 +508,10 @@ async def confirm_output(
"confirm_output", "confirm_output",
br_code, br_code,
) )
if result is not trezorui2.CONFIRMED: )
raise wire.ActionCancelled
result = await interact( await raise_if_not_confirmed(
interact(
ctx, ctx,
_RustLayout( _RustLayout(
trezorui2.confirm_output( trezorui2.confirm_output(
@ -510,8 +523,7 @@ async def confirm_output(
"confirm_output", "confirm_output",
br_code, br_code,
) )
if result is not trezorui2.CONFIRMED: )
raise wire.ActionCancelled
async def confirm_payment_request( async def confirm_payment_request(
@ -605,7 +617,8 @@ async def confirm_blob(
if isinstance(data, bytes): if isinstance(data, bytes):
data = hexlify(data).decode() data = hexlify(data).decode()
result = await interact( await raise_if_not_confirmed(
interact(
ctx, ctx,
_RustLayout( _RustLayout(
trezorui2.confirm_blob( trezorui2.confirm_blob(
@ -619,8 +632,7 @@ async def confirm_blob(
br_type, br_type,
br_code, br_code,
) )
if result is not trezorui2.CONFIRMED: )
raise wire.ActionCancelled
def confirm_address( def confirm_address(
@ -706,7 +718,8 @@ async def confirm_total(
br_type: str = "confirm_total", br_type: str = "confirm_total",
br_code: ButtonRequestType = ButtonRequestType.SignTx, br_code: ButtonRequestType = ButtonRequestType.SignTx,
) -> None: ) -> None:
result = await interact( await raise_if_not_confirmed(
interact(
ctx, ctx,
_RustLayout( _RustLayout(
trezorui2.confirm_output( trezorui2.confirm_output(
@ -718,10 +731,10 @@ async def confirm_total(
"confirm_total", "confirm_total",
br_code, br_code,
) )
if result is not trezorui2.CONFIRMED: )
raise wire.ActionCancelled
result = await interact( await raise_if_not_confirmed(
interact(
ctx, ctx,
_RustLayout( _RustLayout(
trezorui2.confirm_total( trezorui2.confirm_total(
@ -733,14 +746,15 @@ async def confirm_total(
"confirm_total", "confirm_total",
br_code, br_code,
) )
if result is not trezorui2.CONFIRMED: )
raise wire.ActionCancelled
async def confirm_joint_total( async def confirm_joint_total(
ctx: wire.GenericContext, spending_amount: str, total_amount: str ctx: wire.GenericContext, spending_amount: str, total_amount: str
) -> None: ) -> None:
result = await interact(
await raise_if_not_confirmed(
interact(
ctx, ctx,
_RustLayout( _RustLayout(
trezorui2.confirm_joint_total( trezorui2.confirm_joint_total(
@ -751,8 +765,7 @@ async def confirm_joint_total(
"confirm_joint_total", "confirm_joint_total",
ButtonRequestType.SignTx, ButtonRequestType.SignTx,
) )
if result is not trezorui2.CONFIRMED: )
raise wire.ActionCancelled
async def confirm_metadata( async def confirm_metadata(
@ -794,21 +807,21 @@ async def confirm_metadata(
hold=hold, hold=hold,
) )
result = await interact( await raise_if_not_confirmed(
interact(
ctx, ctx,
_RustLayout(layout), _RustLayout(layout),
br_type, br_type,
br_code, br_code,
) )
)
if result is not trezorui2.CONFIRMED:
raise wire.ActionCancelled
async def confirm_replacement( async def confirm_replacement(
ctx: wire.GenericContext, description: str, txid: str ctx: wire.GenericContext, description: str, txid: str
) -> None: ) -> None:
result = await interact( await raise_if_not_confirmed(
interact(
ctx, ctx,
_RustLayout( _RustLayout(
trezorui2.confirm_blob( trezorui2.confirm_blob(
@ -820,8 +833,7 @@ async def confirm_replacement(
"confirm_replacement", "confirm_replacement",
ButtonRequestType.SignTx, ButtonRequestType.SignTx,
) )
if result is not trezorui2.CONFIRMED: )
raise wire.ActionCancelled
async def confirm_modify_output( async def confirm_modify_output(
@ -831,7 +843,8 @@ async def confirm_modify_output(
amount_change: str, amount_change: str,
amount_new: str, amount_new: str,
) -> None: ) -> None:
result = await interact( await raise_if_not_confirmed(
interact(
ctx, ctx,
_RustLayout( _RustLayout(
trezorui2.confirm_modify_output( trezorui2.confirm_modify_output(
@ -844,8 +857,7 @@ async def confirm_modify_output(
"modify_output", "modify_output",
ButtonRequestType.ConfirmOutput, ButtonRequestType.ConfirmOutput,
) )
if result is not trezorui2.CONFIRMED: )
raise wire.ActionCancelled
async def confirm_modify_fee( async def confirm_modify_fee(
@ -855,7 +867,8 @@ async def confirm_modify_fee(
total_fee_new: str, total_fee_new: str,
fee_rate_amount: str | None = None, fee_rate_amount: str | None = None,
) -> None: ) -> None:
result = await interact( await raise_if_not_confirmed(
interact(
ctx, ctx,
_RustLayout( _RustLayout(
trezorui2.confirm_modify_fee( trezorui2.confirm_modify_fee(
@ -867,14 +880,14 @@ async def confirm_modify_fee(
"modify_fee", "modify_fee",
ButtonRequestType.SignTx, ButtonRequestType.SignTx,
) )
if result is not trezorui2.CONFIRMED: )
raise wire.ActionCancelled
async def confirm_coinjoin( async def confirm_coinjoin(
ctx: wire.GenericContext, coin_name: str, max_rounds: int, max_fee_per_vbyte: str ctx: wire.GenericContext, coin_name: str, max_rounds: int, max_fee_per_vbyte: str
) -> None: ) -> None:
result = await interact( await raise_if_not_confirmed(
interact(
ctx, ctx,
_RustLayout( _RustLayout(
trezorui2.confirm_coinjoin( trezorui2.confirm_coinjoin(
@ -886,8 +899,7 @@ async def confirm_coinjoin(
"coinjoin_final", "coinjoin_final",
ButtonRequestType.Other, ButtonRequestType.Other,
) )
if result is not trezorui2.CONFIRMED: )
raise wire.ActionCancelled
# TODO cleanup @ redesign # TODO cleanup @ redesign
@ -907,7 +919,8 @@ async def confirm_signverify(
title = f"SIGN {coin} MESSAGE" title = f"SIGN {coin} MESSAGE"
br_type = "sign_message" br_type = "sign_message"
result = await interact( await raise_if_not_confirmed(
interact(
ctx, ctx,
_RustLayout( _RustLayout(
trezorui2.confirm_blob( trezorui2.confirm_blob(
@ -919,10 +932,10 @@ async def confirm_signverify(
br_type, br_type,
ButtonRequestType.Other, ButtonRequestType.Other,
) )
if result is not trezorui2.CONFIRMED: )
raise wire.ActionCancelled
result = await interact( await raise_if_not_confirmed(
interact(
ctx, ctx,
_RustLayout( _RustLayout(
trezorui2.confirm_blob( trezorui2.confirm_blob(
@ -934,8 +947,7 @@ async def confirm_signverify(
br_type, br_type,
ButtonRequestType.Other, ButtonRequestType.Other,
) )
if result is not trezorui2.CONFIRMED: )
raise wire.ActionCancelled
async def show_popup( async def show_popup(
@ -946,7 +958,7 @@ async def show_popup(
timeout_ms: int = 3000, timeout_ms: int = 3000,
) -> None: ) -> None:
if subtitle: if subtitle:
title += f"\n{subtitle}".format(subtitle) title += f"\n{subtitle}"
await _RustLayout( await _RustLayout(
trezorui2.show_error( trezorui2.show_error(
title=title, title=title,