From 13843300299b376079790692e448baa8006b25c1 Mon Sep 17 00:00:00 2001 From: conduition Date: Sun, 11 Feb 2024 17:14:13 +0000 Subject: [PATCH] taproot: add TapNodeHash getter method on TapTree and NodeInfo Fixes a gap in the API of the taproot module. Callers can now use TapTree::root_hash or NodeInfo::node_hash to extract the taproot tree merkle root hash for fast validation without any ECC overhead. --- bitcoin/src/taproot/mod.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/bitcoin/src/taproot/mod.rs b/bitcoin/src/taproot/mod.rs index 203ac314..14308ac9 100644 --- a/bitcoin/src/taproot/mod.rs +++ b/bitcoin/src/taproot/mod.rs @@ -693,6 +693,9 @@ impl TapTree { /// Returns [`TapTreeIter<'_>`] iterator for a taproot script tree, operating in DFS order over /// tree [`ScriptLeaf`]s. pub fn script_leaves(&self) -> ScriptLeaves { ScriptLeaves { leaf_iter: self.0.leaf_nodes() } } + + /// Returns the root [`TapNodeHash`] of this tree. + pub fn root_hash(&self) -> TapNodeHash { self.0.hash } } impl TryFrom for TapTree { @@ -842,6 +845,9 @@ impl NodeInfo { /// Creates an iterator over all leaves (including hidden leaves) in the tree. pub fn leaf_nodes(&self) -> LeafNodes { LeafNodes { leaf_iter: self.leaves.iter() } } + + /// Returns the root [`TapNodeHash`] of this node info. + pub fn node_hash(&self) -> TapNodeHash { self.hash } } impl TryFrom for NodeInfo {