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
|
|
|
|
|
|
|
|
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:
|
2017-05-08 20:31:21 +00:00
|
|
|
from . import cache
|
|
|
|
if cache.seed is None:
|
|
|
|
cache.seed = await compute_seed(session_id)
|
|
|
|
return cache.seed
|
2016-10-06 13:04:38 +00:00
|
|
|
|
|
|
|
|
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
|
2017-01-24 13:10:36 +00:00
|
|
|
from .request_passphrase import protect_by_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
|
|
|
|
2017-01-24 13:10:36 +00:00
|
|
|
passphrase = await protect_by_passphrase(session_id)
|
2016-10-14 13:35:44 +00:00
|
|
|
return bip39.seed(storage.get_mnemonic(), passphrase)
|
2017-05-23 10:44:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
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
|