From 10bf24726b8ddddfbdefbbb6bd44711dbd8f99f6 Mon Sep 17 00:00:00 2001 From: Andrew Chow Date: Mon, 11 Jan 2021 16:15:04 -0500 Subject: [PATCH] Add PSBT::ComputeLockTime() Function to compute the lock time for the transaction --- src/psbt.cpp | 35 +++++++++++++++++++++++++++++++++++ src/psbt.h | 1 + 2 files changed, 36 insertions(+) diff --git a/src/psbt.cpp b/src/psbt.cpp index 13e166fe1f..d529976c53 100644 --- a/src/psbt.cpp +++ b/src/psbt.cpp @@ -45,6 +45,41 @@ bool PartiallySignedTransaction::Merge(const PartiallySignedTransaction& psbt) return true; } +bool PartiallySignedTransaction::ComputeTimeLock(uint32_t& locktime) const +{ + Optional time_lock{0}; + Optional height_lock{0}; + for (const PSBTInput& input : inputs) { + if (input.time_locktime != nullopt && input.height_locktime == nullopt) { + height_lock.reset(); // Transaction can no longer have a height locktime + if (time_lock == nullopt) { + return false; + } + } else if (input.time_locktime == nullopt && input.height_locktime != nullopt) { + time_lock.reset(); // Transaction can no longer have a time locktime + if (height_lock == nullopt) { + return false; + } + } + if (input.time_locktime && time_lock != nullopt) { + time_lock = std::max(time_lock, input.time_locktime); + } + if (input.height_locktime && height_lock != nullopt) { + height_lock = std::max(height_lock, input.height_locktime); + } + } + if (height_lock != nullopt && *height_lock > 0) { + locktime = *height_lock; + return true; + } + if (time_lock != 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()) { diff --git a/src/psbt.h b/src/psbt.h index cae568d435..7ef46c7ad8 100644 --- a/src/psbt.h +++ b/src/psbt.h @@ -741,6 +741,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() {} explicit PartiallySignedTransaction(const CMutableTransaction& tx);