Things are cooking!
Token controlled contracts - pay to token
Breakthrough script allows contract to be unlocked by a token. Contract & token are in separate UTXOs.
Token input is a standard singleton p2pkh token script:
```
OP_PUSHINPUTREFSINGLETON <tokenRef> OP_DROP <p2pkh>
```
The token output has a state script that binds it to the contract input using `OP_REQUIREINPUTREF`, followed by the contract's script sig hash:
```
OP_REQUIREINPUTREF <contractRef>
<contractScriptSigHash> OP_2DROP // Hash256 of contract's script sig
OP_STATESEPARATOR
OP_PUSHINPUTREFSINGLETON <tokenRef> OP_DROP <p2pkh> // Standard token script
```
Signing the contract's script sig means it can support `SIGHASH_SINGLE|ANYONECANPAY`, while effectively signing as many other inputs with the same signature as needed
The contract will be unlocked when the required ref hash exists in the token output.
A token controlled contract works as follows
Script sig:
```
<hashIndex> // Position of ref hash in token output
<refIndex> // Index of ref in token output data summary
<outputIndex> // Output index of token
```
Script pub key:
```
// Contract ref
OP_PUSHINPUTREFSINGLETON <contractRef> OP_DROP
// Check token ref exists in the token output
OP_DUP OP_ROT OP_REFDATASUMMARY_OUTPUT OP_SWAP 36 OP_MUL OP_SPLIT OP_NIP 32 OP_SPLIT OP_DROP <tokenRef> OP_DUP OP_ROT OP_EQUALVERIFY
// Get ref hash in token output script
OP_ROT OP_STATESCRIPTBYTECODE_OUTPUT OP_SWAP OP_SPLIT OP_NIP 69 OP_SPLIT OP_DROP
// Compare ref hash in token output to ref hash of this script's unlocking code
OP_SWAP 32 OP_INPUTINDEX OP_INPUTBYTECODE OP_HASH256 OP_CAT OP_CAT OP_EQUALVERIFY
// Propagate the contract
OP_INPUTINDEX OP_CODESCRIPTBYTECODE_UTXO OP_HASH256 OP_CODESCRIPTHASHOUTPUTCOUNT_OUTPUTS 1 OP_NUMEQUALVERIFY
//
// Contract code...
//
```
Using ref and hash indexes allows the token to control multiple contracts in the one transaction. The token output state script can contain many ref hash pairs.
This powerful script allows complex contracts to be composed of many UTXOs, each with its own responsibility. Token scripts continue to use a standard script easily understood by wallets but can have any custom contract associated with it. Pay to token contracts can be deployed at any time to extend the functionality of a token.
It allows a contract to be split into multiple parts. Token ownership has its own UTXO, and there can be other UTXOs for data storage and other functionality. This means you can have a standard token script understood by wallets but extend it with any custom functionality. Extensions can be added at any time. So if a developer decides they want to add new functions to a contract, they just deploy a "pay to token" script.
#Dev #GeniusContracts #GeniusTokens #TheFutureIsNow
@RXD_Community @RadiantLayerOne