In this article, I will try to explain the concept and technologies behind Atomic Swaps carried out on the blockchain.
In a nutshell, Atomics Swaps present a way to trade cryptocurrency directly between two sides, without a mediator, which is usually a centralized 3rd party exchange.
Furthermore, you also need a way to make sure this trade will be safe – meaning both parties agreed on the terms of the deal and will keep their end of the bargain.
But you don’t always know the person you’re trading with so how do you make sure they won’t take your Bitcoins and give you nothing in return?
The most common solution for this problem is an exchange, which acts as a “trusted” third party.
So, How Do Crypto Exchanges Work?
When you open an account on an exchange, it will create a wallet for you, and you will buy or move some cryptocurrency to your wallet on the exchange so you can trade with it.
This is called a Custodial wallet because while you’re the owner of the wallet, the exchange is holding the private key for you, all the transactions will be done on the exchange alone, and you don’t have direct access to your wallet or currencies.
Think of a Custodial wallet as a safety deposit box you rent in the bank. You decide what to keep in your deposit box and when to move whatever you keep there, but you can’t access it yourself, you need your bank for that.
So, what’s wrong with this solution? The practice of needing a custodian to access your own funds has been used for years, why does this need updating?
The main problem is that this is a Centralized Exchange meaning there is one entity that keeps all the access keys which makes it very venerable to attacks. And unlike a safe deposit box, your money is not insured against loss.
Another issue is the fee that the exchange charges users to transfer cryptocurrency. If you think about it, it’s kind of funny that a concept based on removing middleman (The Blockchain) relies on a middleman (exchange) to operate.
We realize centralized exchanges don’t execute Satoshi’s vision.
Is there another way? If I don’t have a middleman how can I trust the person I want to trade with?
Well… you don’t have to! You can use Atomic Swaps to ensure both parties will hold up to their end of the deal.
How Do Atomic Swaps Work?
Let’s use the safe deposit box example again.
Say I want to trade some US Dollars with you in return for some Euros, but we just met and have not established trust yet, this is what we do:
We take our two special strongboxes that can communicate with each other, one box has a key hidden in a small locked chamber, that’s my box, and I’ll put my dollars in it and give it to you.
We’ll call this box “the Dollars box.” You cannot take my money from the Dollars box, of course, because you don’t have the key.
Now you take your strongbox and put your Euros inside, lock it, and give it to me. We’ll call this box “the Euros box.” I cannot take your money from the Euros box because I don’t have the key.
So, we both have each other’s box, both are locked, and we can’t access the money. As soon as you’re ready to execute the trade you send me the key for the Euros’ box so I can now open it and take your Euros. Simultaneously, you are granted access to the Dollars in the Dollars’ box.
The boxes communicate and once the agreed upon deal terms are matched, the Dollars box unlocks the chamber that holds the key, and you can now access my Dollars. Both boxes unlock to execute the trade.
The special boxes can also magically return my Dollars to me and your Euros to you if we both didn’t open the box in an agreed upon timeframe.
Let’s see how this works on the actual blockchain.
First, we need to clear up a couple of basic terms that we’re going to use:
- Hash function – a hash function will create a unique output and its impossible to know the input if you only have the output. For example, if my input is “123456” and my hash function gave me an output of “654321,” there is no other number that would output “654321,” and there is no way to reverse the function and to know that I used “123456” as an input.
- Time-lock contract – a Time-lock contract is a smart contract that will expire after the time set for it is over.
Let’s take our example to the cryptocurrency world and say I want to sell you 1 BTC for 3 ETH’s, this is how we’ll do it using Atomic Swaps:
- I deposit my BTC to a smart contract on the Bitcoin chain and lock it with my key “123456” and give you the hash which is “654321.”
- You take your ETH’s and deposit them to a smart contract on the Ethereum chain locking it with “654321”, but your contract will only accept the original input “123456” to hash into “654321” and use as a key.
- When I unlock your contract using “123456”, you now know my key as it is public on Ethereum and you can use it to unlock my contract on Bitcoin.
Both contracts are time-lock contracts, meaning if the transaction does not execute within the time frame, i.e., 24 hours, the coins will revert to the owner. Thus, true P2P trading is born.