@@ -20,6 +20,7 @@ use crate::prelude::*;
20
20
use crate :: io;
21
21
use secp256k1:: { self , Secp256k1 } ;
22
22
23
+ use core:: convert:: TryFrom ;
23
24
use core:: fmt;
24
25
use core:: cmp:: Reverse ;
25
26
@@ -672,6 +673,18 @@ impl TaprootMerkleBranch {
672
673
}
673
674
}
674
675
676
+ /// Creates a merkle proof from list of hashes.
677
+ ///
678
+ /// # Errors
679
+ /// If inner proof length is more than [`TAPROOT_CONTROL_MAX_NODE_COUNT`] (128).
680
+ fn from_collection < T : AsRef < [ sha256:: Hash ] > + Into < Vec < sha256:: Hash > > > ( collection : T ) -> Result < Self , TaprootError > {
681
+ if collection. as_ref ( ) . len ( ) > TAPROOT_CONTROL_MAX_NODE_COUNT {
682
+ Err ( TaprootError :: InvalidMerkleTreeDepth ( collection. as_ref ( ) . len ( ) ) )
683
+ } else {
684
+ Ok ( TaprootMerkleBranch ( collection. into ( ) ) )
685
+ }
686
+ }
687
+
675
688
/// Serializes to a writer.
676
689
///
677
690
/// # Returns
@@ -704,12 +717,9 @@ impl TaprootMerkleBranch {
704
717
/// # Errors
705
718
///
706
719
/// If inner proof length is more than [`TAPROOT_CONTROL_MAX_NODE_COUNT`] (128).
720
+ #[ deprecated( since = "0.29.0" , note = "use try_from instead" ) ]
707
721
pub fn from_inner ( inner : Vec < sha256:: Hash > ) -> Result < Self , TaprootError > {
708
- if inner. len ( ) > TAPROOT_CONTROL_MAX_NODE_COUNT {
709
- Err ( TaprootError :: InvalidMerkleTreeDepth ( inner. len ( ) ) )
710
- } else {
711
- Ok ( TaprootMerkleBranch ( inner) )
712
- }
722
+ Self :: try_from ( inner)
713
723
}
714
724
715
725
/// Returns the inner list of hashes.
@@ -718,6 +728,25 @@ impl TaprootMerkleBranch {
718
728
}
719
729
}
720
730
731
+ macro_rules! impl_try_from {
732
+ ( $from: ty) => {
733
+ impl TryFrom <$from> for TaprootMerkleBranch {
734
+ type Error = TaprootError ;
735
+
736
+ /// Creates a merkle proof from list of hashes.
737
+ ///
738
+ /// # Errors
739
+ /// If inner proof length is more than [`TAPROOT_CONTROL_MAX_NODE_COUNT`] (128).
740
+ fn try_from( v: $from) -> Result <Self , Self :: Error > {
741
+ TaprootMerkleBranch :: from_collection( v)
742
+ }
743
+ }
744
+ }
745
+ }
746
+ impl_try_from ! ( & [ sha256:: Hash ] ) ;
747
+ impl_try_from ! ( Vec <sha256:: Hash >) ;
748
+ impl_try_from ! ( Box <[ sha256:: Hash ] >) ;
749
+
721
750
/// Control block data structure used in Tapscript satisfaction.
722
751
#[ derive( Debug , Clone , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
723
752
#[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
0 commit comments