| 1411 | |
| 1412 | template <class T> |
| 1413 | void PrecomputedTransactionData::Init(const T& txTo, std::vector<CTxOut>&& spent_outputs, bool force) |
| 1414 | { |
| 1415 | assert(!m_spent_outputs_ready); |
| 1416 | |
| 1417 | m_spent_outputs = std::move(spent_outputs); |
| 1418 | if (!m_spent_outputs.empty()) { |
| 1419 | assert(m_spent_outputs.size() == txTo.vin.size()); |
| 1420 | m_spent_outputs_ready = true; |
| 1421 | } |
| 1422 | |
| 1423 | // Determine which precomputation-impacting features this transaction uses. |
| 1424 | bool uses_bip143_segwit = force; |
| 1425 | bool uses_bip341_taproot = force; |
| 1426 | for (size_t inpos = 0; inpos < txTo.vin.size() && !(uses_bip143_segwit && uses_bip341_taproot); ++inpos) { |
| 1427 | if (!txTo.vin[inpos].scriptWitness.IsNull()) { |
| 1428 | if (m_spent_outputs_ready && m_spent_outputs[inpos].scriptPubKey.size() == 2 + WITNESS_V1_TAPROOT_SIZE && |
| 1429 | m_spent_outputs[inpos].scriptPubKey[0] == OP_1) { |
| 1430 | // Treat every witness-bearing spend with 34-byte scriptPubKey that starts with OP_1 as a Taproot |
| 1431 | // spend. This only works if spent_outputs was provided as well, but if it wasn't, actual validation |
| 1432 | // will fail anyway. Note that this branch may trigger for scriptPubKeys that aren't actually segwit |
| 1433 | // but in that case validation will fail as SCRIPT_ERR_WITNESS_UNEXPECTED anyway. |
| 1434 | uses_bip341_taproot = true; |
| 1435 | } else { |
| 1436 | // Treat every spend that's not known to native witness v1 as a Witness v0 spend. This branch may |
| 1437 | // also be taken for unknown witness versions, but it is harmless, and being precise would require |
| 1438 | // P2SH evaluation to find the redeemScript. |
| 1439 | uses_bip143_segwit = true; |
| 1440 | } |
| 1441 | } |
| 1442 | if (uses_bip341_taproot && uses_bip143_segwit) break; // No need to scan further if we already need all. |
| 1443 | } |
| 1444 | |
| 1445 | if (uses_bip143_segwit || uses_bip341_taproot) { |
| 1446 | // Computations shared between both sighash schemes. |
| 1447 | m_prevouts_single_hash = GetPrevoutsSHA256(txTo); |
| 1448 | m_sequences_single_hash = GetSequencesSHA256(txTo); |
| 1449 | m_outputs_single_hash = GetOutputsSHA256(txTo); |
| 1450 | } |
| 1451 | if (uses_bip143_segwit) { |
| 1452 | hashPrevouts = SHA256Uint256(m_prevouts_single_hash); |
| 1453 | hashSequence = SHA256Uint256(m_sequences_single_hash); |
| 1454 | hashOutputs = SHA256Uint256(m_outputs_single_hash); |
| 1455 | m_bip143_segwit_ready = true; |
| 1456 | } |
| 1457 | if (uses_bip341_taproot && m_spent_outputs_ready) { |
| 1458 | m_spent_amounts_single_hash = GetSpentAmountsSHA256(m_spent_outputs); |
| 1459 | m_spent_scripts_single_hash = GetSpentScriptsSHA256(m_spent_outputs); |
| 1460 | m_bip341_taproot_ready = true; |
| 1461 | } |
| 1462 | } |
| 1463 | |
| 1464 | template <class T> |
| 1465 | PrecomputedTransactionData::PrecomputedTransactionData(const T& txTo) |
no test coverage detected