Atomic swaps allow the exchange of cryptocurrencies across different blockchains in a way that’s decentralized and non-custodial. For as long as blockchains have been operating, exchanging assets across networks has only been possible using centralized entities as an intermediary. Therefore, atomic swaps represent an exciting development for cryptocurrency users.
The History of Atomic Swaps
After the launch of Bitcoin, other blockchain networks with their own cryptocurrencies started to form, and the problem of exchanging one asset for another emerged. Sergio Demian Lerner, now Chief Scientist working on the RSK platform, was the first to propose a decentralized peer-to-peer trading protocol in 2012. As is often the case in blockchain, another developer, Tier Nolan, took the idea and ran with it, proposing the concept of atomic swaps in 2013.
Over the years, various others continued to iterate on the concept, but it was only in 2017 that the first atomic swap took place. In September of that year, Litecoin founder Charlie Lee announced via a tweet that he had completed an on-chain atomic swap of Litecoin (LTC) for Decred (DCR). Later, it also became possible to swap LTC for BTC.
How Do Atomic Swaps Work?
There are two types of atomic swaps – on-chain or off-chain. On-chain swaps are when assets from two different blockchains are directly swapped. So in the case above, where Charlie Lee swapped some of his LTC for DCR.
Off-chain involves executing a swap via off-chain channels in a payment layer, such as those used in Bitcoin’s Lightning Network. The critical difference is that off-chain channels enable more and faster transactions because each one can happen within the channel, and the net balances are only updated to the blockchain once the channel is closed.
Furthermore, off-chain channels overcome a key limitation of atomic swaps: on-chain swaps require both blockchains to use the same hashing algorithm. The LTC-BTC swaps were only possible thanks to the use of Lightning Network. Off-chain channels involve the use of a multi-signature wallet, allowing both parties to send and receive funds.
Hashed Timelock Contracts
On-chain swaps use a different technology – a variant of smart contracts called Hashed Timelock Contracts (HTLCs). To illustrate how on-chain atomic swaps work, imagine that Alice and Bob have agreed on a token swap. Alice will give Bob one BTC in exchange for 33 of his ETH.
Alice uses the HTLC to generate two hash locks, which you can think of as a kind of secure address. Both hash locks require the same secret code to unlock. Alice gets the secret code when she creates the hash locks. One lock is based on her public Key A, and the other is based on Bob’s public Key B.
Alice locks up her 1 BTC into the hash lock that can be unlocked with Bob’s Key B, but Bob doesn’t yet know the secret code until Alice shares it with him, so he can’t access the BTC. Once Alice shares the address with him, he can deposit his 33 ETH into the hash lock that can be unlocked with Alice’s Key A to proceed to the swap’s next stage.
Once Bob makes the deposit, Alice can retrieve the 33 ETH using the secret code generated at the start, and her own private key that matches her public Key A. When she enters the secret code, it’s revealed to Bob. Now he can use his own private key, along with the secret code, to unlock the 1 BTC, and the swap is complete.
The Importance of Timelocks for Fair Play
The timelock element of the HTLC comes into play if one of the parties doesn’t hold up their end of the agreement. Let’s say the transaction gets to the point where Bob has deposited his ETH but then Alice disappears. How can he retrieve his ETH?
This time, imagine that the transaction is time-limited. When Alice creates the hashlocks, she’s required to enter an expiry period onto each one. The lock based on Bob’s public key B, where she deposits her BTC, has an expiry period of one day. The timelock conditions require Alice to enter a longer time delay on the lock based on her own public key A, which she sets for one week.
Now, if Alice doesn’t enter the secret key and retrieve Bob’s ETH within one day, thus providing him with the secret key, he can recover his ETH after the day has expired. Similarly, if Alice deposits her BTC and Bob fails to match the deposit with his ETH, she can recover her funds after a week has passed.
The uneven time delay is essential here because it prevents Alice from accessing both sets of funds.
The term “atomic” refers to the fact that all of these conditions must be met before the swaps are finalized. If they aren’t, the transactions simply remain in their original state. The term “Hash Timelocked Contract” refers to the fact that the contract is locked both by hashes of the secret value and by time conditions.
The Importance of Atomic Swaps
The main reason that atomic swaps are so exciting for the cryptocurrency community is their potential to allow the decentralized, peer-to-peer exchange of assets between different platforms. Decentralized exchanges such as Uniswap and SushiSwap have become massively popular in part due to their secure, trustless, and non-custodial nature. Users always retain ownership of their private keys, and smart contracts manage all the trades. However, these are limited to Ethereum-based assets.
Therefore, the cryptocurrency community is still largely dependent on centralized exchanges to trade assets between blockchains. Using centralized exchanges involves trusting the exchange, and unfortunately, as centralized stores of wealth, exchanges have become targets for hackers. Exchanges also charge fees and can be subject to specific regulations depending on the jurisdiction. The idea of decentralized trading is attractive to many in the cryptocurrency community as it holds true to blockchain’s principles.
However, none of this is to say that atomic swaps are a panacea. The technology is still not used on any grand scale, and overall, there are still several critical limitations. One is privacy. On public blockchains, it’s easy to link addresses and trace funds that have been swapped atomically. This isn’t necessarily a show-stopper, as privacy-preserving technologies such as zero-knowledge proofs continue to develop and could ultimately enable private atomic swaps.
Another limitation is that (as previously mentioned) atomic swaps depend on the two blockchains using the same hashing algorithm for the HTLC to work. If the hashing algorithm is different, the HTLC cannot be unlocked, and the swap will fail.
Where are Atomic Swaps Used?
There are still only a small handful of projects using atomic swaps. Atomic Wallet is one example. However, atomic swaps are only enabled for a few select assets. In most cases, Atomic Wallet relies on its exchange partners to make the swaps.
Last year, Liquality, a startup incubated by ConsenSys, launched its own version of an Atomic Swap Wallet.
Atomic Swaps represent an exciting opportunity for the cryptocurrency sector, building on the already sterling work being done in the decentralized finance space. However, they still need a lot of development before their true potential can be realized. With other interoperability protocols also in development, it remains to be seen whether the potential of atomic swaps will ever become a reality.