Skip to content

Commit d43923c

Browse files
committed
Add TaprootBuilder::GetTreeTuples
GetTreeTuples returns the leaves in DFS order as tuples of depth, leaf version, and script. This is a representation of the tree that can be serialized.
1 parent ce91120 commit d43923c

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/script/standard.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,3 +642,19 @@ std::optional<std::vector<std::tuple<int, CScript, int>>> InferTaprootTree(const
642642

643643
return ret;
644644
}
645+
646+
std::vector<std::tuple<uint8_t, uint8_t, CScript>> TaprootBuilder::GetTreeTuples() const
647+
{
648+
assert(IsComplete());
649+
std::vector<std::tuple<uint8_t, uint8_t, CScript>> tuples;
650+
if (m_branch.size()) {
651+
const auto& leaves = m_branch[0]->leaves;
652+
for (const auto& leaf : leaves) {
653+
assert(leaf.merkle_branch.size() <= TAPROOT_CONTROL_MAX_NODE_COUNT);
654+
uint8_t depth = (uint8_t)leaf.merkle_branch.size();
655+
uint8_t leaf_ver = (uint8_t)leaf.leaf_version;
656+
tuples.push_back(std::make_tuple(depth, leaf_ver, leaf.script));
657+
}
658+
}
659+
return tuples;
660+
}

src/script/standard.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,8 @@ class TaprootBuilder
322322
static bool ValidDepths(const std::vector<int>& depths);
323323
/** Compute spending data (after Finalize()). */
324324
TaprootSpendData GetSpendData() const;
325+
/** Returns a vector of tuples representing the depth, leaf version, and script */
326+
std::vector<std::tuple<uint8_t, uint8_t, CScript>> GetTreeTuples() const;
325327
};
326328

327329
/** Given a TaprootSpendData and the output key, reconstruct its script tree.

0 commit comments

Comments
 (0)