What is Atomic Swap and how does it work?
Quite often, developers pay attention to the technology of atomic swaps. Many of them believe that this is a necessary stage in the development of the cryptocurrency sphere, which will introduce significant changes in the process of assets exchange and control.
- What is an atomic swap?
- How does the technology work?
- What user protection takes place during an atomic swap?
- What is the difference between on-chain and off-chain atomic swaps?
- What requirements the cryptocurrency must comply with to maintain an atomic swap?
- Where atomic swaps can be implemented?
- At what stage of development is the technology?
What is an atomic swap?
Atomic swaps allow you to exchange one cryptocurrency for another without the participation of any third parties. This development can completely change the principle of cryptocurrency transfer and will allow transferring coins from one wallet to another directly, without the participation of exchanges and any other intermediary platforms. That is, this development allows you to exchange different cryptocurrencies really peer-to-peer.
The term "atomic" was borrowed from computer science and means the indivisibility of trade. Exchanges and other intermediary platforms divide the transaction into several stages. With an atomic transaction, the trade takes place seamlessly. An atomic swap is performed either completely or not executed at all.
Atomic swaps can be carried out directly between blockchains of various currencies.
How does the technology work?
For example, Alice has bitcoins, and she wants to exchange them with Bob for Zcash. To make the trade in the traditional way, Alice must have an account on the exchange or register if it is not there. Then Alice will make a deposit, put an order, get her assets, and then she will need to withdraw them. In addition, she needs to pay a commission to the exchange twice: when she buys and sells assets.
If currencies support an atomic swap, and Bob is willing to exchange his Zcash, this operation can be carried out without any intermediaries. The technology of such swaps is based on the principles of cryptography, which is related to signatures.
The process itself will take place in several stages, which we consider now:
- Since Alice decided to change one coin for another, she creates the first contract on the principle of a smart contract, which is something like a deposit box. When creating this contract, Alice chooses any number X that only she will know. Important: Alice should not tell Bob this secret number until a certain stage of the operation, because then he can simply go into her “deposit box” and take the asset without her permission. She will allow her to transfer her bitcoins to Bob’s public address only if he knows the hash from Alice’s first contract, and the transaction is signed by both Bob and Alice.
- Alice creates a hash of her secret number X - this is a kind of mixture of the original number or phrase that are mathematically linked. At the same time, it is impossible to recognize the source number from the hash, but it is very easy to get the hash from the source number. That hash will act as a kind of lock, and the key will be the number X, which so far only Alice knows.
- Alice creates a second contract that allows transferring a specified number of bitcoins to her address, and blocks it, for example, for 48 hours, if Alice has the signature.
- Alice sends the second contract to Bob, he signs it and sends it back to Alice.
- After this, Alice must transfer her first contract to the network.
- Bob checks that there are no mistakes in Alice’s contract and creates his own contract that allows him to transfer Zcash to Alice’s public address. It is allowed if the secret number X suits to its hash and is signed by Alice, or signed by both Bob and Alice.
- Bob creates a second contract that will allow Zcash to be transferred to Bob’s public address and block these coins, for example, for 24 hours. This contract must also be signed by Bob.
- Bob sends his second contract to Alice, she signs it and sends it back to Bob.
- Bob sends his first contract to the network.
- Alice receives Bob’s first contract and gives him her secret key X.
- Bob receives Alice's first contract and opens it with the help of secret code X, which he already has.
This is how the atomic transaction takes place. As we can see, both Alice and Bob have created two contracts each.
In this way, when we change one cryptocurrency to another, which are located on different blockchains, both senders must transfer the transaction to their blockchain, i.e. to the one on which this currency is located. At the time of sending, the user in the same way creates a secret key, which is then hashed. Then this key will be used to get cryptocurrency from another blockchain. Accordingly, these are not coins transferred from one blockchain to another, but their equal value.
What user protection takes place during an atomic swap?
But what if someone wants to cheat? This issue is also thought out. In case with atomic swaps, users are dealing with two simultaneous transactions. This means that no user can hold funds during the transaction.
You may also have noticed the time limits at 48 and 24 hours that figured in the process of transferring assets. To do this, there are а hash time locked contracts (HTLC). These contracts require the recipient to confirm that he received payment. For this, he needs to create a cryptographic certificate.
Further, it is given a limited amount of time on this confirmation. During this operation, the system uses the nLockTime parameter. It sets the minimum period of time during which the transaction is not accepted in the block. If the user has not confirmed the transaction within the specified period, all funds will automatically be returned back to the sender. However, for the period specified in the nLockTime parameter, all assets are frozen.
Such a system allows avoiding a situation when the recipient already has his cryptocurrency, but has not sent anything in return.
What is the difference between on-chain and off-chain atomic swaps?
Atomic swaps can be carried out on the blockchain and outside the blockchain.
On-chain swap is carried out directly on the blockchains of currencies that users want to change. At the same time, both cryptocurrencies must support а hash time locked contract (HTLC) and have the same hashing algorithm.
A swap outside the blockchain is conducted on the second layer. This is an extension of the Lightning Network. Thus, various Lightning networks can be connected in different blockchains.
What requirements the cryptocurrency must comply with to maintain an atomic swap?
First, to make a transaction between two different blockchains, they must support the same cryptographic hash function. Without compliance with this requirement, the contract will not function correctly after hashing the secret code.
Secondly, both blockchains should support contracts with а hash time locking feature.
Third, the implementation of Lightning Network. However, opinions are divided on this issue. Some experts believe that this network is not mandatory for an atomic swap; others argue that it is fundamental.
Both atomic swaps and Lightning Network use hash contracts that support time lock. However, atomic swaps link blockchains, and the Lightning Network, in turn, links payment channels.
In the case of the Lightning network, Alice and Bob can create a payment channel with third persons and exchange through them. At the same time, they will not need to trust them.
Both of these systems have the same basic mechanism. Therefore, integrating the Lightning Network into an atomic swap does not cause serious difficulties. Thanks to the possibility of a third person participation in the process of exchanging funds, there is a prospect of the existence of decentralized exchanges.
Where atomic swaps can be implemented?
This technology has all chances to oust the classic cryptocurrency exchanges. It is possible in the case when someone wants to simply change his cryptocurrency, and not make money on the rates. Swaps are convenient whenever of exchanging very small amounts and, conversely, very large amounts.
With atomic swaps, if you need to exchange a very small amount of cryptocurrency, you will not need to pay an exchange fee for depositing and withdrawing funds. In addition, if you need to change 500 BTC to 18,000 ETH, you cannot be afraid that the exchange through which you change them will be hacked. Moreover, you will be sure that if the transaction fails, your 500 bitcoins will come back to you.
Decentralized exchanges based on atomic swaps will allow users to control their assets completely. They can also remain as anonymous as the technology of the chosen cryptocurrency allows.
At what stage of development is the technology?
Atomic swaps have already been carried out between Litecoin, Bitcoin, Vertcoin and Decred cryptocurrencies.
However, the problem is that while the network is not running, the configuration of local servers is required for atomic swaps. It means that users who want to exchange cryptocurrencies need to download the full blockchains of each of them. For inexperienced users, this procedure will be difficult and time-consuming.
Currently, the team of Komodo project is working on developing a common server for atomic swaps – a decentralized exchange using the Electrum server.
The Altcoin.io project also tests atomic swaps between bitcoin and ether, and builds a decentralized exchange based on the swap technology.
More several developers are working on this issue.