@@ -124,10 +124,10 @@ where
124
124
}
125
125
126
126
let key_bytes = self . key ;
127
- let mut mac = Hmac :: < sha2:: Sha512 > :: new_varkey ( key_bytes)
127
+ let mut mac = Hmac :: < sha2:: Sha512 > :: new_from_slice ( key_bytes)
128
128
. map_err ( |_| MasterKeyGenError :: InvalidKeyLength ) ?;
129
- mac. input ( seed_bytes) ;
130
- let result = mac. result ( ) . code ( ) ;
129
+ mac. update ( seed_bytes) ;
130
+ let result = mac. finalize ( ) . into_bytes ( ) ;
131
131
let ( sk_bytes, chain_code_bytes) = result. split_at ( 32 ) ;
132
132
133
133
// secret/chain_code computation might panic if length returned by hmac is wrong
@@ -281,26 +281,26 @@ impl ExtendedSK {
281
281
for index in path. iter ( ) {
282
282
extended_sk = extended_sk. child ( index) ?
283
283
}
284
-
285
284
Ok ( extended_sk)
286
285
}
287
286
288
287
/// Try to get a private child key from parent
289
288
pub fn child ( & self , index : & KeyPathIndex ) -> Result < ExtendedSK , KeyDerivationError > {
290
289
let mut hmac512: Hmac < sha2:: Sha512 > =
291
- Hmac :: new_varkey ( & self . chain_code ) . map_err ( |_| KeyDerivationError :: InvalidKeyLength ) ?;
290
+ Hmac :: new_from_slice ( & self . chain_code . as_ref ( ) ) . map_err ( |_| KeyDerivationError :: InvalidKeyLength ) ?;
292
291
let index_bytes = index. as_ref ( ) . to_be_bytes ( ) ;
293
-
294
292
if index. is_hardened ( ) {
295
- hmac512. input ( & [ 0 ] ) ; // BIP-32 padding that makes key 33 bytes long
296
- hmac512. input ( & self . secret_key [ ..] ) ;
293
+ hmac512. update ( & [ 0u8 ] ) ; // BIP-32 padding that makes key 33 bytes long
294
+ hmac512. update ( & self . secret_key [ ..] ) ;
297
295
} else {
298
- hmac512. input ( & PublicKey :: from_secret_key_global ( & self . secret_key ) . serialize ( ) ) ;
296
+ hmac512. update ( & PublicKey :: from_secret_key_global ( & self . secret_key ) . serialize ( ) . as_ref ( ) ) ;
299
297
}
300
298
301
- let ( chain_code, secret_key) = get_chain_code_and_secret ( & index_bytes, hmac512) ?;
299
+ hmac512. update ( & index_bytes. as_ref ( ) ) ;
300
+
301
+ let ( chain_code, mut secret_key) = get_chain_code_and_secret ( & hmac512) ?;
302
302
303
- secret_key
303
+ secret_key = self . secret_key
304
304
. add_tweak ( & Scalar :: from ( secret_key) )
305
305
. map_err ( KeyDerivationError :: Secp256k1Error ) ?;
306
306
@@ -458,11 +458,10 @@ impl From<Vec<u32>> for KeyPath {
458
458
459
459
#[ inline]
460
460
fn get_chain_code_and_secret (
461
- seed : & [ u8 ] ,
462
- mut hmac512 : Hmac < sha2:: Sha512 > ,
461
+ hmac512 : & Hmac < sha2:: Sha512 > ,
463
462
) -> Result < ( Protected , SecretKey ) , KeyDerivationError > {
464
- hmac512. input ( seed ) ;
465
- let i = hmac512 . result ( ) . code ( ) ;
463
+ let binding = & hmac512. clone ( ) . finalize ( ) . into_bytes ( ) ;
464
+ let i = binding . iter ( ) . as_slice ( ) ;
466
465
let ( il, ir) = i. split_at ( 32 ) ;
467
466
let chain_code = Protected :: from ( ir) ;
468
467
let secret_key = SecretKey :: from_slice ( il) . map_err ( KeyDerivationError :: Secp256k1Error ) ?;
@@ -530,11 +529,11 @@ mod tests {
530
529
531
530
let master_key = MasterKeyGen :: new ( & seed[ ..] ) . generate ( ) . unwrap ( ) ;
532
531
533
- let expected_secret_key = [
532
+ let expected_secret_key: [ u8 ; 32 ] = [
534
533
79 , 67 , 227 , 208 , 107 , 229 , 51 , 169 , 104 , 61 , 121 , 142 , 8 , 143 , 75 , 74 , 235 , 179 , 67 ,
535
534
213 , 108 , 252 , 255 , 16 , 32 , 162 , 57 , 21 , 195 , 162 , 115 , 128 ,
536
535
] ;
537
- assert_eq ! ( expected_secret_key, & master_key. secret_key[ .. ] ) ;
536
+ assert_eq ! ( expected_secret_key, * master_key. secret_key. as_ref ( ) ) ;
538
537
}
539
538
540
539
#[ test]
@@ -555,7 +554,7 @@ mod tests {
555
554
. index ( 0 ) ; // address: 0
556
555
let account = extended_sk. derive ( & path) . unwrap ( ) ;
557
556
558
- let expected_account = [
557
+ let expected_account: [ u8 ; 32 ] = [
559
558
137 , 174 , 230 , 121 , 4 , 190 , 53 , 238 , 47 , 181 , 52 , 226 , 109 , 68 , 153 , 170 , 112 , 150 , 84 ,
560
559
84 , 26 , 177 , 194 , 157 , 76 , 80 , 136 , 25 , 6 , 79 , 247 , 43 ,
561
560
] ;
@@ -575,15 +574,15 @@ mod tests {
575
574
let master_key = MasterKeyGen :: new ( & seed) . generate ( ) . unwrap ( ) ;
576
575
577
576
for ( expected, keypath) in slip32_vectors ( ) {
578
- let key = master_key. derive ( & keypath) . unwrap ( ) ;
577
+ let key = & master_key. derive ( & keypath) . unwrap ( ) ;
579
578
let xprv = key. to_slip32 ( & keypath) . unwrap ( ) ;
580
579
581
580
assert_eq ! ( expected, xprv) ;
582
581
583
582
let ( recovered_key, path) = ExtendedSK :: from_slip32 ( & xprv) . unwrap ( ) ;
584
583
585
584
assert_eq ! ( keypath, path) ;
586
- assert_eq ! ( key, recovered_key) ;
585
+ assert_eq ! ( key, & recovered_key) ;
587
586
}
588
587
}
589
588
}
0 commit comments