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.
This commit is contained in:
Andrew Chow 2021-07-12 17:04:46 -04:00
parent ce911204e4
commit d43923c381
2 changed files with 18 additions and 0 deletions

View File

@ -642,3 +642,19 @@ std::optional<std::vector<std::tuple<int, CScript, int>>> InferTaprootTree(const
return ret;
}
std::vector<std::tuple<uint8_t, uint8_t, CScript>> TaprootBuilder::GetTreeTuples() const
{
assert(IsComplete());
std::vector<std::tuple<uint8_t, uint8_t, CScript>> tuples;
if (m_branch.size()) {
const auto& leaves = m_branch[0]->leaves;
for (const auto& leaf : leaves) {
assert(leaf.merkle_branch.size() <= TAPROOT_CONTROL_MAX_NODE_COUNT);
uint8_t depth = (uint8_t)leaf.merkle_branch.size();
uint8_t leaf_ver = (uint8_t)leaf.leaf_version;
tuples.push_back(std::make_tuple(depth, leaf_ver, leaf.script));
}
}
return tuples;
}

View File

@ -322,6 +322,8 @@ public:
static bool ValidDepths(const std::vector<int>& depths);
/** Compute spending data (after Finalize()). */
TaprootSpendData GetSpendData() const;
/** Returns a vector of tuples representing the depth, leaf version, and script */
std::vector<std::tuple<uint8_t, uint8_t, CScript>> GetTreeTuples() const;
};
/** Given a TaprootSpendData and the output key, reconstruct its script tree.