CashScript / cashscript

⚖️ Easily write and interact with Bitcoin Cash smart contracts
https://cashscript.org
MIT License
112 stars 79 forks source link

Add better support for "state simulation" #106

Closed rkalis closed 10 months ago

rkalis commented 2 years ago

See comments in #98

We can add an OP_CODESEPARATOR between the constructor args and the bytecode. This should make it easier to make "state simulated" contracts. Since then OP_ACTIVEBYTECODE would always be the "base" bytecode, and we could add the "new" state in front of that.

Example syntax (for a contract with name ExampleContract):

bytes34 output = new OutputP2SH(bytes8(1000), hash160(new ExampleContract(tx.locktime + 100, 1000)));

Then the compiled output for new ExampleContract(tx.locktime + 100, 1000) will be something like this:

<VarInt> <1000> OP_CAT <VarInt> <tx.locktime + 100> OP_CAT OP_CAT OP_ACTIVEBYTECODE OP_CAT

We just need to figure out whether we can dynamically determine the VarInts and how to do that.

rkalis commented 2 years ago

After discussion on Telegram I realised that OP_INPUTINDEX OP_UTXOBYTECODE only pushes the locking script (not the redeem script). While OP_ACTIVEBYTECODE pushes the redeem script. So I'll need to include OP_ACTIVEBYTECODE support in the upcoming next release. This makes the construction above slightly more tricky since I'll still have to account for the case with and without constructor args.

rkalis commented 10 months ago

Now that CashTokens are live, I think the best way to do most state simulation is through NFT commitments. So I'll close this issue.