@@ -159,13 +159,18 @@ pub trait MiniscriptKey: Clone + Eq + Ord + fmt::Debug + fmt::Display + hash::Ha
159159 /// Used in the sha256 fragment
160160 type Sha256 : Clone + Eq + Ord + fmt:: Display + fmt:: Debug + hash:: Hash ;
161161
162+ /// The associated [`hash256::Hash`] type for this [`MiniscriptKey`] type.
163+ /// Used in the sha256 fragment
164+ type Hash256 : Clone + Eq + Ord + fmt:: Display + fmt:: Debug + hash:: Hash ;
165+
162166 /// Converts this key to the associated pubkey hash.
163167 fn to_pubkeyhash ( & self ) -> Self :: Hash ;
164168}
165169
166170impl MiniscriptKey for bitcoin:: secp256k1:: PublicKey {
167171 type Hash = hash160:: Hash ;
168172 type Sha256 = sha256:: Hash ;
173+ type Hash256 = hash256:: Hash ;
169174
170175 fn to_pubkeyhash ( & self ) -> Self :: Hash {
171176 hash160:: Hash :: hash ( & self . serialize ( ) )
@@ -180,6 +185,7 @@ impl MiniscriptKey for bitcoin::PublicKey {
180185
181186 type Hash = hash160:: Hash ;
182187 type Sha256 = sha256:: Hash ;
188+ type Hash256 = hash256:: Hash ;
183189
184190 fn to_pubkeyhash ( & self ) -> Self :: Hash {
185191 hash160:: Hash :: hash ( & self . to_bytes ( ) )
@@ -189,6 +195,7 @@ impl MiniscriptKey for bitcoin::PublicKey {
189195impl MiniscriptKey for bitcoin:: secp256k1:: XOnlyPublicKey {
190196 type Hash = hash160:: Hash ;
191197 type Sha256 = sha256:: Hash ;
198+ type Hash256 = hash256:: Hash ;
192199
193200 fn to_pubkeyhash ( & self ) -> Self :: Hash {
194201 hash160:: Hash :: hash ( & self . serialize ( ) )
@@ -202,6 +209,7 @@ impl MiniscriptKey for bitcoin::secp256k1::XOnlyPublicKey {
202209impl MiniscriptKey for String {
203210 type Hash = String ;
204211 type Sha256 = String ; // specify hashes as string
212+ type Hash256 = String ;
205213
206214 fn to_pubkeyhash ( & self ) -> Self :: Hash {
207215 ( & self ) . to_string ( )
@@ -229,6 +237,9 @@ pub trait ToPublicKey: MiniscriptKey {
229237
230238 /// Converts the generic associated [`MiniscriptKey::Sha256`] to [`sha256::Hash`]
231239 fn to_sha256 ( hash : & <Self as MiniscriptKey >:: Sha256 ) -> sha256:: Hash ;
240+
241+ /// Converts the generic associated [`MiniscriptKey::Hash256`] to [`hash256::Hash`]
242+ fn to_hash256 ( hash : & <Self as MiniscriptKey >:: Hash256 ) -> hash256:: Hash ;
232243}
233244
234245impl ToPublicKey for bitcoin:: PublicKey {
@@ -243,6 +254,10 @@ impl ToPublicKey for bitcoin::PublicKey {
243254 fn to_sha256 ( hash : & sha256:: Hash ) -> sha256:: Hash {
244255 * hash
245256 }
257+
258+ fn to_hash256 ( hash : & hash256:: Hash ) -> hash256:: Hash {
259+ * hash
260+ }
246261}
247262
248263impl ToPublicKey for bitcoin:: secp256k1:: PublicKey {
@@ -257,6 +272,10 @@ impl ToPublicKey for bitcoin::secp256k1::PublicKey {
257272 fn to_sha256 ( hash : & sha256:: Hash ) -> sha256:: Hash {
258273 * hash
259274 }
275+
276+ fn to_hash256 ( hash : & hash256:: Hash ) -> hash256:: Hash {
277+ * hash
278+ }
260279}
261280
262281impl ToPublicKey for bitcoin:: secp256k1:: XOnlyPublicKey {
@@ -280,6 +299,10 @@ impl ToPublicKey for bitcoin::secp256k1::XOnlyPublicKey {
280299 fn to_sha256 ( hash : & sha256:: Hash ) -> sha256:: Hash {
281300 * hash
282301 }
302+
303+ fn to_hash256 ( hash : & hash256:: Hash ) -> hash256:: Hash {
304+ * hash
305+ }
283306}
284307
285308/// Dummy key which de/serializes to the empty string; useful sometimes for testing
@@ -300,6 +323,7 @@ impl str::FromStr for DummyKey {
300323impl MiniscriptKey for DummyKey {
301324 type Hash = DummyKeyHash ;
302325 type Sha256 = DummySha256Hash ;
326+ type Hash256 = DummyHash256 ;
303327
304328 fn to_pubkeyhash ( & self ) -> Self :: Hash {
305329 DummyKeyHash
@@ -334,6 +358,11 @@ impl ToPublicKey for DummyKey {
334358 sha256:: Hash :: from_str ( "50863ad64a87ae8a2fe83c1af1a8403cb53f53e486d8511dad8a04887e5b2352" )
335359 . unwrap ( )
336360 }
361+
362+ fn to_hash256 ( _hash : & DummyHash256 ) -> hash256:: Hash {
363+ hash256:: Hash :: from_str ( "50863ad64a87ae8a2fe83c1af1a8403cb53f53e486d8511dad8a04887e5b2352" )
364+ . unwrap ( )
365+ }
337366}
338367
339368/// Dummy keyhash which de/serializes to the empty string; useful sometimes for testing
@@ -390,8 +419,34 @@ impl hash::Hash for DummySha256Hash {
390419 }
391420}
392421
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.
422+ /// Dummy keyhash which de/serializes to the empty string; useful for testing
423+ #[ derive( Copy , Clone , PartialOrd , Ord , PartialEq , Eq , Debug ) ]
424+ pub struct DummyHash256 ;
425+
426+ impl str:: FromStr for DummyHash256 {
427+ type Err = & ' static str ;
428+ fn from_str ( x : & str ) -> Result < DummyHash256 , & ' static str > {
429+ if x. is_empty ( ) {
430+ Ok ( DummyHash256 )
431+ } else {
432+ Err ( "non empty dummy hash" )
433+ }
434+ }
435+ }
436+
437+ impl fmt:: Display for DummyHash256 {
438+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
439+ f. write_str ( "" )
440+ }
441+ }
442+
443+ impl hash:: Hash for DummyHash256 {
444+ fn hash < H : hash:: Hasher > ( & self , state : & mut H ) {
445+ "DummySha256Hash" . hash ( state) ;
446+ }
447+ }
448+
449+ /// Provides the conversion information required in [`TranslatePk`]
395450pub trait Translator < P , Q , E >
396451where
397452 P : MiniscriptKey ,
@@ -403,8 +458,11 @@ where
403458 /// Translates public key hashes P::Hash -> Q::Hash.
404459 fn pkh ( & mut self , pkh : & P :: Hash ) -> Result < Q :: Hash , E > ;
405460
406- /// Translates sha256 hashes from P::Sha256 -> Q::Sha256
461+ /// Provides the translation from P::Sha256 -> Q::Sha256
407462 fn sha256 ( & mut self , sha256 : & P :: Sha256 ) -> Result < Q :: Sha256 , E > ;
463+
464+ /// Provides the translation from P::Hash256 -> Q::Hash256
465+ fn hash256 ( & mut self , hash256 : & P :: Hash256 ) -> Result < Q :: Hash256 , E > ;
408466}
409467
410468/// Provides the conversion information required in [`TranslatePk`].
@@ -426,7 +484,7 @@ impl<P, Q, E, T> Translator<P, Q, E> for T
426484where
427485 T : PkTranslator < P , Q , E > ,
428486 P : MiniscriptKey ,
429- Q : MiniscriptKey < Sha256 = P :: Sha256 > ,
487+ Q : MiniscriptKey < Sha256 = P :: Sha256 , Hash256 = P :: Hash256 > ,
430488{
431489 fn pk ( & mut self , pk : & P ) -> Result < Q , E > {
432490 <Self as PkTranslator < P , Q , E > >:: pk ( self , pk)
@@ -439,6 +497,10 @@ where
439497 fn sha256 ( & mut self , sha256 : & <P as MiniscriptKey >:: Sha256 ) -> Result < <Q >:: Sha256 , E > {
440498 Ok ( sha256. clone ( ) )
441499 }
500+
501+ fn hash256 ( & mut self , hash256 : & <P as MiniscriptKey >:: Hash256 ) -> Result < <Q >:: Hash256 , E > {
502+ Ok ( hash256. clone ( ) )
503+ }
442504}
443505
444506/// Converts a descriptor using abstract keys to one using specific keys. Uses translator `t` to do
0 commit comments