@@ -12,56 +12,68 @@ use bitcoin::{
12
12
13
13
use { MiniscriptKey , ToPublicKey } ;
14
14
15
- /// Single public key without any origin or range information
16
- #[ derive( Debug , Eq , PartialEq , Clone , Ord , PartialOrd , Hash ) ]
17
- pub enum SinglePubKey {
18
- /// FullKey (compressed or uncompressed)
19
- FullKey ( bitcoin:: PublicKey ) ,
20
- /// XOnlyPublicKey
21
- XOnly ( XOnlyPublicKey ) ,
22
- }
23
-
24
- /// The MiniscriptKey corresponding to Descriptors. This can
25
- /// either be Single public key or a Xpub
15
+ /// The descriptor pubkey, either a single pubkey or an xpub.
26
16
#[ derive( Debug , Eq , PartialEq , Clone , Ord , PartialOrd , Hash ) ]
27
17
pub enum DescriptorPublicKey {
28
- /// Single Public Key
29
- SinglePub ( DescriptorSinglePub ) ,
30
- /// Xpub
18
+ /// Single public key.
19
+ Single ( SinglePub ) ,
20
+ /// Extended public key (xpub).
31
21
XPub ( DescriptorXKey < bip32:: ExtendedPubKey > ) ,
32
22
}
33
23
34
- /// A Single Descriptor Key with optional origin information
24
+ /// The descriptor secret key, either a single private key or an xprv.
25
+ #[ derive( Debug ) ]
26
+ pub enum DescriptorSecretKey {
27
+ /// Single private key.
28
+ Single ( SinglePriv ) ,
29
+ /// Extended private key (xpriv).
30
+ XPrv ( DescriptorXKey < bip32:: ExtendedPrivKey > ) ,
31
+ }
32
+
33
+ /// A descriptor [`SinglePubKey`] with optional origin information.
35
34
#[ derive( Debug , Eq , PartialEq , Clone , Ord , PartialOrd , Hash ) ]
36
- pub struct DescriptorSinglePub {
37
- /// Origin information
35
+ pub struct SinglePub {
36
+ /// Origin information (fingerprint and derivation path).
38
37
pub origin : Option < ( bip32:: Fingerprint , bip32:: DerivationPath ) > ,
39
- /// The key
38
+ /// The public key.
40
39
pub key : SinglePubKey ,
41
40
}
42
41
43
- /// A Single Descriptor Secret Key with optional origin information
42
+ /// A descriptor [`bitcoin::PrivateKey`] with optional origin information.
44
43
#[ derive( Debug ) ]
45
- pub struct DescriptorSinglePriv {
46
- /// Origin information
47
- pub origin : Option < bip32:: KeySource > ,
48
- /// The key
44
+ pub struct SinglePriv {
45
+ /// Origin information (fingerprint and derivation path).
46
+ pub origin : Option < ( bip32:: Fingerprint , bip32 :: DerivationPath ) > ,
47
+ /// The private key.
49
48
pub key : bitcoin:: PrivateKey ,
50
49
}
51
50
52
- /// A Secret Key that can be either a single key or an Xprv
53
- #[ derive( Debug ) ]
54
- pub enum DescriptorSecretKey {
55
- /// Single Secret Key
56
- SinglePriv ( DescriptorSinglePriv ) ,
57
- /// Xprv
58
- XPrv ( DescriptorXKey < bip32:: ExtendedPrivKey > ) ,
51
+ /// An extended key with origin, derivation path, and wildcard.
52
+ #[ derive( Debug , Eq , PartialEq , Clone , Ord , PartialOrd , Hash ) ]
53
+ pub struct DescriptorXKey < K : InnerXKey > {
54
+ /// Origin information
55
+ pub origin : Option < ( bip32:: Fingerprint , bip32:: DerivationPath ) > ,
56
+ /// The extended key
57
+ pub xkey : K ,
58
+ /// The derivation path
59
+ pub derivation_path : bip32:: DerivationPath ,
60
+ /// Whether the descriptor is wildcard
61
+ pub wildcard : Wildcard ,
62
+ }
63
+
64
+ /// Single public key without any origin or range information.
65
+ #[ derive( Debug , Eq , PartialEq , Clone , Ord , PartialOrd , Hash ) ]
66
+ pub enum SinglePubKey {
67
+ /// A bitcoin public key (compressed or uncompressed).
68
+ FullKey ( bitcoin:: PublicKey ) ,
69
+ /// An xonly public key.
70
+ XOnly ( XOnlyPublicKey ) ,
59
71
}
60
72
61
73
impl fmt:: Display for DescriptorSecretKey {
62
74
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
63
75
match self {
64
- & DescriptorSecretKey :: SinglePriv ( ref sk) => {
76
+ & DescriptorSecretKey :: Single ( ref sk) => {
65
77
maybe_fmt_master_id ( f, & sk. origin ) ?;
66
78
sk. key . fmt ( f) ?;
67
79
Ok ( ( ) )
@@ -124,28 +136,15 @@ pub enum Wildcard {
124
136
Hardened ,
125
137
}
126
138
127
- /// Instance of an extended key with origin and derivation path
128
- #[ derive( Debug , Eq , PartialEq , Clone , Ord , PartialOrd , Hash ) ]
129
- pub struct DescriptorXKey < K : InnerXKey > {
130
- /// Origin information
131
- pub origin : Option < ( bip32:: Fingerprint , bip32:: DerivationPath ) > ,
132
- /// The extended key
133
- pub xkey : K ,
134
- /// The derivation path
135
- pub derivation_path : bip32:: DerivationPath ,
136
- /// Whether the descriptor is wildcard
137
- pub wildcard : Wildcard ,
138
- }
139
-
140
- impl DescriptorSinglePriv {
139
+ impl SinglePriv {
141
140
/// Returns the public key of this key
142
141
fn as_public < C : Signing > (
143
142
& self ,
144
143
secp : & Secp256k1 < C > ,
145
- ) -> Result < DescriptorSinglePub , DescriptorKeyParseError > {
144
+ ) -> Result < SinglePub , DescriptorKeyParseError > {
146
145
let pub_key = self . key . public_key ( secp) ;
147
146
148
- Ok ( DescriptorSinglePub {
147
+ Ok ( SinglePub {
149
148
origin : self . origin . clone ( ) ,
150
149
key : SinglePubKey :: FullKey ( pub_key) ,
151
150
} )
@@ -219,7 +218,7 @@ impl error::Error for DescriptorKeyParseError {}
219
218
impl fmt:: Display for DescriptorPublicKey {
220
219
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
221
220
match * self {
222
- DescriptorPublicKey :: SinglePub ( ref pk) => {
221
+ DescriptorPublicKey :: Single ( ref pk) => {
223
222
maybe_fmt_master_id ( f, & pk. origin ) ?;
224
223
match pk. key {
225
224
SinglePubKey :: FullKey ( full_key) => full_key. fmt ( f) ,
@@ -244,7 +243,7 @@ impl fmt::Display for DescriptorPublicKey {
244
243
245
244
impl DescriptorSecretKey {
246
245
/// Return the public version of this key, by applying either
247
- /// [`DescriptorSinglePriv ::as_public`] or [`DescriptorXKey<bip32::ExtendedPrivKey>::as_public`]
246
+ /// [`SinglePriv ::as_public`] or [`DescriptorXKey<bip32::ExtendedPrivKey>::as_public`]
248
247
/// depending on the type of key.
249
248
///
250
249
/// If the key is an "XPrv", the hardened derivation steps will be applied before converting it
@@ -255,8 +254,8 @@ impl DescriptorSecretKey {
255
254
secp : & Secp256k1 < C > ,
256
255
) -> Result < DescriptorPublicKey , DescriptorKeyParseError > {
257
256
Ok ( match self {
258
- & DescriptorSecretKey :: SinglePriv ( ref sk) => {
259
- DescriptorPublicKey :: SinglePub ( sk. as_public ( secp) ?)
257
+ & DescriptorSecretKey :: Single ( ref sk) => {
258
+ DescriptorPublicKey :: Single ( sk. as_public ( secp) ?)
260
259
}
261
260
& DescriptorSecretKey :: XPrv ( ref xprv) => {
262
261
DescriptorPublicKey :: XPub ( xprv. as_public ( secp) ?)
@@ -341,10 +340,7 @@ impl FromStr for DescriptorPublicKey {
341
340
) )
342
341
}
343
342
} ;
344
- Ok ( DescriptorPublicKey :: SinglePub ( DescriptorSinglePub {
345
- key,
346
- origin,
347
- } ) )
343
+ Ok ( DescriptorPublicKey :: Single ( SinglePub { key, origin } ) )
348
344
}
349
345
}
350
346
}
@@ -385,7 +381,7 @@ impl DescriptorPublicKey {
385
381
xpub. xkey . fingerprint ( )
386
382
}
387
383
}
388
- DescriptorPublicKey :: SinglePub ( ref single) => {
384
+ DescriptorPublicKey :: Single ( ref single) => {
389
385
if let Some ( ( fingerprint, _) ) = single. origin {
390
386
fingerprint
391
387
} else {
@@ -417,7 +413,7 @@ impl DescriptorPublicKey {
417
413
} ;
418
414
origin_path. extend ( & xpub. derivation_path )
419
415
}
420
- DescriptorPublicKey :: SinglePub ( ref single) => {
416
+ DescriptorPublicKey :: Single ( ref single) => {
421
417
if let Some ( ( _, ref path) ) = single. origin {
422
418
path. clone ( )
423
419
} else {
@@ -430,7 +426,7 @@ impl DescriptorPublicKey {
430
426
/// Whether or not the key has a wildcards
431
427
pub fn is_deriveable ( & self ) -> bool {
432
428
match * self {
433
- DescriptorPublicKey :: SinglePub ( ..) => false ,
429
+ DescriptorPublicKey :: Single ( ..) => false ,
434
430
DescriptorPublicKey :: XPub ( ref xpub) => xpub. wildcard != Wildcard :: None ,
435
431
}
436
432
}
@@ -476,7 +472,7 @@ impl DescriptorPublicKey {
476
472
secp : & Secp256k1 < C > ,
477
473
) -> Result < bitcoin:: PublicKey , ConversionError > {
478
474
match * self {
479
- DescriptorPublicKey :: SinglePub ( ref pk) => match pk. key {
475
+ DescriptorPublicKey :: Single ( ref pk) => match pk. key {
480
476
SinglePubKey :: FullKey ( pk) => Ok ( pk) ,
481
477
SinglePubKey :: XOnly ( xpk) => Ok ( xpk. to_public_key ( ) ) ,
482
478
} ,
@@ -504,7 +500,7 @@ impl FromStr for DescriptorSecretKey {
504
500
if key_part. len ( ) <= 52 {
505
501
let sk = bitcoin:: PrivateKey :: from_str ( key_part)
506
502
. map_err ( |_| DescriptorKeyParseError ( "Error while parsing a WIF private key" ) ) ?;
507
- Ok ( DescriptorSecretKey :: SinglePriv ( DescriptorSinglePriv {
503
+ Ok ( DescriptorSecretKey :: Single ( SinglePriv {
508
504
key : sk,
509
505
origin : None ,
510
506
} ) )
@@ -694,7 +690,7 @@ impl MiniscriptKey for DescriptorPublicKey {
694
690
695
691
fn is_uncompressed ( & self ) -> bool {
696
692
match self {
697
- DescriptorPublicKey :: SinglePub ( DescriptorSinglePub {
693
+ DescriptorPublicKey :: Single ( SinglePub {
698
694
key : SinglePubKey :: FullKey ( ref key) ,
699
695
..
700
696
} ) => key. is_uncompressed ( ) ,
@@ -704,7 +700,7 @@ impl MiniscriptKey for DescriptorPublicKey {
704
700
705
701
fn is_x_only_key ( & self ) -> bool {
706
702
match self {
707
- DescriptorPublicKey :: SinglePub ( DescriptorSinglePub {
703
+ DescriptorPublicKey :: Single ( SinglePub {
708
704
key : SinglePubKey :: XOnly ( ref _key) ,
709
705
..
710
706
} ) => true ,
0 commit comments