From c1f0c642df6b024edc0aac16a59328ac084bcdba Mon Sep 17 00:00:00 2001 From: Andrew Kozlik <42678794+andrewkozlik@users.noreply.github.com> Date: Mon, 2 Sep 2019 12:09:03 +0200 Subject: [PATCH] core: cache seed without passphrase (#478) --- core/src/apps/common/cache.py | 11 +++++++++++ core/src/apps/common/seed.py | 10 ++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/core/src/apps/common/cache.py b/core/src/apps/common/cache.py index 435cc4771b..e22fd48a7f 100644 --- a/core/src/apps/common/cache.py +++ b/core/src/apps/common/cache.py @@ -6,6 +6,7 @@ if False: from typing import Optional _cached_seed = None # type: Optional[bytes] +_cached_seed_without_passphrase = None # type: Optional[bytes] _cached_passphrase = None # type: Optional[str] _cached_passphrase_fprint = b"\x00\x00\x00\x00" # type: bytes @@ -36,6 +37,10 @@ def get_seed() -> Optional[bytes]: return _cached_seed +def get_seed_without_passphrase() -> Optional[bytes]: + return _cached_seed_without_passphrase + + def get_passphrase() -> Optional[str]: return _cached_passphrase @@ -53,6 +58,11 @@ def set_seed(seed: Optional[bytes]) -> None: _cached_seed = seed +def set_seed_without_passphrase(seed: Optional[bytes]) -> None: + global _cached_seed_without_passphrase + _cached_seed_without_passphrase = seed + + def set_passphrase(passphrase: Optional[str]) -> None: global _cached_passphrase, _cached_passphrase_fprint _cached_passphrase = passphrase @@ -61,5 +71,6 @@ def set_passphrase(passphrase: Optional[str]) -> None: def clear(keep_passphrase: bool = False) -> None: set_seed(None) + set_seed_without_passphrase(None) if not keep_passphrase: set_passphrase(None) diff --git a/core/src/apps/common/seed.py b/core/src/apps/common/seed.py index 3f87aeaba8..82adf40eac 100644 --- a/core/src/apps/common/seed.py +++ b/core/src/apps/common/seed.py @@ -126,7 +126,10 @@ def derive_node_without_passphrase( ) -> bip32.HDNode: if not storage.is_initialized(): raise Exception("Device is not initialized") - seed = mnemonic.get_seed(progress_bar=False) + seed = cache.get_seed_without_passphrase() + if seed is None: + seed = mnemonic.get_seed(progress_bar=False) + cache.set_seed_without_passphrase(seed) node = bip32.from_seed(seed, curve_name) node.derive_path(path) return node @@ -135,7 +138,10 @@ def derive_node_without_passphrase( def derive_slip21_node_without_passphrase(path: list) -> Slip21Node: if not storage.is_initialized(): raise Exception("Device is not initialized") - seed = mnemonic.get_seed(progress_bar=False) + seed = cache.get_seed_without_passphrase() + if seed is None: + seed = mnemonic.get_seed(progress_bar=False) + cache.set_seed_without_passphrase(seed) node = Slip21Node(seed) node.derive_path(path) return node