From 7f7a8a2ca6142c800b7abc23021ecfd4cd5ba179 Mon Sep 17 00:00:00 2001 From: andrew Date: Sat, 29 Dec 2018 18:48:30 +0100 Subject: [PATCH] Store boolean values as '0x00' and '0x01' instead of '' and '0x01'. --- src/apps/common/storage.py | 41 +++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/src/apps/common/storage.py b/src/apps/common/storage.py index 7954c015a..0d1c3fd81 100644 --- a/src/apps/common/storage.py +++ b/src/apps/common/storage.py @@ -9,6 +9,8 @@ from apps.common import cache HOMESCREEN_MAXSIZE = 16384 _STORAGE_VERSION = b"\x01" +_FALSE_BYTE = b"\x00" +_TRUE_BYTE = b"\x01" # fmt: off _APP = const(0x01) # app namespace @@ -28,6 +30,16 @@ _AUTOLOCK_DELAY_MS = const(0x0C) # int _NO_BACKUP = const(0x0D) # bool (0x01 or empty) # fmt: on +def _set_bool(app: int, key: int, value: bool, public: bool = False) -> None: + if value: + config.set(app, key, _TRUE_BYTE, public) + else: + config.set(app, key, _FALSE_BYTE, public) + + +def _get_bool(app: int, key: int, public: bool = False) -> bool: + return config.get(app, key, public) == _TRUE_BYTE + def _new_device_id() -> str: return hexlify(random.bytes(12)).decode().upper() @@ -60,7 +72,7 @@ def get_mnemonic() -> str: def has_passphrase() -> bool: - return bool(config.get(_APP, _USE_PASSPHRASE)) + return _get_bool(_APP, _USE_PASSPHRASE) def get_homescreen() -> bytes: @@ -70,18 +82,13 @@ def get_homescreen() -> bytes: def load_mnemonic(mnemonic: str, needs_backup: bool, no_backup: bool) -> None: config.set(_APP, _MNEMONIC, mnemonic.encode()) config.set(_APP, _VERSION, _STORAGE_VERSION) - if no_backup: - config.set(_APP, _NO_BACKUP, b"\x01") - else: - config.set(_APP, _NO_BACKUP, b"") - if needs_backup: - config.set(_APP, _NEEDS_BACKUP, b"\x01") - else: - config.set(_APP, _NEEDS_BACKUP, b"") + _set_bool(_APP, _NO_BACKUP, no_backup) + if not no_backup: + _set_bool(_APP, _NEEDS_BACKUP, needs_backup) def needs_backup() -> bool: - return bool(config.get(_APP, _NEEDS_BACKUP)) + return _get_bool(_APP, _NEEDS_BACKUP) def set_backed_up() -> None: @@ -89,18 +96,15 @@ def set_backed_up() -> None: def unfinished_backup() -> bool: - return bool(config.get(_APP, _UNFINISHED_BACKUP)) + return _get_bool(_APP, _UNFINISHED_BACKUP) def set_unfinished_backup(state: bool) -> None: - if state: - config.set(_APP, _UNFINISHED_BACKUP, b"\x01") - else: - config.set(_APP, _UNFINISHED_BACKUP, b"") + _set_bool(_APP, _UNFINISHED_BACKUP, state) def no_backup() -> bool: - return bool(config.get(_APP, _NO_BACKUP)) + return _get_bool(_APP, _NO_BACKUP) def get_passphrase_source() -> int: @@ -121,10 +125,7 @@ def load_settings( ) -> None: if label is not None: config.set(_APP, _LABEL, label.encode(), True) # public - if use_passphrase is True: - config.set(_APP, _USE_PASSPHRASE, b"\x01") - if use_passphrase is False: - config.set(_APP, _USE_PASSPHRASE, b"") + _set_bool(_APP, _USE_PASSPHRASE, use_passphrase) if homescreen is not None: if homescreen[:8] == b"TOIf\x90\x00\x90\x00": if len(homescreen) <= HOMESCREEN_MAXSIZE: