1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-07-31 10:58:43 +00:00

refactor(core): turn show_success, show_warning into layouts

This commit is contained in:
Martin Milata 2021-01-05 00:57:19 +01:00
parent f38abf9d89
commit 03699f5639
8 changed files with 135 additions and 114 deletions

View File

@ -86,42 +86,6 @@ def address_n_to_str(address_n: Iterable[int]) -> str:
return "m/" + "/".join([path_item(i) for i in address_n]) return "m/" + "/".join([path_item(i) for i in address_n])
async def show_warning(
ctx: wire.GenericContext,
content: Iterable[str],
subheader: Iterable[str] = [],
button: str = "Try again",
) -> None:
text = Text("Warning", ui.ICON_WRONG, ui.RED)
if subheader:
for row in subheader:
text.bold(row)
text.br_half()
for row in content:
text.normal(row)
await require_confirm(
ctx, text, ButtonRequestType.Warning, confirm=button, cancel=None
)
async def show_success(
ctx: wire.GenericContext,
content: Iterable[str] = [],
subheader: Iterable[str] = [],
button: str = "Continue",
) -> None:
text = Text("Success", ui.ICON_CONFIRM, ui.GREEN)
if subheader:
for row in subheader:
text.bold(row)
text.br_half()
for row in content:
text.normal(row)
await require_confirm(
ctx, text, ButtonRequestType.Success, confirm=button, cancel=None
)
def paginate_text( def paginate_text(
text: str, text: str,
header: str, header: str,

View File

@ -3,9 +3,9 @@ from trezor import config, ui, wire
from trezor.messages.Success import Success from trezor.messages.Success import Success
from trezor.pin import pin_to_int from trezor.pin import pin_to_int
from trezor.ui.components.tt.text import Text from trezor.ui.components.tt.text import Text
from trezor.ui.layouts import require, show_success
from apps.common.confirm import require_confirm from apps.common.confirm import require_confirm
from apps.common.layout import show_success
from apps.common.request_pin import ( from apps.common.request_pin import (
error_pin_invalid, error_pin_invalid,
error_pin_matches_wipe_code, error_pin_matches_wipe_code,
@ -47,16 +47,16 @@ async def change_pin(ctx: wire.Context, msg: ChangePin) -> Success:
if newpin: if newpin:
if curpin: if curpin:
msg_screen = "changed your PIN." msg_screen = "You have successfully changed your PIN."
msg_wire = "PIN changed" msg_wire = "PIN changed"
else: else:
msg_screen = "enabled PIN protection." msg_screen = "You have successfully enabled PIN protection."
msg_wire = "PIN enabled" msg_wire = "PIN enabled"
else: else:
msg_screen = "disabled PIN protection." msg_screen = "You have successfully disabled PIN protection."
msg_wire = "PIN removed" msg_wire = "PIN removed"
await show_success(ctx, ("You have successfully", msg_screen)) await require(show_success(ctx, "success_pin", msg_screen))
return Success(message=msg_wire) return Success(message=msg_wire)

View File

@ -3,10 +3,10 @@ from trezor import config, ui, wire
from trezor.messages.Success import Success from trezor.messages.Success import Success
from trezor.pin import pin_to_int from trezor.pin import pin_to_int
from trezor.ui.components.tt.text import Text from trezor.ui.components.tt.text import Text
from trezor.ui.layouts import require, show_success
from trezor.ui.popup import Popup from trezor.ui.popup import Popup
from apps.common.confirm import require_confirm from apps.common.confirm import require_confirm
from apps.common.layout import show_success
from apps.common.request_pin import ( from apps.common.request_pin import (
error_pin_invalid, error_pin_invalid,
request_pin, request_pin,
@ -44,16 +44,16 @@ async def change_wipe_code(ctx: wire.Context, msg: ChangeWipeCode) -> Success:
if wipe_code: if wipe_code:
if has_wipe_code: if has_wipe_code:
msg_screen = "changed the wipe code." msg_screen = "You have successfully changed the wipe code."
msg_wire = "Wipe code changed" msg_wire = "Wipe code changed"
else: else:
msg_screen = "set the wipe code." msg_screen = "You have successfully set the wipe code."
msg_wire = "Wipe code set" msg_wire = "Wipe code set"
else: else:
msg_screen = "disabled the wipe code." msg_screen = "You have successfully disabled the wipe code."
msg_wire = "Wipe code removed" msg_wire = "Wipe code removed"
await show_success(ctx, ("You have successfully", msg_screen)) await require(show_success(ctx, "success_wipe_code", msg_screen))
return Success(message=msg_wire) return Success(message=msg_wire)

View File

@ -8,9 +8,9 @@ from trezor.crypto.hashlib import sha256
from trezor.errors import MnemonicError from trezor.errors import MnemonicError
from trezor.messages import BackupType from trezor.messages import BackupType
from trezor.messages.Success import Success from trezor.messages.Success import Success
from trezor.ui.layouts import require, show_success
from apps.common import mnemonic from apps.common import mnemonic
from apps.common.layout import show_success
from apps.homescreen.homescreen import homescreen from apps.homescreen.homescreen import homescreen
from .. import backup_types from .. import backup_types
@ -146,7 +146,11 @@ async def _finish_recovery(
storage.recovery.end_progress() storage.recovery.end_progress()
await show_success(ctx, ("You have successfully", "recovered your wallet.")) await require(
show_success(
ctx, "success_recovery", "You have successfully recovered your wallet."
)
)
return Success(message="Device recovered") return Success(message="Device recovered")

View File

@ -5,10 +5,10 @@ from trezor.messages import ButtonRequestType
from trezor.ui.components.tt.scroll import Paginated from trezor.ui.components.tt.scroll import Paginated
from trezor.ui.components.tt.text import Text from trezor.ui.components.tt.text import Text
from trezor.ui.components.tt.word_select import WordSelector from trezor.ui.components.tt.word_select import WordSelector
from trezor.ui.layouts import require, show_success, show_warning
from apps.common import button_request from apps.common import button_request
from apps.common.confirm import confirm, info_confirm, require_confirm from apps.common.confirm import confirm, info_confirm, require_confirm
from apps.common.layout import show_success, show_warning
from .. import backup_types from .. import backup_types
from . import word_validity from . import word_validity
@ -131,36 +131,20 @@ async def show_dry_run_result(
) -> None: ) -> None:
if result: if result:
if is_slip39: if is_slip39:
text = ( text = "The entered recovery\nshares are valid and\nmatch what is currently\nin the device."
"The entered recovery",
"shares are valid and",
"match what is currently",
"in the device.",
)
else: else:
text = ( text = "The entered recovery\nseed is valid and\nmatches the one\nin the device."
"The entered recovery", await require(
"seed is valid and", show_success(ctx, "success_dry_recovery", text, button="Continue")
"matches the one", )
"in the device.",
)
await show_success(ctx, text, button="Continue")
else: else:
if is_slip39: if is_slip39:
text = ( text = "The entered recovery\nshares are valid but\ndo not match what is\ncurrently in the device."
"The entered recovery",
"shares are valid but",
"do not match what is",
"currently in the device.",
)
else: else:
text = ( text = "The entered recovery\nseed is valid but does\nnot match the one\nin the device."
"The entered recovery", await require(
"seed is valid but does", show_warning(ctx, "warning_dry_recovery", text, button="Continue")
"not match the one", )
"in the device.",
)
await show_warning(ctx, text, button="Continue")
async def show_dry_run_different_type(ctx: wire.GenericContext) -> None: async def show_dry_run_different_type(ctx: wire.GenericContext) -> None:
@ -175,32 +159,50 @@ async def show_dry_run_different_type(ctx: wire.GenericContext) -> None:
async def show_invalid_mnemonic(ctx: wire.GenericContext, word_count: int) -> None: async def show_invalid_mnemonic(ctx: wire.GenericContext, word_count: int) -> None:
if backup_types.is_slip39_word_count(word_count): if backup_types.is_slip39_word_count(word_count):
await show_warning(ctx, ("You have entered", "an invalid recovery", "share.")) await require(
show_warning(
ctx,
"warning_invalid_share",
"You have entered\nan invalid recovery\nshare.",
)
)
else: else:
await show_warning(ctx, ("You have entered", "an invalid recovery", "seed.")) await require(
show_warning(
ctx,
"warning_invalid_seed",
"You have entered\nan invalid recovery\nseed.",
)
)
async def show_share_already_added(ctx: wire.GenericContext) -> None: async def show_share_already_added(ctx: wire.GenericContext) -> None:
await show_warning( await require(
ctx, ("Share already entered,", "please enter", "a different share.") show_warning(
ctx,
"warning_known_share",
"Share already entered,\nplease enter\na different share.",
)
) )
async def show_identifier_mismatch(ctx: wire.GenericContext) -> None: async def show_identifier_mismatch(ctx: wire.GenericContext) -> None:
await show_warning( await require(
ctx, ("You have entered", "a share from another", "Shamir Backup.") show_warning(
ctx,
"warning_mismatched_share",
"You have entered\na share from another\nShamir Backup.",
)
) )
async def show_group_threshold_reached(ctx: wire.GenericContext) -> None: async def show_group_threshold_reached(ctx: wire.GenericContext) -> None:
await show_warning( await require(
ctx, show_warning(
( ctx,
"Threshold of this", "warning_group_threshold",
"group has been reached.", "Threshold of this\ngroup has been reached.\nInput share from\ndifferent group.",
"Input share from", )
"different group",
),
) )

View File

@ -9,9 +9,9 @@ from trezor.ui.components.tt.info import InfoConfirm
from trezor.ui.components.tt.num_input import NumInput from trezor.ui.components.tt.num_input import NumInput
from trezor.ui.components.tt.scroll import Paginated from trezor.ui.components.tt.scroll import Paginated
from trezor.ui.components.tt.text import Text from trezor.ui.components.tt.text import Text
from trezor.ui.layouts import require, show_success
from apps.common.confirm import confirm, require_confirm, require_hold_to_confirm from apps.common.confirm import confirm, require_confirm, require_hold_to_confirm
from apps.common.layout import show_success
if False: if False:
from trezor import loop from trezor import loop
@ -155,36 +155,33 @@ async def _show_confirmation_success(
ctx, share_index=None, num_of_shares=None, group_index=None ctx, share_index=None, num_of_shares=None, group_index=None
): ):
if share_index is None: # it is a BIP39 backup if share_index is None: # it is a BIP39 backup
subheader = ("You have finished", "verifying your", "recovery seed.") subheader = "You have finished\nverifying your\nrecovery seed."
text = [] text = ""
elif share_index == num_of_shares - 1: elif share_index == num_of_shares - 1:
if group_index is None: if group_index is None:
subheader = ("You have finished", "verifying your", "recovery shares.") subheader = "You have finished\nverifying your\nrecovery shares."
else: else:
subheader = ( subheader = (
"You have finished", "You have finished\nverifying your\nrecovery shares\nfor group %s."
"verifying your", % (group_index + 1)
"recovery shares",
"for group %s." % (group_index + 1),
) )
text = [] text = ""
else: else:
if group_index is None: if group_index is None:
subheader = ( subheader = "Recovery share #%s\nchecked successfully." % (share_index + 1)
"Recovery share #%s" % (share_index + 1), text = "Continue with share #%s." % (share_index + 2)
"checked successfully.",
)
text = ["Continue with share #%s." % (share_index + 2)]
else: else:
subheader = ( subheader = "Group %s - Share %s\nchecked successfully." % (
"Group %s - Share %s" % ((group_index + 1), (share_index + 1)), (group_index + 1),
"checked successfully.", (share_index + 1),
) )
text = ("Continue with the next ", "share.") text = "Continue with the next\nshare."
return await show_success(ctx, text, subheader=subheader) return await require(
show_success(ctx, "success_recovery", text, subheader=subheader)
)
async def _show_confirmation_failure(ctx, share_index): async def _show_confirmation_failure(ctx, share_index):
@ -221,8 +218,10 @@ async def show_backup_warning(ctx, slip39=False):
async def show_backup_success(ctx): async def show_backup_success(ctx):
text = ("Use your backup", "when you need to", "recover your wallet.") text = "Use your backup\nwhen you need to\nrecover your wallet."
await show_success(ctx, text, subheader=["Your backup is done."]) await require(
show_success(ctx, "success_backup", text, subheader="Your backup is done.")
)
# BIP39 # BIP39

View File

@ -6,9 +6,9 @@ from trezor.messages import SdProtectOperationType
from trezor.messages.Success import Success from trezor.messages.Success import Success
from trezor.pin import pin_to_int from trezor.pin import pin_to_int
from trezor.ui.components.tt.text import Text from trezor.ui.components.tt.text import Text
from trezor.ui.layouts import require, show_success
from apps.common.confirm import require_confirm from apps.common.confirm import require_confirm
from apps.common.layout import show_success
from apps.common.request_pin import ( from apps.common.request_pin import (
error_pin_invalid, error_pin_invalid,
request_pin, request_pin,
@ -87,7 +87,9 @@ async def sd_protect_enable(ctx: wire.Context, msg: SdProtect) -> Success:
storage.device.set_sd_salt_auth_key(salt_auth_key) storage.device.set_sd_salt_auth_key(salt_auth_key)
await show_success(ctx, ("You have successfully", "enabled SD protection.")) await require(
show_success(ctx, "success_sd", "You have successfully enabled SD protection.")
)
return Success(message="SD card protection enabled") return Success(message="SD card protection enabled")
@ -119,7 +121,9 @@ async def sd_protect_disable(ctx: wire.Context, msg: SdProtect) -> Success:
# because overall SD-protection was successfully disabled. # because overall SD-protection was successfully disabled.
pass pass
await show_success(ctx, ("You have successfully", "disabled SD protection.")) await require(
show_success(ctx, "success_sd", "You have successfully disabled SD protection.")
)
return Success(message="SD card protection disabled") return Success(message="SD card protection disabled")
@ -154,7 +158,11 @@ async def sd_protect_refresh(ctx: wire.Context, msg: SdProtect) -> Success:
# SD-protection was successfully refreshed. # SD-protection was successfully refreshed.
pass pass
await show_success(ctx, ("You have successfully", "refreshed SD protection.")) await require(
show_success(
ctx, "success_sd", "You have successfully refreshed SD protection."
)
)
return Success(message="SD card protection refreshed") return Success(message="SD card protection refreshed")

View File

@ -39,6 +39,8 @@ __all__ = (
"confirm_backup", "confirm_backup",
"confirm_path_warning", "confirm_path_warning",
"show_address", "show_address",
"show_success",
"show_warning",
"confirm_output", "confirm_output",
"confirm_hex", "confirm_hex",
"confirm_total", "confirm_total",
@ -250,6 +252,48 @@ async def show_address(
return return
def show_warning(
ctx: wire.GenericContext,
br_type: str,
content: str,
subheader: Optional[str] = None,
button: str = "Try again",
) -> LayoutType:
text = Text("Warning", ui.ICON_WRONG, ui.RED, new_lines=False)
if subheader:
text.bold(subheader)
text.br()
text.br_half()
text.normal(content)
return interact(
ctx,
Confirm(text, confirm=button, cancel=None),
br_type,
ButtonRequestType.Warning,
)
def show_success(
ctx: wire.GenericContext,
br_type: str,
content: str,
subheader: Optional[str] = None,
button: str = "Continue",
) -> LayoutType:
text = Text("Success", ui.ICON_CONFIRM, ui.GREEN, new_lines=False)
if subheader:
text.bold(subheader)
text.br()
text.br_half()
text.normal(content)
return interact(
ctx,
Confirm(text, confirm=button, cancel=None),
br_type,
ButtonRequestType.Success,
)
def confirm_output( def confirm_output(
ctx: wire.GenericContext, ctx: wire.GenericContext,
address: str, address: str,