2023-05-04 12:16:40 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2024-11-14 14:11:03 +00:00
|
|
|
import typing as t
|
2023-05-04 12:16:40 +00:00
|
|
|
from enum import Enum
|
|
|
|
|
2024-09-02 11:01:16 +00:00
|
|
|
from trezorlib.debuglink import LayoutType
|
2024-03-09 21:05:05 +00:00
|
|
|
|
2023-05-04 12:16:40 +00:00
|
|
|
from .. import buttons
|
2024-01-29 13:38:02 +00:00
|
|
|
from .. import translations as TR
|
2023-05-04 12:16:40 +00:00
|
|
|
|
2024-11-14 14:11:03 +00:00
|
|
|
if t.TYPE_CHECKING:
|
2023-05-04 12:16:40 +00:00
|
|
|
from trezorlib.debuglink import DebugLink, LayoutContent
|
|
|
|
|
2024-11-14 14:11:03 +00:00
|
|
|
AllActionsType = t.List[t.Union[str, t.Tuple[str, ...]]]
|
|
|
|
|
2023-05-04 12:16:40 +00:00
|
|
|
|
|
|
|
# Passphrases and addresses for both models
|
|
|
|
class CommonPass:
|
|
|
|
RANDOM_25 = "Y@14lw%p)JN@f54MYvys@zj'g"
|
|
|
|
RANDOM_25_ADDRESS = "mnkoxeaMzLgfCxUdDSZWrGactyJJerQVW6"
|
|
|
|
|
|
|
|
SHORT = "abc123ABC_<>"
|
|
|
|
SHORT_ADDRESS = "mtHHfh6uHtJiACwp7kzJZ97yueT6sEdQiG"
|
|
|
|
|
|
|
|
WITH_SPACE = "abc 123"
|
|
|
|
WITH_SPACE_ADDRESS = "mvqzZUb9NaUc62Buk9WCP4L7hunsXFyamT"
|
|
|
|
|
|
|
|
EMPTY_ADDRESS = "mvbu1Gdy8SUjTenqerxUaZyYjmveZvt33q"
|
|
|
|
|
|
|
|
|
|
|
|
class PassphraseCategory(Enum):
|
|
|
|
MENU = "MENU"
|
|
|
|
DIGITS = "123"
|
|
|
|
LOWERCASE = "abc"
|
|
|
|
UPPERCASE = "ABC"
|
|
|
|
SPECIAL = "#$!"
|
|
|
|
|
|
|
|
|
|
|
|
def get_char_category(char: str) -> PassphraseCategory:
|
|
|
|
"""What is the category of a character"""
|
|
|
|
if char.isdigit():
|
|
|
|
return PassphraseCategory.DIGITS
|
|
|
|
if char.islower():
|
|
|
|
return PassphraseCategory.LOWERCASE
|
|
|
|
if char.isupper():
|
|
|
|
return PassphraseCategory.UPPERCASE
|
|
|
|
return PassphraseCategory.SPECIAL
|
|
|
|
|
|
|
|
|
2023-11-21 12:51:59 +00:00
|
|
|
def go_next(debug: "DebugLink") -> LayoutContent:
|
2024-09-02 11:01:16 +00:00
|
|
|
if debug.layout_type is LayoutType.TT:
|
2023-11-21 15:55:46 +00:00
|
|
|
return debug.click(buttons.OK)
|
2024-09-02 11:01:16 +00:00
|
|
|
elif debug.layout_type is LayoutType.TR:
|
2023-11-21 15:55:46 +00:00
|
|
|
return debug.press_right()
|
2024-09-02 11:01:16 +00:00
|
|
|
elif debug.layout_type is LayoutType.Mercury:
|
2023-11-21 12:51:59 +00:00
|
|
|
return debug.swipe_up()
|
2023-05-12 09:19:35 +00:00
|
|
|
else:
|
|
|
|
raise RuntimeError("Unknown model")
|
2023-05-04 12:16:40 +00:00
|
|
|
|
|
|
|
|
2023-11-21 12:51:59 +00:00
|
|
|
def tap_to_confirm(debug: "DebugLink") -> LayoutContent:
|
2024-09-02 11:01:16 +00:00
|
|
|
if debug.layout_type is LayoutType.TT:
|
2023-11-21 15:55:46 +00:00
|
|
|
return debug.read_layout()
|
2024-09-02 11:01:16 +00:00
|
|
|
elif debug.layout_type is LayoutType.TR:
|
2023-11-21 15:55:46 +00:00
|
|
|
return debug.read_layout()
|
2024-09-02 11:01:16 +00:00
|
|
|
elif debug.layout_type is LayoutType.Mercury:
|
2023-11-21 15:55:46 +00:00
|
|
|
return debug.click(buttons.TAP_TO_CONFIRM)
|
2024-06-05 08:52:50 +00:00
|
|
|
else:
|
|
|
|
raise RuntimeError("Unknown model")
|
|
|
|
|
|
|
|
|
2023-11-21 12:51:59 +00:00
|
|
|
def go_back(debug: "DebugLink", r_middle: bool = False) -> LayoutContent:
|
2024-09-02 11:01:16 +00:00
|
|
|
if debug.layout_type in (LayoutType.TT, LayoutType.Mercury):
|
2023-11-21 15:55:46 +00:00
|
|
|
return debug.click(buttons.CANCEL)
|
2024-09-02 11:01:16 +00:00
|
|
|
elif debug.layout_type is LayoutType.TR:
|
2023-08-23 08:45:59 +00:00
|
|
|
if r_middle:
|
2023-11-21 12:51:59 +00:00
|
|
|
return debug.press_middle()
|
2023-08-23 08:45:59 +00:00
|
|
|
else:
|
2023-11-21 12:51:59 +00:00
|
|
|
return debug.press_left()
|
2023-05-12 09:19:35 +00:00
|
|
|
else:
|
|
|
|
raise RuntimeError("Unknown model")
|
|
|
|
|
|
|
|
|
|
|
|
def navigate_to_action_and_press(
|
|
|
|
debug: "DebugLink",
|
|
|
|
wanted_action: str,
|
2024-11-14 14:11:03 +00:00
|
|
|
all_actions: AllActionsType,
|
2023-05-12 09:19:35 +00:00
|
|
|
is_carousel: bool = True,
|
2023-09-20 13:40:11 +00:00
|
|
|
hold_ms: int = 0,
|
2023-05-12 09:19:35 +00:00
|
|
|
) -> None:
|
|
|
|
"""Navigate to the button with certain action and press it"""
|
|
|
|
# Orient
|
|
|
|
try:
|
|
|
|
_get_action_index(wanted_action, all_actions)
|
|
|
|
except ValueError:
|
|
|
|
raise ValueError(f"Action {wanted_action} is not supported in {all_actions}")
|
|
|
|
|
|
|
|
def current_action() -> str:
|
|
|
|
return layout.get_middle_choice()
|
|
|
|
|
|
|
|
def current_is_wanted(wanted_action: str) -> bool:
|
|
|
|
# Allowing for possible multiple actions on one button
|
|
|
|
return (
|
|
|
|
current_action() == wanted_action
|
|
|
|
or current_action() in wanted_action.split("|")
|
|
|
|
)
|
|
|
|
|
|
|
|
# Navigate
|
|
|
|
layout = debug.read_layout()
|
|
|
|
while not current_is_wanted(wanted_action):
|
|
|
|
layout = _move_one_closer(
|
|
|
|
debug=debug,
|
|
|
|
wanted_action=wanted_action,
|
|
|
|
current_action=current_action(),
|
|
|
|
all_actions=all_actions,
|
|
|
|
is_carousel=is_carousel,
|
|
|
|
)
|
|
|
|
|
2023-09-20 13:40:11 +00:00
|
|
|
# Press or hold
|
2023-11-21 12:51:59 +00:00
|
|
|
debug.press_middle(hold_ms=hold_ms)
|
2023-05-12 09:19:35 +00:00
|
|
|
|
|
|
|
|
2023-11-21 12:51:59 +00:00
|
|
|
def unlock_gesture(debug: "DebugLink") -> LayoutContent:
|
2024-09-02 11:01:16 +00:00
|
|
|
if debug.layout_type is LayoutType.TT:
|
2023-11-21 12:51:59 +00:00
|
|
|
return debug.click(buttons.OK)
|
2024-09-02 11:01:16 +00:00
|
|
|
elif debug.layout_type is LayoutType.TR:
|
2023-11-21 12:51:59 +00:00
|
|
|
return debug.press_right()
|
2024-09-02 11:01:16 +00:00
|
|
|
elif debug.layout_type is LayoutType.Mercury:
|
2023-11-21 12:51:59 +00:00
|
|
|
return debug.click(buttons.TAP_TO_CONFIRM)
|
2024-05-09 16:01:51 +00:00
|
|
|
else:
|
|
|
|
raise RuntimeError("Unknown model")
|
|
|
|
|
|
|
|
|
2024-11-14 14:11:03 +00:00
|
|
|
def _get_action_index(wanted_action: str, all_actions: AllActionsType) -> int:
|
2023-05-12 09:19:35 +00:00
|
|
|
"""Get index of the action in the list of all actions"""
|
|
|
|
if wanted_action in all_actions:
|
|
|
|
return all_actions.index(wanted_action)
|
2024-11-14 14:11:03 +00:00
|
|
|
for index, action in enumerate(all_actions):
|
|
|
|
if not isinstance(action, tuple):
|
|
|
|
action = (action,)
|
|
|
|
for subaction in action:
|
|
|
|
try:
|
|
|
|
tr = TR.translate(subaction)
|
|
|
|
except KeyError:
|
|
|
|
continue
|
|
|
|
if tr == wanted_action:
|
2023-05-12 09:19:35 +00:00
|
|
|
return index
|
|
|
|
|
|
|
|
raise ValueError(f"Action {wanted_action} is not supported in {all_actions}")
|
|
|
|
|
|
|
|
|
|
|
|
def _move_one_closer(
|
|
|
|
debug: "DebugLink",
|
|
|
|
wanted_action: str,
|
|
|
|
current_action: str,
|
2024-11-14 14:11:03 +00:00
|
|
|
all_actions: AllActionsType,
|
2023-05-12 09:19:35 +00:00
|
|
|
is_carousel: bool,
|
2023-11-21 12:51:59 +00:00
|
|
|
) -> LayoutContent:
|
2023-05-12 09:19:35 +00:00
|
|
|
"""Pressing either left or right regarding to the current situation"""
|
|
|
|
index_diff = _get_action_index(wanted_action, all_actions) - _get_action_index(
|
|
|
|
current_action, all_actions
|
|
|
|
)
|
|
|
|
if not is_carousel:
|
|
|
|
# Simply move according to the index in a closed list
|
|
|
|
if index_diff > 0:
|
2023-11-21 12:51:59 +00:00
|
|
|
return debug.press_right()
|
2023-05-12 09:19:35 +00:00
|
|
|
else:
|
2023-11-21 12:51:59 +00:00
|
|
|
return debug.press_left()
|
2023-05-12 09:19:35 +00:00
|
|
|
else:
|
|
|
|
# Carousel can move in a circle - over the edges
|
|
|
|
# Always move the shortest way
|
|
|
|
action_half = len(all_actions) // 2
|
|
|
|
if index_diff > action_half or -action_half < index_diff < 0:
|
2023-11-21 12:51:59 +00:00
|
|
|
return debug.press_left()
|
2023-05-12 09:19:35 +00:00
|
|
|
else:
|
2023-11-21 12:51:59 +00:00
|
|
|
return debug.press_right()
|