When you own cryptos , no matter which ones, and want to exchange them for others, you must use an intermediary for this to be done. This intermediary is usually an exchange .
While the use of exchanges to buy and resell cryptocurrencies is widespread, there are some issues with it :
- Exchanges can be hacked .
- They take commissions on transactions.
- Commissions are also levied on withdrawals and deposits in order to pay, in part, the miners .
In order to solve the problem of hacks, decentralized exchanges (called DEX, for Decentralized Exchange) are starting to be created little by little. One example is Komodo . Unfortunately, they are far from effective. And for now, only ERC-20 tokens are supported. In addition, the latter also take a fee to operate.
Currently, if you want to exchange crypto-currencies between individuals outside the exchange, this can only be done if the two people each send the agreed amount. The door is therefore open to all scams , because a gullible person can send his cryptos to a person who will not send him anything in return.
This is where the famous Atomic Swap comes in, which will solve this problem of trust. Let’s take a closer look at how this technology works.
Atomic Swap: definition
For once, the term Atomic Swap comes from English . Atomic is a computer term that roughly means: ” a thing happens completely or not at all “.
This definition is important because an Atomic Swap makes it possible to exchange crypto-assets between 2 people without an intermediary and without the need to trust the other person. If one of the two people does not respect his commitment and does not send the tokens, then the first person will be reimbursed for his sending thanks to a protection registered in a smart contract .
Detailed operation of the Atomic Swap
To explain how Atomic Swap works, we will take the example of two people who want to exchange ETH for BTC .
Practical example of the use of an Atomic Swap
Jean and Robin are two friends, but in order to avoid any worries, they decide to use the Atomic Swap to make an exchange between Ethereum and Bitcoin.
- Robin owns 10 ETH.
- John owns 0.5 BTC.
- They want to exchange these two crypto-assets without using an intermediary.
1. Robin creates a hashlock
A hashlock is a smart contract that will block tokens until a secret code is disclosed. To do this, Robin uses the following method:
- It chooses a large number , which is called a pre-image. This will be the secret code.
- It will then use this pre-image to create another number, which will be called a hash .
- Robin will then have to create a smart contract to send 10 ETH to Jean, who will be blocked with this hash. The ETH will only be sent if John is able to indicate the pre-image that will match the hash.
Calculating the hash of the pre-image in step 2 is easy, but calculating the pre-image based on the hash is (nearly) impossible . This means that John will not be able to receive the ETH without Robin providing him with the pre-image.
2. John does the same
Jean verifies that Robin sent a smart contract hashlock for 10 ETH. He can verify this by looking on the public blockchain .
If so, John then in turn sends a contract hashlock to Robin for 0.5 BTC which is locked with the same hash that Robin used. This will link the two transactions together. But the transactions will need to be unblocked for the exchange to take place.
3. Unblocking the tokens
When Robin wants to unlock the bitcoins that John sent him, he will need to use the pre-image number to authorize the transaction.
When he does this, the pre-mining number will go on the blockchain and become public information. John can then use this information to unlock his 10 ETH, because the hash used in both transactions is the same.
So that’s what an Atomic Swap is … or thereabouts.
We forgot to consider an option: what if Robin never gives the preimage to John? In this case the smart contracts will be blocked indefinitely.
4. Reimbursement of a failed Atomic Swap
If an Atomic Swap fails, then it cannot be canceled and transactions will be blocked ad vitam eternam . Unless we add security in the smart contract. For example, let’s say if Robin hasn’t given the pre-image count within 24h, then the transaction is automatically rolled back.
This protection is called time lock . From now on, if Robin does not show the pre-image to Jean, they will both be reimbursed.
Are things settled yet? Not quite, because Robin can still cause problems .
5. A double duration of time lock for Robin
Let’s say Robin is a smartass and tries to abuse the system by deciding to use his pre-image count after 11:55 p.m. to unlock the contract. That would only give John 5 minutes to unlock his. In this case, there is a good chance that Jean will not be able to recover what Robin owes him because of the too short delay.
Robin will then recover Jean’s BTCs and see his 10 ETH return to his wallet thanks to the smart contract. Robin would therefore have stolen Jean’s bitcoins.
This is solved with a very simple solution: double the time lock so that Robin gets his money back. This way, if Robin tries to scam Jean, he will have 24 more hours to collect his part of the contract.
So here we are with a perfectly functional and safe Atomic Swap!
Disadvantages of Atomic Swaps
As we have seen, on paper Atomic Swaps are well constructed and look interesting. However, here are some issues as of this writing (October 2018):
- This technology is very slow .
- Most exchanges and wallets do not support Atomic Swaps.
- It only works with crypto-assets for which you can write a smart contract.
These issues should be resolved over time. A day will come when crypto-currencies can be exchanged between individuals in a simple, efficient and secure way.