diff options
author | Andrew Chow <achow101-github@achow101.com> | 2021-07-12 17:04:46 -0400 |
---|---|---|
committer | Andrew Chow <achow101-github@achow101.com> | 2022-06-27 16:47:48 -0400 |
commit | d43923c38155fdadad3837d79c19a84c9d2d7f50 (patch) | |
tree | f5149f49e948a4ad2585ab51053ec4d08d736224 /src/script | |
parent | ce911204e42b8653cad791d1727aa625de9d0079 (diff) |
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.
Diffstat (limited to 'src/script')
-rw-r--r-- | src/script/standard.cpp | 16 | ||||
-rw-r--r-- | src/script/standard.h | 2 |
2 files changed, 18 insertions, 0 deletions
diff --git a/src/script/standard.cpp b/src/script/standard.cpp index e25155d3dd..063e149d36 100644 --- a/src/script/standard.cpp +++ b/src/script/standard.cpp @@ -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; +} diff --git a/src/script/standard.h b/src/script/standard.h index 6a15ba4e3d..448fdff010 100644 --- a/src/script/standard.h +++ b/src/script/standard.h @@ -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. |