Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

meep fails on OP_DUP after OP_UTXOVALUE #13

Open
kliyer-ai opened this issue May 29, 2022 · 1 comment
Open

meep fails on OP_DUP after OP_UTXOVALUE #13

kliyer-ai opened this issue May 29, 2022 · 1 comment

Comments

@kliyer-ai
Copy link

It runs up until this OP code:
image

Then it fails:
image

Let me know what else you need to help me. Thanks.

@kliyer-ai
Copy link
Author

This is the contract:

pragma cashscript ^0.7.0;

contract CashBox(bytes20 creator, bytes20 admin, int valuePerRedeem, int numRedeemsLeft) {

    function redeem(pubkey adminPK, sig adminSig, bytes20 redeemer) {

        // if this is the case, the creator needs to call the withdraw function
        require(numRedeemsLeft > 0);

        // only admin can call this function
        require(hash160(adminPK) == admin);
        require(checkSig(adminSig, adminPK));

        // the script moust contain enough value so that all future redeems can be processed
        int minerFee = 1000; // hardcoded fee
        int amountSent = tx.inputs[this.activeInputIndex].value;
        require(amountSent >= valuePerRedeem * numRedeemsLeft + minerFee * numRedeemsLeft);

        // the first output needs to be money sent to the redeemer
        bytes25 redeemerLock = new LockingBytecodeP2PKH(redeemer);
        require(tx.outputs[0].lockingBytecode == redeemerLock);
        require(tx.outputs[0].value == valuePerRedeem);

        // the second output has to be a modified version of this contract (simulating state)

        int newContractValue = amountSent - minerFee - valuePerRedeem;
        require(tx.outputs[1].value == newContractValue);

        // Cut out old initialBlock (OP_PUSHBYTES_8 <initialBlock>)
        // Insert new initialBlock (OP_PUSHBYTES_8 <tx.locktime>)
        // Note that constructor parameters are added in reverse order,
        // so initialBlock is the first statement in the contract bytecode.
        bytes newContract = 0x08 + bytes8(numRedeemsLeft - 1) + this.activeBytecode.split(9)[1];
        // Create the locking bytecode for the new contract and check that
        // the change output sends to that contract
        bytes23 newContractLock = new LockingBytecodeP2SH(hash160(newContract));
        require(tx.outputs[1].lockingBytecode == newContractLock);
    }

    function end(pubkey creatorPK, sig creatorSig){
        // only the creator can end the contract
        require(hash160(creatorPK) == creator);
        require(checkSig(creatorSig, creatorPK));

        int minerFee = 1000; // hardcoded fee
        int amountSent = tx.inputs[this.activeInputIndex].value;
        bytes25 creatorLock = new LockingBytecodeP2PKH(creator);
        require(tx.outputs[0].lockingBytecode == creatorLock);
        require(tx.outputs[0].value == amountSent - minerFee);
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant