Add PSBT::GetUnsignedTx

A helper function for getting the unsigned transaction regardless of
psbt version.
This commit is contained in:
Ava Chow 2024-07-22 17:14:18 -04:00
parent e27e795445
commit 1514c34d46
2 changed files with 29 additions and 0 deletions

View file

@ -6,6 +6,7 @@
#include <node/types.h>
#include <policy/policy.h>
#include <primitives/transaction.h>
#include <script/signingprovider.h>
#include <util/check.h>
#include <util/strencodings.h>
@ -82,6 +83,33 @@ bool PartiallySignedTransaction::ComputeTimeLock(uint32_t& locktime) const
return true;
}
CMutableTransaction PartiallySignedTransaction::GetUnsignedTx() const
{
if (tx != std::nullopt) {
return *tx;
}
CMutableTransaction mtx;
mtx.version = *tx_version;
bool locktime_success = ComputeTimeLock(mtx.nLockTime);
assert(locktime_success);
uint32_t max_sequence = CTxIn::SEQUENCE_FINAL;
for (const PSBTInput& input : inputs) {
CTxIn txin;
txin.prevout.hash = input.prev_txid;
txin.prevout.n = *input.prev_out;
txin.nSequence = input.sequence.value_or(max_sequence);
mtx.vin.push_back(txin);
}
for (const PSBTOutput& output : outputs) {
CTxOut txout;
txout.nValue = *output.amount;
txout.scriptPubKey = *output.script;
mtx.vout.push_back(txout);
}
return mtx;
}
bool PartiallySignedTransaction::AddInput(const CTxIn& txin, PSBTInput& psbtin)
{
if (std::find(tx->vin.begin(), tx->vin.end(), txin) != tx->vin.end()) {

View file

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