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
|
|
|
|
|
|
|
|
2018-02-06 13:28:22 +00:00
|
|
|
async def derive_node(ctx: wire.Context, path=[], curve_name=_DEFAULT_CURVE):
|
|
|
|
seed = await _get_seed(ctx)
|
|
|
|
node = bip32.from_seed(seed, curve_name)
|
|
|
|
if len(path) > 0:
|
|
|
|
node.derive_path(path)
|
|
|
|
return node
|
2016-10-06 13:04:38 +00:00
|
|
|
|
|
|
|
|
2018-02-06 13:28:22 +00:00
|
|
|
async def _get_seed(ctx: wire.Context) -> bytes:
|
2017-05-08 20:31:21 +00:00
|
|
|
from . import cache
|
2018-02-09 17:59:26 +00:00
|
|
|
if cache.get_seed() is None:
|
2018-02-24 17:58:02 +00:00
|
|
|
seed, passphrase = await _compute_seed(ctx)
|
|
|
|
cache.set_seed(seed, passphrase)
|
2018-02-09 17:59:26 +00:00
|
|
|
return cache.get_seed()
|
2016-10-06 13:04:38 +00:00
|
|
|
|
|
|
|
|
2018-02-24 17:58:02 +00:00
|
|
|
async def _compute_seed(ctx: wire.Context) -> (bytes, str):
|
2017-07-28 16:19:43 +00:00
|
|
|
from trezor.messages.FailureType import ProcessError
|
2017-01-24 13:10:36 +00:00
|
|
|
from .request_passphrase import protect_by_passphrase
|
2016-10-06 13:04:38 +00:00
|
|
|
from . import storage
|
|
|
|
|
2016-10-14 13:35:44 +00:00
|
|
|
if not storage.is_initialized():
|
2017-07-28 16:19:43 +00:00
|
|
|
raise wire.FailureError(ProcessError, 'Device is not initialized')
|
2016-10-06 13:04:38 +00:00
|
|
|
|
2017-08-15 13:09:09 +00:00
|
|
|
passphrase = await protect_by_passphrase(ctx)
|
2018-02-24 17:58:02 +00:00
|
|
|
return bip39.seed(storage.get_mnemonic(), passphrase), passphrase
|
2017-05-23 10:44:36 +00:00
|
|
|
|
|
|
|
|
2018-02-06 13:28:22 +00:00
|
|
|
def derive_node_without_passphrase(path, curve_name=_DEFAULT_CURVE):
|
2017-05-23 10:44:36 +00:00
|
|
|
from . import storage
|
|
|
|
if not storage.is_initialized():
|
|
|
|
raise Exception('Device is not initialized')
|
|
|
|
seed = bip39.seed(storage.get_mnemonic(), '')
|
2018-02-06 13:28:22 +00:00
|
|
|
node = bip32.from_seed(seed, curve_name)
|
|
|
|
node.derive_path(path)
|
|
|
|
return node
|