1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-17 03:48:09 +00:00
trezor-firmware/core/src/storage/common.py
matejcik 5c93ecd53a core: create top-level storage module
This is to avoid including app-specific functionality in storage and
avoid circular imports. The following policy is now in effect: modules
from `storage` namespace must not import from `apps` namespace.

In most files, the change only involves changing import paths.

A minor refactor was needed in case of webauthn: basic get/set/delete
functionality was left in storage.webauthn, and more advanced logic on
top of it was moved to apps.webauthn.resident_credentials.

A significant refactor was needed for sd_salt, where application (and
UI) logic was tightly coupled with the IO code. This is now separated,
and storage.sd_salt deals exclusively with the IO side, while the app/UI
logic is implemented on top of it in apps.common.sd_salt and
apps.management.sd_protect.
2019-10-31 16:21:56 +01:00

81 lines
1.9 KiB
Python

from micropython import const
from trezor import config
if False:
from typing import Optional
# Namespaces:
# fmt: off
APP_DEVICE = const(0x01)
APP_RECOVERY = const(0x02)
APP_RECOVERY_SHARES = const(0x03)
APP_WEBAUTHN = const(0x04)
# fmt: on
_FALSE_BYTE = b"\x00"
_TRUE_BYTE = b"\x01"
STORAGE_VERSION_01 = b"\x01"
STORAGE_VERSION_CURRENT = b"\x02"
def set(app: int, key: int, data: bytes, public: bool = False) -> None:
config.set(app, key, data, public)
def get(app: int, key: int, public: bool = False) -> Optional[bytes]:
return config.get(app, key, public)
def delete(app: int, key: int, public: bool = False) -> None:
config.delete(app, key, public)
def set_true_or_delete(app: int, key: int, value: bool) -> None:
if value:
set_bool(app, key, value)
else:
delete(app, key)
def set_bool(app: int, key: int, value: bool, public: bool = False) -> None:
if value:
set(app, key, _TRUE_BYTE, public)
else:
set(app, key, _FALSE_BYTE, public)
def get_bool(app: int, key: int, public: bool = False) -> bool:
return get(app, key, public) == _TRUE_BYTE
def set_uint8(app: int, key: int, val: int) -> None:
set(app, key, val.to_bytes(1, "big"))
def get_uint8(app: int, key: int) -> Optional[int]:
val = get(app, key)
if not val:
return None
return int.from_bytes(val, "big")
def set_uint16(app: int, key: int, val: int) -> None:
set(app, key, val.to_bytes(2, "big"))
def get_uint16(app: int, key: int) -> Optional[int]:
val = get(app, key)
if not val:
return None
return int.from_bytes(val, "big")
def next_counter(app: int, key: int, public: bool = False) -> Optional[int]:
return config.next_counter(app, key, public)
def set_counter(app: int, key: int, count: int, public: bool = False) -> None:
config.set_counter(app, key, count, public)