@@ -22,10 +22,10 @@ use bitcoin::secp256k1::{self, Secp256k1, Signing};
22
22
use bitcoin:: util:: bip32;
23
23
use bitcoin:: { Network , PrivateKey , PublicKey } ;
24
24
25
- use miniscript:: descriptor:: { Descriptor , DescriptorXKey , Wildcard } ;
25
+ use miniscript:: descriptor:: { Descriptor , Wildcard } ;
26
26
pub use miniscript:: descriptor:: {
27
- DescriptorPublicKey , DescriptorSecretKey , DescriptorSinglePriv , DescriptorSinglePub , KeyMap ,
28
- SortedMultiVec ,
27
+ DescriptorPublicKey , DescriptorSecretKey , DescriptorSinglePriv , DescriptorSinglePub ,
28
+ DescriptorXKey , KeyMap , SortedMultiVec ,
29
29
} ;
30
30
pub use miniscript:: ScriptContext ;
31
31
use miniscript:: { Miniscript , Terminal } ;
@@ -94,6 +94,13 @@ impl<Ctx: ScriptContext> DescriptorKey<Ctx> {
94
94
}
95
95
}
96
96
97
+ pub fn as_public ( & self , secp : & SecpCtx ) -> Result < DescriptorPublicKey , KeyError > {
98
+ match self {
99
+ DescriptorKey :: Public ( pk, _, _) => Ok ( pk. clone ( ) ) ,
100
+ DescriptorKey :: Secret ( secret, _, _) => Ok ( secret. as_public ( secp) ?) ,
101
+ }
102
+ }
103
+
97
104
// This method is used internally by `bdk::fragment!` and `bdk::descriptor!`. It has to be
98
105
// public because it is effectively called by external crates, once the macros are expanded,
99
106
// but since it is not meant to be part of the public api we hide it from the docs.
@@ -102,18 +109,15 @@ impl<Ctx: ScriptContext> DescriptorKey<Ctx> {
102
109
self ,
103
110
secp : & SecpCtx ,
104
111
) -> Result < ( DescriptorPublicKey , KeyMap , ValidNetworks ) , KeyError > {
112
+ let public = self . as_public ( secp) ?;
113
+
105
114
match self {
106
- DescriptorKey :: Public ( public , valid_networks, _) => {
115
+ DescriptorKey :: Public ( _ , valid_networks, _) => {
107
116
Ok ( ( public, KeyMap :: default ( ) , valid_networks) )
108
117
}
109
118
DescriptorKey :: Secret ( secret, valid_networks, _) => {
110
119
let mut key_map = KeyMap :: with_capacity ( 1 ) ;
111
-
112
- let public = secret
113
- . as_public ( secp)
114
- . map_err ( |e| miniscript:: Error :: Unexpected ( e. to_string ( ) ) ) ?;
115
120
key_map. insert ( public. clone ( ) , secret) ;
116
-
117
121
Ok ( ( public, key_map, valid_networks) )
118
122
}
119
123
}
@@ -891,9 +895,15 @@ pub enum KeyError {
891
895
Bip32 ( bitcoin:: util:: bip32:: Error ) ,
892
896
/// Miniscript error
893
897
Miniscript ( miniscript:: Error ) ,
898
+ KeyParseError ( miniscript:: descriptor:: DescriptorKeyParseError ) ,
894
899
}
895
900
896
901
impl_error ! ( miniscript:: Error , Miniscript , KeyError ) ;
902
+ impl_error ! (
903
+ miniscript:: descriptor:: DescriptorKeyParseError ,
904
+ KeyParseError ,
905
+ KeyError
906
+ ) ;
897
907
impl_error ! ( bitcoin:: util:: bip32:: Error , Bip32 , KeyError ) ;
898
908
899
909
impl std:: fmt:: Display for KeyError {
0 commit comments