Refresh to view the latest content.

atomicdex
arrow
komodo-wallet

It's a new era!
We have officially changed our name from "AtomicDEX" to "Komodo Wallet"

Official Press Release

18 August 2022

Updated: 11 November 2022

Atomic Swaps: Trustless, P2P, and Cross-Chain Crypto Trading

Share:

twitter
Atomic Swaps: Trustless, P2P, and Cross-Chain Crypto Trading

Table of contents

Imagine being able to trade cryptocurrencies like BTC, ETH, BNB, and more across multiple blockchains on a P2P exchange without intermediaries. This is what atomic swaps already allow users to do.

At the forefront of atomic swap technology is AtomicDEX — an atomic-swap-based decentralized exchange that supports cross-chain/cross-protocol trading for dozens of blockchain protocols and is compatible with 99% of cryptocurrencies.

This post dives into how atomic swap technology works and how AtomicDEX is leading the way for blockchain and crypto innovation.

Key Takeaways

  • Atomic swaps are peer-to-peer crypto trades that don't require a third-party intermediary or centralized liquidity pool.
  • Atomic swaps provide a secure way to trade between various blockchain networks without the need for centralized bridges or wrapped tokens.
  • AtomicDEX is an atomic swap-powered exchange that offers the widest cross-chain/cross-protocol support among all DEXs. Supported cryptocurrencies include BTC, BNB and all BEP-20 tokens, ETH and all ERC-20 tokens, and numerous others across dozens of blockchain networks.

What Are Atomic Swaps?

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 (P2P) manner.

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 non-custodial 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.

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.

 first LTC-BTC atomic swap
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.

Self-Sovereignty

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

Secure Trade Execution

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.

Affordable Gas and Trading Fees

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!

Decentralized Cross-Chain Trading

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.

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

The Maker Payment's refund to Maker is 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 delay before Maker Payment is allowed to be refunded to the Maker is 15,600 seconds, which is 4 hours and 20 minutes.

If the time lock expires, and the Taker hasn't spent it yet (Step 5), the Maker is able to reclaim 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's refund back to Taker is also timelocked, but with a length of time that is half of that used to lock the Maker Payment.

This allows the Taker to reclaim their coins if the Maker doesn't proceed with the next step of the swap for any reason.  

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.

Atomic Swap Process

Here’s just one example of the nearly 350,000 successful atomic swaps that have been completed on AtomicDEX! Trade between blockchains protocols, including both UTXO and account-based networks, seamlessly with no proxy token required.

Successful atomic swap on AtomicDEX

AtomicDEX — Industry-Leading Atomic Swap Exchange

For years, Komodo's AtomicDEX has led the blockchain space with regard to atomic swaps and decentralized trading technologies. This is widely acknowledged throughout the entire industry.

For instance, Binance tweeted to thank Komodo for playing an "important role by experimenting with atomic swap protocols."

Start making your very first atomic coin swaps with the AtomicDEX wallet today.