@@ -277,7 +277,7 @@ def xpub_from_xprv(xprv: BIP32Key) -> str:
277
277
278
278
279
279
@dataclass
280
- class _ExtendedBIP32KeyData (BIP32KeyData ):
280
+ class _BIP32KeyData (BIP32KeyData ):
281
281
# extensions used to cache intermediate results
282
282
# in multi-level derivation: do not rely on them elsewhere
283
283
@@ -309,16 +309,8 @@ def __init__(
309
309
self .assert_valid ()
310
310
311
311
312
- def __child_key_derivation (xkey : _ExtendedBIP32KeyData , index : int ) -> None :
313
- xkey .depth += 1
312
+ def __prv_key_derivation (xkey : _BIP32KeyData , index : int ) -> None :
314
313
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 :
322
314
Q_bytes = bytes_from_point (mult (xkey .prv_key_int ))
323
315
xkey .parent_fingerprint = hash160 (Q_bytes )[:4 ]
324
316
hmac_ = (
@@ -341,10 +333,9 @@ def __private_key_derivation(xkey: _ExtendedBIP32KeyData, index: int) -> None:
341
333
xkey .pub_key_point = INF
342
334
343
335
344
- def __public_key_derivation (xkey : _ExtendedBIP32KeyData , index : int ) -> None :
336
+ def __pub_key_derivation (xkey : _BIP32KeyData , index : int ) -> None :
337
+ xkey .index = index
345
338
xkey .parent_fingerprint = hash160 (xkey .key )[:4 ]
346
- if xkey .is_hardened :
347
- raise BTClibValueError ("invalid hardened derivation from public key" )
348
339
hmac_ = hmac .new (
349
340
xkey .chain_code ,
350
341
xkey .key + index .to_bytes (4 , byteorder = "big" , signed = False ),
@@ -370,16 +361,14 @@ def _derive(
370
361
err_msg = f"final depth greater than 255: { final_depth } "
371
362
raise BTClibValueError (err_msg )
372
363
373
- xkey = _ExtendedBIP32KeyData (
364
+ xkey = _BIP32KeyData (
374
365
version = xkey .version ,
375
- depth = xkey . depth ,
366
+ depth = final_depth ,
376
367
parent_fingerprint = xkey .parent_fingerprint ,
377
368
index = xkey .index ,
378
369
chain_code = xkey .chain_code ,
379
370
key = xkey .key ,
380
371
)
381
- for index in indexes :
382
- __child_key_derivation (xkey , index )
383
372
384
373
if forced_version :
385
374
if xkey .version in XPRV_VERSIONS_ALL :
@@ -393,6 +382,15 @@ def _derive(
393
382
raise BTClibValueError (err_msg )
394
383
xkey .version = fversion
395
384
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
+
396
394
return xkey
397
395
398
396
@@ -438,7 +436,7 @@ def _derive_from_account(
438
436
if address_index >= 0x80000000 :
439
437
raise BTClibValueError ("invalid private derivation at address index level" )
440
438
if address_index > max_index :
441
- raise BTClibValueError (f"too high address index: { branch } " )
439
+ raise BTClibValueError (f"too high address index: { address_index } " )
442
440
443
441
return _derive (mxkey , f"m/{ branch } /{ address_index } " )
444
442
0 commit comments