Add PSBT::GetUniqueID

The unique ID for PSBTv2 is different from v0. Use this function to get
the ID without requiring the caller to know the version number.
This commit is contained in:
Ava Chow 2024-07-22 17:14:20 -04:00
parent 1514c34d46
commit 05ef17b5e2
2 changed files with 17 additions and 1 deletions

View file

@ -26,7 +26,7 @@ bool PartiallySignedTransaction::IsNull() const
bool PartiallySignedTransaction::Merge(const PartiallySignedTransaction& psbt)
{
// Prohibited to merge two PSBTs over different transactions
if (tx->GetHash() != psbt.tx->GetHash()) {
if (GetUniqueID() != psbt.GetUniqueID()) {
return false;
}
@ -110,6 +110,21 @@ CMutableTransaction PartiallySignedTransaction::GetUnsignedTx() const
return mtx;
}
uint256 PartiallySignedTransaction::GetUniqueID() const
{
if (m_version == 0) {
return tx->GetHash();
}
// Get the unsigned transaction
CMutableTransaction mtx = GetUnsignedTx();
// Set the sequence numbers to 0
for (CTxIn& txin : mtx.vin) {
txin.nSequence = 0;
}
return mtx.GetHash();
}
bool PartiallySignedTransaction::AddInput(const CTxIn& txin, PSBTInput& psbtin)
{
if (std::find(tx->vin.begin(), tx->vin.end(), txin) != tx->vin.end()) {

View file

@ -1329,6 +1329,7 @@ struct PartiallySignedTransaction
void CacheUnsignedTxPieces();
bool ComputeTimeLock(uint32_t& locktime) const;
CMutableTransaction GetUnsignedTx() const;
uint256 GetUniqueID() const;
PartiallySignedTransaction() = default;
explicit PartiallySignedTransaction(const CMutableTransaction& tx);