def reset() -> None: """ Wipes storage but keeps the device id unchanged. """ device_id = device.get_device_id() wipe() common.set(common.APP_DEVICE, device.DEVICE_ID, device_id.encode(), public=True)
def set_homescreen(homescreen: bytes) -> None: if len(homescreen) > HOMESCREEN_MAXSIZE: raise ValueError # homescreen too large if homescreen[:8] == b"TOIf\x90\x00\x90\x00" or homescreen == b"": common.set(_NAMESPACE, _HOMESCREEN, homescreen, public=True) else: raise ValueError # invalid homescreen
def load_settings( label: str = None, use_passphrase: bool = None, homescreen: bytes = None, passphrase_source: int = None, display_rotation: int = None, ) -> None: if label is not None: common.set(_NAMESPACE, _LABEL, label.encode(), True) # public if use_passphrase is not None: common.set_bool(_NAMESPACE, _USE_PASSPHRASE, use_passphrase) if homescreen is not None: if homescreen[:8] == b"TOIf\x90\x00\x90\x00": if len(homescreen) <= HOMESCREEN_MAXSIZE: common.set(_NAMESPACE, _HOMESCREEN, homescreen, True) # public else: common.set(_NAMESPACE, _HOMESCREEN, b"", True) # public if passphrase_source is not None: if passphrase_source in (0, 1, 2): common.set(_NAMESPACE, _PASSPHRASE_SOURCE, bytes([passphrase_source])) if display_rotation is not None: if display_rotation not in (0, 90, 180, 270): raise ValueError("Unsupported display rotation degrees: %d" % display_rotation) else: common.set(_NAMESPACE, _ROTATION, display_rotation.to_bytes(2, "big"), True) # public
def set_flags(flags: int) -> None: b = common.get(_NAMESPACE, _FLAGS) if b is None: i = 0 else: i = int.from_bytes(b, "big") flags = (flags | i) & 0xFFFF_FFFF if flags != i: common.set(_NAMESPACE, _FLAGS, flags.to_bytes(4, "big"))
def store_mnemonic_secret( secret: bytes, backup_type: EnumTypeBackupType, needs_backup: bool = False, no_backup: bool = False, ) -> None: set_version(common.STORAGE_VERSION_CURRENT) common.set(_NAMESPACE, _MNEMONIC_SECRET, secret) common.set_uint8(_NAMESPACE, _BACKUP_TYPE, backup_type) common.set_true_or_delete(_NAMESPACE, _NO_BACKUP, no_backup) if not no_backup: common.set_true_or_delete(_NAMESPACE, _NEEDS_BACKUP, needs_backup)
def load_settings( label: str = None, use_passphrase: bool = None, homescreen: bytes = None, passphrase_always_on_device: bool = None, display_rotation: int = None, autolock_delay_ms: int = None, ) -> None: if use_passphrase is False: passphrase_always_on_device = False if label is not None: common.set(_NAMESPACE, _LABEL, label.encode(), True) # public if use_passphrase is not None: common.set_bool(_NAMESPACE, _USE_PASSPHRASE, use_passphrase) if homescreen is not None: if homescreen[:8] == b"TOIf\x90\x00\x90\x00": if len(homescreen) <= HOMESCREEN_MAXSIZE: common.set(_NAMESPACE, _HOMESCREEN, homescreen, True) # public else: common.set(_NAMESPACE, _HOMESCREEN, b"", True) # public if passphrase_always_on_device is not None: common.set_bool(_NAMESPACE, _PASSPHRASE_ALWAYS_ON_DEVICE, passphrase_always_on_device) if display_rotation is not None: if display_rotation not in (0, 90, 180, 270): raise ValueError("Unsupported display rotation degrees: %d" % display_rotation) else: common.set(_NAMESPACE, _ROTATION, display_rotation.to_bytes(2, "big"), True) # public if autolock_delay_ms is not None: set_autolock_delay_ms(autolock_delay_ms)
def set_slip39_remaining_shares(shares_remaining: int, group_index: int) -> None: """ 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. """ _require_progress() remaining = common.get(_NAMESPACE, _REMAINING) group_count = get_slip39_group_count() if not group_count: raise RuntimeError if remaining is None: remaining = bytearray([slip39.MAX_SHARE_COUNT] * group_count) remaining = bytearray(remaining) remaining[group_index] = shares_remaining common.set(_NAMESPACE, _REMAINING, remaining)
def set_sd_salt_auth_key(auth_key: bytes | None) -> None: """ The key used to check the authenticity of the SD card salt. """ if auth_key is not None: if len(auth_key) != SD_SALT_AUTH_KEY_LEN_BYTES: raise ValueError return common.set(_NAMESPACE, _SD_SALT_AUTH_KEY, auth_key, public=True) else: return common.delete(_NAMESPACE, _SD_SALT_AUTH_KEY, public=True)
def set_autolock_delay_ms(delay_ms: int) -> None: delay_ms = _normalize_autolock_delay(delay_ms) common.set(_NAMESPACE, _AUTOLOCK_DELAY_MS, delay_ms.to_bytes(4, "big"))
def set_homescreen(homescreen: bytes) -> None: if len(homescreen) > HOMESCREEN_MAXSIZE: raise ValueError # homescreen too large common.set(_NAMESPACE, _HOMESCREEN, homescreen, public=True)
def set_label(label: str) -> None: if len(label) > LABEL_MAXLENGTH: raise ValueError # label too long common.set(_NAMESPACE, _LABEL, label.encode(), True) # public
def set(index: int, group_index: int, mnemonic: str) -> None: common.set( common.APP_RECOVERY_SHARES, index + group_index * slip39.MAX_SHARE_COUNT, mnemonic.encode(), )
def get_device_id() -> str: dev_id = common.get(_NAMESPACE, DEVICE_ID, public=True) if not dev_id: dev_id = _new_device_id().encode() common.set(_NAMESPACE, DEVICE_ID, dev_id, public=True) return dev_id.decode()
def set_autolock_delay_ms(delay_ms: int) -> None: if delay_ms < 60 * 1000: delay_ms = 60 * 1000 common.set(_NAMESPACE, _AUTOLOCK_DELAY_MS, delay_ms.to_bytes(4, "big"))
def set(index: int, data: bytes) -> None: if not 0 <= index < MAX_RESIDENT_CREDENTIALS: raise ValueError # invalid credential index common.set(common.APP_WEBAUTHN, index + _RESIDENT_CREDENTIAL_START_KEY, data)
def set_version(version: bytes) -> None: common.set(_NAMESPACE, _VERSION, version)
def set_label(label: str) -> None: common.set(_NAMESPACE, _LABEL, label.encode(), True) # public
def set_rotation(value: int) -> None: if value not in (0, 90, 180, 270): raise ValueError # unsupported display rotation common.set(_NAMESPACE, _ROTATION, value.to_bytes(2, "big"), True) # public
def set_autolock_delay_ms(delay_ms: int) -> None: delay_ms = max(delay_ms, AUTOLOCK_DELAY_MINIMUM) delay_ms = min(delay_ms, AUTOLOCK_DELAY_MAXIMUM) common.set(_NAMESPACE, _AUTOLOCK_DELAY_MS, delay_ms.to_bytes(4, "big"))