mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-12-12 13:43:43 +01:00
Construct and use PrecomputedTransactionData in PSBT signing
This commit is contained in:
26
src/psbt.cpp
26
src/psbt.cpp
@@ -227,7 +227,24 @@ void UpdatePSBTOutput(const SigningProvider& provider, PartiallySignedTransactio
|
||||
psbt_out.FromSignatureData(sigdata);
|
||||
}
|
||||
|
||||
bool SignPSBTInput(const SigningProvider& provider, PartiallySignedTransaction& psbt, int index, int sighash, SignatureData* out_sigdata, bool use_dummy)
|
||||
PrecomputedTransactionData PrecomputePSBTData(const PartiallySignedTransaction& psbt)
|
||||
{
|
||||
const CMutableTransaction& tx = *psbt.tx;
|
||||
bool have_all_spent_outputs = true;
|
||||
std::vector<CTxOut> utxos(tx.vin.size());
|
||||
for (size_t idx = 0; idx < tx.vin.size(); ++idx) {
|
||||
if (!psbt.GetInputUTXO(utxos[idx], idx)) have_all_spent_outputs = false;
|
||||
}
|
||||
PrecomputedTransactionData txdata;
|
||||
if (have_all_spent_outputs) {
|
||||
txdata.Init(tx, std::move(utxos), true);
|
||||
} else {
|
||||
txdata.Init(tx, {}, true);
|
||||
}
|
||||
return txdata;
|
||||
}
|
||||
|
||||
bool SignPSBTInput(const SigningProvider& provider, PartiallySignedTransaction& psbt, int index, const PrecomputedTransactionData* txdata, int sighash, SignatureData* out_sigdata)
|
||||
{
|
||||
PSBTInput& input = psbt.inputs.at(index);
|
||||
const CMutableTransaction& tx = *psbt.tx;
|
||||
@@ -267,10 +284,10 @@ bool SignPSBTInput(const SigningProvider& provider, PartiallySignedTransaction&
|
||||
|
||||
sigdata.witness = false;
|
||||
bool sig_complete;
|
||||
if (use_dummy) {
|
||||
if (txdata == nullptr) {
|
||||
sig_complete = ProduceSignature(provider, DUMMY_SIGNATURE_CREATOR, utxo.scriptPubKey, sigdata);
|
||||
} else {
|
||||
MutableTransactionSignatureCreator creator(&tx, index, utxo.nValue, sighash);
|
||||
MutableTransactionSignatureCreator creator(&tx, index, utxo.nValue, txdata, sighash);
|
||||
sig_complete = ProduceSignature(provider, creator, utxo.scriptPubKey, sigdata);
|
||||
}
|
||||
// Verify that a witness signature was produced in case one was required.
|
||||
@@ -302,8 +319,9 @@ bool FinalizePSBT(PartiallySignedTransaction& psbtx)
|
||||
// PartiallySignedTransaction did not understand them), this will combine them into a final
|
||||
// script.
|
||||
bool complete = true;
|
||||
const PrecomputedTransactionData txdata = PrecomputePSBTData(psbtx);
|
||||
for (unsigned int i = 0; i < psbtx.tx->vin.size(); ++i) {
|
||||
complete &= SignPSBTInput(DUMMY_SIGNING_PROVIDER, psbtx, i, SIGHASH_ALL);
|
||||
complete &= SignPSBTInput(DUMMY_SIGNING_PROVIDER, psbtx, i, &txdata, SIGHASH_ALL);
|
||||
}
|
||||
|
||||
return complete;
|
||||
|
||||
Reference in New Issue
Block a user