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:
Andrew Chow 2021-01-11 15:51:44 -05:00
parent 32bdf2d97e
commit bd1a57bd80
2 changed files with 19 additions and 1 deletions

View file

@ -24,7 +24,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;
}
@ -108,6 +108,23 @@ CMutableTransaction PartiallySignedTransaction::GetUnsignedTx() const
return mtx;
}
uint256 PartiallySignedTransaction::GetUniqueID() const
{
if (tx != nullopt) {
return tx->GetHash();
}
// Get the unsigned transaction
CMutableTransaction mtx = GetUnsignedTx();
// Set the locktime to 0
mtx.nLockTime = 0;
// 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

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