Skip to content

Commit 5cb6502

Browse files
committed
Construct and use PrecomputedTransactionData in SignTransaction
1 parent 5d2e224 commit 5cb6502

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

src/script/sign.cpp

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,26 @@ bool SignTransaction(CMutableTransaction& mtx, const SigningProvider* keystore,
488488
// Use CTransaction for the constant parts of the
489489
// transaction to avoid rehashing.
490490
const CTransaction txConst(mtx);
491+
492+
PrecomputedTransactionData txdata;
493+
std::vector<CTxOut> spent_outputs;
494+
spent_outputs.resize(mtx.vin.size());
495+
bool have_all_spent_outputs = true;
496+
for (unsigned int i = 0; i < mtx.vin.size(); i++) {
497+
CTxIn& txin = mtx.vin[i];
498+
auto coin = coins.find(txin.prevout);
499+
if (coin == coins.end() || coin->second.IsSpent()) {
500+
have_all_spent_outputs = false;
501+
} else {
502+
spent_outputs[i] = CTxOut(coin->second.out.nValue, coin->second.out.scriptPubKey);
503+
}
504+
}
505+
if (have_all_spent_outputs) {
506+
txdata.Init(txConst, std::move(spent_outputs), true);
507+
} else {
508+
txdata.Init(txConst, {}, true);
509+
}
510+
491511
// Sign what we can:
492512
for (unsigned int i = 0; i < mtx.vin.size(); i++) {
493513
CTxIn& txin = mtx.vin[i];
@@ -502,7 +522,7 @@ bool SignTransaction(CMutableTransaction& mtx, const SigningProvider* keystore,
502522
SignatureData sigdata = DataFromTransaction(mtx, i, coin->second.out);
503523
// Only sign SIGHASH_SINGLE if there's a corresponding output:
504524
if (!fHashSingle || (i < mtx.vout.size())) {
505-
ProduceSignature(*keystore, MutableTransactionSignatureCreator(&mtx, i, amount, nHashType), prevPubKey, sigdata);
525+
ProduceSignature(*keystore, MutableTransactionSignatureCreator(&mtx, i, amount, &txdata, nHashType), prevPubKey, sigdata);
506526
}
507527

508528
UpdateInput(txin, sigdata);
@@ -514,7 +534,7 @@ bool SignTransaction(CMutableTransaction& mtx, const SigningProvider* keystore,
514534
}
515535

516536
ScriptError serror = SCRIPT_ERR_OK;
517-
if (!VerifyScript(txin.scriptSig, prevPubKey, &txin.scriptWitness, STANDARD_SCRIPT_VERIFY_FLAGS, TransactionSignatureChecker(&txConst, i, amount, MissingDataBehavior::FAIL), &serror)) {
537+
if (!VerifyScript(txin.scriptSig, prevPubKey, &txin.scriptWitness, STANDARD_SCRIPT_VERIFY_FLAGS, TransactionSignatureChecker(&txConst, i, amount, txdata, MissingDataBehavior::FAIL), &serror)) {
518538
if (serror == SCRIPT_ERR_INVALID_STACK_OPERATION) {
519539
// Unable to sign input and verification failed (possible attempt to partially sign).
520540
input_errors[i] = "Unable to sign input, invalid stack size (possibly missing key)";

0 commit comments

Comments
 (0)