1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-23 06:48:16 +00:00

storage: change api to protobuf

This commit is contained in:
Jan Pochyla 2016-10-06 15:03:54 +02:00
parent 6b94511472
commit d0bb7edbfa
4 changed files with 36 additions and 29 deletions

View File

@ -1,18 +1,27 @@
from micropython import const from micropython import const
from trezor import config from trezor import config
from trezor.messages.Storage import Storage
APP_MANAGEMENT = const(1) APP_COMMON = const(1)
CFG_STORAGE = const(1) CFG_STORAGE = const(1)
def get_storage(session_id): def has(session_id):
return config.get(session_id, APP_MANAGEMENT, CFG_STORAGE) buf = config.get(session_id, APP_COMMON, CFG_STORAGE)
return bool(buf)
def set_storage(session_id, buf): def get(session_id):
config.set(session_id, APP_MANAGEMENT, CFG_STORAGE, buf) buf = config.get(session_id, APP_COMMON, CFG_STORAGE)
if not buf:
raise KeyError('Storage is not initialized')
return Storage.loads(buf)
def clear_storage(session_id): def set(session_id, st):
set_storage(session_id, b'') config.set(session_id, APP_COMMON, CFG_STORAGE, st.dumps())
def clear(session_id):
config.set(session_id, APP_COMMON, CFG_STORAGE, b'')

View File

@ -9,34 +9,33 @@ async def layout_load_device(message, session_id):
from trezor.messages.Success import Success from trezor.messages.Success import Success
from trezor.messages.FailureType import UnexpectedMessage, Other from trezor.messages.FailureType import UnexpectedMessage, Other
from trezor.ui.text import Text from trezor.ui.text import Text
from ..common.storage import get_storage, set_storage
from ..common.confirm import require_confirm from ..common.confirm import require_confirm
from ..common import storage
if get_storage(session_id): if storage.has(session_id):
raise wire.FailureError(UnexpectedMessage, 'Already initialized') raise wire.FailureError(UnexpectedMessage, 'Already initialized')
storage = Storage()
storage.imported = True st = Storage()
st.imported = True
st.version = 1
st.pin = message.pin
st.passphrase_protection = message.passphrase_protection,
st.language = message.language
st.label = message.label
if hasattr(message, 'node'): if hasattr(message, 'node'):
storage.node = message.node st.node = message.node
elif hasattr(message, 'mnemonic'): elif hasattr(message, 'mnemonic'):
st.mnemonic = message.mnemonic
if not message.skip_checksum and not bip39.check(message.mnemonic): if not message.skip_checksum and not bip39.check(message.mnemonic):
raise wire.FailureError(Other, 'Mnemonic is not valid') raise wire.FailureError(Other, 'Mnemonic is not valid')
storage.mnemonic = message.mnemonic
ui.display.clear()
await require_confirm(session_id, Text( await require_confirm(session_id, Text(
'Loading seed', 'Loading seed',
ui.BOLD, 'Loading private seed', 'is not recommended.', ui.BOLD, 'Loading private seed', 'is not recommended.',
ui.NORMAL, 'Continue only if you', 'know what you are doing!')) ui.NORMAL, 'Continue only if you', 'know what you are doing!'))
storage.version = 1 storage.set(session_id, st)
storage.pin = message.pin
storage.passphrase_protection = message.passphrase_protection,
storage.language = message.language
storage.label = message.label
set_storage(session_id, await storage.dumps())
return Success(message='Device loaded') return Success(message='Device loaded')

View File

@ -8,10 +8,10 @@ async def layout_reset_device(message, session_id):
from trezor.messages.Success import Success from trezor.messages.Success import Success
from trezor.messages.Storage import Storage from trezor.messages.Storage import Storage
from trezor.messages.FailureType import UnexpectedMessage from trezor.messages.FailureType import UnexpectedMessage
from ..common.storage import get_storage, set_storage
from ..common.request_pin import request_pin_twice from ..common.request_pin import request_pin_twice
from ..common import storage
if get_storage(session_id): if storage.has(session_id):
raise wire.FailureError(UnexpectedMessage, 'Already initialized') raise wire.FailureError(UnexpectedMessage, 'Already initialized')
mnemonic = await generate_mnemonic( mnemonic = await generate_mnemonic(
@ -23,11 +23,10 @@ async def layout_reset_device(message, session_id):
else: else:
pin = '' pin = ''
storage = Storage( storage.set(session_id, Storage(
version=1, pin=pin, mnemonic=mnemonic, version=1, pin=pin, mnemonic=mnemonic,
passphrase_protection=message.passphrase_protection, passphrase_protection=message.passphrase_protection,
language=message.language, label=message.label) language=message.language, label=message.label))
set_storage(session_id, await storage.dumps())
return Success(message='Initialized') return Success(message='Initialized')

View File

@ -7,7 +7,7 @@ async def layout_wipe_device(message, session_id):
from trezor.messages.Success import Success from trezor.messages.Success import Success
from trezor.ui.text import Text from trezor.ui.text import Text
from ..common.confirm import hold_to_confirm from ..common.confirm import hold_to_confirm
from ..common.storage import clear_storage from ..common import storage
ui.display.clear() ui.display.clear()
@ -17,6 +17,6 @@ async def layout_wipe_device(message, session_id):
ui.NORMAL, '', 'All data will be lost.') ui.NORMAL, '', 'All data will be lost.')
await hold_to_confirm(session_id, content) await hold_to_confirm(session_id, content)
clear_storage(session_id) storage.clear(session_id)
return Success(message='Device wiped') return Success(message='Device wiped')