Atomic Swaps Explained: The Ultimate Beginner's Guide

DanielAugust 3, 2018

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 intermediaries or middleman. Atomic swaps are also referred to as “trustless.” This means it’s not necessary for traders to trust one another in order to make a swap.

This post will dive deeper into what atomic swaps are, provide a brief history of atomic swaps, discuss the benefits of atomic swap trading, and describe how Komodo is pushing this crucial technology to the forefront of the blockchain industry.

atomic swaps

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 exchanges 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 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 funds they started with (minus a very small transaction fee for the “order-taker”). Atomic swaps make digital sset 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.

Step 0

Bob (the 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 take place before the atomic swap can begin.

Step 1: <DEXfee> payment

Alice (the Taker) sees Bob’s offer and accepts it. She commits to the trade by paying the atomic swap fee, which is only 0.15% of the total trade amount. The purpose of the atomic swap fee is to make sure Alice (and other users) don’t spam the network with rapid requests. Note that Bob does not have to pay any transaction fees for the trade; Makers do not pay trading fees on AtomicDEX.

Once Alice has paid the fee, the atomic swap has officially begun. Bob receives the fee payment and initiates the second step of the swap.

Step 2: <MakerPayment> Is Sent

Bob sends his payment, known as the Maker Payment, which in this case is the BTC that Bob wants to swap for Alice's KMD. The Maker Payment is sent to an P2SH address that is hashlocked, timelocked, and a 2-of-2 multi-sig address that requires approval from both Bob and Alice before a transaction can be made.

The Maker Payment is locked with the hash of a 32 byte secret code that Bob randomly generates. Only Bob knows the secret but the hash of the secret is public. This means that Alice 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 10 minute block times, in this example the lock time would be considerably longer than normal--  a full 43 hours. In most cases, the lock time is set at 7800 seconds, which is just over 2 hours. If the timelock expires, then the funds are automatically sent back to Bob's wallet.

Step 3: <TakerPayment> Is Sent

Alice sees that Bob has sent the hashlocked and timelocked Maker Payment. At that point, she sends her KMD-- the Taker Payment-- to a second secure P2SH address. 

Significantly, the Taker Payment is hashlocked with the same hash that Bob used to lock the Maker Payment. Alice can use the hash to lock the funds, despite the fact that she still does not know the secret code that Bob 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. In the current example, the Taker Payment would be timelocked at 78000 seconds, which is roughly 21.5 hours. If Bob has not spent the Taker Payment within this allotted time frame, then the funds would automatically be sent back to Alice's wallet.

Step 4: <TakerPayment> Is Spent

Bob sees that Alice has sent the Taker Payment, which is locked by the hash of the 32 byte secret code he generated in Step 2. The Taker Payment is the KMD that Bob wants to collect. So, Bob reveals the 32 byte secret code, broadcasting it to the blockchain network in order to send the KMD to his personal wallet. Once this transaction has been initiated, the secret code is publicly visible.

Step 5: <MakerPayment> Is Spent

Alice sees that Bob 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-- that is, the BTC that Bob sent in Step 2-- and sends the funds to her personal wallet.

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 trusted third-parties, 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:

Download AtomicDEX for Android on Google Play here.

Download AtomicDEX for iOS through Test Flight here.

If you’d like to get all of the latest updates from Komodo, sign up for the weekly newsletter. You can also join our thriving community on Discord to chat with Komodo’s developers and other core team members.

Join us at the cutting edge of the blockchain industry.