2019-09-12 12:18:00 +00:00
|
|
|
from micropython import const
|
|
|
|
|
2019-07-01 07:45:30 +00:00
|
|
|
from trezor import config
|
|
|
|
|
2019-07-09 12:05:35 +00:00
|
|
|
if False:
|
|
|
|
from typing import Optional
|
|
|
|
|
2019-07-01 07:45:30 +00:00
|
|
|
# Namespaces:
|
|
|
|
# fmt: off
|
2019-09-12 12:18:00 +00:00
|
|
|
APP_DEVICE = const(0x01)
|
|
|
|
APP_RECOVERY = const(0x02)
|
|
|
|
APP_RECOVERY_SHARES = const(0x03)
|
2019-09-19 15:17:22 +00:00
|
|
|
APP_WEBAUTHN = const(0x04)
|
2019-07-01 07:45:30 +00:00
|
|
|
# fmt: on
|
|
|
|
|
|
|
|
_FALSE_BYTE = b"\x00"
|
|
|
|
_TRUE_BYTE = b"\x01"
|
|
|
|
|
2019-09-12 12:18:00 +00:00
|
|
|
STORAGE_VERSION_01 = b"\x01"
|
|
|
|
STORAGE_VERSION_CURRENT = b"\x02"
|
2019-07-01 07:45:30 +00:00
|
|
|
|
|
|
|
|
2019-09-12 12:18:00 +00:00
|
|
|
def set(app: int, key: int, data: bytes, public: bool = False) -> None:
|
2019-07-01 07:45:30 +00:00
|
|
|
config.set(app, key, data, public)
|
|
|
|
|
|
|
|
|
2019-09-12 12:18:00 +00:00
|
|
|
def get(app: int, key: int, public: bool = False) -> Optional[bytes]:
|
2019-07-01 07:45:30 +00:00
|
|
|
return config.get(app, key, public)
|
|
|
|
|
|
|
|
|
2019-08-13 15:50:06 +00:00
|
|
|
def delete(app: int, key: int, public: bool = False) -> None:
|
|
|
|
config.delete(app, key, public)
|
2019-07-01 07:45:30 +00:00
|
|
|
|
|
|
|
|
2019-09-12 12:18:00 +00:00
|
|
|
def set_true_or_delete(app: int, key: int, value: bool) -> None:
|
2019-07-01 07:45:30 +00:00
|
|
|
if value:
|
2019-09-12 12:18:00 +00:00
|
|
|
set_bool(app, key, value)
|
2019-07-01 07:45:30 +00:00
|
|
|
else:
|
2019-09-12 12:18:00 +00:00
|
|
|
delete(app, key)
|
2019-07-01 07:45:30 +00:00
|
|
|
|
|
|
|
|
2019-09-12 12:18:00 +00:00
|
|
|
def set_bool(app: int, key: int, value: bool, public: bool = False) -> None:
|
2019-07-01 07:45:30 +00:00
|
|
|
if value:
|
2019-09-12 12:18:00 +00:00
|
|
|
set(app, key, _TRUE_BYTE, public)
|
2019-07-01 07:45:30 +00:00
|
|
|
else:
|
2019-09-12 12:18:00 +00:00
|
|
|
set(app, key, _FALSE_BYTE, public)
|
2019-07-01 07:45:30 +00:00
|
|
|
|
|
|
|
|
2019-09-12 12:18:00 +00:00
|
|
|
def get_bool(app: int, key: int, public: bool = False) -> bool:
|
|
|
|
return get(app, key, public) == _TRUE_BYTE
|
2019-07-01 07:45:30 +00:00
|
|
|
|
|
|
|
|
2019-09-12 12:18:00 +00:00
|
|
|
def set_uint8(app: int, key: int, val: int) -> None:
|
|
|
|
set(app, key, val.to_bytes(1, "big"))
|
2019-07-01 07:45:30 +00:00
|
|
|
|
|
|
|
|
2019-09-12 12:18:00 +00:00
|
|
|
def get_uint8(app: int, key: int) -> Optional[int]:
|
|
|
|
val = get(app, key)
|
2019-07-01 07:45:30 +00:00
|
|
|
if not val:
|
|
|
|
return None
|
|
|
|
return int.from_bytes(val, "big")
|
|
|
|
|
|
|
|
|
2019-09-12 12:18:00 +00:00
|
|
|
def set_uint16(app: int, key: int, val: int) -> None:
|
|
|
|
set(app, key, val.to_bytes(2, "big"))
|
2019-07-01 07:45:30 +00:00
|
|
|
|
|
|
|
|
2019-09-12 12:18:00 +00:00
|
|
|
def get_uint16(app: int, key: int) -> Optional[int]:
|
|
|
|
val = get(app, key)
|
2019-07-01 07:45:30 +00:00
|
|
|
if not val:
|
|
|
|
return None
|
|
|
|
return int.from_bytes(val, "big")
|
|
|
|
|
|
|
|
|
2019-09-12 12:18:00 +00:00
|
|
|
def next_counter(app: int, key: int, public: bool = False) -> Optional[int]:
|
2019-07-01 07:45:30 +00:00
|
|
|
return config.next_counter(app, key, public)
|
|
|
|
|
|
|
|
|
2019-09-12 12:18:00 +00:00
|
|
|
def set_counter(app: int, key: int, count: int, public: bool = False) -> None:
|
2019-07-01 07:45:30 +00:00
|
|
|
config.set_counter(app, key, count, public)
|