@@ -28,8 +28,9 @@ use hashes::{sha512, Hash, HashEngine, Hmac, HmacEngine};
28
28
use secp256k1:: { self , Secp256k1 } ;
29
29
30
30
use network:: constants:: Network ;
31
- use util:: { base58, endian, key} ;
32
- use util:: ecdsa:: { PublicKey , PrivateKey } ;
31
+ use util:: { base58, endian} ;
32
+ use util:: key;
33
+ use io:: Write ;
33
34
34
35
/// A chain code
35
36
#[ derive( Copy , Clone , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
@@ -44,7 +45,8 @@ impl_array_newtype!(Fingerprint, u8, 4);
44
45
impl_bytes_newtype ! ( Fingerprint , 4 ) ;
45
46
46
47
/// Extended private key
47
- #[ derive( Copy , Clone , PartialEq , Eq , Debug ) ]
48
+ #[ derive( Copy , Clone , PartialEq , Eq ) ]
49
+ #[ cfg_attr( feature = "std" , derive( Debug ) ) ]
48
50
pub struct ExtendedPrivKey {
49
51
/// The network this key is to be used on
50
52
pub network : Network ,
@@ -55,12 +57,26 @@ pub struct ExtendedPrivKey {
55
57
/// Child number of the key used to derive from parent (0 for master)
56
58
pub child_number : ChildNumber ,
57
59
/// Private key
58
- pub private_key : PrivateKey ,
60
+ pub private_key : secp256k1 :: SecretKey ,
59
61
/// Chain code
60
62
pub chain_code : ChainCode
61
63
}
62
64
serde_string_impl ! ( ExtendedPrivKey , "a BIP-32 extended private key" ) ;
63
65
66
+ #[ cfg( not( feature = "std" ) ) ]
67
+ #[ cfg_attr( docsrs, doc( cfg( not( feature = "std" ) ) ) ) ]
68
+ impl fmt:: Debug for ExtendedPrivKey {
69
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
70
+ f. debug_struct ( "ExtendedPrivKey" )
71
+ . field ( "network" , & self . network )
72
+ . field ( "depth" , & self . depth )
73
+ . field ( "parent_fingerprint" , & self . parent_fingerprint )
74
+ . field ( "child_number" , & self . child_number )
75
+ . field ( "chain_code" , & self . chain_code )
76
+ . finish_non_exhaustive ( )
77
+ }
78
+ }
79
+
64
80
/// Extended public key
65
81
#[ derive( Copy , Clone , PartialEq , Eq , Debug , PartialOrd , Ord , Hash ) ]
66
82
pub struct ExtendedPubKey {
@@ -73,7 +89,7 @@ pub struct ExtendedPubKey {
73
89
/// Child number of the key used to derive from parent (0 for master)
74
90
pub child_number : ChildNumber ,
75
91
/// Public key
76
- pub public_key : PublicKey ,
92
+ pub public_key : secp256k1 :: PublicKey ,
77
93
/// Chain code
78
94
pub chain_code : ChainCode
79
95
}
@@ -506,7 +522,7 @@ impl ExtendedPrivKey {
506
522
depth : 0 ,
507
523
parent_fingerprint : Default :: default ( ) ,
508
524
child_number : ChildNumber :: from_normal_idx ( 0 ) ?,
509
- private_key : PrivateKey :: from_slice ( & hmac_result[ ..32 ] , network ) ?,
525
+ private_key : secp256k1 :: SecretKey :: from_slice ( & hmac_result[ ..32 ] ) ?,
510
526
chain_code : ChainCode :: from ( & hmac_result[ 32 ..] ) ,
511
527
} )
512
528
}
@@ -532,7 +548,7 @@ impl ExtendedPrivKey {
532
548
match i {
533
549
ChildNumber :: Normal { .. } => {
534
550
// Non-hardened key: compute public data and use that
535
- hmac_engine. input ( & PublicKey :: from_private_key ( secp, & self . private_key ) . key . serialize ( ) [ ..] ) ;
551
+ hmac_engine. input ( & secp256k1 :: PublicKey :: from_secret_key ( secp, & self . private_key ) . serialize ( ) [ ..] ) ;
536
552
}
537
553
ChildNumber :: Hardened { .. } => {
538
554
// Hardened key: use only secret data to prevent public derivation
@@ -543,8 +559,8 @@ impl ExtendedPrivKey {
543
559
544
560
hmac_engine. input ( & endian:: u32_to_array_be ( u32:: from ( i) ) ) ;
545
561
let hmac_result: Hmac < sha512:: Hash > = Hmac :: from_engine ( hmac_engine) ;
546
- let mut sk = PrivateKey :: from_slice ( & hmac_result[ ..32 ] , self . network ) ?;
547
- sk. key . add_assign ( & self . private_key [ ..] ) ?;
562
+ let mut sk = secp256k1 :: SecretKey :: from_slice ( & hmac_result[ ..32 ] ) ?;
563
+ sk. add_assign ( & self . private_key [ ..] ) ?;
548
564
549
565
Ok ( ExtendedPrivKey {
550
566
network : self . network ,
@@ -578,7 +594,7 @@ impl ExtendedPrivKey {
578
594
parent_fingerprint : Fingerprint :: from ( & data[ 5 ..9 ] ) ,
579
595
child_number : endian:: slice_to_u32_be ( & data[ 9 ..13 ] ) . into ( ) ,
580
596
chain_code : ChainCode :: from ( & data[ 13 ..45 ] ) ,
581
- private_key : PrivateKey :: from_slice ( & data[ 46 ..78 ] , network ) ?,
597
+ private_key : secp256k1 :: SecretKey :: from_slice ( & data[ 46 ..78 ] ) ?,
582
598
} )
583
599
}
584
600
@@ -617,7 +633,7 @@ impl ExtendedPubKey {
617
633
depth : sk. depth ,
618
634
parent_fingerprint : sk. parent_fingerprint ,
619
635
child_number : sk. child_number ,
620
- public_key : PublicKey :: from_private_key ( secp, & sk. private_key ) ,
636
+ public_key : secp256k1 :: PublicKey :: from_secret_key ( secp, & sk. private_key ) ,
621
637
chain_code : sk. chain_code
622
638
}
623
639
}
@@ -638,19 +654,19 @@ impl ExtendedPubKey {
638
654
}
639
655
640
656
/// Compute the scalar tweak added to this key to get a child key
641
- pub fn ckd_pub_tweak ( & self , i : ChildNumber ) -> Result < ( PrivateKey , ChainCode ) , Error > {
657
+ pub fn ckd_pub_tweak ( & self , i : ChildNumber ) -> Result < ( secp256k1 :: SecretKey , ChainCode ) , Error > {
642
658
match i {
643
659
ChildNumber :: Hardened { .. } => {
644
660
Err ( Error :: CannotDeriveFromHardenedKey )
645
661
}
646
662
ChildNumber :: Normal { index : n } => {
647
663
let mut hmac_engine: HmacEngine < sha512:: Hash > = HmacEngine :: new ( & self . chain_code [ ..] ) ;
648
- hmac_engine. input ( & self . public_key . key . serialize ( ) [ ..] ) ;
664
+ hmac_engine. input ( & self . public_key . serialize ( ) [ ..] ) ;
649
665
hmac_engine. input ( & endian:: u32_to_array_be ( n) ) ;
650
666
651
667
let hmac_result: Hmac < sha512:: Hash > = Hmac :: from_engine ( hmac_engine) ;
652
668
653
- let private_key = PrivateKey :: from_slice ( & hmac_result[ ..32 ] , self . network ) ?;
669
+ let private_key = secp256k1 :: SecretKey :: from_slice ( & hmac_result[ ..32 ] ) ?;
654
670
let chain_code = ChainCode :: from ( & hmac_result[ 32 ..] ) ;
655
671
Ok ( ( private_key, chain_code) )
656
672
}
@@ -665,7 +681,7 @@ impl ExtendedPubKey {
665
681
) -> Result < ExtendedPubKey , Error > {
666
682
let ( sk, chain_code) = self . ckd_pub_tweak ( i) ?;
667
683
let mut pk = self . public_key ;
668
- pk. key . add_exp_assign ( secp, & sk[ ..] ) ?;
684
+ pk. add_exp_assign ( secp, & sk[ ..] ) ?;
669
685
670
686
Ok ( ExtendedPubKey {
671
687
network : self . network ,
@@ -697,7 +713,7 @@ impl ExtendedPubKey {
697
713
parent_fingerprint : Fingerprint :: from ( & data[ 5 ..9 ] ) ,
698
714
child_number : endian:: slice_to_u32_be ( & data[ 9 ..13 ] ) . into ( ) ,
699
715
chain_code : ChainCode :: from ( & data[ 13 ..45 ] ) ,
700
- public_key : PublicKey :: from_slice ( & data[ 45 ..78 ] ) ?,
716
+ public_key : secp256k1 :: PublicKey :: from_slice ( & data[ 45 ..78 ] ) ?,
701
717
} )
702
718
}
703
719
@@ -712,14 +728,14 @@ impl ExtendedPubKey {
712
728
ret[ 5 ..9 ] . copy_from_slice ( & self . parent_fingerprint [ ..] ) ;
713
729
ret[ 9 ..13 ] . copy_from_slice ( & endian:: u32_to_array_be ( u32:: from ( self . child_number ) ) ) ;
714
730
ret[ 13 ..45 ] . copy_from_slice ( & self . chain_code [ ..] ) ;
715
- ret[ 45 ..78 ] . copy_from_slice ( & self . public_key . key . serialize ( ) [ ..] ) ;
731
+ ret[ 45 ..78 ] . copy_from_slice ( & self . public_key . serialize ( ) [ ..] ) ;
716
732
ret
717
733
}
718
734
719
735
/// Returns the HASH160 of the chaincode
720
736
pub fn identifier ( & self ) -> XpubIdentifier {
721
737
let mut engine = XpubIdentifier :: engine ( ) ;
722
- self . public_key . write_into ( & mut engine ) . expect ( "engines don't error" ) ;
738
+ engine . write ( & self . public_key . serialize ( ) ) . expect ( "engines don't error" ) ;
723
739
XpubIdentifier :: from_engine ( engine)
724
740
}
725
741
0 commit comments