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

45 lines
1.3 KiB
Python
Raw Normal View History

from trezor import wire
from trezor.crypto import bip32
from trezor.crypto import bip39
_DEFAULT_CURVE = 'secp256k1'
async def get_root(session_id: int, curve_name=_DEFAULT_CURVE):
seed = await get_seed(session_id)
root = bip32.from_seed(seed, curve_name)
return root
async def get_seed(session_id: int) -> bytes:
from . import cache
if cache.seed is None:
cache.seed = await compute_seed(session_id)
return cache.seed
async def compute_seed(session_id: int) -> bytes:
from trezor.messages.FailureType import Other
2017-01-24 13:10:36 +00:00
from .request_passphrase import protect_by_passphrase
from .request_pin import protect_by_pin
from . import storage
if not storage.is_initialized():
raise wire.FailureError(Other, 'Device is not initialized')
await protect_by_pin(session_id)
2017-01-24 13:10:36 +00:00
passphrase = await protect_by_passphrase(session_id)
return bip39.seed(storage.get_mnemonic(), passphrase)
def get_root_without_passphrase(curve_name=_DEFAULT_CURVE):
from . import storage
if not storage.is_initialized():
raise Exception('Device is not initialized')
if storage.is_locked():
raise Exception('Unlock first')
seed = bip39.seed(storage.get_mnemonic(), '')
root = bip32.from_seed(seed, curve_name)
return root