psbt: avoid duplicate global xpub keys when merging

The global xpubs are stored in a map of key origin to set of xpubs,
while the serialization writes one record per xpub, keyed by the xpub.
Merging two PSBTs origin-by-origin can store the same xpub under two
origins, which serializes as a PSBT with duplicate keys that the
deserializer (and BIP 174) reject. Notably, combinepsbt returned a
PSBT that no RPC could parse again when the combined PSBTs provide
different key origins for the same xpub.

Deduplicate by xpub when merging, keeping the origin that is already
present, and share the logic between combinepsbt and joinpsbts.
This commit is contained in:
Thomas
2026-06-12 08:01:08 +02:00
parent 2063f02bd5
commit 7c632c0e2a
3 changed files with 17 additions and 14 deletions

View File

@@ -13,6 +13,8 @@
#include <util/result.h>
#include <util/strencodings.h>
#include <algorithm>
using common::PSBTError;
PartiallySignedTransaction::PartiallySignedTransaction(const CMutableTransaction& tx, uint32_t version) : m_version(version)
@@ -58,13 +60,7 @@ bool PartiallySignedTransaction::Merge(const PartiallySignedTransaction& psbt)
return false;
}
}
for (auto& xpub_pair : psbt.m_xpubs) {
if (!m_xpubs.contains(xpub_pair.first)) {
m_xpubs[xpub_pair.first] = xpub_pair.second;
} else {
m_xpubs[xpub_pair.first].insert(xpub_pair.second.begin(), xpub_pair.second.end());
}
}
MergeGlobalXPubs(psbt);
if (fallback_locktime == std::nullopt && psbt.fallback_locktime != std::nullopt) fallback_locktime = psbt.fallback_locktime;
// Set m_tx_modifiable only if either PSBT had it set
@@ -85,6 +81,16 @@ bool PartiallySignedTransaction::Merge(const PartiallySignedTransaction& psbt)
return true;
}
void PartiallySignedTransaction::MergeGlobalXPubs(const PartiallySignedTransaction& psbt)
{
for (const auto& [origin, xpubs] : psbt.m_xpubs) {
for (const CExtPubKey& xpub : xpubs) {
const bool known{std::ranges::any_of(m_xpubs, [&](const auto& entry) { return entry.second.contains(xpub); })};
if (!known) m_xpubs[origin].insert(xpub);
}
}
}
std::optional<uint32_t> PartiallySignedTransaction::ComputeTimeLock() const
{
if (GetVersion() >= 2) {