22use std:: borrow:: Borrow ;
33use std:: convert:: TryInto ;
44use std:: str:: FromStr ;
5- use std:: { error, fmt} ;
5+ use std:: { error, fmt, hash } ;
66
77use bitcoin:: bip32:: XKeyIdentifier ;
88use bitcoin:: { self , bip32} ;
@@ -196,6 +196,49 @@ pub enum Wildcard {
196196 Hardened ,
197197}
198198
199+ impl fmt:: Display for Wildcard {
200+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
201+ match self {
202+ Wildcard :: None => write ! ( f, "" ) ,
203+ Wildcard :: Unhardened => write ! ( f, "/*" ) ,
204+ Wildcard :: Hardened => write ! ( f, "/*h" ) ,
205+ }
206+ }
207+ }
208+
209+ impl fmt:: Display for SinglePub {
210+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
211+ maybe_fmt_master_id ( f, & self . origin ) ?;
212+ match self . key {
213+ SinglePubKey :: FullKey ( full_key) => full_key. fmt ( f) ,
214+ SinglePubKey :: XOnly ( x_only_key) => x_only_key. fmt ( f) ,
215+ } ?;
216+ Ok ( ( ) )
217+ }
218+ }
219+
220+ impl MiniscriptKey for SinglePub {
221+ type Sha256 = sha256:: Hash ;
222+ type Hash256 = hash256:: Hash ;
223+ type Ripemd160 = ripemd160:: Hash ;
224+ type Hash160 = hash160:: Hash ;
225+
226+ fn is_x_only_key ( & self ) -> bool {
227+ matches ! ( self . key, SinglePubKey :: XOnly ( _) )
228+ }
229+
230+ fn num_der_paths ( & self ) -> usize {
231+ 0
232+ }
233+
234+ fn is_uncompressed ( & self ) -> bool {
235+ match self . key {
236+ SinglePubKey :: FullKey ( ref key) => key. is_uncompressed ( ) ,
237+ _ => false ,
238+ }
239+ }
240+ }
241+
199242impl SinglePriv {
200243 /// Returns the public key of this key.
201244 fn to_public < C : Signing > ( & self , secp : & Secp256k1 < C > ) -> SinglePub {
@@ -262,6 +305,62 @@ impl DescriptorXKey<bip32::Xpriv> {
262305 }
263306}
264307
308+ impl < K : InnerXKey > fmt:: Display for DescriptorXKey < K > {
309+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
310+ maybe_fmt_master_id ( f, & self . origin ) ?;
311+ self . xkey . fmt ( f) ?;
312+ fmt_derivation_path ( f, & self . derivation_path ) ?;
313+ self . wildcard . fmt ( f) ?;
314+ Ok ( ( ) )
315+ }
316+ }
317+
318+ impl < K > MiniscriptKey for DescriptorXKey < K >
319+ where
320+ K : InnerXKey + Clone + Ord + Eq + hash:: Hash + fmt:: Debug ,
321+ {
322+ type Sha256 = sha256:: Hash ;
323+ type Hash256 = hash256:: Hash ;
324+ type Ripemd160 = ripemd160:: Hash ;
325+ type Hash160 = hash160:: Hash ;
326+
327+ fn is_x_only_key ( & self ) -> bool {
328+ false
329+ }
330+
331+ fn num_der_paths ( & self ) -> usize {
332+ 1
333+ }
334+ }
335+
336+ impl < K : InnerXKey > fmt:: Display for DescriptorMultiXKey < K > {
337+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
338+ maybe_fmt_master_id ( f, & self . origin ) ?;
339+ self . xkey . fmt ( f) ?;
340+ fmt_derivation_paths ( f, self . derivation_paths . paths ( ) ) ?;
341+ self . wildcard . fmt ( f) ?;
342+ Ok ( ( ) )
343+ }
344+ }
345+
346+ impl < K > MiniscriptKey for DescriptorMultiXKey < K >
347+ where
348+ K : InnerXKey + Clone + Ord + Eq + hash:: Hash + fmt:: Debug ,
349+ {
350+ type Sha256 = sha256:: Hash ;
351+ type Hash256 = hash256:: Hash ;
352+ type Ripemd160 = ripemd160:: Hash ;
353+ type Hash160 = hash160:: Hash ;
354+
355+ fn is_x_only_key ( & self ) -> bool {
356+ false
357+ }
358+
359+ fn num_der_paths ( & self ) -> usize {
360+ self . derivation_paths . paths ( ) . len ( )
361+ }
362+ }
363+
265364/// Descriptor Key parsing errors
266365// FIXME: replace with error enums
267366#[ derive( Debug , PartialEq , Clone , Copy ) ]
@@ -282,36 +381,9 @@ impl error::Error for DescriptorKeyParseError {
282381impl fmt:: Display for DescriptorPublicKey {
283382 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
284383 match * self {
285- DescriptorPublicKey :: Single ( ref pk) => {
286- maybe_fmt_master_id ( f, & pk. origin ) ?;
287- match pk. key {
288- SinglePubKey :: FullKey ( full_key) => full_key. fmt ( f) ,
289- SinglePubKey :: XOnly ( x_only_key) => x_only_key. fmt ( f) ,
290- } ?;
291- Ok ( ( ) )
292- }
293- DescriptorPublicKey :: XPub ( ref xpub) => {
294- maybe_fmt_master_id ( f, & xpub. origin ) ?;
295- xpub. xkey . fmt ( f) ?;
296- fmt_derivation_path ( f, & xpub. derivation_path ) ?;
297- match xpub. wildcard {
298- Wildcard :: None => { }
299- Wildcard :: Unhardened => write ! ( f, "/*" ) ?,
300- Wildcard :: Hardened => write ! ( f, "/*h" ) ?,
301- }
302- Ok ( ( ) )
303- }
304- DescriptorPublicKey :: MultiXPub ( ref xpub) => {
305- maybe_fmt_master_id ( f, & xpub. origin ) ?;
306- xpub. xkey . fmt ( f) ?;
307- fmt_derivation_paths ( f, xpub. derivation_paths . paths ( ) ) ?;
308- match xpub. wildcard {
309- Wildcard :: None => { }
310- Wildcard :: Unhardened => write ! ( f, "/*" ) ?,
311- Wildcard :: Hardened => write ! ( f, "/*h" ) ?,
312- }
313- Ok ( ( ) )
314- }
384+ Self :: Single ( ref pk) => pk. fmt ( f) ,
385+ Self :: XPub ( ref xpub) => xpub. fmt ( f) ,
386+ Self :: MultiXPub ( ref xpub) => xpub. fmt ( f) ,
315387 }
316388 }
317389}
@@ -1006,9 +1078,9 @@ impl MiniscriptKey for DescriptorPublicKey {
10061078
10071079 fn num_der_paths ( & self ) -> usize {
10081080 match self {
1009- DescriptorPublicKey :: Single ( _ ) => 0 ,
1010- DescriptorPublicKey :: XPub ( _ ) => 1 ,
1011- DescriptorPublicKey :: MultiXPub ( xpub) => xpub. derivation_paths . paths ( ) . len ( ) ,
1081+ DescriptorPublicKey :: Single ( single ) => single . num_der_paths ( ) ,
1082+ DescriptorPublicKey :: XPub ( xpub ) => xpub . num_der_paths ( ) ,
1083+ DescriptorPublicKey :: MultiXPub ( xpub) => xpub. num_der_paths ( ) ,
10121084 }
10131085 }
10141086}
0 commit comments