Merge rust-bitcoin/rust-bitcoin#7098: bitcoin: include hidden nodes in the `leaves` list
What changed, and why it matters
This change fixes a bug in how Taproot tree data is saved to JSON or other serialized formats. Previously, deliberately hidden branches of a Taproot tree were left out of the 'leaves' list, so after saving and loading the tree back, the hidden part was missing. The fix adds a placeholder hidden leaf so round-trip serialization works correctly. It is a correctness/data-integrity bug, not a direct theft-of-funds vulnerability.
Treat as a routine bug fix. No immediate security response required, but downstream users relying on serialized Taproot tree representations should update to avoid silent loss of hidden-node information on save/load.
Security signals we found
Data integrity / serialization round-trip failure
Taproot Merkle tree node representation bug
Fix includes regression test for serde hidden-node round trip
Evidence from the diff
In rust-bitcoin’s Taproot module, NodeInfo::new_hidden_node previously created a node with leaves: vec![] and has_hidden_nodes: true. When such a node was serialized (e.g., via serde), the hidden subtree was not represented in the leaves vector, so deserialization could not reconstruct the same node hash or hidden structure. The patch inserts LeafNode::new_hidden(hash) into the leaves vector, and adds a serde round-trip test combining a known leaf with a hidden node.
Changed components
bitcoin/src/taproot/mod.rsNodeInfo::new_hidden_nodeTaproot serde serialization/deserializationInspect captured patch +15 / −1
### bitcoin/src/taproot/mod.rs
@@ -738,7 +738,7 @@ impl Eq for NodeInfo {}
impl NodeInfo {
/// Constructs a new [`NodeInfo`] with omitted/hidden info.
pub fn new_hidden_node(hash: TapNodeHash) -> Self {
- Self { hash, leaves: vec![], has_hidden_nodes: true }
+ Self { hash, leaves: vec![LeafNode::new_hidden(hash)], has_hidden_nodes: true }
}
/// Constructs a new leaf [`NodeInfo`] with given [`TapScriptBuf`] and [`LeafVersion`].
@@ -1694,6 +1694,20 @@ mod test {
}
}
+ #[test]
+ #[cfg(feature = "serde")]
+ fn hidden_node_serde_roundtrip() {
+ let known = NodeInfo::new_leaf_with_ver(TapScriptBuf::new(), LeafVersion::TapScript);
+ let hidden = NodeInfo::new_hidden_node(TapNodeHash::assume_hidden([42; 32]));
+ let tree = NodeInfo::combine(known, hidden).unwrap();
+
+ let json = serde_json::to_string(&tree).unwrap();
+ let restored: NodeInfo = serde_json::from_str(&json).unwrap();
+
+ assert_eq!(restored.node_hash(), tree.node_hash());
+ assert!(restored.leaf_nodes().eq(tree.leaf_nodes()));
+ }
+
#[test]
fn taptree_builder() {
let internal_key = "93c7378d96518a75448821c4f7c8f4bae7ce60f804d03d1f0628dd5dd0f5de51"Why this scored 27/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.