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 863cf47b33
commit 5770dbd39f
2 changed files with 40 additions and 1 deletions

View File

@@ -61,11 +61,49 @@ bool PartiallySignedTransaction::Merge(const PartiallySignedTransaction& psbt)
return true;
}
std::optional<uint32_t> PartiallySignedTransaction::ComputeTimeLock() const
{
if (GetVersion() >= 2) {
std::optional<uint32_t> time_lock{0};
std::optional<uint32_t> height_lock{0};
for (const PSBTInput& input : inputs) {
if (input.time_locktime.has_value() && !input.height_locktime.has_value()) {
height_lock.reset(); // Transaction can no longer have a height locktime
if (!time_lock.has_value()) {
return std::nullopt;
}
} else if (!input.time_locktime.has_value() && input.height_locktime.has_value()) {
time_lock.reset(); // Transaction can no longer have a time locktime
if (!height_lock.has_value()) {
return std::nullopt;
}
}
if (input.time_locktime && time_lock.has_value()) {
time_lock = std::max(time_lock, input.time_locktime);
}
if (input.height_locktime && height_lock.has_value()) {
height_lock = std::max(height_lock, input.height_locktime);
}
}
if (height_lock.has_value() && *height_lock > 0) {
return *height_lock;
}
if (time_lock.has_value() && *time_lock > 0) {
return *time_lock;
}
}
return fallback_locktime.value_or(0);
}
std::optional<CMutableTransaction> PartiallySignedTransaction::GetUnsignedTx() const
{
CMutableTransaction mtx;
mtx.version = tx_version;
mtx.nLockTime = fallback_locktime.value_or(0);
std::optional<uint32_t> locktime = ComputeTimeLock();
if (!locktime) {
return std::nullopt;
}
mtx.nLockTime = *locktime;
uint32_t max_sequence = CTxIn::SEQUENCE_FINAL;
for (const PSBTInput& input : inputs) {
CTxIn txin;

View File

@@ -1256,6 +1256,7 @@ public:
[[nodiscard]] bool Merge(const PartiallySignedTransaction& psbt);
bool AddInput(const PSBTInput& psbtin);
bool AddOutput(const PSBTOutput& psbtout);
std::optional<uint32_t> ComputeTimeLock() const;
std::optional<CMutableTransaction> GetUnsignedTx() const;
std::optional<Txid> GetUniqueID() const;
explicit PartiallySignedTransaction(const CMutableTransaction& tx);