@@ -152,20 +152,26 @@ pub trait MiniscriptKey: Clone + Eq + Ord + fmt::Debug + fmt::Display + hash::Ha
152152 false
153153 }
154154
155- /// The associated [`Hash`] type for this pubkey.
155+ /// The associated PublicKey Hash for this [`MiniscriptKey`],
156+ /// used in the hash256 fragment.
156157 type Hash : Clone + Eq + Ord + fmt:: Display + fmt:: Debug + hash:: Hash ;
157158
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.
160161 type Sha256 : Clone + Eq + Ord + fmt:: Display + fmt:: Debug + hash:: Hash ;
161162
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+
162167 /// Converts this key to the associated pubkey hash.
163168 fn to_pubkeyhash ( & self ) -> Self :: Hash ;
164169}
165170
166171impl MiniscriptKey for bitcoin:: secp256k1:: PublicKey {
167172 type Hash = hash160:: Hash ;
168173 type Sha256 = sha256:: Hash ;
174+ type Hash256 = hash256:: Hash ;
169175
170176 fn to_pubkeyhash ( & self ) -> Self :: Hash {
171177 hash160:: Hash :: hash ( & self . serialize ( ) )
@@ -180,6 +186,7 @@ impl MiniscriptKey for bitcoin::PublicKey {
180186
181187 type Hash = hash160:: Hash ;
182188 type Sha256 = sha256:: Hash ;
189+ type Hash256 = hash256:: Hash ;
183190
184191 fn to_pubkeyhash ( & self ) -> Self :: Hash {
185192 hash160:: Hash :: hash ( & self . to_bytes ( ) )
@@ -189,6 +196,7 @@ impl MiniscriptKey for bitcoin::PublicKey {
189196impl MiniscriptKey for bitcoin:: secp256k1:: XOnlyPublicKey {
190197 type Hash = hash160:: Hash ;
191198 type Sha256 = sha256:: Hash ;
199+ type Hash256 = hash256:: Hash ;
192200
193201 fn to_pubkeyhash ( & self ) -> Self :: Hash {
194202 hash160:: Hash :: hash ( & self . serialize ( ) )
@@ -202,6 +210,7 @@ impl MiniscriptKey for bitcoin::secp256k1::XOnlyPublicKey {
202210impl MiniscriptKey for String {
203211 type Hash = String ;
204212 type Sha256 = String ; // specify hashes as string
213+ type Hash256 = String ;
205214
206215 fn to_pubkeyhash ( & self ) -> Self :: Hash {
207216 ( & self ) . to_string ( )
@@ -229,6 +238,9 @@ pub trait ToPublicKey: MiniscriptKey {
229238
230239 /// Converts the generic associated [`MiniscriptKey::Sha256`] to [`sha256::Hash`]
231240 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 ;
232244}
233245
234246impl ToPublicKey for bitcoin:: PublicKey {
@@ -243,6 +255,10 @@ impl ToPublicKey for bitcoin::PublicKey {
243255 fn to_sha256 ( hash : & sha256:: Hash ) -> sha256:: Hash {
244256 * hash
245257 }
258+
259+ fn to_hash256 ( hash : & hash256:: Hash ) -> hash256:: Hash {
260+ * hash
261+ }
246262}
247263
248264impl ToPublicKey for bitcoin:: secp256k1:: PublicKey {
@@ -257,6 +273,10 @@ impl ToPublicKey for bitcoin::secp256k1::PublicKey {
257273 fn to_sha256 ( hash : & sha256:: Hash ) -> sha256:: Hash {
258274 * hash
259275 }
276+
277+ fn to_hash256 ( hash : & hash256:: Hash ) -> hash256:: Hash {
278+ * hash
279+ }
260280}
261281
262282impl ToPublicKey for bitcoin:: secp256k1:: XOnlyPublicKey {
@@ -280,6 +300,10 @@ impl ToPublicKey for bitcoin::secp256k1::XOnlyPublicKey {
280300 fn to_sha256 ( hash : & sha256:: Hash ) -> sha256:: Hash {
281301 * hash
282302 }
303+
304+ fn to_hash256 ( hash : & hash256:: Hash ) -> hash256:: Hash {
305+ * hash
306+ }
283307}
284308
285309/// Dummy key which de/serializes to the empty string; useful sometimes for testing
@@ -300,6 +324,7 @@ impl str::FromStr for DummyKey {
300324impl MiniscriptKey for DummyKey {
301325 type Hash = DummyKeyHash ;
302326 type Sha256 = DummySha256Hash ;
327+ type Hash256 = DummyHash256 ;
303328
304329 fn to_pubkeyhash ( & self ) -> Self :: Hash {
305330 DummyKeyHash
@@ -334,6 +359,11 @@ impl ToPublicKey for DummyKey {
334359 sha256:: Hash :: from_str ( "50863ad64a87ae8a2fe83c1af1a8403cb53f53e486d8511dad8a04887e5b2352" )
335360 . unwrap ( )
336361 }
362+
363+ fn to_hash256 ( _hash : & DummyHash256 ) -> hash256:: Hash {
364+ hash256:: Hash :: from_str ( "50863ad64a87ae8a2fe83c1af1a8403cb53f53e486d8511dad8a04887e5b2352" )
365+ . unwrap ( )
366+ }
337367}
338368
339369/// Dummy keyhash which de/serializes to the empty string; useful sometimes for testing
@@ -390,8 +420,34 @@ impl hash::Hash for DummySha256Hash {
390420 }
391421}
392422
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`]
395451pub trait Translator < P , Q , E >
396452where
397453 P : MiniscriptKey ,
@@ -403,8 +459,11 @@ where
403459 /// Translates public key hashes P::Hash -> Q::Hash.
404460 fn pkh ( & mut self , pkh : & P :: Hash ) -> Result < Q :: Hash , E > ;
405461
406- /// Translates sha256 hashes from P::Sha256 -> Q::Sha256
462+ /// Provides the translation from P::Sha256 -> Q::Sha256
407463 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 > ;
408467}
409468
410469/// Provides the conversion information required in [`TranslatePk`].
@@ -426,7 +485,7 @@ impl<P, Q, E, T> Translator<P, Q, E> for T
426485where
427486 T : PkTranslator < P , Q , E > ,
428487 P : MiniscriptKey ,
429- Q : MiniscriptKey < Sha256 = P :: Sha256 > ,
488+ Q : MiniscriptKey < Sha256 = P :: Sha256 , Hash256 = P :: Hash256 > ,
430489{
431490 fn pk ( & mut self , pk : & P ) -> Result < Q , E > {
432491 <Self as PkTranslator < P , Q , E > >:: pk ( self , pk)
@@ -439,6 +498,10 @@ where
439498 fn sha256 ( & mut self , sha256 : & <P as MiniscriptKey >:: Sha256 ) -> Result < <Q >:: Sha256 , E > {
440499 Ok ( sha256. clone ( ) )
441500 }
501+
502+ fn hash256 ( & mut self , hash256 : & <P as MiniscriptKey >:: Hash256 ) -> Result < <Q >:: Hash256 , E > {
503+ Ok ( hash256. clone ( ) )
504+ }
442505}
443506
444507/// Converts a descriptor using abstract keys to one using specific keys. Uses translator `t` to do
0 commit comments