Skip to content

Instantly share code, notes, and snippets.

@thr0wn
Last active October 30, 2020 06:51
Show Gist options
  • Save thr0wn/2dfd29f72888c05bce2ccabf03c8c9e8 to your computer and use it in GitHub Desktop.
Save thr0wn/2dfd29f72888c05bce2ccabf03c8c9e8 to your computer and use it in GitHub Desktop.
pragma cashscript ^0.4.0;
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));
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;
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));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment