Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BugFix] Make PSBT serializations 174 compliant by excluding empty taptree from serialization #25856

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 18 additions & 11 deletions src/psbt.h
Original file line number Diff line number Diff line change
Expand Up @@ -755,19 +755,23 @@ struct PSBTOutput

// Write taproot tree
if (m_tap_tree.has_value()) {
SerializeToVector(s, PSBT_OUT_TAP_TREE);
std::vector<unsigned char> value;
CVectorWriter s_value(s.GetType(), s.GetVersion(), value, 0);
// do not write an empty tree to be BIP-174 compliant, which
// requires one-or-more tuple
const auto& tuples = m_tap_tree->GetTreeTuples();
for (const auto& tuple : tuples) {
uint8_t depth = std::get<0>(tuple);
uint8_t leaf_ver = std::get<1>(tuple);
CScript script = std::get<2>(tuple);
s_value << depth;
s_value << leaf_ver;
s_value << script;
if (!tuples.empty()) {
SerializeToVector(s, PSBT_OUT_TAP_TREE);
std::vector<unsigned char> value;
CVectorWriter s_value(s.GetType(), s.GetVersion(), value, 0);
for (const auto& tuple : tuples) {
uint8_t depth = std::get<0>(tuple);
uint8_t leaf_ver = std::get<1>(tuple);
CScript script = std::get<2>(tuple);
s_value << depth;
s_value << leaf_ver;
s_value << script;
}
s << value;
}
s << value;
}

// Write taproot bip32 keypaths
Expand Down Expand Up @@ -862,6 +866,9 @@ struct PSBTOutput
std::vector<unsigned char> tree_v;
s >> tree_v;
SpanReader s_tree(s.GetType(), s.GetVersion(), tree_v);
if (s_tree.empty()) {
throw std::ios_base::failure("Output Taproot tree must not be empty");
}
while (!s_tree.empty()) {
uint8_t depth;
uint8_t leaf_ver;
Expand Down
24 changes: 14 additions & 10 deletions src/rpc/rawtransaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1244,17 +1244,21 @@ static RPCHelpMan decodepsbt()
if (output.m_tap_tree.has_value()) {
UniValue tree(UniValue::VARR);
const auto& tuples = output.m_tap_tree->GetTreeTuples();
for (const auto& tuple : tuples) {
uint8_t depth = std::get<0>(tuple);
uint8_t leaf_ver = std::get<1>(tuple);
CScript script = std::get<2>(tuple);
UniValue elem(UniValue::VOBJ);
elem.pushKV("depth", (int)depth);
elem.pushKV("leaf_ver", (int)leaf_ver);
elem.pushKV("script", HexStr(script));
tree.push_back(elem);
// do not write an empty tree to be BIP-174 compliant, which
// requires one-or-more tuple
if (!tuples.empty()) {
for (const auto& tuple : tuples) {
uint8_t depth = std::get<0>(tuple);
uint8_t leaf_ver = std::get<1>(tuple);
CScript script = std::get<2>(tuple);
UniValue elem(UniValue::VOBJ);
elem.pushKV("depth", (int)depth);
elem.pushKV("leaf_ver", (int)leaf_ver);
elem.pushKV("script", HexStr(script));
tree.push_back(elem);
}
out.pushKV("taproot_tree", tree);
}
out.pushKV("taproot_tree", tree);
}

// Taproot bip32 keypaths
Expand Down