From d43923c38155fdadad3837d79c19a84c9d2d7f50 Mon Sep 17 00:00:00 2001 From: Andrew Chow Date: Mon, 12 Jul 2021 17:04:46 -0400 Subject: [PATCH] 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. --- src/script/standard.cpp | 16 ++++++++++++++++ src/script/standard.h | 2 ++ 2 files changed, 18 insertions(+) diff --git a/src/script/standard.cpp b/src/script/standard.cpp index e25155d3dd7..063e149d36c 100644 --- a/src/script/standard.cpp +++ b/src/script/standard.cpp @@ -642,3 +642,19 @@ std::optional>> InferTaprootTree(const return ret; } + +std::vector> TaprootBuilder::GetTreeTuples() const +{ + assert(IsComplete()); + std::vector> 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; +} diff --git a/src/script/standard.h b/src/script/standard.h index 6a15ba4e3d5..448fdff0109 100644 --- a/src/script/standard.h +++ b/src/script/standard.h @@ -322,6 +322,8 @@ public: static bool ValidDepths(const std::vector& depths); /** Compute spending data (after Finalize()). */ TaprootSpendData GetSpendData() const; + /** Returns a vector of tuples representing the depth, leaf version, and script */ + std::vector> GetTreeTuples() const; }; /** Given a TaprootSpendData and the output key, reconstruct its script tree.