@@ -128,7 +128,7 @@ use core::{fmt, hash, str};
128
128
use std:: error;
129
129
130
130
use bitcoin:: blockdata:: { opcodes, script} ;
131
- use bitcoin:: hashes:: { hash160, sha256, Hash } ;
131
+ use bitcoin:: hashes:: { hash160, ripemd160 , sha256, Hash } ;
132
132
133
133
pub use crate :: descriptor:: { Descriptor , DescriptorPublicKey } ;
134
134
pub use crate :: interpreter:: Interpreter ;
@@ -165,6 +165,13 @@ pub trait MiniscriptKey: Clone + Eq + Ord + fmt::Debug + fmt::Display + hash::Ha
165
165
/// The associated [`hash256::Hash`] for this [`MiniscriptKey`],
166
166
/// used in the hash256 fragment.
167
167
type Hash256 : Clone + Eq + Ord + fmt:: Display + fmt:: Debug + hash:: Hash ;
168
+ /// The associated [`ripedmd160::Hash`] for this [`MiniscriptKey`] type.
169
+ /// used in the ripemd160 fragment
170
+ type Ripemd160 : Clone + Eq + Ord + fmt:: Display + fmt:: Debug + hash:: Hash ;
171
+
172
+ /// The associated [`hash160::Hash`] for this [`MiniscriptKey`] type.
173
+ /// used in the hash160 fragment
174
+ type Hash160 : Clone + Eq + Ord + fmt:: Display + fmt:: Debug + hash:: Hash ;
168
175
169
176
/// Converts this key to the associated pubkey hash.
170
177
fn to_pubkeyhash ( & self ) -> Self :: RawPkHash ;
@@ -174,6 +181,8 @@ impl MiniscriptKey for bitcoin::secp256k1::PublicKey {
174
181
type RawPkHash = hash160:: Hash ;
175
182
type Sha256 = sha256:: Hash ;
176
183
type Hash256 = hash256:: Hash ;
184
+ type Ripemd160 = ripemd160:: Hash ;
185
+ type Hash160 = hash160:: Hash ;
177
186
178
187
fn to_pubkeyhash ( & self ) -> Self :: RawPkHash {
179
188
hash160:: Hash :: hash ( & self . serialize ( ) )
@@ -189,6 +198,8 @@ impl MiniscriptKey for bitcoin::PublicKey {
189
198
type RawPkHash = hash160:: Hash ;
190
199
type Sha256 = sha256:: Hash ;
191
200
type Hash256 = hash256:: Hash ;
201
+ type Ripemd160 = ripemd160:: Hash ;
202
+ type Hash160 = hash160:: Hash ;
192
203
193
204
fn to_pubkeyhash ( & self ) -> Self :: RawPkHash {
194
205
hash160:: Hash :: hash ( & self . to_bytes ( ) )
@@ -199,6 +210,8 @@ impl MiniscriptKey for bitcoin::secp256k1::XOnlyPublicKey {
199
210
type RawPkHash = hash160:: Hash ;
200
211
type Sha256 = sha256:: Hash ;
201
212
type Hash256 = hash256:: Hash ;
213
+ type Ripemd160 = ripemd160:: Hash ;
214
+ type Hash160 = hash160:: Hash ;
202
215
203
216
fn to_pubkeyhash ( & self ) -> Self :: RawPkHash {
204
217
hash160:: Hash :: hash ( & self . serialize ( ) )
@@ -213,6 +226,8 @@ impl MiniscriptKey for String {
213
226
type RawPkHash = String ;
214
227
type Sha256 = String ; // specify hashes as string
215
228
type Hash256 = String ;
229
+ type Ripemd160 = String ;
230
+ type Hash160 = String ;
216
231
217
232
fn to_pubkeyhash ( & self ) -> Self :: RawPkHash {
218
233
( & self ) . to_string ( )
@@ -243,6 +258,12 @@ pub trait ToPublicKey: MiniscriptKey {
243
258
244
259
/// Converts the generic associated [`MiniscriptKey::Hash256`] to [`hash256::Hash`]
245
260
fn to_hash256 ( hash : & <Self as MiniscriptKey >:: Hash256 ) -> hash256:: Hash ;
261
+
262
+ /// Converts the generic associated [`MiniscriptKey::Ripemd160`] to [`ripemd160::Hash`]
263
+ fn to_ripemd160 ( hash : & <Self as MiniscriptKey >:: Ripemd160 ) -> ripemd160:: Hash ;
264
+
265
+ /// Converts the generic associated [`MiniscriptKey::Hash160`] to [`hash160::Hash`]
266
+ fn to_hash160 ( hash : & <Self as MiniscriptKey >:: Hash160 ) -> hash160:: Hash ;
246
267
}
247
268
248
269
impl ToPublicKey for bitcoin:: PublicKey {
@@ -261,6 +282,14 @@ impl ToPublicKey for bitcoin::PublicKey {
261
282
fn to_hash256 ( hash : & hash256:: Hash ) -> hash256:: Hash {
262
283
* hash
263
284
}
285
+
286
+ fn to_ripemd160 ( hash : & ripemd160:: Hash ) -> ripemd160:: Hash {
287
+ * hash
288
+ }
289
+
290
+ fn to_hash160 ( hash : & hash160:: Hash ) -> hash160:: Hash {
291
+ * hash
292
+ }
264
293
}
265
294
266
295
impl ToPublicKey for bitcoin:: secp256k1:: PublicKey {
@@ -279,6 +308,14 @@ impl ToPublicKey for bitcoin::secp256k1::PublicKey {
279
308
fn to_hash256 ( hash : & hash256:: Hash ) -> hash256:: Hash {
280
309
* hash
281
310
}
311
+
312
+ fn to_ripemd160 ( hash : & ripemd160:: Hash ) -> ripemd160:: Hash {
313
+ * hash
314
+ }
315
+
316
+ fn to_hash160 ( hash : & hash160:: Hash ) -> hash160:: Hash {
317
+ * hash
318
+ }
282
319
}
283
320
284
321
impl ToPublicKey for bitcoin:: secp256k1:: XOnlyPublicKey {
@@ -306,6 +343,14 @@ impl ToPublicKey for bitcoin::secp256k1::XOnlyPublicKey {
306
343
fn to_hash256 ( hash : & hash256:: Hash ) -> hash256:: Hash {
307
344
* hash
308
345
}
346
+
347
+ fn to_ripemd160 ( hash : & ripemd160:: Hash ) -> ripemd160:: Hash {
348
+ * hash
349
+ }
350
+
351
+ fn to_hash160 ( hash : & hash160:: Hash ) -> hash160:: Hash {
352
+ * hash
353
+ }
309
354
}
310
355
311
356
/// Dummy key which de/serializes to the empty string; useful sometimes for testing
@@ -327,6 +372,8 @@ impl MiniscriptKey for DummyKey {
327
372
type RawPkHash = DummyKeyHash ;
328
373
type Sha256 = DummySha256Hash ;
329
374
type Hash256 = DummyHash256 ;
375
+ type Ripemd160 = DummyRipemd160Hash ;
376
+ type Hash160 = DummyHash160Hash ;
330
377
331
378
fn to_pubkeyhash ( & self ) -> Self :: RawPkHash {
332
379
DummyKeyHash
@@ -366,6 +413,14 @@ impl ToPublicKey for DummyKey {
366
413
hash256:: Hash :: from_str ( "50863ad64a87ae8a2fe83c1af1a8403cb53f53e486d8511dad8a04887e5b2352" )
367
414
. unwrap ( )
368
415
}
416
+
417
+ fn to_ripemd160 ( _: & DummyRipemd160Hash ) -> ripemd160:: Hash {
418
+ ripemd160:: Hash :: from_str ( "f54a5851e9372b87810a8e60cdd2e7cfd80b6e31" ) . unwrap ( )
419
+ }
420
+
421
+ fn to_hash160 ( _: & DummyHash160Hash ) -> hash160:: Hash {
422
+ hash160:: Hash :: from_str ( "f54a5851e9372b87810a8e60cdd2e7cfd80b6e31" ) . unwrap ( )
423
+ }
369
424
}
370
425
371
426
/// Dummy keyhash which de/serializes to the empty string; useful sometimes for testing
@@ -437,19 +492,74 @@ impl str::FromStr for DummyHash256 {
437
492
}
438
493
}
439
494
495
+ /// Dummy keyhash which de/serializes to the empty string; useful for testing
496
+ #[ derive( Copy , Clone , PartialOrd , Ord , PartialEq , Eq , Debug ) ]
497
+ pub struct DummyRipemd160Hash ;
498
+
499
+ impl str:: FromStr for DummyRipemd160Hash {
500
+ type Err = & ' static str ;
501
+ fn from_str ( x : & str ) -> Result < DummyRipemd160Hash , & ' static str > {
502
+ dbg ! ( & x) ;
503
+ if x. is_empty ( ) {
504
+ Ok ( DummyRipemd160Hash )
505
+ } else {
506
+ Err ( "non empty dummy hash" )
507
+ }
508
+ }
509
+ }
510
+
440
511
impl fmt:: Display for DummyHash256 {
441
512
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
442
513
f. write_str ( "" )
443
514
}
444
515
}
516
+ impl fmt:: Display for DummyRipemd160Hash {
517
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
518
+ f. write_str ( "" )
519
+ }
520
+ }
445
521
446
522
impl hash:: Hash for DummyHash256 {
447
523
fn hash < H : hash:: Hasher > ( & self , state : & mut H ) {
448
524
"DummySha256Hash" . hash ( state) ;
449
525
}
450
526
}
451
527
452
- /// Provides the conversion information required in [`TranslatePk`]
528
+ impl hash:: Hash for DummyRipemd160Hash {
529
+ fn hash < H : hash:: Hasher > ( & self , state : & mut H ) {
530
+ "DummyRipemd160Hash" . hash ( state) ;
531
+ }
532
+ }
533
+
534
+ /// Dummy keyhash which de/serializes to the empty string; useful for testing
535
+ #[ derive( Copy , Clone , PartialOrd , Ord , PartialEq , Eq , Debug ) ]
536
+ pub struct DummyHash160Hash ;
537
+
538
+ impl str:: FromStr for DummyHash160Hash {
539
+ type Err = & ' static str ;
540
+ fn from_str ( x : & str ) -> Result < DummyHash160Hash , & ' static str > {
541
+ dbg ! ( & x) ;
542
+ if x. is_empty ( ) {
543
+ Ok ( DummyHash160Hash )
544
+ } else {
545
+ Err ( "non empty dummy hash" )
546
+ }
547
+ }
548
+ }
549
+
550
+ impl fmt:: Display for DummyHash160Hash {
551
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
552
+ f. write_str ( "" )
553
+ }
554
+ }
555
+
556
+ impl hash:: Hash for DummyHash160Hash {
557
+ fn hash < H : hash:: Hasher > ( & self , state : & mut H ) {
558
+ "DummyHash160Hash" . hash ( state) ;
559
+ }
560
+ }
561
+ /// Describes an object that can translate various keys and hashes from one key to the type
562
+ /// associated with the other key. Used by the [`TranslatePk`] trait to do the actual translations.
453
563
pub trait Translator < P , Q , E >
454
564
where
455
565
P : MiniscriptKey ,
@@ -466,6 +576,12 @@ where
466
576
467
577
/// Provides the translation from P::Hash256 -> Q::Hash256
468
578
fn hash256 ( & mut self , hash256 : & P :: Hash256 ) -> Result < Q :: Hash256 , E > ;
579
+
580
+ /// Translates ripemd160 hashes from P::Ripemd160 -> Q::Ripemd160
581
+ fn ripemd160 ( & mut self , ripemd160 : & P :: Ripemd160 ) -> Result < Q :: Ripemd160 , E > ;
582
+
583
+ /// Translates hash160 hashes from P::Hash160 -> Q::Hash160
584
+ fn hash160 ( & mut self , hash160 : & P :: Hash160 ) -> Result < Q :: Hash160 , E > ;
469
585
}
470
586
471
587
/// Provides the conversion information required in [`TranslatePk`].
@@ -487,7 +603,12 @@ impl<P, Q, E, T> Translator<P, Q, E> for T
487
603
where
488
604
T : PkTranslator < P , Q , E > ,
489
605
P : MiniscriptKey ,
490
- Q : MiniscriptKey < Sha256 = P :: Sha256 , Hash256 = P :: Hash256 > ,
606
+ Q : MiniscriptKey <
607
+ Sha256 = P :: Sha256 ,
608
+ Hash256 = P :: Hash256 ,
609
+ Ripemd160 = P :: Ripemd160 ,
610
+ Hash160 = P :: Hash160 ,
611
+ > ,
491
612
{
492
613
fn pk ( & mut self , pk : & P ) -> Result < Q , E > {
493
614
<Self as PkTranslator < P , Q , E > >:: pk ( self , pk)
@@ -507,6 +628,17 @@ where
507
628
fn hash256 ( & mut self , hash256 : & <P as MiniscriptKey >:: Hash256 ) -> Result < <Q >:: Hash256 , E > {
508
629
Ok ( hash256. clone ( ) )
509
630
}
631
+
632
+ fn ripemd160 (
633
+ & mut self ,
634
+ ripemd160 : & <P as MiniscriptKey >:: Ripemd160 ,
635
+ ) -> Result < <Q >:: Ripemd160 , E > {
636
+ Ok ( ripemd160. clone ( ) )
637
+ }
638
+
639
+ fn hash160 ( & mut self , hash160 : & <P as MiniscriptKey >:: Hash160 ) -> Result < <Q >:: Hash160 , E > {
640
+ Ok ( hash160. clone ( ) )
641
+ }
510
642
}
511
643
512
644
/// Converts a descriptor using abstract keys to one using specific keys. Uses translator `t` to do
0 commit comments