Atomic Swaps Explained: The Ultimate Beginner's Guide

Atomic Swaps Explained: The Ultimate Beginner's Guide
Table of Contents
Table of Contents

An atomic swap is a trade of cryptocurrency made directly from one user to another, without any intermediary to facilitate the transaction. These swaps are 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 made wallet-to-wallet, in a fully peer-to-peer manner.

This post will dive deeper into what atomic swaps are, explain how the technology works, and provide a brief history of atomic swap development. We'll also explore the benefits of atomic swap trading and discuss 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 like a centralized cryptocurrency exchange.

It’s important to emphasize that both traders hold their Bitcoin private key throughout the entire atomic swap process. As a result, atomic swap trading is magnitudes more secure than trading assets on a centralized exchange.

Centralized 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. 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 at all times. 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 a wallet that's never sent or received a transaction (a “cold” wallet). However, this method of storage does not allow any form of trading-- once you send coins from a particular address, it is no longer considered "cold." 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 answering what is an atomic swap, 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 the 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 swapping. 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 Litecoin to Bitcoin atomic swap brought a great deal of public attention. Litecoin atomic swaps sparked new momentum around this 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 July 2019, after years of hard work, Komodo released a third-generation, atomic swap-powered decentralized exchange called AtomicDEX for mobile. In April 2020, support was expanded for desktop.

BarterDEX was an early GUI for atomic swaps - Source:

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 codebase 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 first LTC-BTC atomic swap required the use of a CLI - Source: Exchange Union YouTube

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.

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.

AtomicDEX supports atomic swaps from desktop, mobile, and CLI.

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

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 Industry-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.

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.

Great! Next, complete checkout for full access to Komodo Platform Blog | En
Welcome back! You've successfully signed in
You've successfully subscribed to Komodo Platform Blog | En
Success! Your account is fully activated, you now have access to all content
Success! Your billing info has been updated
Your billing was not updated