You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
136 lines
3.8 KiB
136 lines
3.8 KiB
5 years ago
|
from micropython import const
|
||
|
|
||
5 years ago
|
from storage import common, recovery_shares
|
||
5 years ago
|
from trezor.crypto import slip39
|
||
|
|
||
5 years ago
|
# Namespace:
|
||
5 years ago
|
_NAMESPACE = common.APP_RECOVERY
|
||
5 years ago
|
|
||
|
# fmt: off
|
||
|
# Keys:
|
||
|
_IN_PROGRESS = const(0x00) # bool
|
||
|
_DRY_RUN = const(0x01) # bool
|
||
|
_SLIP39_IDENTIFIER = const(0x03) # bytes
|
||
|
_SLIP39_THRESHOLD = const(0x04) # int
|
||
5 years ago
|
_REMAINING = const(0x05) # int
|
||
5 years ago
|
_SLIP39_ITERATION_EXPONENT = const(0x06) # int
|
||
5 years ago
|
_SLIP39_GROUP_COUNT = const(0x07) # int
|
||
5 years ago
|
|
||
|
# Deprecated Keys:
|
||
|
# _WORD_COUNT = const(0x02) # int
|
||
5 years ago
|
# fmt: on
|
||
|
|
||
5 years ago
|
# Default values:
|
||
|
_DEFAULT_SLIP39_GROUP_COUNT = const(1)
|
||
|
|
||
|
|
||
5 years ago
|
if False:
|
||
5 years ago
|
from typing import List, Optional
|
||
5 years ago
|
|
||
|
|
||
5 years ago
|
def _require_progress() -> None:
|
||
5 years ago
|
if not is_in_progress():
|
||
|
raise RuntimeError
|
||
|
|
||
|
|
||
5 years ago
|
def set_in_progress(val: bool) -> None:
|
||
5 years ago
|
common.set_bool(_NAMESPACE, _IN_PROGRESS, val)
|
||
5 years ago
|
|
||
|
|
||
5 years ago
|
def is_in_progress() -> bool:
|
||
5 years ago
|
return common.get_bool(_NAMESPACE, _IN_PROGRESS)
|
||
5 years ago
|
|
||
|
|
||
5 years ago
|
def set_dry_run(val: bool) -> None:
|
||
5 years ago
|
_require_progress()
|
||
5 years ago
|
common.set_bool(_NAMESPACE, _DRY_RUN, val)
|
||
5 years ago
|
|
||
|
|
||
|
def is_dry_run() -> bool:
|
||
5 years ago
|
_require_progress()
|
||
5 years ago
|
return common.get_bool(_NAMESPACE, _DRY_RUN)
|
||
5 years ago
|
|
||
|
|
||
|
def set_slip39_identifier(identifier: int) -> None:
|
||
5 years ago
|
_require_progress()
|
||
5 years ago
|
common.set_uint16(_NAMESPACE, _SLIP39_IDENTIFIER, identifier)
|
||
5 years ago
|
|
||
|
|
||
|
def get_slip39_identifier() -> Optional[int]:
|
||
5 years ago
|
_require_progress()
|
||
5 years ago
|
return common.get_uint16(_NAMESPACE, _SLIP39_IDENTIFIER)
|
||
5 years ago
|
|
||
|
|
||
|
def set_slip39_iteration_exponent(exponent: int) -> None:
|
||
5 years ago
|
_require_progress()
|
||
5 years ago
|
common.set_uint8(_NAMESPACE, _SLIP39_ITERATION_EXPONENT, exponent)
|
||
5 years ago
|
|
||
|
|
||
|
def get_slip39_iteration_exponent() -> Optional[int]:
|
||
5 years ago
|
_require_progress()
|
||
5 years ago
|
return common.get_uint8(_NAMESPACE, _SLIP39_ITERATION_EXPONENT)
|
||
5 years ago
|
|
||
|
|
||
5 years ago
|
def set_slip39_group_count(group_count: int) -> None:
|
||
5 years ago
|
_require_progress()
|
||
5 years ago
|
common.set_uint8(_NAMESPACE, _SLIP39_GROUP_COUNT, group_count)
|
||
5 years ago
|
|
||
|
|
||
5 years ago
|
def get_slip39_group_count() -> int:
|
||
5 years ago
|
_require_progress()
|
||
|
return (
|
||
|
common.get_uint8(_NAMESPACE, _SLIP39_GROUP_COUNT) or _DEFAULT_SLIP39_GROUP_COUNT
|
||
|
)
|
||
5 years ago
|
|
||
|
|
||
5 years ago
|
def set_slip39_remaining_shares(shares_remaining: int, group_index: int) -> None:
|
||
5 years ago
|
"""
|
||
|
We store the remaining shares as a bytearray of length group_count.
|
||
|
Each byte represents share remaining for group of that group_index.
|
||
|
0x10 (16) was chosen as the default value because it's the max
|
||
|
share count for a group.
|
||
|
"""
|
||
5 years ago
|
_require_progress()
|
||
5 years ago
|
remaining = common.get(_NAMESPACE, _REMAINING)
|
||
5 years ago
|
group_count = get_slip39_group_count()
|
||
|
if not group_count:
|
||
|
raise RuntimeError
|
||
5 years ago
|
if remaining is None:
|
||
5 years ago
|
remaining = bytearray([slip39.MAX_SHARE_COUNT] * group_count)
|
||
5 years ago
|
remaining = bytearray(remaining)
|
||
|
remaining[group_index] = shares_remaining
|
||
5 years ago
|
common.set(_NAMESPACE, _REMAINING, remaining)
|
||
5 years ago
|
|
||
|
|
||
5 years ago
|
def get_slip39_remaining_shares(group_index: int) -> Optional[int]:
|
||
5 years ago
|
_require_progress()
|
||
5 years ago
|
remaining = common.get(_NAMESPACE, _REMAINING)
|
||
5 years ago
|
if remaining is None or remaining[group_index] == slip39.MAX_SHARE_COUNT:
|
||
|
return None
|
||
|
else:
|
||
|
return remaining[group_index]
|
||
|
|
||
|
|
||
|
def fetch_slip39_remaining_shares() -> Optional[List[int]]:
|
||
5 years ago
|
_require_progress()
|
||
5 years ago
|
remaining = common.get(_NAMESPACE, _REMAINING)
|
||
5 years ago
|
if not remaining:
|
||
|
return None
|
||
|
|
||
5 years ago
|
group_count = get_slip39_group_count()
|
||
|
if not group_count:
|
||
|
raise RuntimeError
|
||
5 years ago
|
return list(remaining[:group_count])
|
||
5 years ago
|
|
||
|
|
||
5 years ago
|
def end_progress() -> None:
|
||
5 years ago
|
_require_progress()
|
||
5 years ago
|
common.delete(_NAMESPACE, _IN_PROGRESS)
|
||
|
common.delete(_NAMESPACE, _DRY_RUN)
|
||
|
common.delete(_NAMESPACE, _SLIP39_IDENTIFIER)
|
||
|
common.delete(_NAMESPACE, _SLIP39_THRESHOLD)
|
||
|
common.delete(_NAMESPACE, _REMAINING)
|
||
|
common.delete(_NAMESPACE, _SLIP39_ITERATION_EXPONENT)
|
||
|
common.delete(_NAMESPACE, _SLIP39_GROUP_COUNT)
|
||
5 years ago
|
recovery_shares.delete()
|