Skip to content

Commit 78b9893

Browse files
committed
Remove op== on PSBTs; check compatibility in Merge
Remove the op== on PartiallySignedTransaction, which only checks that the CTransactions are equal. Instead, check this directly in Merge, and return false if the CTransactions are not equal (so the PSBTs cannot be merged.)
1 parent bd0dbe8 commit 78b9893

File tree

3 files changed

+13
-14
lines changed

3 files changed

+13
-14
lines changed

src/psbt.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,22 @@ bool PartiallySignedTransaction::IsNull() const
1616
return !tx && inputs.empty() && outputs.empty() && unknown.empty();
1717
}
1818

19-
void PartiallySignedTransaction::Merge(const PartiallySignedTransaction& psbt)
19+
bool PartiallySignedTransaction::Merge(const PartiallySignedTransaction& psbt)
2020
{
21+
// Prohibited to merge two PSBTs over different transactions
22+
if (tx->GetHash() != psbt.tx->GetHash()) {
23+
return false;
24+
}
25+
2126
for (unsigned int i = 0; i < inputs.size(); ++i) {
2227
inputs[i].Merge(psbt.inputs[i]);
2328
}
2429
for (unsigned int i = 0; i < outputs.size(); ++i) {
2530
outputs[i].Merge(psbt.outputs[i]);
2631
}
2732
unknown.insert(psbt.unknown.begin(), psbt.unknown.end());
33+
34+
return true;
2835
}
2936

3037
bool PartiallySignedTransaction::IsSane() const

src/psbt.h

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -384,22 +384,15 @@ struct PartiallySignedTransaction
384384
std::map<std::vector<unsigned char>, std::vector<unsigned char>> unknown;
385385

386386
bool IsNull() const;
387-
void Merge(const PartiallySignedTransaction& psbt);
387+
388+
/** Merge psbt into this. The two psbts must have the same underlying CTransaction (i.e. the
389+
* same actual Bitcoin transaction.) Returns true if the merge succeeded, false otherwise. */
390+
NODISCARD bool Merge(const PartiallySignedTransaction& psbt);
388391
bool IsSane() const;
389392
PartiallySignedTransaction() {}
390393
PartiallySignedTransaction(const PartiallySignedTransaction& psbt_in) : tx(psbt_in.tx), inputs(psbt_in.inputs), outputs(psbt_in.outputs), unknown(psbt_in.unknown) {}
391394
explicit PartiallySignedTransaction(const CMutableTransaction& tx);
392395

393-
// Only checks if they refer to the same transaction
394-
friend bool operator==(const PartiallySignedTransaction& a, const PartiallySignedTransaction &b)
395-
{
396-
return a.tx->GetHash() == b.tx->GetHash();
397-
}
398-
friend bool operator!=(const PartiallySignedTransaction& a, const PartiallySignedTransaction &b)
399-
{
400-
return !(a == b);
401-
}
402-
403396
template <typename Stream>
404397
inline void Serialize(Stream& s) const {
405398

src/rpc/rawtransaction.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1481,10 +1481,9 @@ UniValue combinepsbt(const JSONRPCRequest& request)
14811481

14821482
// Merge
14831483
for (auto it = std::next(psbtxs.begin()); it != psbtxs.end(); ++it) {
1484-
if (*it != merged_psbt) {
1484+
if (!merged_psbt.Merge(*it)) {
14851485
throw JSONRPCError(RPC_INVALID_PARAMETER, "PSBTs do not refer to the same transactions.");
14861486
}
1487-
merged_psbt.Merge(*it);
14881487
}
14891488
if (!merged_psbt.IsSane()) {
14901489
throw JSONRPCError(RPC_INVALID_PARAMETER, "Merged PSBT is inconsistent");

0 commit comments

Comments
 (0)