1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-16 04:29:08 +00:00
trezor-firmware/src/apps/common/seed.py

51 lines
1.5 KiB
Python
Raw Normal View History

from trezor import wire
2018-02-27 15:35:21 +00:00
from trezor.crypto import bip32, bip39
2018-07-03 14:20:26 +00:00
2018-02-27 15:35:21 +00:00
from apps.common import cache, storage
from apps.common.request_passphrase import protect_by_passphrase
2018-07-03 14:20:58 +00:00
_DEFAULT_CURVE = "secp256k1"
2018-07-03 14:20:58 +00:00
async def derive_node(
ctx: wire.Context, path: list, curve_name: str = _DEFAULT_CURVE
) -> bip32.HDNode:
seed = await _get_cached_seed(ctx)
node = bip32.from_seed(seed, curve_name)
2018-06-21 12:00:02 +00:00
node.derive_path(path)
return node
async def _get_cached_seed(ctx: wire.Context) -> bytes:
if not storage.is_initialized():
2018-07-03 14:20:58 +00:00
raise wire.ProcessError("Device is not initialized")
if cache.get_seed() is None:
passphrase = await _get_cached_passphrase(ctx)
seed = bip39.seed(storage.get_mnemonic(), passphrase)
cache.set_seed(seed)
return cache.get_seed()
async def _get_cached_passphrase(ctx: wire.Context) -> str:
if cache.get_passphrase() is None:
passphrase = await protect_by_passphrase(ctx)
cache.set_passphrase(passphrase)
return cache.get_passphrase()
2018-07-03 14:20:58 +00:00
def derive_node_without_passphrase(
path: list, curve_name: str = _DEFAULT_CURVE
) -> bip32.HDNode:
if not storage.is_initialized():
2018-07-03 14:20:58 +00:00
raise Exception("Device is not initialized")
2018-02-27 15:35:21 +00:00
2018-07-03 14:20:58 +00:00
seed = bip39.seed(storage.get_mnemonic(), "")
node = bip32.from_seed(seed, curve_name)
node.derive_path(path)
return node
2018-05-11 13:31:53 +00:00
def remove_ed25519_public_key_prefix(pubkey: bytes) -> bytes:
# 0x01 prefix is not part of the actual public key, hence removed
return pubkey[1:]