1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-27 16:48:09 +00:00
trezor-firmware/src/apps/common/storage.py

64 lines
1.5 KiB
Python
Raw Normal View History

2016-09-29 10:29:43 +00:00
from micropython import const
2017-09-16 13:00:31 +00:00
2016-09-26 11:06:28 +00:00
from trezor import config
2016-11-23 13:46:55 +00:00
2017-10-24 11:58:40 +00:00
_STORAGE_VERSION = b'\x01'
2016-11-23 13:46:55 +00:00
2017-10-24 11:58:40 +00:00
_APP = const(0x0001) # app namespace
_DEVICE_ID = const(0x0000) # bytes
_VERSION = const(0x0001) # int
_MNEMONIC = const(0x0002) # str
_LANGUAGE = const(0x0003) # str
_LABEL = const(0x0004) # str
_USE_PASSPHRASE = const(0x0005) # 0x01 or empty
2016-09-26 11:06:28 +00:00
def get_device_id() -> str:
2017-10-24 11:58:40 +00:00
dev_id = config.get(_APP, _DEVICE_ID).decode()
if not dev_id:
2016-11-23 13:46:55 +00:00
dev_id = new_device_id()
2017-10-24 11:58:40 +00:00
config.set(_APP, _DEVICE_ID, dev_id.encode())
2016-11-23 13:46:55 +00:00
return dev_id
2016-09-26 11:06:28 +00:00
def is_initialized() -> bool:
2017-10-24 11:58:40 +00:00
return bool(config.get(_APP, _VERSION))
2016-11-15 10:50:45 +00:00
def get_label() -> str:
2017-10-24 11:58:40 +00:00
return config.get(_APP, _LABEL).decode()
2016-12-15 11:48:33 +00:00
def get_mnemonic() -> str:
2017-10-24 11:58:40 +00:00
return config.get(_APP, _MNEMONIC).decode()
2016-11-23 13:46:55 +00:00
2017-10-24 11:58:40 +00:00
def has_passphrase() -> bool:
return bool(config.get(_APP, _USE_PASSPHRASE))
def load_mnemonic(mnemonic: str):
2017-10-24 11:58:40 +00:00
config.set(_APP, _VERSION, _STORAGE_VERSION)
config.set(_APP, _MNEMONIC, mnemonic.encode())
2017-10-24 11:58:40 +00:00
def load_settings(label: str = None, use_passphrase: bool = None):
if label is not None:
config.set(_APP, _LABEL, label.encode())
if use_passphrase is True:
config.set(_APP, _USE_PASSPHRASE, b'\x01')
if use_passphrase is False:
config.set(_APP, _USE_PASSPHRASE, b'')
2016-12-15 11:48:33 +00:00
2016-11-23 13:46:55 +00:00
def wipe():
from . import cache
2016-11-23 13:46:55 +00:00
config.wipe()
cache.clear()
2016-11-23 13:46:55 +00:00
def new_device_id() -> str:
from ubinascii import hexlify
from trezor.crypto import random
return hexlify(random.bytes(12)).decode('ascii').upper()