Skip to content

Commit ae0c1a3

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 ae0c1a3

File tree

1 file changed

+13
-15
lines changed

1 file changed

+13
-15
lines changed

pycardano/crypto/bip32.py

Lines changed: 13 additions & 15 deletions
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:

0 commit comments

Comments
 (0)