Skip to content

Instantly share code, notes, and snippets.

@thr0wn
Last active October 30, 2020 06:51
Show Gist options
  • Select an option

  • Save thr0wn/2dfd29f72888c05bce2ccabf03c8c9e8 to your computer and use it in GitHub Desktop.

Select an option

Save thr0wn/2dfd29f72888c05bce2ccabf03c8c9e8 to your computer and use it in GitHub Desktop.

Revisions

  1. thr0wn revised this gist Oct 30, 2020. 1 changed file with 26 additions and 20 deletions.
    46 changes: 26 additions & 20 deletions swap.cash
    Original file line number Diff line number Diff line change
    @@ -1,26 +1,32 @@
    pragma cashscript ^0.4.0;

    contract Swap(pubkey escrowPk) {
    function spendTokenA(sig address2Sig, pubkey address2Pk, datasig escrowSig, bytes escrowMessage) {
    // Check that the message was signed by the escrow
    require(checkDataSig(escrowSig, escrowMessage, escrowPk));
    contract Swap() {
    function claimToken(sig selfSig, pubkey selfPk, pubkey otherPk, datasig selfDatasig, datasig otherDatasig, bytes message) {
    // Check that the message was signed by both parts
    require(checkDataSig(selfDatasig, message, otherPk));
    require(checkDataSig(otherDatasig, message, selfPk));

    // check itself sig
    require(checkSig(selfSig, selfPk));

    // Check that the transaction was signed by the contract owner
    require(checkSig(address2Sig, address2Pk));
    pubkey mSelfPk = int(message.split(?)[?]);
    bytes? mSelfToken = int(message.split(?)[?]);
    bytes? mSelfAmount = int(message.split(?)[?]);
    pubkey mOtherPk = int(message.split(?)[?]);
    bytes? mOtherToken = int(message.split(?)[?]);
    bytes? mOtherAmount = int(message.split(?)[?]);
    int minerFee = 1000;

    // Check if recipient2Pk is address2Pk
    pubkey recipient2Pk = int(escrowMessage.split(4)[0]);
    require(recipient2Pk === address2Pk);
    }
    function spendTokenB(sig address1Sig, pubkey address1Pk, datasig escrowSig, bytes escrowMessage) {
    // Check that the message was signed by the escrow
    require(checkDataSig(escrowSig, escrowMessage, escrowPk));

    // Check that the transaction was signed by the contract owner
    require(checkSig(address1Sig, address1Pk));

    // Check if recipient1Pk is address1Pk
    pubkey recipient1Pk = int(escrowMessage.split(4)[1]);
    require(recipient1Pk === address1Pk);
    if (mSelfPk === selfPk) {
    bytes swap = new OutputNullData([mSelfToken, mSelfAmount]);
    int changeAmount = int(bytes(tx.value)) - minerFee;
    bytes32 change = new OutputP2SH(bytes8(changeAmount), hash160(selfPk));
    require(tx.hashOutputs == hash256(swap + change));
    } else {
    bytes swap = new OutputNullData([mOtherToken, mOtherAmount]);
    int changeAmount = int(bytes(tx.value)) - minerFee;
    bytes32 change = new OutputP2SH(bytes8(changeAmount), hash160(otherPk));
    require(tx.hashOutputs == hash256(swap + change));
    }
    }
    }
  2. thr0wn created this gist Oct 28, 2020.
    26 changes: 26 additions & 0 deletions swap.cash
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    pragma cashscript ^0.4.0;

    contract Swap(pubkey escrowPk) {
    function spendTokenA(sig address2Sig, pubkey address2Pk, datasig escrowSig, bytes escrowMessage) {
    // Check that the message was signed by the escrow
    require(checkDataSig(escrowSig, escrowMessage, escrowPk));

    // Check that the transaction was signed by the contract owner
    require(checkSig(address2Sig, address2Pk));

    // Check if recipient2Pk is address2Pk
    pubkey recipient2Pk = int(escrowMessage.split(4)[0]);
    require(recipient2Pk === address2Pk);
    }
    function spendTokenB(sig address1Sig, pubkey address1Pk, datasig escrowSig, bytes escrowMessage) {
    // Check that the message was signed by the escrow
    require(checkDataSig(escrowSig, escrowMessage, escrowPk));

    // Check that the transaction was signed by the contract owner
    require(checkSig(address1Sig, address1Pk));

    // Check if recipient1Pk is address1Pk
    pubkey recipient1Pk = int(escrowMessage.split(4)[1]);
    require(recipient1Pk === address1Pk);
    }
    }