2022-10-25 10:46:37 +00:00
|
|
|
import time
|
2022-01-28 18:26:03 +00:00
|
|
|
from typing import Iterator, Tuple
|
|
|
|
|
2025-03-05 15:07:08 +00:00
|
|
|
from trezorlib.debuglink import LayoutType
|
|
|
|
|
2023-05-04 12:20:00 +00:00
|
|
|
Coords = Tuple[int, int]
|
|
|
|
|
2019-09-27 13:38:15 +00:00
|
|
|
|
2025-03-05 15:07:08 +00:00
|
|
|
# display dimensions
|
|
|
|
def display_height(layout_type):
|
|
|
|
if layout_type in (LayoutType.Bolt, LayoutType.Delizia):
|
|
|
|
return 240
|
|
|
|
else:
|
|
|
|
raise ValueError("Wrong layout type")
|
|
|
|
|
|
|
|
|
|
|
|
def display_width(layout_type):
|
|
|
|
if layout_type in (LayoutType.Bolt, LayoutType.Delizia):
|
|
|
|
return 240
|
|
|
|
else:
|
|
|
|
raise ValueError("Wrong layout type")
|
2019-09-27 13:38:15 +00:00
|
|
|
|
2025-03-05 15:07:08 +00:00
|
|
|
|
|
|
|
# grid coordinates
|
2022-01-28 18:26:03 +00:00
|
|
|
def grid(dim: int, grid_cells: int, cell: int) -> int:
|
2019-09-27 13:38:15 +00:00
|
|
|
step = dim // grid_cells
|
|
|
|
ofs = step // 2
|
|
|
|
return cell * step + ofs
|
|
|
|
|
|
|
|
|
2025-03-05 15:07:08 +00:00
|
|
|
def grid35(x: int, y: int, layout_type: LayoutType) -> Coords:
|
|
|
|
return grid(display_width(layout_type), 3, x), grid(
|
|
|
|
display_height(layout_type), 5, y
|
|
|
|
)
|
2024-08-02 09:52:45 +00:00
|
|
|
|
|
|
|
|
2025-03-05 15:07:08 +00:00
|
|
|
def grid34(x: int, y: int, layout_type: LayoutType) -> Coords:
|
|
|
|
assert layout_type in (LayoutType.Bolt, LayoutType.Delizia)
|
|
|
|
return grid(display_width(layout_type), 3, x), grid(
|
|
|
|
display_height(layout_type), 4, y
|
|
|
|
)
|
2024-08-02 09:52:45 +00:00
|
|
|
|
|
|
|
|
2025-03-05 15:07:08 +00:00
|
|
|
def _grid34_from_index(idx: int, layout_type: LayoutType) -> Coords:
|
2024-08-02 09:52:45 +00:00
|
|
|
grid_x = idx % 3
|
|
|
|
grid_y = idx // 3 + 1 # first line is empty
|
2025-03-05 15:07:08 +00:00
|
|
|
return grid34(grid_x, grid_y, layout_type)
|
|
|
|
|
|
|
|
|
|
|
|
# Horizontal coordinates
|
|
|
|
def left(layout_type: LayoutType):
|
|
|
|
return grid(display_width(layout_type), 3, 0)
|
|
|
|
|
2024-08-02 09:52:45 +00:00
|
|
|
|
2025-03-05 15:07:08 +00:00
|
|
|
def mid(layout_type: LayoutType):
|
|
|
|
return grid(display_width(layout_type), 3, 1)
|
2024-08-02 09:52:45 +00:00
|
|
|
|
2019-09-27 13:38:15 +00:00
|
|
|
|
2025-03-05 15:07:08 +00:00
|
|
|
def right(layout_type: LayoutType):
|
|
|
|
return grid(display_width(layout_type), 3, 2)
|
2019-09-27 13:38:15 +00:00
|
|
|
|
|
|
|
|
2025-03-05 15:07:08 +00:00
|
|
|
# Vertical coordinates
|
|
|
|
def top(layout_type: LayoutType):
|
|
|
|
return grid(display_height(layout_type), 6, 0)
|
2024-01-29 13:38:02 +00:00
|
|
|
|
2023-05-04 12:20:00 +00:00
|
|
|
|
2025-03-05 15:07:08 +00:00
|
|
|
def bottom(layout_type: LayoutType):
|
|
|
|
return grid(display_height(layout_type), 6, 5)
|
2019-09-27 13:38:15 +00:00
|
|
|
|
2024-08-02 09:52:45 +00:00
|
|
|
|
2025-03-05 15:07:08 +00:00
|
|
|
# Buttons
|
2024-05-27 22:19:01 +00:00
|
|
|
|
2025-03-05 15:07:08 +00:00
|
|
|
|
|
|
|
def ok(layout_type: LayoutType) -> Coords:
|
|
|
|
return (right(layout_type), bottom(layout_type))
|
|
|
|
|
|
|
|
|
|
|
|
def cancel(layout_type: LayoutType) -> Coords:
|
|
|
|
return (left(layout_type), bottom(layout_type))
|
|
|
|
|
|
|
|
|
|
|
|
def info(layout_type: LayoutType) -> Coords:
|
|
|
|
return (mid(layout_type), bottom(layout_type))
|
|
|
|
|
|
|
|
|
|
|
|
def recovery_delete(layout_type: LayoutType) -> Coords:
|
|
|
|
if layout_type in (LayoutType.Bolt, LayoutType.Delizia):
|
|
|
|
return (left(layout_type), top(layout_type))
|
2024-05-27 22:19:01 +00:00
|
|
|
else:
|
2025-03-05 15:07:08 +00:00
|
|
|
raise ValueError("Wrong layout type")
|
2024-05-27 22:19:01 +00:00
|
|
|
|
|
|
|
|
2025-03-05 15:07:08 +00:00
|
|
|
def corner_button(layout_type: LayoutType) -> Coords:
|
|
|
|
if layout_type in (LayoutType.Bolt, LayoutType.Delizia):
|
|
|
|
return (215, 25)
|
2024-05-27 22:19:01 +00:00
|
|
|
else:
|
2025-03-05 15:07:08 +00:00
|
|
|
raise ValueError("Wrong layout type")
|
2024-05-27 22:19:01 +00:00
|
|
|
|
2019-11-04 14:45:54 +00:00
|
|
|
|
2025-03-05 15:07:08 +00:00
|
|
|
def confirm_word(layout_type: LayoutType) -> Coords:
|
|
|
|
if layout_type in (LayoutType.Bolt, LayoutType.Delizia):
|
|
|
|
return (mid(layout_type), top(layout_type))
|
|
|
|
else:
|
|
|
|
raise ValueError("Wrong layout type")
|
|
|
|
|
|
|
|
|
|
|
|
# PIN/passphrase input
|
|
|
|
def input(layout_type: LayoutType) -> Coords:
|
|
|
|
return (mid(layout_type), top(layout_type))
|
|
|
|
|
|
|
|
|
|
|
|
# Yes/No decision component
|
|
|
|
def ui_yes(layout_type: LayoutType) -> Coords:
|
|
|
|
assert layout_type is LayoutType.Delizia
|
|
|
|
return grid34(2, 2, layout_type)
|
|
|
|
|
|
|
|
|
|
|
|
def ui_no(layout_type: LayoutType) -> Coords:
|
|
|
|
assert layout_type is LayoutType.Delizia
|
|
|
|
return grid34(0, 2, layout_type)
|
|
|
|
|
|
|
|
|
|
|
|
# +/- buttons in number input component
|
|
|
|
def reset_minus(layout_type: LayoutType) -> Coords:
|
|
|
|
if layout_type is LayoutType.Bolt:
|
|
|
|
return (left(layout_type), grid(display_height(layout_type), 5, 1))
|
|
|
|
elif layout_type is LayoutType.Caesar:
|
|
|
|
# TODO temporary workaround to make the 'set_selection' function work
|
|
|
|
return (left(layout_type), grid(display_height(layout_type), 5, 1))
|
|
|
|
elif layout_type is LayoutType.Delizia:
|
|
|
|
return (left(layout_type), grid(display_height(layout_type), 5, 3))
|
|
|
|
else:
|
|
|
|
raise ValueError("Wrong layout type")
|
|
|
|
|
|
|
|
|
|
|
|
def reset_plus(layout_type: LayoutType) -> Coords:
|
|
|
|
if layout_type is LayoutType.Bolt:
|
|
|
|
return (right(layout_type), grid(display_height(layout_type), 5, 1))
|
|
|
|
elif layout_type is LayoutType.Caesar:
|
|
|
|
# TODO temporary workaround to make the 'set_selection' function work
|
|
|
|
return (right(layout_type), grid(display_height(layout_type), 5, 1))
|
|
|
|
elif layout_type is LayoutType.Delizia:
|
|
|
|
return (right(layout_type), grid(display_height(layout_type), 5, 3))
|
|
|
|
else:
|
|
|
|
raise ValueError("Wrong layout type")
|
|
|
|
|
|
|
|
|
|
|
|
# select word component buttons
|
|
|
|
def reset_word_check(layout_type: LayoutType) -> Coords:
|
|
|
|
if layout_type in (LayoutType.Bolt, LayoutType.Delizia):
|
|
|
|
return [
|
|
|
|
(mid(layout_type), grid(display_height(layout_type), 5, 2)),
|
|
|
|
(mid(layout_type), grid(display_height(layout_type), 5, 3)),
|
|
|
|
(mid(layout_type), grid(display_height(layout_type), 5, 4)),
|
|
|
|
]
|
|
|
|
else:
|
|
|
|
raise ValueError("Wrong layout type")
|
|
|
|
|
|
|
|
|
|
|
|
# vertical menu buttons
|
|
|
|
def vertical_menu(layout_type: LayoutType) -> Coords:
|
|
|
|
if layout_type in (LayoutType.Bolt, LayoutType.Delizia):
|
|
|
|
return [
|
|
|
|
(mid(layout_type), grid(display_height(layout_type), 4, 1)),
|
|
|
|
(mid(layout_type), grid(display_height(layout_type), 4, 2)),
|
|
|
|
(mid(layout_type), grid(display_height(layout_type), 4, 3)),
|
|
|
|
]
|
|
|
|
else:
|
|
|
|
raise ValueError("Wrong layout type")
|
2019-09-27 13:38:15 +00:00
|
|
|
|
2024-05-06 22:06:32 +00:00
|
|
|
|
2025-03-05 15:07:08 +00:00
|
|
|
def tap_to_confirm(layout_type: LayoutType) -> Coords:
|
|
|
|
assert layout_type is LayoutType.Delizia
|
|
|
|
return vertical_menu(layout_type)[1]
|
2024-05-06 22:06:32 +00:00
|
|
|
|
2019-09-27 13:38:15 +00:00
|
|
|
|
2022-10-25 10:46:37 +00:00
|
|
|
BUTTON_LETTERS_BIP39 = ("abc", "def", "ghi", "jkl", "mno", "pqr", "stu", "vwx", "yz")
|
|
|
|
BUTTON_LETTERS_SLIP39 = ("ab", "cd", "ef", "ghij", "klm", "nopq", "rs", "tuv", "wxyz")
|
2019-09-27 13:38:15 +00:00
|
|
|
|
2023-05-04 12:20:00 +00:00
|
|
|
# fmt: off
|
|
|
|
PASSPHRASE_LOWERCASE = (" ", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz", "*#")
|
|
|
|
PASSPHRASE_UPPERCASE = (" ", "ABC", "DEF", "GHI", "JKL", "MNO", "PQRS", "TUV", "WXYZ", "*#")
|
|
|
|
PASSPHRASE_DIGITS = ("1", "2", "3", "4", "5", "6", "7", "8", "9", "0")
|
|
|
|
PASSPHRASE_SPECIAL = ("_<>", ".:@", "/|\\", "!()", "+%&", "-[]", "?{}", ",'`", ";\"~", "$^=")
|
|
|
|
# fmt: on
|
|
|
|
|
|
|
|
|
|
|
|
def get_passphrase_choices(char: str) -> tuple[str, ...]:
|
|
|
|
if char in " *#":
|
|
|
|
return PASSPHRASE_LOWERCASE
|
|
|
|
|
|
|
|
if char.islower():
|
|
|
|
return PASSPHRASE_LOWERCASE
|
|
|
|
elif char.isupper():
|
|
|
|
return PASSPHRASE_UPPERCASE
|
|
|
|
elif char.isdigit():
|
|
|
|
return PASSPHRASE_DIGITS
|
|
|
|
else:
|
|
|
|
return PASSPHRASE_SPECIAL
|
|
|
|
|
|
|
|
|
2025-03-05 15:07:08 +00:00
|
|
|
def passphrase(char: str, layout_type: LayoutType) -> Tuple[Coords, int]:
|
2023-05-04 12:20:00 +00:00
|
|
|
choices = get_passphrase_choices(char)
|
|
|
|
idx = next(i for i, letters in enumerate(choices) if char in letters)
|
|
|
|
click_amount = choices[idx].index(char) + 1
|
2025-03-05 15:07:08 +00:00
|
|
|
return pin_passphrase_index(idx, layout_type), click_amount
|
2023-05-04 12:20:00 +00:00
|
|
|
|
|
|
|
|
2025-03-05 15:07:08 +00:00
|
|
|
def pin_passphrase_index(idx: int, layout_type: LayoutType) -> Coords:
|
2023-05-04 12:20:00 +00:00
|
|
|
if idx == 9:
|
|
|
|
idx = 10 # last digit is in the middle
|
2025-03-05 15:07:08 +00:00
|
|
|
return pin_passphrase_grid(idx, layout_type)
|
2023-05-04 12:20:00 +00:00
|
|
|
|
|
|
|
|
2025-03-05 15:07:08 +00:00
|
|
|
def pin_passphrase_grid(idx: int, layout_type: LayoutType) -> Coords:
|
2023-05-04 12:20:00 +00:00
|
|
|
grid_x = idx % 3
|
|
|
|
grid_y = idx // 3 + 1 # first line is empty
|
2025-03-05 15:07:08 +00:00
|
|
|
return grid35(grid_x, grid_y, layout_type)
|
2023-05-04 12:20:00 +00:00
|
|
|
|
2019-09-27 13:38:15 +00:00
|
|
|
|
2025-03-05 15:07:08 +00:00
|
|
|
def type_word(
|
|
|
|
word: str, layout_type: LayoutType, is_slip39: bool = False
|
|
|
|
) -> Iterator[Coords]:
|
2022-10-25 10:46:37 +00:00
|
|
|
if is_slip39:
|
2025-03-05 15:07:08 +00:00
|
|
|
yield from _type_word_slip39(word, layout_type)
|
2022-10-25 10:46:37 +00:00
|
|
|
else:
|
2025-03-05 15:07:08 +00:00
|
|
|
yield from _type_word_bip39(word, layout_type)
|
2022-10-25 10:46:37 +00:00
|
|
|
|
|
|
|
|
2025-03-05 15:07:08 +00:00
|
|
|
def _type_word_slip39(word: str, layout_type: LayoutType) -> Iterator[Coords]:
|
2019-09-27 13:38:15 +00:00
|
|
|
for l in word:
|
2022-10-25 10:46:37 +00:00
|
|
|
idx = next(i for i, letters in enumerate(BUTTON_LETTERS_SLIP39) if l in letters)
|
2025-03-05 15:07:08 +00:00
|
|
|
yield _grid34_from_index(idx, layout_type)
|
2022-10-25 10:46:37 +00:00
|
|
|
|
|
|
|
|
2025-03-05 15:07:08 +00:00
|
|
|
def _type_word_bip39(word: str, layout_type: LayoutType) -> Iterator[Coords]:
|
2023-05-04 12:20:00 +00:00
|
|
|
coords_prev: Coords | None = None
|
2022-10-25 10:46:37 +00:00
|
|
|
for letter in word:
|
2023-05-04 12:20:00 +00:00
|
|
|
time.sleep(0.1) # not being so quick to miss something
|
2025-03-05 15:07:08 +00:00
|
|
|
coords, amount = _letter_coords_and_amount(letter, layout_type)
|
2022-10-25 10:46:37 +00:00
|
|
|
# If the button is the same as for the previous letter,
|
|
|
|
# waiting a second before pressing it again.
|
|
|
|
if coords == coords_prev:
|
2023-05-04 12:20:00 +00:00
|
|
|
time.sleep(1.1)
|
2022-10-25 10:46:37 +00:00
|
|
|
coords_prev = coords
|
|
|
|
for _ in range(amount):
|
|
|
|
yield coords
|
|
|
|
|
|
|
|
|
2025-03-05 15:07:08 +00:00
|
|
|
def _letter_coords_and_amount(
|
|
|
|
letter: str, layout_type: LayoutType
|
|
|
|
) -> Tuple[Coords, int]:
|
2022-10-25 10:46:37 +00:00
|
|
|
idx = next(i for i, letters in enumerate(BUTTON_LETTERS_BIP39) if letter in letters)
|
|
|
|
click_amount = BUTTON_LETTERS_BIP39[idx].index(letter) + 1
|
2025-03-05 15:07:08 +00:00
|
|
|
return _grid34_from_index(idx, layout_type), click_amount
|