Atomic-Swap for Smart-Contracts

Written by

In our previous post about sending Dai between chains we did not mention smart-contracts. We showed how to send Dai from an account on Kovan to the same account on Tobalaba. This does not work if your tokens sit in a multi-sig smart-contract or are held by a uPort identity.

Registering Address Pairs

In order to transfer a token from an address on Kovan to a different address on Tobalaba, the origin and destination addresses need to be paired. The challenge is to make such pairs safe for production use.

Identifying Pairs

To avoid complex signing, registration and authentication mechanisms, we opted to require that both the origin and destination addresses be smart-contracts and both have to be created by the same account.

We are aware that this rules out wallets created from factories but that is a limitation of this first version. In the future, we might add an address registration contract which will enable a user to prove the control of a particular contract.

Posting a Pair

An additional dedicated end-point has been added address-pairwhich must be called with a POST, to comply with Restful paradigms.

Here is an example of how to create an address pair with node.js:

var request = require("request");
var options = 
    { method: 'POST',
      url: 'https://atomic-swap.test.shareandcharge.com/address-pair',
      headers: 
       { 'Content-Type': 'application/x-www-form-urlencoded' },
      form: 
       { originTx: '0xebcd316d369ea95ec2e4e3e5fc76fee5bb1506cfd11f593e95b03656e7a25313',
         destinationTx: '0x936655614f9bb1d18316ed5ec9773ebe509fab21354695534b17b15871f43f9a',
         parentChainName: 'kovan'
       } 
    };
request(options, function (error, response, body) {
  if (error) throw new Error(error);
  console.log(body);
});

If you look at the transactions behind the hashes in the code (click on them to open etherscan), you will notice that both have been sent by the same address 0xc5fdf4076b8f3a5357c5e395ab970b5b54098fef. This is what makes the oracle accept the smart-contracts as pairs.

Using Address Pairs

Once registered, an address pair will just work. It can be used in the same manner as with an account. Follow the steps in the Try Out section of the previous post.

Conclusion

We now have an easy to use atomic-swap facility that allows the sending of tokens from ethereum accounts and smart-contracts. If you want to contribute or review or simply are curious, head over to the git repo, it’s all open-source.

Please create an issue in bitbucketif you spot a bug or have an idea on how to make this better.

Share this article
Share on facebook
Share on twitter
Share on email

Sign up to our newsletter