Add PSBT::ComputeLockTime()

Function to compute the lock time for the transaction
This commit is contained in:
Ava Chow 2024-07-22 17:14:17 -04:00
parent bb4da6631e
commit e27e795445
2 changed files with 36 additions and 0 deletions

View file

@ -47,6 +47,41 @@ bool PartiallySignedTransaction::Merge(const PartiallySignedTransaction& psbt)
return true;
}
bool PartiallySignedTransaction::ComputeTimeLock(uint32_t& locktime) const
{
std::optional<uint32_t> time_lock{0};
std::optional<uint32_t> height_lock{0};
for (const PSBTInput& input : inputs) {
if (input.time_locktime != std::nullopt && input.height_locktime == std::nullopt) {
height_lock.reset(); // Transaction can no longer have a height locktime
if (time_lock == std::nullopt) {
return false;
}
} else if (input.time_locktime == std::nullopt && input.height_locktime != std::nullopt) {
time_lock.reset(); // Transaction can no longer have a time locktime
if (height_lock == std::nullopt) {
return false;
}
}
if (input.time_locktime && time_lock != std::nullopt) {
time_lock = std::max(time_lock, input.time_locktime);
}
if (input.height_locktime && height_lock != std::nullopt) {
height_lock = std::max(height_lock, input.height_locktime);
}
}
if (height_lock != std::nullopt && *height_lock > 0) {
locktime = *height_lock;
return true;
}
if (time_lock != std::nullopt && *time_lock > 0) {
locktime = *time_lock;
return true;
}
locktime = fallback_locktime.value_or(0);
return true;
}
bool PartiallySignedTransaction::AddInput(const CTxIn& txin, PSBTInput& psbtin)
{
if (std::find(tx->vin.begin(), tx->vin.end(), txin) != tx->vin.end()) {

View file

@ -1327,6 +1327,7 @@ struct PartiallySignedTransaction
bool AddOutput(const CTxOut& txout, const PSBTOutput& psbtout);
void SetupFromTx(const CMutableTransaction& tx);
void CacheUnsignedTxPieces();
bool ComputeTimeLock(uint32_t& locktime) const;
PartiallySignedTransaction() = default;
explicit PartiallySignedTransaction(const CMutableTransaction& tx);