Skip to content

Commit d6838b3

Browse files
UPDATE. explicitly requiring from_entropy() method to have a string default value for passphrase parameter
REFACTOR. pulling out duplicate seed creating logic into a class method ADD. adding test_is_entropy() testcase against meumonic package generated entropy value
1 parent 692682e commit d6838b3

File tree

2 files changed

+21
-15
lines changed

2 files changed

+21
-15
lines changed

pycardano/crypto/bip32.py

+13-15
Original file line numberDiff line numberDiff line change
@@ -155,18 +155,8 @@ def from_mnemonic(cls, mnemonic: str, passphrase: str = "") -> HDWallet:
155155
raise ValueError("Invalid mnemonic words.")
156156

157157
mnemonic = unicodedata.normalize("NFKD", mnemonic)
158-
passphrase = str(passphrase) if passphrase else ""
159158
entropy = Mnemonic(language="english").to_entropy(words=mnemonic)
160-
161-
seed = bytearray(
162-
hashlib.pbkdf2_hmac(
163-
"sha512",
164-
password=passphrase.encode(),
165-
salt=entropy,
166-
iterations=4096,
167-
dklen=96,
168-
)
169-
)
159+
seed = cls._generate_seed(passphrase, entropy)
170160

171161
return cls.from_seed(
172162
seed=hexlify(seed).decode(),
@@ -176,7 +166,7 @@ def from_mnemonic(cls, mnemonic: str, passphrase: str = "") -> HDWallet:
176166
)
177167

178168
@classmethod
179-
def from_entropy(cls, entropy: str, passphrase: str = None) -> HDWallet:
169+
def from_entropy(cls, entropy: str, passphrase: str = "") -> HDWallet:
180170
"""
181171
Create master key and HDWallet from Mnemonic words.
182172
@@ -191,12 +181,20 @@ def from_entropy(cls, entropy: str, passphrase: str = None) -> HDWallet:
191181
if not cls.is_entropy(entropy):
192182
raise ValueError("Invalid entropy")
193183

194-
seed = bytearray(
184+
seed = cls._generate_seed(passphrase, bytearray.fromhex(entropy))
185+
return cls.from_seed(seed=hexlify(seed).decode(), entropy=entropy)
186+
187+
@classmethod
188+
def _generate_seed(cls, passphrase: str, entropy: bytearray) -> bytearray:
189+
return bytearray(
195190
hashlib.pbkdf2_hmac(
196-
"sha512", password=passphrase, salt=entropy, iterations=4096, dklen=96
191+
"sha512",
192+
password=passphrase.encode(),
193+
salt=entropy,
194+
iterations=4096,
195+
dklen=96,
197196
)
198197
)
199-
return cls.from_seed(seed=hexlify(seed).decode(), entropy=entropy)
200198

201199
@classmethod
202200
def _tweak_bits(cls, seed: bytearray) -> bytes:

test/pycardano/backend/test_bip32.py

+8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import pytest
2+
13
from pycardano.address import Address
24
from pycardano.crypto.bip32 import HDWallet
35
from pycardano.key import PaymentVerificationKey
@@ -108,3 +110,9 @@ def test_payment_address_24_base():
108110
Address(spend_vk.hash(), stake_vk.hash(), network=Network.MAINNET).encode()
109111
== "addr1qyy6nhfyks7wdu3dudslys37v252w2nwhv0fw2nfawemmn8k8ttq8f3gag0h89aepvx3xf69g0l9pf80tqv7cve0l33sdn8p3d"
110112
)
113+
114+
115+
def test_is_entropy():
116+
entropy = "df9ed25ed146bf43336a5d7cf7395994"
117+
is_entropy = HDWallet.is_entropy(entropy)
118+
assert is_entropy

0 commit comments

Comments
 (0)