1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-05-21 16:28:46 +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,24 +198,25 @@ 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(
ctx, interact(
_RustLayout( ctx,
trezorui2.confirm_action( _RustLayout(
title=title.upper(), trezorui2.confirm_action(
action=action, title=title.upper(),
description=description, action=action,
verb=verb, description=description,
verb_cancel=verb_cancel, verb=verb,
hold=hold, verb_cancel=verb_cancel,
reverse=reverse, hold=hold,
) reverse=reverse,
)
),
br_type,
br_code,
), ),
br_type, exc,
br_code,
) )
if result is not trezorui2.CONFIRMED:
raise exc
async def confirm_reset_device( async def confirm_reset_device(
@ -217,19 +227,21 @@ async def confirm_reset_device(
else: else:
title = "CREATE NEW WALLET" title = "CREATE NEW WALLET"
result = await interact( await raise_if_not_confirmed(
ctx, interact(
_RustLayout( ctx,
trezorui2.confirm_reset_device( _RustLayout(
title=title.upper(), trezorui2.confirm_reset_device(
prompt=prompt.replace("\n", " "), title=title.upper(),
) prompt=prompt.replace("\n", " "),
), )
"recover_device" if recovery else "setup_device", ),
ButtonRequestType.ProtectCall if recovery else ButtonRequestType.ResetDevice, "recover_device" if recovery else "setup_device",
ButtonRequestType.ProtectCall
if recovery
else ButtonRequestType.ResetDevice,
)
) )
if result is not trezorui2.CONFIRMED:
raise wire.ActionCancelled
# TODO cleanup @ redesign # TODO cleanup @ redesign
@ -271,19 +283,19 @@ 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(
ctx, interact(
_RustLayout( ctx,
trezorui2.show_warning( _RustLayout(
title="Unknown path", trezorui2.show_warning(
description=path, title="Unknown path",
) description=path,
), )
"path_warning", ),
ButtonRequestType.UnknownDerivationPath, "path_warning",
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(
ctx, interact(
_show_xpub(xpub, title, cancel), ctx,
"show_xpub", _show_xpub(xpub, title, cancel),
ButtonRequestType.PublicKey, "show_xpub",
ButtonRequestType.PublicKey,
)
) )
if result is not trezorui2.CONFIRMED:
raise wire.ActionCancelled
async def show_address( async def show_address(
@ -423,21 +435,21 @@ 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(
ctx, interact(
_RustLayout( ctx,
trezorui2.show_warning( _RustLayout(
title=content.replace("\n", " "), trezorui2.show_warning(
description=subheader or "", title=content.replace("\n", " "),
button=button.upper(), description=subheader or "",
allow_cancel=False, button=button.upper(),
) allow_cancel=False,
), )
br_type, ),
br_code, br_type,
br_code,
)
) )
if result is not trezorui2.CONFIRMED:
raise wire.ActionCancelled
async def show_success( async def show_success(
@ -447,21 +459,21 @@ 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(
ctx, interact(
_RustLayout( ctx,
trezorui2.show_success( _RustLayout(
title=content.replace("\n", " "), trezorui2.show_success(
description=subheader or "", title=content.replace("\n", " "),
button=button.upper(), description=subheader or "",
allow_cancel=False, button=button.upper(),
) allow_cancel=False,
), )
br_type, ),
ButtonRequestType.Success, br_type,
ButtonRequestType.Success,
)
) )
if result is not trezorui2.CONFIRMED:
raise wire.ActionCancelled
async def confirm_output( async def confirm_output(
@ -483,35 +495,35 @@ 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(
ctx, interact(
_RustLayout( ctx,
trezorui2.confirm_output( _RustLayout(
title=title, trezorui2.confirm_output(
description="To:", title=title,
value=address, description="To:",
) value=address,
), )
"confirm_output", ),
br_code, "confirm_output",
br_code,
)
) )
if result is not trezorui2.CONFIRMED:
raise wire.ActionCancelled
result = await interact( await raise_if_not_confirmed(
ctx, interact(
_RustLayout( ctx,
trezorui2.confirm_output( _RustLayout(
title=title, trezorui2.confirm_output(
description="Amount:", title=title,
value=amount, description="Amount:",
) value=amount,
), )
"confirm_output", ),
br_code, "confirm_output",
br_code,
)
) )
if result is not trezorui2.CONFIRMED:
raise wire.ActionCancelled
async def confirm_payment_request( async def confirm_payment_request(
@ -605,22 +617,22 @@ 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(
ctx, interact(
_RustLayout( ctx,
trezorui2.confirm_blob( _RustLayout(
title=title.upper(), trezorui2.confirm_blob(
description=description or "", title=title.upper(),
data=data, description=description or "",
ask_pagination=ask_pagination, data=data,
hold=hold, ask_pagination=ask_pagination,
) hold=hold,
), )
br_type, ),
br_code, br_type,
br_code,
)
) )
if result is not trezorui2.CONFIRMED:
raise wire.ActionCancelled
def confirm_address( def confirm_address(
@ -706,53 +718,54 @@ 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(
ctx, interact(
_RustLayout( ctx,
trezorui2.confirm_output( _RustLayout(
title=title.upper(), trezorui2.confirm_output(
description="Fee:", title=title.upper(),
value=fee_amount, description="Fee:",
) value=fee_amount,
), )
"confirm_total", ),
br_code, "confirm_total",
br_code,
)
) )
if result is not trezorui2.CONFIRMED:
raise wire.ActionCancelled
result = await interact( await raise_if_not_confirmed(
ctx, interact(
_RustLayout( ctx,
trezorui2.confirm_total( _RustLayout(
title=title.upper(), trezorui2.confirm_total(
description="Total amount:", title=title.upper(),
value=total_amount, description="Total amount:",
) value=total_amount,
), )
"confirm_total", ),
br_code, "confirm_total",
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(
ctx, await raise_if_not_confirmed(
_RustLayout( interact(
trezorui2.confirm_joint_total( ctx,
spending_amount=spending_amount, _RustLayout(
total_amount=total_amount, trezorui2.confirm_joint_total(
) spending_amount=spending_amount,
), total_amount=total_amount,
"confirm_joint_total", )
ButtonRequestType.SignTx, ),
"confirm_joint_total",
ButtonRequestType.SignTx,
)
) )
if result is not trezorui2.CONFIRMED:
raise wire.ActionCancelled
async def confirm_metadata( async def confirm_metadata(
@ -794,34 +807,33 @@ async def confirm_metadata(
hold=hold, hold=hold,
) )
result = await interact( await raise_if_not_confirmed(
ctx, interact(
_RustLayout(layout), ctx,
br_type, _RustLayout(layout),
br_code, br_type,
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(
ctx, interact(
_RustLayout( ctx,
trezorui2.confirm_blob( _RustLayout(
title=description.upper(), trezorui2.confirm_blob(
description="Confirm transaction ID:", title=description.upper(),
data=txid, description="Confirm transaction ID:",
) data=txid,
), )
"confirm_replacement", ),
ButtonRequestType.SignTx, "confirm_replacement",
ButtonRequestType.SignTx,
)
) )
if result is not trezorui2.CONFIRMED:
raise wire.ActionCancelled
async def confirm_modify_output( async def confirm_modify_output(
@ -831,21 +843,21 @@ 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(
ctx, interact(
_RustLayout( ctx,
trezorui2.confirm_modify_output( _RustLayout(
address=address, trezorui2.confirm_modify_output(
sign=sign, address=address,
amount_change=amount_change, sign=sign,
amount_new=amount_new, amount_change=amount_change,
) amount_new=amount_new,
), )
"modify_output", ),
ButtonRequestType.ConfirmOutput, "modify_output",
ButtonRequestType.ConfirmOutput,
)
) )
if result is not trezorui2.CONFIRMED:
raise wire.ActionCancelled
async def confirm_modify_fee( async def confirm_modify_fee(
@ -855,39 +867,39 @@ 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(
ctx, interact(
_RustLayout( ctx,
trezorui2.confirm_modify_fee( _RustLayout(
sign=sign, trezorui2.confirm_modify_fee(
user_fee_change=user_fee_change, sign=sign,
total_fee_new=total_fee_new, user_fee_change=user_fee_change,
) total_fee_new=total_fee_new,
), )
"modify_fee", ),
ButtonRequestType.SignTx, "modify_fee",
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(
ctx, interact(
_RustLayout( ctx,
trezorui2.confirm_coinjoin( _RustLayout(
coin_name=coin_name, trezorui2.confirm_coinjoin(
max_rounds=str(max_rounds), coin_name=coin_name,
max_feerate=f"{max_fee_per_vbyte} sats/vbyte", max_rounds=str(max_rounds),
) max_feerate=f"{max_fee_per_vbyte} sats/vbyte",
), )
"coinjoin_final", ),
ButtonRequestType.Other, "coinjoin_final",
ButtonRequestType.Other,
)
) )
if result is not trezorui2.CONFIRMED:
raise wire.ActionCancelled
# TODO cleanup @ redesign # TODO cleanup @ redesign
@ -907,35 +919,35 @@ 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(
ctx, interact(
_RustLayout( ctx,
trezorui2.confirm_blob( _RustLayout(
title=title, trezorui2.confirm_blob(
description="Confirm address:", title=title,
data=address, description="Confirm address:",
) data=address,
), )
br_type, ),
ButtonRequestType.Other, br_type,
ButtonRequestType.Other,
)
) )
if result is not trezorui2.CONFIRMED:
raise wire.ActionCancelled
result = await interact( await raise_if_not_confirmed(
ctx, interact(
_RustLayout( ctx,
trezorui2.confirm_blob( _RustLayout(
title=title, trezorui2.confirm_blob(
description="Confirm message:", title=title,
data=message, description="Confirm message:",
) data=message,
), )
br_type, ),
ButtonRequestType.Other, br_type,
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,