mirror of
https://github.com/trezor/trezor-firmware.git
synced 2025-04-28 13:09:04 +00:00
refactor(core/cardano): update caching mechanism
This commit is contained in:
parent
3cdb09c294
commit
276bb59dba
@ -71,24 +71,31 @@ def derive_path_cardano(root: bip32.HDNode, path: Bip32Path) -> bip32.HDNode:
|
|||||||
return node
|
return node
|
||||||
|
|
||||||
|
|
||||||
@cache.stored_async(cache.APP_CARDANO_ROOT)
|
@cache.stored_async(cache.APP_CARDANO_PASSPHRASE)
|
||||||
async def get_keychain(ctx: wire.Context) -> Keychain:
|
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():
|
if not device.is_initialized():
|
||||||
raise wire.NotInitialized("Device is not 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():
|
if mnemonic.is_bip39():
|
||||||
passphrase = await get_passphrase(ctx)
|
return await _get_keychain_bip39(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)
|
|
||||||
else:
|
else:
|
||||||
# derive the root node via SLIP-0023 https://github.com/satoshilabs/slips/blob/master/slip-0022.md
|
# derive the root node via SLIP-0023 https://github.com/satoshilabs/slips/blob/master/slip-0022.md
|
||||||
seed = await get_seed(ctx)
|
seed = await get_seed(ctx)
|
||||||
root = bip32.from_seed(seed, "ed25519 cardano seed")
|
return Keychain(bip32.from_seed(seed, "ed25519 cardano seed"))
|
||||||
|
|
||||||
keychain = Keychain(root)
|
|
||||||
return keychain
|
|
||||||
|
|
||||||
|
|
||||||
def with_keychain(func: HandlerWithKeychain[MsgIn, MsgOut]) -> Handler[MsgIn, MsgOut]:
|
def with_keychain(func: HandlerWithKeychain[MsgIn, MsgOut]) -> Handler[MsgIn, MsgOut]:
|
||||||
|
@ -12,7 +12,7 @@ _SESSION_ID_LENGTH = 32
|
|||||||
|
|
||||||
# Traditional cache keys
|
# Traditional cache keys
|
||||||
APP_COMMON_SEED = 0
|
APP_COMMON_SEED = 0
|
||||||
APP_CARDANO_ROOT = 1
|
APP_CARDANO_PASSPHRASE = 1
|
||||||
APP_MONERO_LIVE_REFRESH = 2
|
APP_MONERO_LIVE_REFRESH = 2
|
||||||
APP_BASE_AUTHORIZATION = 3
|
APP_BASE_AUTHORIZATION = 3
|
||||||
|
|
||||||
@ -50,7 +50,7 @@ class SessionCache(DataCache):
|
|||||||
self.session_id = bytearray(_SESSION_ID_LENGTH)
|
self.session_id = bytearray(_SESSION_ID_LENGTH)
|
||||||
self.fields = (
|
self.fields = (
|
||||||
64, # APP_COMMON_SEED
|
64, # APP_COMMON_SEED
|
||||||
128, # APP_CARDANO_ROOT
|
50, # APP_CARDANO_PASSPHRASE
|
||||||
1, # APP_MONERO_LIVE_REFRESH
|
1, # APP_MONERO_LIVE_REFRESH
|
||||||
128, # APP_BASE_AUTHORIZATION
|
128, # APP_BASE_AUTHORIZATION
|
||||||
)
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user