Atomic swaps are often discussed but rarely understood in full. What are atomic swaps? How do they work? And what makes them so important?
Stated simply, atomic swaps are trades of cryptocurrencies made directly from one user to another. They're called "atomic" because either the trade is successfully completed and each trader receives the other one's funds, or nothing happens and both traders simply keep the funds they started with. Atomic swaps are sometimes called “peer-to-peer swaps” because the trades are made wallet-to-wallet, without any intermediary or middleman.
If you'd like to learn more about atomic swaps before testing the tech yourself, then keep reading. This post will dive deeper into what atomic swaps are, provide a brief history of the technology, discuss the benefits of atomic swap trading, and describe how Komodo is pushing this crucial technology to the forefront of the blockchain industry.
What Are Atomic Swaps?
Atomic swaps are secure, peer-to-peer trades of any two digital assets, without any intermediaries or trusted third-parties.
It’s important to emphasize that both traders hold their private keys throughout the entire atomic swap process. As a result, atomic swap trading is magnitudes more secure than trading assets on a centralized exchange.
Centralized digital asset exchanges do not allow users to retain custody of their private keys which, in effect, means that users do not actually own their coins and tokens. As the old crypto mantra goes:
If you do not hold your private keys, then you do not own your coins!
The logical conclusion, then, is that centralized exchanges— not the users— own the coins. Traders are choosing, perhaps unwittingly, to let the centralized exchange seize ownership of their coins and tokens. If the exchange gets hacked and funds are comprised, there is little recourse for the users who have had their coins and tokens stolen.
This stands in stark contrast to trading via atomic swap technology for two reasons. First, atomic swap trading allows users to keep control of their private keys. Second, there is no increase in security risk when performing an atomic swap. The risk is exactly the same as storing your coins or tokens in any online wallet (a “hot” wallet).
Of course, the safest way to store digital assets is in an offline wallet (a “cold” wallet). However, this method of storage does not allow any form of trading. So if you want to exchange your coins or tokens for other cryptocurrencies, atomic swap trading is indisputably the most secure way to do it.
A Brief History Of Atomic Swaps
Before we dive any deeper into how atomic swaps work, let’s take a minute to cover some history.
The idea of trustless, peer-to-peer trades of cryptocurrency has been a topic of public discussion since at least 2012. For instance, a developer named Sergio Demian Lerner created a first draft of a trustless exchange protocol in July 2012. While this was a good start, the idea was not yet fully fleshed out.
Later, in May 2013, Tier Nolan provided the first full account of a procedure for atomic swaps. Tier Nolan is widely credited with being the inventor of atomic swaps.
At that point, the idea of an atomic swap was little more than that-- an idea. While everyone recognized the benefits such a trading mechanism would bring, no one had written the code to make atomic swaps possible.
Just one year after Tier Nolan presented the idea of an atomic swap protocol, in May 2014, Komodo’s Lead Developer jl777 wrote the code that allowed some of the very first peer-to-peer swaps.
At first, jl777’s code only permitted atomic swaps between a select few assets, all based on the UTXO model. Soon, jl777 developed a method of atomic swaps that allowed Nxt assets to be exchanged with Bitcoin. The code was eventually extended to allow atomic swaps between any Bitcoin-protocol coins.
Many of the first atomic swaps were performed via a command line interface between developers testing out the brand-new technology. As such, most of the evidence of the earliest atomic swap exists only on chat forums.
During the summer of 2017, Komodo developed BarterDEX, the first GUI for a fully atomic-swap-powered crypto trading marketplace. Komodo began integrating dozens of Bitcoin-protocol coins and publicly performing thousands of atomic swaps on BarterDEX. (Please note that BarterDEX has now been retired in favor of AtomicDEX, Komodo's third-generation decentralized exchange.)
Atomic swaps didn’t become widely known until several years after the first atomic swaps were made. In September 2017, Litecoin Founder Charlie Lee Tweeted about a successful swap from Litecoin to Bitcoin, bringing a great deal of public attention to the groundbreaking technology.
The next big development came the following month, in October 2017, when Komodo Platform devised a method of atomic swap trading with SPV servers. This allows traders to execute atomic swaps without needing to download the entire blockchains for the coins they’d like to exchange. This is often called trading in “Light Mode."
In February 2018, Komodo bridged the gap between Ethereum and Bitcoin-protocol coins with a swap between ETH and DOGE. It appears that Komodo was the first blockchain project to successfully execute an atomic swap between ETH and a BTC-protocol coin.
In April 2018, the Komodo Development Team began working on an updated atomic swap protocol that would remove several of the limitations present in the first iteration. This includes eliminating the needs for: making a deposit before beginning an atomic swap; acquiring proxy tokens before swapping from Bitcoin protocol to ETH and ERC tokens; and manually splitting UTXO to the right number and amounts. The Komodo Dev Team also sought to make the atomic swap protocol more stable and reliable.
In July 2019, after more than a year of hard work, Komodo released a third-generation, mobile-ready atomic swap DEX known as AtomicDEX. AtomicDEX is in a public beta phase and you can download AtomicDEX on Google Play for Android and download AtomicDEX from Apple App Store for iOS.
Interested readers can read a full history of atomic swap technology here.
The Technical Secrets Of Atomic Swaps
Atomic swaps are made possible through two specific features in the Bitcoin code.
The first is a command in the Bitcoin Script known as OP_CHECKLOCKTIMEVERIFY (CLTV). This command was not present in the original Bitcoin code but was added later through what’s known as a Bitcoin Improvement Proposal (BIP). BIPs are numbered chronologically and the proposal that added CLTV to the Bitcoin code base is known as BIP-65, which was accepted at the end of October 2015.
The Check-Lock-Time-Verify command, according to the BIP itself, “allows a transaction output to be made unspendable until some point in the future.” In plain English, this means that a payment can be locked for a specific length of time, preventing the receiver of the payment from accessing the funds until that specific period of time has passed.
The second feature in the Bitcoin code that makes atomic swaps possible is called pay-to-script-hash (P2SH) transactions. P2SH transactions send funds to an address that requires special permission before the funds can be spent again. The special permission might include authorization from several different private keys, in what's known as a multi-signature payment.
Or, the special permission might be entering a secret code that acts as a password. This is called a hashlock. A hashlock “restricts the spending of an output until a specified piece of data is publicly revealed.”
More precisely, it locks a transaction with the hash of a secret code— that is, the data that results from putting the secret code through a cryptographic hash function— such that the funds can only be spent once the secret code is publicly revealed.
Whereas CLTV locks funds until a set period of time has expired, hashlocks lock funds until a secret code— the code that can accurately produce the hash that was used to lock the funds in the first place— is publicly broadcast to the blockchain’s peer to peer network.
When used in combination, Check-Lock-Time-Verify and hashlocks become what is known as Hash Time Lock Contracts (HTLC). HTLC allow funds to be both locked with a secret code and bound by a specific period of time. If the secret is not revealed and the funds are not spent before the set length of time expires, then the funds are automatically sent back to the address from which they originated prior to being hashlocked. In short, this is how atomic swaps work.
The basic idea is that Bob and Alice can send each other funds that are locked by the hash of a predetermined secret code. Bob publicly reveals the secret code to collect Alice's funds, which allows Alice to also see the secret code and use it to collect Bob's funds. If Bob doesn't collect Alice's funds, then Alice can never spend Bob's funds. In this case, the locktime set by the CLTV command would expire and both Bob and Alice would get their money back. That's what makes the swap atomic.
The Benefits of Atomic Swap Trading
Now that we understand a little more about how atomic swaps work, let’s take a closer look at the benefits of atomic swap trading.
First and foremost, users do not need to give up their private keys at any point in time. When trading via atomic swaps, you always own your private keys (and thus your coins and tokens). You never have to give up control of your funds to a third party.
Second, atomic swaps are designed such that the swap takes place and both parties receive the funds they desire, or nothing happens at all and both parties retain the funds they started with (minus a very small transaction fee for the “order-taker”). Atomic swaps make digital asset trading as secure as it can possibly be.
Third, atomic swaps are much cheaper than trading on centralized exchanges. Most centralized exchanges charge relatively high fees, normally 0.2% of every transaction for each party in a trade. Most centralized exchanges also charge a fee to withdraw funds. Imagine that— paying a fee to a centralized exchange just for them to give you back control of your own funds!
Last, but not least, atomic swaps allow you to trade between a wider variety of coins and tokens. AtomicDEX, Komodo’s decentralized exchange, for example, bridged the gap between Bitcoin-protocol-based coins and Ethereum-based ERC-20 tokens. As such, AtomicDEX supports direct trading pairs between 95% of all the coins and tokens in existence.
For instance, a user can trade directly from a BTC-based altcoin to an ERC-20 token (or vice versa). Before Komodo Platform made this possible, a trader would have had to make multiple exchanges to get the same result. The trading process would have been BTC-based altcoin—>Bitcoin—>Ether—>ERC-20 token, with a fee for each of the three transactions.
With all of these enormous benefits, you can see why atomic swaps generate so much excitement in the blockchain industry.
The Atomic Swap Process Explained
Suppose that there are two traders, Bob and Alice. Let’s say that Bob has BTC he would like to exchange for KMD. Meanwhile, Alice has KMD and she is hoping to swap it for BTC. Here’s exactly how the atomic swap would unfold, step-by-step.
The Market Maker posts a trade order on AtomicDEX. This is listed as Step 0 because, technically, it’s not part of the atomic swap process. However, this step must happen before the atomic swap can begin.
Step 1: <DEXfee> payment
The Taker sees Maker’s offer and accepts it. She commits to the trade by paying the DEX fee, which is just 0.15% of the total trade amount. The purpose of the DEX fee is to make sure Takers don’t spam the network with rapid requests. Note that Makers do not pay any trading fees on AtomicDEX.
Once Taker has paid the DEX fee, the atomic swap has officially begun. Maker receives the fee payment and initiates the second step of the swap.
Step 2: <MakerPayment> Is Sent
Maker sends his payment, known as the Maker Payment, which is sent to a P2SH address (for Bitcoin-protocol coins) or to a smart contract address (for ETH and ERC-20 tokens).
The Maker Payment is locked with the hash of a 32 byte secret code that Maker randomly generates. Only Maker knows the secret but the hash of the secret is public. This means that Taker knows the hash of the secret but she cannot spend the funds until she knows the 32 byte secret code itself. To better understand the concept of hashes, read this guide on cryptographic hash functions.
The Maker Payment is also time locked. The length of the time lock varies according to the block times of the two assets being swapped. Since Bitcoin has long 10 minute block times, the lock time must be longer, too. In most cases, the lock time for Maker Payment is 15,600 seconds, which is 4 hours and 20 minutes.
If the time lock expires before the atomic swap is complete, then Maker receives the Maker Payment back.
Step 3: <TakerPayment> Is Sent
Taker sees that Maker has sent the hashlocked and timelocked Maker Payment. At that point, she sends the Taker Payment to a second secure P2SH address.
Significantly, the Taker Payment is hashlocked with the same hash that Maker used to lock the Maker Payment. Taker can use the hash to lock the funds, despite the fact that she still does not know the secret code that Maker used to generate the hash.
The Taker Payment is timelocked, but with a length of time that is half of that used to lock the Maker Payment.
Step 4: <TakerPayment> Is Spent
Maker sees that Taker has sent the Taker Payment, which is locked by the hash of the 32 byte secret code he generated in Step 2. Maker reveals the 32 byte secret code, broadcasting it to the blockchain's peer to peer network in order to send the Taker Payment to his own address. Once this transaction has been initiated, the secret code is publicly visible.
Step 5: <MakerPayment> Is Spent
Taker sees that Maker has spent the Taker Payment and also sees that the secret code has been revealed. She uses the same secret code to unlock the Maker Payment and sends the funds to her personal address.
That’s all there is to it! The atomic swap is now complete.
This process ensures a secure, trustless, peer-to-peer exchange. There are no intermediaries, only a cleverly designed protocol that leverages cryptographic techniques to make sure neither party in the trade can cheat. It is also an atomic process, which literally means that either: (a) the swap occurs exactly as Bob and Alice agreed, or (b) nothing takes place and both Bob’s and Alice’s funds are returned to them.
Komodo's Indsutry-Leading Atomic Swap Tech
For years, Komodo has led the blockchain space with regard to atomic swaps and decentralized trading technologies. This is widely acknowledged throughout the entire industry.
For instance, an article published in Forbes notes that "Komodo, an atomic swap early adopter launched the first atomic swap across ethereum." In another example, Binance tweeted to thank Komodo for playing an "important role by experimenting with atomic swap protocols." Lastly, a different article on CryptoBriefing notes that Komodo's Lead Dev "jl777 coordinated a Dogecoin to Litecoin swap on an NXT forum in 2014, preceding his own atomic exchange, InstantDEX, by about two years."
Now, after years of pioneering this groundbreaking technology, AtomicDEX, Komodo's third-generation atomic swap exchange, is in a public beta phase.
AtomicDEX is the industry's only product to meet the 4 core requirements of a truly decentralized exchange. It's also the first fully-functional, atomic-swap DEX that is available for mobile devices.
Start making your very first atomic swaps with AtomicDEX today:
To get all the latest updates from Komodo, join the monthly email list. On the first Friday of every month, you'll receive a newsletter with information about all of the most important developments from the previous month. You can also join the Komodo Discord server to chat with other community members and the Komodo team.
Join us at the cutting edge of the blockchain industry.