Atomic Swaps Simply Explained: How to Swap Cryptocurrencies without a Middleman

If you want to buy or sell cryptocurrency, currently you have only one good option: to use a centralized exchange.

For that reason we made HodlBot, an algo bot that automatically diversifies into the top 20 cryptocurrencies, compatible with exchanges like Binance.

But certainly there are better ways to exchange cryptocurrencies. Centralized exchanges have some big problems. For starters, just in the past couple months we’ve lived through two exchange hacks where hackers made off with $500MM worth of NEM coins and $170MM of NANO. And it’s not just hacks either. Exchanges charge high withdrawal fees and trade fees, costing crypto traders a lot of money.

The great irony is that cryptocurrency gave us a way to transact money peer-to-peer, but we still have to go through a third party to exchange them.

Thinkers in crypto have been working hard on solving this. Among their ingenious solutions, one of the most promising is called an Atomic Swap.

What is an Atomic Swap?

An Atomic Swap is a peer-to-peer swap between different cryptocurrencies without a middleman. The word “atomic” is a fancy computer science term meaning they either happen completely or not at all.

The surprising thing is that atomicity allows people to swap without trusting each other. Let’s explore how it works with a real-life story.

Exchanging Gold Coins with Dollar Bills

Bob’s grandfather passes away and he inherits a small pouch of 24-carat gold coins. He checks online and finds out they could be worth up to $30,000 USD. He decides to sell them.

In a first attempt, he brings them to a dingy pawn shop. Very predictably, the owner quotes him a horrible price which would leave many thousands of dollars on the table.

Oh Rick

After some deliberation, he figures it’d be much better to sell to another person directly, instead of through a shop or a broker.

He clicks around in an online forum for a while and comes into contact with a woman named Alice, who wants to buy his coins for $27,000 in cash. Score! But after some back and forth something becomes obvious: neither of them trusts the other enough to mail over their share first.

So what do they do? They use a recent invention in this fictional world called a Super Safe Swap. These utilize something called a Super Safe, a safe that looks like a microwave oven. Each has a door, a window and a button that opens the door, and two of them are connected together to do a Super Safe Swap.

How a Super Safe Swap works

Alice and Bob both take their Super Safe out of their home and move it out onto the street.

Warning: Super Safes do not have the ability to heat up food.

They connect them together. From this point on their doors will only open if the button on both safes is pressed at the same time.

Alice and Bob put their items (cash and coins respectively) in their safes and close it. They share their location, then find their way to the other’s safe. Upon arrival, they peek through the safe window and check whether it contains the right items. If they’re happy with the contents, they press the button.

If they both press the button indicating they’re okay with the swap, both safe doors pop open. Alice can take Bob’s gold coins, and Bob can take Alice’s cash, and they both walk away happy.

But if either Alice or Bob doesn’t want to go through with the swap anymore, they don’t press the button. Neither Safe pops open, and neither person gets the other’s items.

Nobody can steal because the swap either happens completely or not at all. With the Super Safe they’re able to make a trade without a middleman and without having to trust each other.

Atomic Swaps, Explained

Imagine Bob has 0.05 Bitcoin that he wants to swap with Alice for 1 Ether. Let’s how we would use an Atomic Swap to do this.

Thinking back to the Super Safe Swap, we need a container to put our money in and lock it. For this, we use a special type of smart contract called a hashlock.

Alice makes a hashlock

A hashlock is a smart contract that locks coins until a certain “secret passcode” is revealed.

Alice makes one using the following steps:

  1. Pick a big, random number called the preimage. This is the “secret passcode”.
  2. Use this preimage to calculate another number, called a hash.
  3. Create a smart contract to send Bob 1 Ether, locked with this hash. The coins are only released if Bob can show the preimage that maps to the hash.

Calculating the hash from the preimage in step 2 is easy, but calculating the preimage given the hash is (nearly) impossible. This means that Bob can’t unlock the coins unless Alice gives him the preimage.

This is analogous to locking cash in the Super Safe.

Initial transaction

Bob reciprocates

Bob checks to see whether Alice has sent a hashlock contract for 1 Ether. He can verify this by looking at the public blockchain. Thereafter Bob sends Alice a hashlock contract for 0.05 Bitcoin that is locked by the same hash as Alice used. This locks the two transactions together, analogous to the linked safes in a Super Safe Swap.

Reciprocal transaction

Unlocking the coins

When Alice wants to unlock the Bitcoin that Bob sent her, she’ll need to use the preimage (secret passcode) to unlock it.

But when she does that, the preimage goes onto the blockchain and becomes public information. Bob can then use it to unlock Alice’s 1 Ether, because the same hash was used to lock both contracts.

So you can see that just like in the Super Safe Swap, this swap happens atomically. Either the trade executes successfully for both parties, or it doesn’t go through at all.

But there’s a big problem we didn’t consider: what if Alice never shows the preimage to unlock Bob’s Bitcoins? Then both contracts would remain forever locked.

Refunding a failed swap

This problem happens because we have no way to roll back the swap in case of a failure.

So let’s add one: If after 24 hours Alice hasn’t shown the preimage, then refund both contracts. This is called the timelock.

Hashlocked transactions with timelock

Now, if Alice doesn’t show the preimage in 24 hours, Bob can get back his 0.05 Bitcoin, and Alice her 1 Ether.

Sounds good, right? But there’s still a way that Alice can cause jeopardy.

A longer timelock for Alice

Let’s say Alice waits 23 hours and 55 minutes until she uses the preimage to unlock Bob’s contract. Then Bob only has 5 minutes to unlock Alice’s contract. If internet connectivity (or other) issues prevent Bob from doing this, then Alice’s contract is refunded. Alice has stolen Bob’s Bitcoins.

The solution? Double the refund delay for Alice’s contract to 48 hours. Now Bob still has 24 hours to unlock if Alice waits for 24 hours.

The final product

And there you have it: A fully functional Atomic Swap!

Drawbacks

Atomic Swaps have great potential, but there are issues:

  1. They are slow (unless you use something like Lightning Network).
  2. They aren’t yet supported in major wallets or exchanges.
  3. They won’t work for cryptocurrencies that don’t have smart contract support.

However, these issues will be solved with time. As they do, exchanging crypto through an intermediary will become a thing of the past.

The Future

Atomic Swaps are just part of the solution for the exchange problem. We also need Decentralized Exchanges: exchanges not owned by a single person but by a network, thereby preventing single points of failure.

As a company, our mission is to make crypto portfolio management easy for everyone. For that reason we currently only support popular exchanges, because that’s where most people are.

However, we believe decentralized exchange tech such as Atomic Swaps is going to be increasingly important in the shift to a decentralized world.

Topics of interest

More Related Stories