Add PSBT::CacheUnsignedTxPieces

Fetches the PSBTv2 fields from PSBTv0's global unsigned tx. This allows
us to pretend everything internally is a PSBTv2 and makes things easier
to work with.
This commit is contained in:
Ava Chow 2024-07-22 17:14:10 -04:00
parent 057249398d
commit f4da139c60
2 changed files with 38 additions and 0 deletions

View file

@ -14,6 +14,7 @@ PartiallySignedTransaction::PartiallySignedTransaction(const CMutableTransaction
{
inputs.resize(tx.vin.size(), PSBTInput(GetVersion()));
outputs.resize(tx.vout.size(), PSBTOutput(GetVersion()));
CacheUnsignedTxPieces();
}
bool PartiallySignedTransaction::IsNull() const
@ -566,3 +567,36 @@ uint32_t PartiallySignedTransaction::GetVersion() const
}
return 0;
}
void PartiallySignedTransaction::SetupFromTx(const CMutableTransaction& tx)
{
tx_version = tx.version;
fallback_locktime = tx.nLockTime;
uint32_t i;
for (i = 0; i < tx.vin.size(); ++i) {
PSBTInput& input = inputs.at(i);
const CTxIn& txin = tx.vin.at(i);
input.prev_txid = txin.prevout.hash;
input.prev_out = txin.prevout.n;
input.sequence = txin.nSequence;
}
for (i = 0; i < tx.vout.size(); ++i) {
PSBTOutput& output = outputs.at(i);
const CTxOut& txout = tx.vout.at(i);
output.amount = txout.nValue;
output.script = txout.scriptPubKey;
}
}
void PartiallySignedTransaction::CacheUnsignedTxPieces()
{
// To make things easier, we split up the global unsigned transaction
// and use the PSBTv2 fields for PSBTv0.
if (tx != std::nullopt) {
SetupFromTx(*tx);
}
}

View file

@ -1318,6 +1318,8 @@ struct PartiallySignedTransaction
[[nodiscard]] bool Merge(const PartiallySignedTransaction& psbt);
bool AddInput(const CTxIn& txin, PSBTInput& psbtin);
bool AddOutput(const CTxOut& txout, const PSBTOutput& psbtout);
void SetupFromTx(const CMutableTransaction& tx);
void CacheUnsignedTxPieces();
PartiallySignedTransaction() = default;
explicit PartiallySignedTransaction(const CMutableTransaction& tx);
/**
@ -1695,6 +1697,8 @@ struct PartiallySignedTransaction
if (outputs.size() != output_count) {
throw std::ios_base::failure("Outputs provided does not match the number of outputs in transaction.");
}
CacheUnsignedTxPieces();
}
template <typename Stream>