1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-22 22:38:08 +00:00

refactor(core/cardano): update caching mechanism

This commit is contained in:
matejcik 2021-03-05 15:37:08 +01:00 committed by matejcik
parent 3cdb09c294
commit 276bb59dba
2 changed files with 20 additions and 13 deletions

View File

@ -71,24 +71,31 @@ def derive_path_cardano(root: bip32.HDNode, path: Bip32Path) -> bip32.HDNode:
return node
@cache.stored_async(cache.APP_CARDANO_ROOT)
async def get_keychain(ctx: wire.Context) -> Keychain:
@cache.stored_async(cache.APP_CARDANO_PASSPHRASE)
async def _get_passphrase(ctx: wire.Context) -> bytes:
return (await get_passphrase(ctx)).encode()
async def _get_keychain_bip39(ctx: wire.Context) -> Keychain:
if not device.is_initialized():
raise wire.NotInitialized("Device is not initialized")
# ask for passphrase, loading from cache if necessary
passphrase = await _get_passphrase(ctx)
# derive the root node from mnemonic and passphrase via Cardano Icarus algorithm
secret_bytes = mnemonic.get_secret()
assert secret_bytes is not None
root = bip32.from_mnemonic_cardano(secret_bytes.decode(), passphrase.decode())
return Keychain(root)
async def get_keychain(ctx: wire.Context) -> Keychain:
if mnemonic.is_bip39():
passphrase = await get_passphrase(ctx)
# derive the root node from mnemonic and passphrase via Cardano Icarus algorithm
secret_bytes = mnemonic.get_secret()
assert secret_bytes is not None
root = bip32.from_mnemonic_cardano(secret_bytes.decode(), passphrase)
return await _get_keychain_bip39(ctx)
else:
# derive the root node via SLIP-0023 https://github.com/satoshilabs/slips/blob/master/slip-0022.md
seed = await get_seed(ctx)
root = bip32.from_seed(seed, "ed25519 cardano seed")
keychain = Keychain(root)
return keychain
return Keychain(bip32.from_seed(seed, "ed25519 cardano seed"))
def with_keychain(func: HandlerWithKeychain[MsgIn, MsgOut]) -> Handler[MsgIn, MsgOut]:

View File

@ -12,7 +12,7 @@ _SESSION_ID_LENGTH = 32
# Traditional cache keys
APP_COMMON_SEED = 0
APP_CARDANO_ROOT = 1
APP_CARDANO_PASSPHRASE = 1
APP_MONERO_LIVE_REFRESH = 2
APP_BASE_AUTHORIZATION = 3
@ -50,7 +50,7 @@ class SessionCache(DataCache):
self.session_id = bytearray(_SESSION_ID_LENGTH)
self.fields = (
64, # APP_COMMON_SEED
128, # APP_CARDANO_ROOT
50, # APP_CARDANO_PASSPHRASE
1, # APP_MONERO_LIVE_REFRESH
128, # APP_BASE_AUTHORIZATION
)