There have been complaints voiced about being too paiful to work with CTV. Especially when it comes to Vaults. OP_TX and OP_TXHASH/VERIFY are examples how developers sought to overcome certain pain points. However they expand the scope considerably and also rely on 64 bit integer arithmetic to handle
amounts. Here we aim to show an alternative approach that covers a lot of
useful ground in the in-between without state-carrying covenants or general
detailed introspection.
Naturally some of this functionality could be coupled into CTV by adding new template types TXHASH style, there are advantages and disadvantages to that.
For now, let’s put that aside and take a look at what a highly specialized hash function can do for amount flexibility!
Yes, this is yet an other “since we don’t have OP_CAT…” thing.
CTV++
Two additional opcodes to consider as an addition to OP_CHECKTEMPLATEVERIFY:
- OP_TEMPLATEHASH
- OP_INPUTAMOUNTS
Together they relax the strict limitations that OP_CHECKTEMPLATEVERIFY
imposes, because it explicitly commits to the output amounts and therefore
implicitly commits to the spendable input amounts in a lot of cases.
OP_TEMPLATEHASH
Builds the CTV template hash from the stack.
<inputIndex>
# FOR i = inputCount-1; i >= 0; i--; LOOP
<sequence[i]>
# END LOOP
<inputCount>
<nLockTime>
# FOR j = outputCount-1; j >= 0; j--; LOOP
<out[j].amount>
<out[j].script>
# END LOOP
<outputCount>
<nVersion>
OP_TEMPLATEHASH
OP_CHECKTEMPLATEVERIFY
OP_INPUTAMOUNTS
Taproot only. Consumes a 32 bit signed integer n as parameter
n = 0return the SUM of all input amounts with same scriptn < 0return the SUM of last abs(n) input amounts including current inputn > 0return the SUM of first n input amounts of the transaction
fails if
n < 0andabs(n) > inputIndex + 1n > inputCount
<n>
OP_INPUTAMOUNTS
Example use:
This contract below allows the combining of UTXOs locked by the same script for something like a withdrawal from a Vault contract to a staging address where the relative time lock can begin. It works with any amount UTXOs unlike basic CTV Vaults. Also allows for paying fees endogenously and registering a change address. The fee paying input would sign with SIGHASH_ALL.
### Witness stack BEGIN ###
<inputIndex>
# FOR i = inputCount-1; i >= 0; i--; LOOP
<sequence[i]>
# END LOOP
<inputCount>
<nLockTime>
<changeAmount> # out[1].amount
<changeScriptPub> # out[1].script
### Witness stack END ###
<0> # sum of all inputs with same script
OP_INPUTAMOUNTS # out[0].amount
<stagingScriptPub> # out[0].script 33 bytes for P2TR
<2> # outputCount
<2> # nVersion
OP_TEMPLATEHASH
OP_CHECKTEMPLATEVERIFY
Credits:
- Jeremy Rubin who have already came up with everything many years ago
- James O’Beirne for his awesome work on OP_VAULT
- Salvatore Ingala for his work on CCV/MATT and to generalize state carrying covenants
- Many others who have explored the covenant design space and CTV in particular