Skip to content

Commit fd643cf

Browse files
authored
fixed error message and cleaned up code (#103)
1 parent db08bef commit fd643cf

File tree

3 files changed

+20
-22
lines changed

3 files changed

+20
-22
lines changed

btclib/bip32/bip32.py

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ def xpub_from_xprv(xprv: BIP32Key) -> str:
277277

278278

279279
@dataclass
280-
class _ExtendedBIP32KeyData(BIP32KeyData):
280+
class _BIP32KeyData(BIP32KeyData):
281281
# extensions used to cache intermediate results
282282
# in multi-level derivation: do not rely on them elsewhere
283283

@@ -309,16 +309,8 @@ def __init__(
309309
self.assert_valid()
310310

311311

312-
def __child_key_derivation(xkey: _ExtendedBIP32KeyData, index: int) -> None:
313-
xkey.depth += 1
312+
def __prv_key_derivation(xkey: _BIP32KeyData, index: int) -> None:
314313
xkey.index = index
315-
if xkey.is_private:
316-
__private_key_derivation(xkey, index)
317-
else:
318-
__public_key_derivation(xkey, index)
319-
320-
321-
def __private_key_derivation(xkey: _ExtendedBIP32KeyData, index: int) -> None:
322314
Q_bytes = bytes_from_point(mult(xkey.prv_key_int))
323315
xkey.parent_fingerprint = hash160(Q_bytes)[:4]
324316
hmac_ = (
@@ -341,10 +333,9 @@ def __private_key_derivation(xkey: _ExtendedBIP32KeyData, index: int) -> None:
341333
xkey.pub_key_point = INF
342334

343335

344-
def __public_key_derivation(xkey: _ExtendedBIP32KeyData, index: int) -> None:
336+
def __pub_key_derivation(xkey: _BIP32KeyData, index: int) -> None:
337+
xkey.index = index
345338
xkey.parent_fingerprint = hash160(xkey.key)[:4]
346-
if xkey.is_hardened:
347-
raise BTClibValueError("invalid hardened derivation from public key")
348339
hmac_ = hmac.new(
349340
xkey.chain_code,
350341
xkey.key + index.to_bytes(4, byteorder="big", signed=False),
@@ -370,16 +361,14 @@ def _derive(
370361
err_msg = f"final depth greater than 255: {final_depth}"
371362
raise BTClibValueError(err_msg)
372363

373-
xkey = _ExtendedBIP32KeyData(
364+
xkey = _BIP32KeyData(
374365
version=xkey.version,
375-
depth=xkey.depth,
366+
depth=final_depth,
376367
parent_fingerprint=xkey.parent_fingerprint,
377368
index=xkey.index,
378369
chain_code=xkey.chain_code,
379370
key=xkey.key,
380371
)
381-
for index in indexes:
382-
__child_key_derivation(xkey, index)
383372

384373
if forced_version:
385374
if xkey.version in XPRV_VERSIONS_ALL:
@@ -393,6 +382,15 @@ def _derive(
393382
raise BTClibValueError(err_msg)
394383
xkey.version = fversion
395384

385+
if xkey.is_private:
386+
for index in indexes:
387+
__prv_key_derivation(xkey, index)
388+
else:
389+
if any(index >= 0x80000000 for index in indexes):
390+
raise BTClibValueError("invalid hardened derivation from public key")
391+
for index in indexes:
392+
__pub_key_derivation(xkey, index)
393+
396394
return xkey
397395

398396

@@ -438,7 +436,7 @@ def _derive_from_account(
438436
if address_index >= 0x80000000:
439437
raise BTClibValueError("invalid private derivation at address index level")
440438
if address_index > max_index:
441-
raise BTClibValueError(f"too high address index: {branch}")
439+
raise BTClibValueError(f"too high address index: {address_index}")
442440

443441
return _derive(mxkey, f"m/{branch}/{address_index}")
444442

btclib/ec/curve_group.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ def multiples(Q: JacPoint, size: int, ec: CurveGroup) -> list[JacPoint]:
481481
MAX_W = 5
482482

483483

484-
@functools.lru_cache() # least recently used cache
484+
@functools.lru_cache() # results are cached to increase efficiency
485485
def cached_multiples(Q: JacPoint, ec: CurveGroup) -> list[JacPoint]:
486486
T = [INFJ, Q]
487487
for i in range(3, 2**MAX_W, 2):
@@ -490,7 +490,7 @@ def cached_multiples(Q: JacPoint, ec: CurveGroup) -> list[JacPoint]:
490490
return T
491491

492492

493-
@functools.lru_cache()
493+
@functools.lru_cache() # results are cached to increase efficiency
494494
def cached_multiples_fixwind(
495495
Q: JacPoint, ec: CurveGroup, w: int = 4
496496
) -> list[list[JacPoint]]:

tests/bip32/test_bip32.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,15 +300,15 @@ def test_derive_from_account() -> None:
300300
with pytest.raises(BTClibValueError, match=err_msg):
301301
derive_from_account(mxpub, 0xFFFF + 1, 0)
302302

303-
err_msg = "invalid branch: "
303+
err_msg = "invalid branch: 2"
304304
with pytest.raises(BTClibValueError, match=err_msg):
305305
derive_from_account(mxpub, 2, 0)
306306

307307
err_msg = "invalid private derivation at address index level"
308308
with pytest.raises(BTClibValueError, match=err_msg):
309309
derive_from_account(mxpub, 0, 0x80000000)
310310

311-
err_msg = "too high address index: "
311+
err_msg = "too high address index: 65536"
312312
with pytest.raises(BTClibValueError, match=err_msg):
313313
derive_from_account(mxpub, 0, 0xFFFF + 1)
314314

0 commit comments

Comments
 (0)