@@ -57,7 +57,6 @@ for purposes of confirmation using CHECKTEMPLATEVERIFY. Then, some time later, t
57
57
be expanded out of that UTXO when the demand for blockspace is decreased. These payments can be
58
58
structured in a tree-like fashion to reduce individual costs of redemption.
59
59
60
-
61
60
The below chart showcases the structure of these transactions in comparison to
62
61
normal transactions and batched transactions.
63
62
@@ -71,6 +70,7 @@ is provided in this BIP's subdirectory.
71
70
<img src="bip-0119/fifty.png" align="middle"></img>
72
71
73
72
===Payment Channels ===
73
+
74
74
There are numerous payment channel related uses.
75
75
76
76
====Channel Factories ====
@@ -84,6 +84,7 @@ penultimate transaction node.
84
84
Thus, coins sent using a congestion controlled transaction can still enjoy instant liquidity.
85
85
86
86
====Non-Interactive Channels ====
87
+
87
88
When opening a traditional payment channel, both parties to the channel must participate. This is
88
89
because the channel uses pre-signed multi-sig transactions to ensure that a channel can always be
89
90
exited by either party, before entering.
@@ -94,6 +95,7 @@ for their private key to be online.
94
95
<img src="bip-0119/nic.svg" align="middle"></img>
95
96
96
97
====Increased Channel Routes ====
98
+
97
99
In the Lightning Network protocol, Hashed Time Locked Contracts (HTLCS) are used in the construction
98
100
of channels. A new HTLC is required per route that the channel is serving in.
99
101
In BOLT #2, this maximum number of HTLCs in a channel is hard limited to 483 as the maximum safe
@@ -107,7 +109,6 @@ HTLCS.
107
109
Because each HTLC can have its own relative time lock in the tree, this also improves the latency
108
110
sensitivity of the lightning protocol on contested channel close.
109
111
110
-
111
112
===Wallet Vaults ===
112
113
113
114
When greater security is required for cold storage solutions, there can be
@@ -133,15 +134,22 @@ before. Further Each participant doesn't need to know the totality of the outpu
133
134
that output, they only have to verify their own sub-tree will pay them.
134
135
135
136
==Detailed Specification ==
137
+
136
138
The below code is the main logic for verifying CHECKTEMPLATEVERIFY, and is the canonical
137
139
specification for the semantics of OP_CHECKTEMPLATEVERIFY.
138
140
139
141
case OP_CHECKTEMPLATEVERIFY:
140
142
{
141
143
// if flags not enabled; treat as a NOP4
142
- if (!(flags & SCRIPT_VERIFY_DEFAULT_CHECK_TEMPLATE_VERIFY_HASH)) break;
144
+ if (!(flags & SCRIPT_VERIFY_DEFAULT_CHECK_TEMPLATE_VERIFY_HASH)) {
145
+ if (flags & SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_NOPS)
146
+ return set_error(serror, SCRIPT_ERR_DISCOURAGE_UPGRADABLE_NOPS);
147
+ break;
148
+ }
149
+
143
150
if (stack.size() < 1)
144
151
return set_error(serror, SCRIPT_ERR_INVALID_STACK_OPERATION);
152
+
145
153
// If the argument was not 32 bytes, treat as OP_NOP4:
146
154
switch (stack.back().size()) {
147
155
case 32:
@@ -197,7 +205,6 @@ The hash is computed as follows:
197
205
return h.GetSHA256();
198
206
}
199
207
200
-
201
208
A PayToBareDefaultCheckTemplateVerifyHash output matches the following template:
202
209
203
210
bool CScript::IsPayToBareDefaultCheckTemplateVerifyHash() const
@@ -210,16 +217,23 @@ A PayToBareDefaultCheckTemplateVerifyHash output matches the following template:
210
217
211
218
==Deployment ==
212
219
213
- Deployment should be done via BIP 9 VersionBits.
220
+ Deployment should be done via BIP 9 VersionBits deployed through Speedy Trial .
214
221
215
222
The start time and bit in the implementation are currently set to bit 5 and
216
- March 1st, 2020 , but this is subject to change while the BIP is a draft.
223
+ NEVER_ACTIVE/NO_TIMEOUT , but this is subject to change while the BIP is a draft.
217
224
218
- For the avoidance of unclarity, the parameters are:
225
+ For the avoidance of unclarity, the parameters to be determined are:
219
226
227
+ // Deployment of CTV (BIP 119)
220
228
consensus.vDeployments[Consensus::DEPLOYMENT_CHECKTEMPLATEVERIFY].bit = 5;
221
- consensus.vDeployments[Consensus::DEPLOYMENT_CHECKTEMPLATEVERIFY].nStartTime = 1583020800; // March 1, 2020
222
- consensus.vDeployments[Consensus::DEPLOYMENT_CHECKTEMPLATEVERIFY].nTimeout = 1614556800; // March 1, 2021
229
+ consensus.vDeployments[Consensus::DEPLOYMENT_CHECKTEMPLATEVERIFY].nStartTime = Consensus::BIP9Deployment::NEVER_ACTIVE;
230
+ consensus.vDeployments[Consensus::DEPLOYMENT_CHECKTEMPLATEVERIFY].nTimeout = Consensus::BIP9Deployment::NO_TIMEOUT;
231
+ consensus.vDeployments[Consensus::DEPLOYMENT_CHECKTEMPLATEVERIFY].min_activation_height = 0;
232
+
233
+ Until BIP-119 reaches ACTIVE state and the
234
+ SCRIPT_VERIFY_DEFAULT_CHECK_TEMPLATE_VERIFY_HASH flag is set, the network should
235
+ execute a NOP4 as SCRIPT_ERR_DISCOURAGE_UPGRADABLE_NOPS for policy and a NOP for
236
+ consensus.
223
237
224
238
In order to facilitate using CHECKTEMPLATEVERIFY, the common case of a
225
239
PayToBareDefaultCheckTemplateVerifyHash
@@ -231,26 +245,20 @@ standardized later as policy changes.
231
245
A reference implementation and tests are available here:
232
246
https://github.com/JeremyRubin/bitcoin/tree/checktemplateverify.
233
247
234
-
235
248
==Rationale ==
236
249
237
250
The goal of CHECKTEMPLATEVERIFY is to be minimal impact on the existing codebase -- in the
238
251
future, as we become aware of more complex but shown to be safe use cases new template types can be added.
239
252
240
-
241
253
Below we'll discuss the rules one-by-one:
242
254
243
-
244
-
245
255
====The DefaultCheckTemplateVerifyHash of the transaction at the current input index matches the top of the stack ====
246
256
247
257
The set of data committed to is a superset of data which can impact the TXID of the transaction,
248
258
other than the inputs. This ensures that for a given known input, the TXIDs can also be known ahead
249
259
of time. Otherwise, CHECKTEMPLATEVERIFY would not be usable for Channel Factory type constructions
250
260
as the redemption TXID could be malleated and pre-signed transactions invalidated.
251
261
252
-
253
-
254
262
=====Committing to the version and locktime =====
255
263
256
264
Were these values not committed, it would be possible to delay the spending of
@@ -282,7 +290,6 @@ precomputed for each transaction to optimize SIGHASH_ALL signatures.
282
290
Committing to the hash additionally makes it simpler to construct DefaultCheckTemplateVerifyHash safely and unambiguously from
283
291
script.
284
292
285
-
286
293
=====Committing to the number of inputs =====
287
294
288
295
If we allow more than one input to be spent in the transaction then it would be
@@ -380,12 +387,41 @@ Furthermore, if OP_SHA256STREAM is added in the future, it may be possible to wr
380
387
allows adding a single output to a list of outputs without incurring O(n) overhead by committing to
381
388
a hash midstate in the script.
382
389
390
+ =====Using SHA256 =====
391
+
392
+ SHA256 is a 32 byte hash which meets Bitcoin's security standards and is
393
+ available already inside of Bitcoin Script for programmatic creation of template
394
+ programs.
395
+
396
+ RIPEMD160, a 20 byte hash, might also be a viable hash in some contexts and has some benefits. For fee efficiency,
397
+ RIPEMD160 saves 12 bytes. However, RIPEMD160 was not chosen for BIP-119 because it introduces
398
+ risks around the verification of programs created by third parties to be subject to a
399
+ [birthday-attack https://bitcoin.stackexchange.com/questions/54841/birthday-attack-on-p2sh ] on
400
+ transaction preimages.
401
+
402
+ =====Using Non-Tagged Hashes =====
403
+
404
+ The Taproot/Schnorr BIPs use Tagged Hashes
405
+ (`SHA256(SHA256(tag)||SHA256(tag)||msg)`) to prevent taproot leafs, branches,
406
+ tweaks, and signatures from overlapping in a way that might introduce a security
407
+ [vulnerability https://lists.linuxfoundation.org/pipermail/bitcoin-dev/2018-June/016091.html ].
408
+
409
+ OP_CHECKTEMPLATEVERIFY is not subject to this sort of vulnerability as the
410
+ hashes are effectively tagged externally, that is, by OP_CHECKTEMPLATEVERIFY
411
+ itself and therefore cannot be confused for another hash.
412
+
413
+ It would be a conservative design decisison to make it a tagged hash even if
414
+ there was no obvious benefit and no cost. However, in the future, if OP_CAT were
415
+ to be introduced to Bitcoin, it would make programs which dynamically build
416
+ OP_CHECKTEMPLATEVERIFY hashes less space-efficient. Therefore, bare untagged hashes
417
+ are used in BIP-119.
383
418
384
419
=====The Ordering of Fields =====
385
420
386
- Strictly speaking, the ordering of fields is insignificant. However, with a carefully selected
387
- order, the efficiency of future scripts (e.g., those using a OP_CAT or OP_SHA256STREAM) may be
388
- improved.
421
+ Strictly speaking, the ordering of fields is insignificant. However, with a
422
+ carefully selected order, the efficiency of future scripts (e.g., those using a
423
+ OP_CAT or OP_SHA256STREAM) may be improved (as described in the Future Upgrades
424
+ section).
389
425
390
426
In particular, the order is selected in order of least likely to change to most.
391
427
@@ -416,13 +452,6 @@ does not make sense for input index to be the last field. However, given the des
416
452
able to express a "don't care" index easily (e.g., for decentralized kickstarter-type transactions),
417
453
this value is placed last.
418
454
419
- As an example, the following code checks an input index argument and concatenates it to the template and
420
- checks the template matches the transaction.
421
-
422
- OP_SIZE 4 OP_EQUALVERIF
423
- <nVersion || nLockTime || input count || sequences hash || output count || outputs hash>
424
- OP_SWAP OP_CAT OP_SHA256 OP_CHECKTEMPLATEVERIFY
425
-
426
455
===Design Tradeoffs and Risks ===
427
456
Covenants have historically been controversial given their potential for fungibility risks -- coins
428
457
could be minted which have a permanent restriction on how they may or may not be spent or required
@@ -437,17 +466,18 @@ transactions which create all the inputs directly in this regard.
437
466
Furthermore, templates are restricted to be spendable as a known number of inputs only, preventing
438
467
unintentional introduction of the 'half spend' problem.
439
468
440
-
441
469
Templates, as restricted as they are, bear some risks.
442
470
443
471
====Permanently Unspendable Outputs ====
472
+
444
473
The preimage argument passed to CHECKTEMPLATEVERIFY may be unknown or otherwise unsatisfiable.
445
474
However, requiring knowledge that an address is spendable from is incompatible with sender's ability
446
475
to spend to any address (especially, OP_RETURN). If a sender needs to know the template can be spent
447
476
from before sending, they may request a signature of an provably non-transaction challenge string
448
477
from the leafs of the CHECKTEMPLATEVERIFY tree.
449
478
450
479
====Forwarding Addresses ====
480
+
451
481
Key-reuse with CHECKTEMPLATEVERIFY may be used as a form of "forwarding address contract".
452
482
A forwarding address is an address which can automatically execute in a predefined way.
453
483
For example, a exchange's hot wallet might use an address which can automatically be moved to a cold
@@ -473,7 +503,6 @@ reuse-unsafe.
473
503
Because CHECKTEMPLATEVERIFY commits to the input index currently being spent, reused-keys are
474
504
guaranteed to execute in separate transactions which reduces the risk of "half-spend" type issues.
475
505
476
-
477
506
====NOP-Default and Standardness Rules ====
478
507
479
508
If the argument length is not exactly 32, CHECKTEMPLATEVERIFY treats it as a NOP.
@@ -486,8 +515,8 @@ stricter standardness rules to be enforced during consensus. Should that develop
486
515
transaction directly to the network relying on standardness rejection, an standardness-invalid but
487
516
consensus-valid transaction may be caused, leading to a potential loss of funds.
488
517
489
-
490
518
====Feature Redundancy ====
519
+
491
520
CHECKTEMPLATEVERIFY templates are substantially less risky than other covenant systems. If
492
521
implemented, other covenant systems could make the CHECKTEMPLATEVERIFY's functionality redundant.
493
522
However, given CHECKTEMPLATEVERIFY's simple semantics and low on chain cost it's likely that it
@@ -501,26 +530,91 @@ unintended behavior.
501
530
Alternatively, SIGHASH_ANYPREVOUTANYSCRIPT based covenant designs can implement
502
531
something similar to templates, via a scriptPubKey like:
503
532
504
-
505
533
<sig of desired TX with PK and fixed nonce R || SIGHASH_ANYPREVOUTANYSCRIPT <PK with public SK> OP_CHECKSIG
506
534
507
- SIGHASH_ANYPREVOUTANYSCRIPT bears additional technical and implementation risks that may preclude
508
- its viability for inclusion in Bitcoin, but the capabilities above are similar to what
509
- CHECKTEMPLATEVERIFY offers. However, CHECKTEMPLATEVERIFY has benefits in terms of verification
510
- speed, as it requires only hash computation rather than signature operations. This can be
511
- significant when constructing large payment trees or programmatic compilations. CHECKTEMPLATEVERIFY
512
- also has a feature-wise benefit in that it provides a robust pathway for future template upgrades.
513
-
514
- CHECKSIGFROMSTACK along with OP_CAT may also be used to emulate CHECKTEMPLATEVERIFY. However such
515
- constructions are more complicated to use than CHECKTEMPLATEVERIFY, and encumbers additional
516
- verification overhead absent from CHECKTEMPLATEVERIFY. These types of covenants also bear similar
517
- potential recursion issues to OP_COV which make it unlikely for inclusion in Bitcoin.
518
-
535
+ SIGHASH_ANYPREVOUTANYSCRIPT bears additional technical and implementation risks
536
+ that may preclude its viability for inclusion in Bitcoin, but the capabilities
537
+ above are similar to what CHECKTEMPLATEVERIFY offers. The key functional
538
+ difference between SIGHASH_ANYPREVOUTANYSCRIPT and OP_CHECKTEMPLATEVERIFY is
539
+ that OP_CHECKTEMPLATEVERIFY restricts the number of additional inputs and
540
+ precludes dynamically determined change outputs while
541
+ SIGHASH_ANYPREVOUTANYSCRIPT can be combined with SIGHASH_SINGLE or
542
+ SIGHASH_ANYONECANPAY. For the additional inputs, OP_CHECKTEMPLATEVERIFY also
543
+ commits to the scriptsig and sequence, which allows for specifying specific P2SH
544
+ scripts (or segwit v0 P2SH) which have some use cases. Furthermore,
545
+ CHECKTEMPLATEVERIFY has benefits in terms of script size (depending on choice of
546
+ PK, SIGHASH_ANYPREVOUTANYSCRIPT may use about 2x-3x the bytes) and verification
547
+ speed, as OP_CHECKTEMPLATEVERIFY requires only hash computation rather than
548
+ signature operations. This can be significant when constructing large payment
549
+ trees or programmatic compilations. CHECKTEMPLATEVERIFY also has a feature-wise
550
+ benefit in that it provides a robust pathway for future template upgrades.
551
+
552
+ OP_CHECKSIGFROMSTACKVERIFY along with OP_CAT may also be used to emulate
553
+ CHECKTEMPLATEVERIFY. However such constructions are more complicated to use
554
+ than CHECKTEMPLATEVERIFY, and encumbers additional verification overhead absent
555
+ from CHECKTEMPLATEVERIFY. These types of covenants also bear similar potential
556
+ recursion issues to OP_COV which make it unlikely for inclusion in Bitcoin.
519
557
520
558
Given the simplicity of this approach to implement and analyze, and the benefits realizable by user
521
559
applications, CHECKTEMPLATEVERIFY's template based approach is proposed in lieu of more complete
522
560
covenants system.
523
561
562
+ ====Future Upgrades ====
563
+
564
+ This section describes updates to OP_CHECKTEMPLATEVERIFY that are possible in
565
+ the future as well as synergies with other possible upgrades.
566
+
567
+ =====CHECKTEMPLATEVERIFY Versions =====
568
+
569
+ OP_CHECKTEMPLATEVERIFY currently only verifies properties of 32 byte arguments.
570
+ In the future, meaning could be ascribed to other length arguments. For
571
+ example, a 33-byte argument could just the last byte as a control program. In
572
+ that case, DefaultCheckTemplateVerifyHash could be computed when the flag byte
573
+ is set to CTVHASH_ALL. Other programs could be added similar to SIGHASH_TYPEs.
574
+ For example, CTVHASH_GROUP could read data from the Taproot Annex for
575
+ compatibility with SIGHASH_GROUP type proposals and allow dynamic malleability
576
+ of which indexes get hashed for bundling.
577
+
578
+ =====Eltoo with OP_CHECKSIGFROMSTACKVERIFY =====
579
+
580
+ Were both OP_CHECKTEMPLATEVERIFY and OP_CHECKSIGFROMSTACKVERIFY to be added to
581
+ Bitcoin, it would be possible to implement a variant of Eltoo's floating
582
+ transactions using the following script:
583
+
584
+ witness(S+n): <sig> <H(tx with nLockTime S+n paying to program(S+n))>
585
+ program(S): OP_CHECKTEMPLATEVERIFY <musig_key(pk_update_a, pk_update_b)> OP_CHECKSIGFROMSTACKVERIFY <S+1> OP_CHECKLOCKTIMEVERIFY
586
+
587
+ Compared to SIGHASH_ANYPREVOUTANYSCRIPT, because OP_CHECKTEMPLATEVERIFY does not
588
+ allow something similar to SIGHASH_ANYONECANPAY or SIGHASH_SINGLE, protocol
589
+ implementers might elect to sign multiple versions of transactions with CPFP
590
+ Anchor Outputs or Inputs for paying fees or an alternative such as transaction
591
+ sponsors might be considered.
592
+
593
+ =====OP_AMOUNTVERIFY =====
594
+
595
+ An opcode which verifies the exact amount that is being spent in the
596
+ transaction, the amount paid as fees, or made available in a given output could
597
+ be used to make safer OP_CHECKTEMPLATEVERIFY addressses. For instance, if the
598
+ OP_CHECKTEMPLATEVERIFY program P expects exactly S satoshis, sending S-1
599
+ satoshis would result in a frozen UTXO and sending S+n satoshis would result in
600
+ n satoshis being paid to fee. A range check could restrict the program to only
601
+ apply for expected values and default to a keypath otherwise, e.g.:
602
+
603
+ IF OP_AMOUNTVERIFY <N> OP_GREATER <PK> CHECKSIG ELSE <H> OP_CHECKTEMPLATEVERIFY
604
+
605
+ =====OP_CAT/OP_SHA256STREAM =====
606
+
607
+ OP_CHECKTEMPLATEVERIFY is (as described in the Ordering of Fields section)
608
+ efficient for building covenants dynamically should Bitcoin get enhanced string
609
+ manipulation opcodes.
610
+
611
+ As an example, the following code checks an input index argument and
612
+ concatenates it to the template and checks the template matches the transaction.
613
+
614
+ OP_SIZE 4 OP_EQUALVERIF
615
+ <nVersion || nLockTime || input count || sequences hash || output count || outputs hash>
616
+ OP_SWAP OP_CAT OP_SHA256 OP_CHECKTEMPLATEVERIFY
617
+
524
618
== Backwards Compatibility ==
525
619
526
620
OP_CHECKTEMPLATEVERIFY replaces a OP_NOP4 with stricter verification semantics. Therefore, scripts
@@ -529,15 +623,18 @@ for an OP_NOP are a soft fork, so existing software will be fully functional wit
529
623
for mining and block validation. Similar soft forks for OP_CHECKSEQUENCEVERIFY and OP_CHECKLOCKTIMEVERIFY
530
624
(see BIP-0065 and BIP-0112) have similarly changed OP_NOP semantics without introducing compatibility issues.
531
625
626
+ In contrast to previous forks, OP_CHECKTEMPLATEVERIFY will not make scripts
627
+ valid for policy until the new rule is active.
628
+
532
629
Older wallet software will be able to accept spends from OP_CHECKTEMPLATEVERIFY outputs, but will
533
630
require an upgrade in order to treat PayToBareDefaultCheckTemplateVerifyHash chains with a confirmed ancestor as
534
631
being "trusted" (i.e., eligible for spending before the transaction is confirmed).
535
632
536
633
Backports of OP_CHECKTEMPLATEVERIFY can be trivially prepared (see the reference implementation)
537
634
for older node versions that can be patched but not upgraded to a newer major release.
538
635
539
-
540
636
== References ==
637
+
541
638
*[https://utxos.org utxos.org informational site ]
542
639
*[https://www.youtube.com/watch?v=YxsjdIl0034&t=2451 Scaling Bitcoin Presentation ]
543
640
*[https://bitcoinops.org/en/newsletters/2019/05/29/ Optech Newsletter Covering OP_CHECKOUTPUTSHASHVERIFY ]
@@ -549,6 +646,7 @@ for older node versions that can be patched but not upgraded to a newer major re
549
646
550
647
551
648
===Note on Similar Alternatives ===
649
+
552
650
An earlier version of CHECKTEMPLATEVERIFY, CHECKOUTPUTSHASHVERIFY, is withdrawn
553
651
in favor of CHECKTEMPLATEVERIFY. CHECKOUTPUTSHASHVERIFY did not commit to the
554
652
version or lock time and was thus insecure.
@@ -561,4 +659,5 @@ CHECKTEMPLATEVERIFY has also been previously referred to as OP_SECURETHEBAG, whi
561
659
to aid in searching and referencing discussion on this BIP.
562
660
563
661
==Copyright ==
662
+
564
663
This document is licensed under the 3-clause BSD license.
0 commit comments