@@ -152,20 +152,26 @@ pub trait MiniscriptKey: Clone + Eq + Ord + fmt::Debug + fmt::Display + hash::Ha
152
152
false
153
153
}
154
154
155
- /// The associated [`Hash`] type for this pubkey.
155
+ /// The associated PublicKey Hash for this [`MiniscriptKey`],
156
+ /// used in the pkh fragment
156
157
type Hash : Clone + Eq + Ord + fmt:: Display + fmt:: Debug + hash:: Hash ;
157
158
158
- /// The associated [`sha256::Hash`] type for this [`MiniscriptKey`] type.
159
- /// Used in the sha256 fragment
159
+ /// The associated [`sha256::Hash`] for this [`MiniscriptKey`],
160
+ /// used in the hash256 fragment.
160
161
type Sha256 : Clone + Eq + Ord + fmt:: Display + fmt:: Debug + hash:: Hash ;
161
162
163
+ /// The associated [`hash256::Hash`] for this [`MiniscriptKey`],
164
+ /// used in the hash256 fragment.
165
+ type Hash256 : Clone + Eq + Ord + fmt:: Display + fmt:: Debug + hash:: Hash ;
166
+
162
167
/// Converts this key to the associated pubkey hash.
163
168
fn to_pubkeyhash ( & self ) -> Self :: Hash ;
164
169
}
165
170
166
171
impl MiniscriptKey for bitcoin:: secp256k1:: PublicKey {
167
172
type Hash = hash160:: Hash ;
168
173
type Sha256 = sha256:: Hash ;
174
+ type Hash256 = hash256:: Hash ;
169
175
170
176
fn to_pubkeyhash ( & self ) -> Self :: Hash {
171
177
hash160:: Hash :: hash ( & self . serialize ( ) )
@@ -180,6 +186,7 @@ impl MiniscriptKey for bitcoin::PublicKey {
180
186
181
187
type Hash = hash160:: Hash ;
182
188
type Sha256 = sha256:: Hash ;
189
+ type Hash256 = hash256:: Hash ;
183
190
184
191
fn to_pubkeyhash ( & self ) -> Self :: Hash {
185
192
hash160:: Hash :: hash ( & self . to_bytes ( ) )
@@ -189,6 +196,7 @@ impl MiniscriptKey for bitcoin::PublicKey {
189
196
impl MiniscriptKey for bitcoin:: secp256k1:: XOnlyPublicKey {
190
197
type Hash = hash160:: Hash ;
191
198
type Sha256 = sha256:: Hash ;
199
+ type Hash256 = hash256:: Hash ;
192
200
193
201
fn to_pubkeyhash ( & self ) -> Self :: Hash {
194
202
hash160:: Hash :: hash ( & self . serialize ( ) )
@@ -202,6 +210,7 @@ impl MiniscriptKey for bitcoin::secp256k1::XOnlyPublicKey {
202
210
impl MiniscriptKey for String {
203
211
type Hash = String ;
204
212
type Sha256 = String ; // specify hashes as string
213
+ type Hash256 = String ;
205
214
206
215
fn to_pubkeyhash ( & self ) -> Self :: Hash {
207
216
( & self ) . to_string ( )
@@ -229,6 +238,9 @@ pub trait ToPublicKey: MiniscriptKey {
229
238
230
239
/// Converts the generic associated [`MiniscriptKey::Sha256`] to [`sha256::Hash`]
231
240
fn to_sha256 ( hash : & <Self as MiniscriptKey >:: Sha256 ) -> sha256:: Hash ;
241
+
242
+ /// Converts the generic associated [`MiniscriptKey::Hash256`] to [`hash256::Hash`]
243
+ fn to_hash256 ( hash : & <Self as MiniscriptKey >:: Hash256 ) -> hash256:: Hash ;
232
244
}
233
245
234
246
impl ToPublicKey for bitcoin:: PublicKey {
@@ -243,6 +255,10 @@ impl ToPublicKey for bitcoin::PublicKey {
243
255
fn to_sha256 ( hash : & sha256:: Hash ) -> sha256:: Hash {
244
256
* hash
245
257
}
258
+
259
+ fn to_hash256 ( hash : & hash256:: Hash ) -> hash256:: Hash {
260
+ * hash
261
+ }
246
262
}
247
263
248
264
impl ToPublicKey for bitcoin:: secp256k1:: PublicKey {
@@ -257,6 +273,10 @@ impl ToPublicKey for bitcoin::secp256k1::PublicKey {
257
273
fn to_sha256 ( hash : & sha256:: Hash ) -> sha256:: Hash {
258
274
* hash
259
275
}
276
+
277
+ fn to_hash256 ( hash : & hash256:: Hash ) -> hash256:: Hash {
278
+ * hash
279
+ }
260
280
}
261
281
262
282
impl ToPublicKey for bitcoin:: secp256k1:: XOnlyPublicKey {
@@ -280,6 +300,10 @@ impl ToPublicKey for bitcoin::secp256k1::XOnlyPublicKey {
280
300
fn to_sha256 ( hash : & sha256:: Hash ) -> sha256:: Hash {
281
301
* hash
282
302
}
303
+
304
+ fn to_hash256 ( hash : & hash256:: Hash ) -> hash256:: Hash {
305
+ * hash
306
+ }
283
307
}
284
308
285
309
/// Dummy key which de/serializes to the empty string; useful sometimes for testing
@@ -300,6 +324,7 @@ impl str::FromStr for DummyKey {
300
324
impl MiniscriptKey for DummyKey {
301
325
type Hash = DummyKeyHash ;
302
326
type Sha256 = DummySha256Hash ;
327
+ type Hash256 = DummyHash256 ;
303
328
304
329
fn to_pubkeyhash ( & self ) -> Self :: Hash {
305
330
DummyKeyHash
@@ -334,6 +359,11 @@ impl ToPublicKey for DummyKey {
334
359
sha256:: Hash :: from_str ( "50863ad64a87ae8a2fe83c1af1a8403cb53f53e486d8511dad8a04887e5b2352" )
335
360
. unwrap ( )
336
361
}
362
+
363
+ fn to_hash256 ( _hash : & DummyHash256 ) -> hash256:: Hash {
364
+ hash256:: Hash :: from_str ( "50863ad64a87ae8a2fe83c1af1a8403cb53f53e486d8511dad8a04887e5b2352" )
365
+ . unwrap ( )
366
+ }
337
367
}
338
368
339
369
/// Dummy keyhash which de/serializes to the empty string; useful sometimes for testing
@@ -390,8 +420,34 @@ impl hash::Hash for DummySha256Hash {
390
420
}
391
421
}
392
422
393
- /// Describes an object that can translate various keys and hashes from one key to the type
394
- /// associated with the other key. Used by the [`TranslatePk`] trait to do the actual translations.
423
+ /// Dummy keyhash which de/serializes to the empty string; useful for testing
424
+ #[ derive( Copy , Clone , PartialOrd , Ord , PartialEq , Eq , Debug ) ]
425
+ pub struct DummyHash256 ;
426
+
427
+ impl str:: FromStr for DummyHash256 {
428
+ type Err = & ' static str ;
429
+ fn from_str ( x : & str ) -> Result < DummyHash256 , & ' static str > {
430
+ if x. is_empty ( ) {
431
+ Ok ( DummyHash256 )
432
+ } else {
433
+ Err ( "non empty dummy hash" )
434
+ }
435
+ }
436
+ }
437
+
438
+ impl fmt:: Display for DummyHash256 {
439
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
440
+ f. write_str ( "" )
441
+ }
442
+ }
443
+
444
+ impl hash:: Hash for DummyHash256 {
445
+ fn hash < H : hash:: Hasher > ( & self , state : & mut H ) {
446
+ "DummySha256Hash" . hash ( state) ;
447
+ }
448
+ }
449
+
450
+ /// Provides the conversion information required in [`TranslatePk`]
395
451
pub trait Translator < P , Q , E >
396
452
where
397
453
P : MiniscriptKey ,
@@ -403,8 +459,11 @@ where
403
459
/// Translates public key hashes P::Hash -> Q::Hash.
404
460
fn pkh ( & mut self , pkh : & P :: Hash ) -> Result < Q :: Hash , E > ;
405
461
406
- /// Translates sha256 hashes from P::Sha256 -> Q::Sha256
462
+ /// Provides the translation from P::Sha256 -> Q::Sha256
407
463
fn sha256 ( & mut self , sha256 : & P :: Sha256 ) -> Result < Q :: Sha256 , E > ;
464
+
465
+ /// Provides the translation from P::Hash256 -> Q::Hash256
466
+ fn hash256 ( & mut self , hash256 : & P :: Hash256 ) -> Result < Q :: Hash256 , E > ;
408
467
}
409
468
410
469
/// Provides the conversion information required in [`TranslatePk`].
@@ -426,7 +485,7 @@ impl<P, Q, E, T> Translator<P, Q, E> for T
426
485
where
427
486
T : PkTranslator < P , Q , E > ,
428
487
P : MiniscriptKey ,
429
- Q : MiniscriptKey < Sha256 = P :: Sha256 > ,
488
+ Q : MiniscriptKey < Sha256 = P :: Sha256 , Hash256 = P :: Hash256 > ,
430
489
{
431
490
fn pk ( & mut self , pk : & P ) -> Result < Q , E > {
432
491
<Self as PkTranslator < P , Q , E > >:: pk ( self , pk)
@@ -439,6 +498,10 @@ where
439
498
fn sha256 ( & mut self , sha256 : & <P as MiniscriptKey >:: Sha256 ) -> Result < <Q >:: Sha256 , E > {
440
499
Ok ( sha256. clone ( ) )
441
500
}
501
+
502
+ fn hash256 ( & mut self , hash256 : & <P as MiniscriptKey >:: Hash256 ) -> Result < <Q >:: Hash256 , E > {
503
+ Ok ( hash256. clone ( ) )
504
+ }
442
505
}
443
506
444
507
/// Converts a descriptor using abstract keys to one using specific keys. Uses translator `t` to do
0 commit comments