Skip to content

Commit

Permalink
use embit for key generation
Browse files Browse the repository at this point in the history
  • Loading branch information
callebtc committed Feb 5, 2024
1 parent e02e4bb commit 3152bed
Showing 1 changed file with 31 additions and 11 deletions.
42 changes: 31 additions & 11 deletions cashu/core/crypto/keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,15 @@
import random
from typing import Dict

from bip32 import BIP32
# we import bip32 from embit if bip32 is not installed
try:
from bip32 import BIP32

use_bip32_lib = True
except ImportError:
from embit import bip32 # type: ignore

use_bip32_lib = False

from ..settings import settings
from .secp import PrivateKey, PublicKey
Expand All @@ -13,16 +21,28 @@ def derive_keys(mnemonic: str, derivation_path: str):
"""
Deterministic derivation of keys for 2^n values.
"""
bip32 = BIP32.from_seed(mnemonic.encode())
orders_str = [f"/{i}'" for i in range(settings.max_order)]
return {
2
** i: PrivateKey(
bip32.get_privkey_from_path(derivation_path + orders_str[i]),
raw=True,
)
for i in range(settings.max_order)
}
if use_bip32_lib:
root = BIP32.from_seed(mnemonic.encode()) # type: ignore
orders_str = [f"/{i}'" for i in range(settings.max_order)]
return {
2
** i: PrivateKey(
root.get_privkey_from_path(derivation_path + orders_str[i]),
raw=True,
)
for i in range(settings.max_order)
}
else:
root = bip32.HDKey.from_seed(mnemonic.encode()) # type: ignore
orders_str = [f"/{i}'" for i in range(settings.max_order)]
return {
2
** i: PrivateKey(
root.derive(derivation_path + orders_str[i]).key.serialize(), # type: ignore
raw=True,
)
for i in range(settings.max_order)
}


def derive_keys_sha256(master_key: str, derivation_path: str = ""):
Expand Down

0 comments on commit 3152bed

Please sign in to comment.