{"number":446,"title":"OP_TEMPLATEHASH","authors":["Gregory Sanders","Antoine Poinsot","Steven Roose"],"status":"Draft","type":"Specification","layer":"Consensus (soft fork)","created":"2026-02-06","license":"CC0-1.0","requires":[],"replaces":[],"replacedBy":[],"discussion":[],"abstract":"This document proposes a new operation for Tapscript: OP_TEMPLATEHASH . It introduces the ability to push on the stack a hash of the transaction spending an output.","eli5":"Proposes OP_TEMPLATEHASH, a Taproot script instruction that produces a fingerprint of the transaction spending a coin. Scripts can use it to require that coins be spent only in a specific pre-agreed way, a building block for vaults and payment channel improvements.","categories":["taproot","consensus","transactions"],"filename":"bip-0446.md","githubUrl":"https://github.com/bitcoin/bips/blob/master/bip-0446.md","format":"markdown","readingMinutes":7,"inbound":1,"revisions":1,"impact":27,"core":false,"references":[119,340,341,342],"lastModified":"2026-03-17","html":"<h2 id=\"abstract\">Abstract</h2>\n<p>This document proposes a new operation for <a href=\"/bip/342\">Tapscript</a>: <code>OP_TEMPLATEHASH</code>. It introduces the ability to\npush on the stack a hash of the transaction spending an output.</p>\n<h2 id=\"motivation\">Motivation</h2>\n<p><code>OP_TEMPLATEHASH</code> can be used to commit to the transaction spending an output[^commit-exact-tx]. This capability\ncan replace the use of pre-signed transactions in second-layer protocols. By reducing interactivity it makes such\nprotocols simpler, safer, and sometimes notably more efficient. For instance it can remove the need to share HTLC\nsignatures in the Lightning Network protocol&#39;s <a href=\"https://github.com/lightning/bolts/blob/master/02-peer-protocol.md#committing-updates-so-far-commitment_signed\"><code>commitment_signed</code> message</a>[^ln-second-stage], make\nreceiving an Ark &quot;VTXO&quot; <a href=\"https://delvingbitcoin.org/t/the-ark-case-for-ctv/1528\">non-interactive</a>, and <a href=\"https://delvingbitcoin.org/t/ln-symmetry-project-recap/359\">reduces roundtrips</a> in the implementation\nof LN-Symmetry. It is also a <a href=\"https://gnusha.org/pi/bitcoindev/CAH5Bsr2vxL3FWXnJTszMQj83jTVdRvvuVpimEfY7JpFCyP1AZA@mail.gmail.com\">significant optimisation</a> for <a href=\"https://bitcoinops.org/en/topics/discreet-log-contracts\">Discreet Log Contracts</a>.</p>\n<h2 id=\"specification\">Specification</h2>\n<p><code>OP_TEMPLATEHASH</code> redefines <code>OP_SUCCESS206</code> (0xce) in the Tapscript execution context with further restrictions.</p>\n<p>Upon execution of the opcode, the template hash of the transaction in context is pushed onto the stack as defined below,\nand script execution continues.</p>\n<p>The template hash uses a tagged hash as introduced by <a href=\"/bip/340\">BIP340</a> and <a href=\"/bip/341\">BIP341</a>. We use a new tag\nfor this purpose: <em>TemplateHash</em>.</p>\n<p>The template hash re-uses the <em>sha_sequences</em>, <em>sha_outputs</em> and <em>sha_annex</em> pre-computed transaction data introduced in\nBIP341. Numerical values in 4-byte are encoded in little-endian.</p>\n<p>The template hash is the <em>hash<sub>TemplateHash</sub></em> of the following transaction fields concatenated:</p>\n<ul>\n<li>Transaction data:<ul>\n<li><em>nVersion</em> (4): the version of the transaction.</li>\n<li><em>nLockTime</em> (4): the locktime of the transaction.</li>\n<li><em>sha_sequences</em> (32): the SHA256 of the serialization of all input sequence, as per BIP341.</li>\n<li><em>sha_outputs</em> (32): the SHA256 of the serialization of all outputs in <code>CTxOut</code> format, as per BIP341.</li>\n</ul>\n</li>\n<li>Data about this input:<ul>\n<li><em>annex_present</em> (1): as defined in BIP341 (0 if no annex is present, or 1 otherwise).</li>\n<li><em>input_index</em> (4): index of this input in the transaction input vector. Index of the first input is 0.</li>\n<li>If an annex is present:<ul>\n<li><em>sha_annex</em> (32): the SHA256 of the annex, as per BIP341.</li>\n</ul>\n</li>\n</ul>\n</li>\n</ul>\n<h2 id=\"rationale\">Rationale</h2>\n<p>The template hash follows BIP341&#39;s signature message format, with minimal necessary deviations. This reuses a\ntried-and-proven approach to hashed messages, and importantly makes it possible to reuse the pre-computed subfields\nintroduced by BIP341 to prevent quadratic hashing. Besides the hash tags, this results in at most 109 bytes being hashed\nupon execution of the operation[^hashed-msg-max-size]. This is strictly less hashing than is necessary for other\nexisting operations.</p>\n<p>The specific fields from the BIP341 signature message that are ommitted when computing the template hash are the\nfollowing:</p>\n<ul>\n<li><em>hash_type</em>: refers to the sighash type identifier in the context of BIP341 signatures. The input for <code>OP_TEMPLATEHASH</code> is fixed, so there\nis no need for a mechanism to modify the hash composition.</li>\n<li><em>spend_type</em>: this value is defined by BIP341 as <em>2*ext_flag + annex_present</em>. Since no extension is appended to the\nsignature message, <em>ext_flag</em> is set to 0. Therefore we commit directly to <em>annex_present</em>.</li>\n<li><em>sha_prevouts</em> / <em>sha_scriptpubkeys</em>: committing to these fields as is would introduce a hash cycle when the hash is\ncommitted in the output itself. Committing to all other prevouts or scriptpubkeys would introduce hashing a quantity\nof data quadratic in the number of inputs. It would also prevent spending two coins encumbered by a template hash\ncheck in the same transaction. Finally, the flexibility of not committing to the specific coins spent is also\ndesirable to recover from mistakes[^no-commit-other-coins].</li>\n<li><em>sha_amounts</em>: the BIP341 rationale for committing to the amounts of all spent coins is to be able to prove to an offline\nsigner the fees of a transaction. Although <code>OP_TEMPLATEHASH</code> can be used as a building block for rebindable\nsignatures, the utility of committing to spent amounts but not spent scriptpubkeys <a href=\"https://gnusha.org/pi/bitcoindev/CAB3F3Dv1kuJdu8veNUHa4b58TvWy=BT6zfxdhqEPBQ8rjDfWtA@mail.gmail.com\">is\nlimited</a>. Still for rebindable signatures, committing to spent amounts can be justified\nas defense-in-depth against implementation mistakes[^commit-spent-amounts]. However, the lack of flexibility this\nintroduces also makes it harder to recover from a mistake in committing to the next\ntransaction[^no-commit-other-coins]. Furthermore, committing to all spent amounts also makes overcommitting funds to\nsuch a script result in the output being forever unspendable instead of the excess just going to fees at spend time.</li>\n</ul>\n<p>The design of <code>OP_TEMPLATEHASH</code> was inspired by the design of <a href=\"/bip/119\">BIP119</a> <code>OP_CHECKTEMPLATEVERIFY</code> but differs in\nseveral important ways.</p>\n<p>First of all, <code>OP_TEMPLATEHASH</code> is only defined for Tapscript, as modifying legacy Script comes with an unnecessarily\nincreased risk surface. In addition, sticking to Tapscript allows leveraging more powerful upgrade hooks (<code>OP_SUCCESS</code>s\ninstead of <code>OP_NOP</code>s) which make it possible to push the template hash on the stack instead of being constrained to\nstrict assertions with no stack modification. Pushing the template hash on the stack substantially improves the\nefficiency of using <code>OP_TEMPLATEHASH</code> as a building block for rebindable signatures.</p>\n<p>Unlike <code>OP_TEMPLATEHASH</code>, <code>OP_CHECKTEMPLATEVERIFY</code> also commits to the scriptSig of all inputs of the spending\ntransaction. <code>OP_CHECKTEMPLATEVERIFY</code> gives txid stability when the committed spending transaction has a single input,\nand when the scriptSig of this single input has been committed by the hash.\nTaproot scriptSigs must be empty and therefore under the single input case <code>OP_TEMPLATEHASH</code> has no requirement\nto commit to scriptSigs to achieve txid stability.</p>\n<p>Finally, BIP119 <code>OP_CHECKTEMPLATEVERIFY</code> does not commit to the Taproot annex (or its absence). <code>OP_TEMPLATEHASH</code> does.\nDeviating from other operations which do commit to the annex would be unnecessary and surprising. Committing to the\nannex also makes usage of <code>OP_TEMPLATEHASH</code> forward compatible with potential future meaning that it could be given. Not\ncommitting to it would also prevent using <code>OP_TEMPLATEHASH</code> in conjunction with annex-based proof of publication\ntechniques unless additional signatures are included, as used for instance <a href=\"https://github.com/instagibbs/bolts/blob/eltoo_draft/XX-eltoo-transactions.md#update-transaction\">in the LN-Symmetry demo</a>.</p>\n<p>Programmable transaction introspection capabilities have been proposed as an alternative to a primitive which only\nallows committing to the exact next transaction. It remains to be shown that these more flexible capabilities do\nenable important use cases which justify each proposed change&#39;s specific semantics and implementation complexity. It\nhas been suggested that the new primitive should have its own upgrade hook from which to softfork in additional\nconsensus meaning for more flexible introspection at some future point. We have not done so due to the fact that Taproot\nand Tapscript already presents plentiful upgrade hooks for the future.</p>\n<h2 id=\"backward-compatibility\">Backward compatibility</h2>\n<p>This document proposes to give meaning to a Tapscript <code>OP_SUCCESS</code> operation. The presence of an <code>OP_SUCCESS</code> in a\nTapscript would previously make it unconditionally succeed. This proposal therefore only tightens the block validation\nrules: there is no block that is valid under the rules proposed in this BIP but not under the existing Bitcoin consensus\nrules. As a consequence these changes are backward-compatible with non-upgraded node software. That said, the authors\nstrongly encourage node operators to upgrade in order to fully validate all consensus rules.</p>\n<h2 id=\"implementation\">Implementation</h2>\n<ul>\n<li><a href=\"https://github.com/instagibbs/bitcoin/tree/2025-07-op_templatehash\">https://github.com/instagibbs/bitcoin/tree/2025-07-op_templatehash</a></li>\n</ul>\n<h2 id=\"test-vectors\">Test Vectors</h2>\n<p>For development and testing purposes, we provide a <a href=\"https://github.com/bitcoin/bips/blob/master/bip-0446/\">collection of test vectors</a>. The test\nvectors are separated into two JSON files. The <a href=\"https://github.com/bitcoin/bips/blob/master/bip-0446/basics.json\">first one</a> is a short list of\nsimple test cases exercising the various fields of a transaction committed to when using <code>OP_TEMPLATEHASH</code>. The <a href=\"https://github.com/bitcoin/bips/blob/master/bip-0446/script_assets_test.json\">second\none</a> is a more exhaustive suite of tests exercising <code>OP_TEMPLATEHASH</code>\nunder a large number of different conditions. It reuses the <a href=\"https://github.com/bitcoin/bitcoin/blob/v29.0/test/functional/feature_taproot.py\">Bitcoin Core Taproot test framework</a>\nintroduced with the implementation of BIP341. Format details and usage demonstration are available\n<a href=\"https://github.com/bitcoin/bips/blob/master/bip-0446/README.md\">here</a>.</p>\n<h2 id=\"acknowledgements\">Acknowledgements</h2>\n<p>Credit to Jeremy Rubin for his leadership and perseverance in defending how a simple primitive which\nallows committing to the entire spending transaction is useful for reducing\ninteractivity in second layer protocols. This BIP draws on the design of BIP119 and is\nheavily inspired by his exploration of the potential uses for such a primitive.</p>\n<h2 id=\"copyright\">Copyright</h2>\n<p>This document is licensed under the Creative Commons CC0 1.0 Universal license.</p>\n<p>[^ln-second-stage]: Second-stage HTLC transactions are currently enforced through a 2-of-2 multisig between the channel\npartners. Committing to the HTLC transaction directly in the commitment transaction&#39;s HTLC output is a drop-in\nreplacement for the multisig, which has the advantage of not requiring Lightning nodes to transmit and store signatures\nfor every HTLC at every state it is still active for.\n[^commit-exact-tx]: All the transaction&#39;s fields are committed to except the inputs&#39; prevout. This means the output must\nbe spent by this exact transaction, although the other spent outputs may vary.\n[^no-commit-other-coins]: It is possible to commit to an underfunded transaction to spend a coin. If the transaction\ncommits to more than one input, it is possible to recover from the mistake by creating a separate coin of an appropriate\nvalue and spending it along with the encumbered coin. Committing to other inputs this transaction must spend or their\ninput removes the ability to recover from such a mistake.\n[^commit-spent-amounts]: Adding commitments to the spent amounts may offer extra protection when reusing a public key\npreviously associate with a rebindable signature. See <a href=\"https://github.com/bitcoin/bips/blob/master/bip-0118.mediawiki#cite_note-3\">BIP118&#39;s rationale</a> for more about this.\n[^hashed-msg-max-size]: If no annex is committed, 77 bytes are hashed: 72 bytes of transaction data + 5 bytes of data\nabout this input. Committing to an annex adds 32 additional bytes of data about this input, bringing the total to 109.</p>\n","toc":[{"id":"abstract","title":"Abstract","level":2},{"id":"motivation","title":"Motivation","level":2},{"id":"specification","title":"Specification","level":2},{"id":"rationale","title":"Rationale","level":2},{"id":"backward-compatibility","title":"Backward compatibility","level":2},{"id":"implementation","title":"Implementation","level":2},{"id":"test-vectors","title":"Test Vectors","level":2},{"id":"acknowledgements","title":"Acknowledgements","level":2},{"id":"copyright","title":"Copyright","level":2}],"history":{"totalCommits":1,"firstCommit":"2026-03-17T16:01:23-04:00","lastUpdated":"2026-03-17T16:01:23-04:00","events":[{"sha":"2778442c21cef2290ae3e0c843d9f3179aa1cdd0","date":"2026-03-17T16:01:23-04:00","author":"Gregory Sanders","message":"Add BIP446: OP_TEMPLATEHASH, BIP448: Taproot-native (Re)bindable Transactions (#1974)","url":"https://github.com/bitcoin/bips/commit/2778442c21cef2290ae3e0c843d9f3179aa1cdd0","kind":"edit"}]}}