2022-10-25 10:46:37 +00:00
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
|
2024-03-09 21:05:05 +00:00
|
|
|
from trezorlib import models
|
|
|
|
|
2019-10-25 08:07:09 +00:00
|
|
|
from .. import buttons
|
2023-08-11 15:57:32 +00:00
|
|
|
from .. import translations as TR
|
2024-01-29 13:38:02 +00:00
|
|
|
from .common import get_possible_btn_texts, go_next
|
2019-10-25 08:07:09 +00:00
|
|
|
|
2022-10-25 10:46:37 +00:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
from trezorlib.debuglink import DebugLink, LayoutContent
|
|
|
|
|
2019-10-25 08:07:09 +00:00
|
|
|
|
2024-01-29 13:38:02 +00:00
|
|
|
DELETE_BTN_TEXTS = get_possible_btn_texts("inputs__delete") + get_possible_btn_texts(
|
|
|
|
"inputs__previous"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-10-25 10:46:37 +00:00
|
|
|
def enter_word(
|
|
|
|
debug: "DebugLink", word: str, is_slip39: bool = False
|
|
|
|
) -> "LayoutContent":
|
2024-03-09 21:05:05 +00:00
|
|
|
if debug.model in (models.T2T1, models.T3T1):
|
2023-05-12 09:19:35 +00:00
|
|
|
typed_word = word[:4]
|
|
|
|
for coords in buttons.type_word(typed_word, is_slip39=is_slip39):
|
|
|
|
debug.click(coords)
|
|
|
|
|
|
|
|
return debug.click(buttons.CONFIRM_WORD, wait=True)
|
2024-03-09 21:05:05 +00:00
|
|
|
elif debug.model in (models.T2B1,):
|
2023-05-12 09:19:35 +00:00
|
|
|
letter_index = 0
|
|
|
|
layout = debug.read_layout()
|
|
|
|
|
|
|
|
# Letter choices
|
|
|
|
while layout.find_values_by_key("letter_choices"):
|
|
|
|
letter = word[letter_index]
|
|
|
|
while not layout.get_middle_choice() == letter:
|
|
|
|
layout = debug.press_right(wait=True)
|
|
|
|
|
|
|
|
layout = debug.press_middle(wait=True)
|
|
|
|
letter_index += 1
|
|
|
|
|
|
|
|
# Word choices
|
|
|
|
while not layout.get_middle_choice() == word:
|
|
|
|
layout = debug.press_right(wait=True)
|
|
|
|
|
|
|
|
return debug.press_middle(wait=True)
|
|
|
|
else:
|
|
|
|
raise ValueError("Unknown model")
|
2019-10-25 08:07:09 +00:00
|
|
|
|
|
|
|
|
2023-05-04 12:23:33 +00:00
|
|
|
def confirm_recovery(debug: "DebugLink") -> None:
|
2023-07-27 13:55:01 +00:00
|
|
|
layout = debug.wait_layout()
|
2023-08-11 15:57:32 +00:00
|
|
|
TR.assert_equals(layout.title(), "recovery__title")
|
2024-03-09 21:05:05 +00:00
|
|
|
if debug.model in (models.T2T1, models.T3T1):
|
2023-05-12 09:19:35 +00:00
|
|
|
debug.click(buttons.OK, wait=True)
|
2024-03-09 21:05:05 +00:00
|
|
|
elif debug.model in (models.T2B1,):
|
2023-05-12 09:19:35 +00:00
|
|
|
debug.press_right(wait=True)
|
|
|
|
debug.press_right()
|
|
|
|
|
|
|
|
|
|
|
|
def select_number_of_words(
|
|
|
|
debug: "DebugLink", num_of_words: int = 20, wait: bool = True
|
|
|
|
) -> None:
|
|
|
|
if wait:
|
|
|
|
debug.wait_layout()
|
2023-08-11 15:57:32 +00:00
|
|
|
TR.assert_equals(debug.read_layout().text_content(), "recovery__num_of_words")
|
2024-03-09 21:05:05 +00:00
|
|
|
if debug.model in (models.T2T1, models.T3T1):
|
2023-05-12 09:19:35 +00:00
|
|
|
# click the number
|
|
|
|
word_option_offset = 6
|
|
|
|
word_options = (12, 18, 20, 24, 33)
|
|
|
|
index = word_option_offset + word_options.index(
|
|
|
|
num_of_words
|
|
|
|
) # raises if num of words is invalid
|
|
|
|
coords = buttons.grid34(index % 3, index // 3)
|
|
|
|
layout = debug.click(coords, wait=True)
|
2024-03-09 21:05:05 +00:00
|
|
|
elif debug.model in (models.T2B1,):
|
2023-05-12 09:19:35 +00:00
|
|
|
layout = debug.press_right(wait=True)
|
2023-08-11 15:57:32 +00:00
|
|
|
TR.assert_equals(layout.title(), "word_count__title")
|
2019-11-11 10:07:23 +00:00
|
|
|
|
2023-05-12 09:19:35 +00:00
|
|
|
# navigate to the number and confirm it
|
|
|
|
word_options = (12, 18, 20, 24, 33)
|
|
|
|
index = word_options.index(num_of_words)
|
|
|
|
for _ in range(index):
|
|
|
|
debug.press_right(wait=True)
|
|
|
|
layout = debug.press_middle(wait=True)
|
2022-10-25 10:46:37 +00:00
|
|
|
else:
|
2023-05-12 09:19:35 +00:00
|
|
|
raise ValueError("Unknown model")
|
2023-05-04 12:16:40 +00:00
|
|
|
|
2023-07-27 13:55:01 +00:00
|
|
|
if num_of_words in (20, 33):
|
2023-08-11 15:57:32 +00:00
|
|
|
TR.assert_in(layout.text_content(), "recovery__enter_any_share")
|
2023-07-27 13:55:01 +00:00
|
|
|
else:
|
2023-08-11 15:57:32 +00:00
|
|
|
TR.assert_in(layout.text_content(), "recovery__enter_backup")
|
2019-10-25 08:07:09 +00:00
|
|
|
|
|
|
|
|
2023-06-29 15:26:51 +00:00
|
|
|
def enter_share(
|
|
|
|
debug: "DebugLink", share: str, is_first: bool = True
|
|
|
|
) -> "LayoutContent":
|
2023-08-11 15:57:32 +00:00
|
|
|
TR.assert_in(debug.read_layout().title(), "recovery__title_recover")
|
2024-03-09 21:05:05 +00:00
|
|
|
if debug.model in (models.T2B1,):
|
2023-08-11 15:57:32 +00:00
|
|
|
layout = debug.wait_layout()
|
|
|
|
for _ in range(layout.page_count()):
|
|
|
|
layout = debug.press_right(wait=True)
|
|
|
|
else:
|
2023-05-12 09:19:35 +00:00
|
|
|
layout = debug.click(buttons.OK, wait=True)
|
2019-10-25 08:07:09 +00:00
|
|
|
|
2023-08-11 15:57:32 +00:00
|
|
|
assert "MnemonicKeyboard" in layout.all_components()
|
2019-10-25 08:07:09 +00:00
|
|
|
|
2023-08-11 15:57:32 +00:00
|
|
|
for word in share.split(" "):
|
|
|
|
layout = enter_word(debug, word, is_slip39=True)
|
2023-05-12 09:19:35 +00:00
|
|
|
|
2023-08-11 15:57:32 +00:00
|
|
|
return layout
|
2019-10-25 08:07:09 +00:00
|
|
|
|
|
|
|
|
2022-10-25 10:46:37 +00:00
|
|
|
def enter_shares(debug: "DebugLink", shares: list[str]) -> None:
|
2023-08-11 15:57:32 +00:00
|
|
|
TR.assert_in(debug.read_layout().text_content(), "recovery__enter_any_share")
|
2023-06-29 15:26:51 +00:00
|
|
|
for index, share in enumerate(shares):
|
2023-08-11 15:57:32 +00:00
|
|
|
enter_share(debug, share, is_first=index == 0)
|
|
|
|
if index < len(shares) - 1:
|
|
|
|
TR.assert_in(
|
|
|
|
debug.read_layout().text_content(),
|
|
|
|
"recovery__x_of_y_entered_template",
|
|
|
|
template=(index + 1, len(shares)),
|
|
|
|
)
|
2019-10-25 08:07:09 +00:00
|
|
|
|
2023-08-11 15:57:32 +00:00
|
|
|
TR.assert_in(debug.read_layout().text_content(), "recovery__wallet_recovered")
|
2023-05-04 12:16:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
def enter_seed(debug: "DebugLink", seed_words: list[str]) -> None:
|
2024-01-29 13:38:02 +00:00
|
|
|
prepare_enter_seed(debug)
|
|
|
|
|
|
|
|
for word in seed_words:
|
|
|
|
enter_word(debug, word, is_slip39=False)
|
|
|
|
|
|
|
|
TR.assert_in(debug.read_layout().text_content(), "recovery__wallet_recovered")
|
|
|
|
|
|
|
|
|
|
|
|
def enter_seed_previous_correct(
|
|
|
|
debug: "DebugLink", seed_words: list[str], bad_indexes: dict[int, str]
|
|
|
|
) -> None:
|
|
|
|
prepare_enter_seed(debug)
|
|
|
|
|
|
|
|
i = 0
|
|
|
|
go_back = False
|
|
|
|
bad_word = ""
|
|
|
|
while True:
|
|
|
|
assert i >= 0
|
|
|
|
|
|
|
|
if i >= len(seed_words):
|
|
|
|
break
|
|
|
|
|
|
|
|
if go_back:
|
|
|
|
go_back = False
|
2024-03-09 21:05:05 +00:00
|
|
|
if debug.model in (models.T2T1, models.T3T1):
|
2024-01-29 13:38:02 +00:00
|
|
|
debug.swipe_right(wait=True)
|
|
|
|
for _ in range(len(bad_word)):
|
|
|
|
debug.click(buttons.RECOVERY_DELETE, wait=True)
|
2024-03-09 21:05:05 +00:00
|
|
|
elif debug.model in (models.T2B1,):
|
2024-01-29 13:38:02 +00:00
|
|
|
layout = debug.read_layout()
|
|
|
|
|
|
|
|
while layout.get_middle_choice() not in DELETE_BTN_TEXTS:
|
|
|
|
layout = debug.press_right(wait=True)
|
|
|
|
layout = debug.press_middle(wait=True)
|
|
|
|
|
|
|
|
for _ in range(len(bad_word)):
|
|
|
|
while layout.get_middle_choice() not in DELETE_BTN_TEXTS:
|
|
|
|
layout = debug.press_left(wait=True)
|
|
|
|
layout = debug.press_middle(wait=True)
|
|
|
|
continue
|
|
|
|
|
|
|
|
if i in bad_indexes:
|
|
|
|
word = bad_indexes.pop(i)
|
|
|
|
bad_word = word
|
|
|
|
go_back = True
|
|
|
|
else:
|
|
|
|
word = seed_words[i]
|
|
|
|
i += 1
|
|
|
|
layout = enter_word(debug, word, is_slip39=False)
|
|
|
|
|
|
|
|
TR.assert_in(debug.read_layout().text_content(), "recovery__wallet_recovered")
|
|
|
|
|
|
|
|
|
|
|
|
def prepare_enter_seed(debug: "DebugLink") -> None:
|
2023-08-11 15:57:32 +00:00
|
|
|
TR.assert_in(debug.read_layout().text_content(), "recovery__enter_backup")
|
2024-03-09 21:05:05 +00:00
|
|
|
if debug.model in (models.T2T1, models.T3T1):
|
2023-08-11 15:57:32 +00:00
|
|
|
debug.click(buttons.OK, wait=True)
|
2024-03-09 21:05:05 +00:00
|
|
|
elif debug.model in (models.T2B1,):
|
2023-08-11 15:57:32 +00:00
|
|
|
debug.press_right(wait=True)
|
|
|
|
TR.assert_equals(debug.read_layout().title(), "recovery__title_recover")
|
2023-06-29 15:26:51 +00:00
|
|
|
debug.press_right()
|
2024-01-29 13:38:02 +00:00
|
|
|
layout = debug.press_right(wait=True)
|
|
|
|
assert "MnemonicKeyboard" in layout.all_components()
|
2019-10-25 08:07:09 +00:00
|
|
|
|
|
|
|
|
2022-10-25 10:46:37 +00:00
|
|
|
def finalize(debug: "DebugLink") -> None:
|
2023-05-12 09:19:35 +00:00
|
|
|
layout = go_next(debug, wait=True)
|
|
|
|
assert layout is not None
|
2023-05-04 12:16:40 +00:00
|
|
|
assert layout.main_component() == "Homescreen"
|