Atomic Swaps: Decentralized trade
By: Marcelo Morgado
Tags: Bitcoin, Ethereum, Blockchain, Atomic Swap, Decentralization

Usually when we want to trade between different cryptocurrencies we use exchanges or p2p trades (with or without escrow).

In both alternatives we have to trust on the intermediary (exchange / escrow) or the other party.

Don’t trust, verify!

The great innovation introduced by Bitcoin was the viability of a financial system without a trusted third party, that is, a trustless system.

Using this principle Atomic Swap was created, it’s a solution that makes possible exchange different cryptocurrencies without a third party. In the following sections I will explain how its works and give some examples of its implementation:

Requeriments

Both coins should support smart contract wich allows these aspects:

Time verify (TimeLock) — Example: Funds could only be spent after 1 week; Hash function verify (HashLock) — Example: Funds could only be spent when a challenge question (“hidden” by hash) be discovered; Visible hash input (Public Pre-image) — Example: It’s possible see challenge answer by looking transaction data.

Contracts wich has both 1 and 2 verifications are know as HTLC (Hashed TimeLock Contract) and also are used on Lightning Network protocol.

How it works

Let’s see a successfull trade of 1 BTC to 10 ETHs between Alice and Bob.

Alice generates a random value (secret), execute a hash function and obtain hashedSecret; Alice transfer 1 BTC to smart contract. To redeem funds the following conditions need to be true:

A) The transaction must contain the secret value;

B) The recipient must be Bob’s BTC address;

C) The transaction must be made within 48 hours of the contract creation. Bob transfer 10 ETHs to smart contract. The contract is similar to that created by Alice:

A) The transaction must contain the secret value;

B) The recipient must be Alice’s ETH address;

C) The transaction must be made within 24 hours of the contract creation. At this point the funds of Alice and Bob are under contracts control and the can be spent only if the secret value is know.

How Alice who generated this value, she is able to redeem to her wallet the 10 ETHs sent by Bob. Analyzing the last transaction of Alice, Bob is able to know the secret value (Public Pre-image) and have access to the 1 BTC and finalize the trade.

And if something goes wrong?

The previous example is based on a successful operation. Some problems may occur and the solution is designed to deal with them.

If Bob does not create his contract, does Alice get his 1 BTC frozen? No. To simplify the above explanation, another condition that the contract has been omitted is: After 48 hours of contract creation, Alice can refund. Bob is the exclusive recipient of funds only within the 48-hour window, after that period only Alice can receive them. The same goes for Bob, who after 24 hours can refund if Alice does not revealing the value of secret by redeem the 10 ETHs. The timelocks are distinct (48h and 24h) because otherwise it would be possible for Alice to redeem the ETHs at the end of the deadline and still have the possibility to refund her 1 BTC if Bob did not redeem quickly.

Implementation

At this link, it is possible checkout compatibility of some criptocurrencies with atomic swaps requirements.

Bitcoin

Bitcoin’s smart contracts uses Turing incomplete Script language, the code below is based on implementation at Decred’s repository.

OP_IF OP_SHA256 <HASHED_SECRET> OP_EQUALVERIFY OP_DUP OP_HASH160 <COUNTER_PARTY_PUBKEY> OP_ELSE <LOCK_TIME> OP_CHECKLOCKTIMEVERIFY OP_DROP OP_DUP OP_HASH160 <MY_PUBKEY> OP_ENDIF OP_EQUALVERIFY OP_CHECKSIG

Ethereum

The following is a possible implementation of a contract using Solidity language (based on this implementation)

Cons

Privacy: It is not difficult to trace values exchanged between different blockchains since the same hashedSecret will be visible on both platforms. Usability: There are still a few tools / wallets that supports the technology. Speed: Operations are slow if compared with centralized alternatives. Compatibility: Not all of cryptocoins are compatible.

Conclusion

There are already initiatives to use second layer solutions (such as the Lightning Network) to perform off-chain exchanges, solving problems 1 and 3.

Atomic Swap is an important step towards the decentralization of trade operations. It is currently a great solution for large-value trading, but more development of tools and technologies is still required to enable mass adoption.

Further reading