| 2544 | |
| 2545 | template <class T> |
| 2546 | void PrecomputedTransactionData::Init(const T& txTo, std::vector<CTxOut>&& spent_outputs, bool force) |
| 2547 | { |
| 2548 | assert(!m_spent_outputs_ready); |
| 2549 | |
| 2550 | m_spent_outputs = std::move(spent_outputs); |
| 2551 | if (!m_spent_outputs.empty()) { |
| 2552 | assert(m_spent_outputs.size() == txTo.vin.size()); |
| 2553 | m_spent_outputs_ready = true; |
| 2554 | } |
| 2555 | |
| 2556 | // Determine which precomputation-impacting features this transaction uses. |
| 2557 | bool uses_bip143_segwit = force; |
| 2558 | bool uses_bip341_taproot = force; |
| 2559 | for (size_t inpos = 0; inpos < txTo.vin.size() && !(uses_bip143_segwit && uses_bip341_taproot); ++inpos) { |
| 2560 | if (inpos < txTo.witness.vtxinwit.size() && !txTo.witness.vtxinwit[inpos].scriptWitness.IsNull()) { |
| 2561 | if (m_spent_outputs_ready && m_spent_outputs[inpos].scriptPubKey.size() == 2 + WITNESS_V1_TAPROOT_SIZE && |
| 2562 | m_spent_outputs[inpos].scriptPubKey[0] == OP_1) { |
| 2563 | // Treat every witness-bearing spend with 34-byte scriptPubKey that starts with OP_1 as a Taproot |
| 2564 | // spend. This only works if spent_outputs was provided as well, but if it wasn't, actual validation |
| 2565 | // will fail anyway. Note that this branch may trigger for scriptPubKeys that aren't actually segwit |
| 2566 | // but in that case validation will fail as SCRIPT_ERR_WITNESS_UNEXPECTED anyway. |
| 2567 | uses_bip341_taproot = true; |
| 2568 | } else { |
| 2569 | // Treat every spend that's not known to native witness v1 as a Witness v0 spend. This branch may |
| 2570 | // also be taken for unknown witness versions, but it is harmless, and being precise would require |
| 2571 | // P2SH evaluation to find the redeemScript. |
| 2572 | uses_bip143_segwit = true; |
| 2573 | } |
| 2574 | } |
| 2575 | if (uses_bip341_taproot && uses_bip143_segwit) break; // No need to scan further if we already need all. |
| 2576 | } |
| 2577 | |
| 2578 | if (uses_bip143_segwit || uses_bip341_taproot) { |
| 2579 | // Computations shared between both sighash schemes. |
| 2580 | m_prevouts_single_hash = GetPrevoutsSHA256(txTo); |
| 2581 | m_sequences_single_hash = GetSequencesSHA256(txTo); |
| 2582 | m_outputs_single_hash = GetOutputsSHA256(txTo); |
| 2583 | m_issuances_single_hash = GetIssuanceSHA256(txTo); |
| 2584 | } |
| 2585 | if (uses_bip143_segwit) { |
| 2586 | hashPrevouts = SHA256Uint256(m_prevouts_single_hash); |
| 2587 | hashSequence = SHA256Uint256(m_sequences_single_hash); |
| 2588 | hashIssuance = SHA256Uint256(m_issuances_single_hash); |
| 2589 | hashOutputs = SHA256Uint256(m_outputs_single_hash); |
| 2590 | hashRangeproofs = GetRangeproofsHash(txTo); |
| 2591 | m_bip143_segwit_ready = true; |
| 2592 | } |
| 2593 | if (uses_bip341_taproot && m_spent_outputs_ready) { |
| 2594 | // line copied from GetTransactionWeight() in src/consensus/validation.h |
| 2595 | // (we cannot directly use that function for type reasons) |
| 2596 | m_tx_weight = ::GetSerializeSize(txTo, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS) * (WITNESS_SCALE_FACTOR - 1) + ::GetSerializeSize(txTo, PROTOCOL_VERSION); |
| 2597 | m_outpoints_flag_single_hash = GetOutpointFlagsSHA256(txTo); |
| 2598 | m_spent_asset_amounts_single_hash = GetSpentAssetsAmountsSHA256(m_spent_outputs); |
| 2599 | m_issuance_rangeproofs_single_hash = GetIssuanceRangeproofsSHA256(txTo); |
| 2600 | m_output_witnesses_single_hash = GetOutputWitnessesSHA256(txTo); |
| 2601 | m_spent_scripts_single_hash = GetSpentScriptsSHA256(m_spent_outputs); |
| 2602 | m_spent_output_spk_single_hashes = GetSpentScriptPubKeysSHA256(m_spent_outputs); |
| 2603 | m_output_spk_single_hashes = GetOutputScriptPubKeysSHA256(txTo); |
no test coverage detected