@@ -1213,6 +1213,20 @@ bool EvalScript(std::vector<std::vector<unsigned char> >& stack, const CScript&
12131213 }
12141214 break ;
12151215
1216+ case OP_TEMPLATEHASH:
1217+ {
1218+ // OP_TEMPLATEHASH is only available in Tapscript. Note this is the exact same error
1219+ // returned in the default case, which would be the one hit by OP_SUCCESS187 before
1220+ // the introduction of OP_TEMPLATEHASH.
1221+ if (sigversion == SigVersion::BASE || sigversion == SigVersion::WITNESS_V0) {
1222+ return set_error (serror, SCRIPT_ERR_BAD_OPCODE);
1223+ }
1224+
1225+ const uint256 template_hash{checker.GetTemplateHash (execdata)};
1226+ stack.push_back ({template_hash.begin (), template_hash.end ()});
1227+ }
1228+ break ;
1229+
12161230 default :
12171231 return set_error (serror, SCRIPT_ERR_BAD_OPCODE);
12181232 }
@@ -1461,6 +1475,7 @@ template PrecomputedTransactionData::PrecomputedTransactionData(const CMutableTr
14611475const HashWriter HASHER_TAPSIGHASH{TaggedHash (" TapSighash" )};
14621476const HashWriter HASHER_TAPLEAF{TaggedHash (" TapLeaf" )};
14631477const HashWriter HASHER_TAPBRANCH{TaggedHash (" TapBranch" )};
1478+ const HashWriter HASHER_TEMPLATEHASH{TaggedHash (" TemplateHash" )};
14641479
14651480static bool HandleMissingData (MissingDataBehavior mdb)
14661481{
@@ -1781,6 +1796,27 @@ bool GenericTransactionSignatureChecker<T>::CheckSequence(const CScriptNum& nSeq
17811796 return true ;
17821797}
17831798
1799+ template <class T >
1800+ uint256 GenericTransactionSignatureChecker<T>::GetTemplateHash(ScriptExecutionData& execdata) const
1801+ {
1802+ assert (txdata && txdata->m_bip341_taproot_ready );
1803+ assert (execdata.m_annex_init );
1804+
1805+ HashWriter ss{HASHER_TEMPLATEHASH};
1806+ ss << txTo->version ;
1807+ ss << txTo->nLockTime ;
1808+ ss << txdata->m_sequences_single_hash ;
1809+ ss << txdata->m_outputs_single_hash ;
1810+ const uint8_t annex_present = (execdata.m_annex_present ? 1 : 0 );
1811+ ss << annex_present;
1812+ ss << nIn;
1813+ if (execdata.m_annex_present ) {
1814+ ss << execdata.m_annex_hash ;
1815+ }
1816+
1817+ return ss.GetSHA256 ();
1818+ }
1819+
17841820// explicit instantiation
17851821template class GenericTransactionSignatureChecker <CTransaction>;
17861822template class GenericTransactionSignatureChecker <CMutableTransaction>;
@@ -1800,6 +1836,16 @@ static bool ExecuteWitnessScript(const std::span<const valtype>& stack_span, con
18001836 }
18011837 // New opcodes will be listed here. May use a different sigversion to modify existing opcodes.
18021838 if (IsOpSuccess (opcode)) {
1839+ // Do not return early success on OP_TEMPLATEHASH (OP_SUCCESS187) once it is active. It is
1840+ // non-standard until then.
1841+ if (opcode == OP_TEMPLATEHASH) {
1842+ if (flags & SCRIPT_VERIFY_DISCOURAGE_TEMPLATEHASH) {
1843+ return set_error (serror, SCRIPT_ERR_DISCOURAGE_TEMPLATEHASH);
1844+ }
1845+ if (flags & SCRIPT_VERIFY_TEMPLATEHASH) {
1846+ continue ;
1847+ }
1848+ }
18031849 if (flags & SCRIPT_VERIFY_DISCOURAGE_OP_SUCCESS) {
18041850 return set_error (serror, SCRIPT_ERR_DISCOURAGE_OP_SUCCESS);
18051851 }
0 commit comments