2016-10-06 13:04:38 +00:00
|
|
|
from trezor import wire
|
2016-12-12 14:18:46 +00:00
|
|
|
from trezor.crypto import bip32
|
|
|
|
from trezor.crypto import bip39
|
2016-10-06 13:04:38 +00:00
|
|
|
|
2016-12-12 14:18:46 +00:00
|
|
|
_DEFAULT_CURVE = 'secp256k1'
|
2016-10-06 13:04:38 +00:00
|
|
|
|
|
|
|
_cached_seed = None
|
|
|
|
|
|
|
|
|
2016-12-12 14:18:46 +00:00
|
|
|
async def get_root(session_id: int, curve_name=_DEFAULT_CURVE):
|
2016-10-06 13:04:38 +00:00
|
|
|
seed = await get_seed(session_id)
|
2016-12-12 14:18:46 +00:00
|
|
|
root = bip32.from_seed(seed, curve_name)
|
|
|
|
return root
|
2016-10-06 13:04:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def get_seed(session_id: int) -> bytes:
|
|
|
|
global _cached_seed
|
|
|
|
if _cached_seed is None:
|
|
|
|
_cached_seed = await compute_seed(session_id)
|
|
|
|
return _cached_seed
|
|
|
|
|
|
|
|
|
2016-12-12 14:18:46 +00:00
|
|
|
async def compute_seed(session_id: int) -> bytes:
|
2016-11-23 13:51:39 +00:00
|
|
|
from trezor.messages.FailureType import Other
|
2016-10-20 13:04:54 +00:00
|
|
|
from .request_passphrase import request_passphrase
|
2016-11-23 13:51:39 +00:00
|
|
|
from .request_pin import protect_by_pin
|
2016-10-06 13:04:38 +00:00
|
|
|
from . import storage
|
|
|
|
|
2016-10-14 13:35:44 +00:00
|
|
|
if not storage.is_initialized():
|
2016-10-06 13:04:38 +00:00
|
|
|
raise wire.FailureError(Other, 'Device is not initialized')
|
|
|
|
|
2016-11-23 13:51:39 +00:00
|
|
|
await protect_by_pin(session_id)
|
2016-10-06 13:04:38 +00:00
|
|
|
|
2016-10-14 13:35:44 +00:00
|
|
|
if storage.is_protected_by_passphrase():
|
2016-10-20 13:04:54 +00:00
|
|
|
passphrase = await request_passphrase(session_id)
|
2016-10-06 13:04:38 +00:00
|
|
|
else:
|
|
|
|
passphrase = ''
|
2016-10-14 13:35:44 +00:00
|
|
|
return bip39.seed(storage.get_mnemonic(), passphrase)
|