A Beginner’s Guide to Atomic Swaps
Atomic swaps are often regarded as being one of the few truly peer-to-peer methods for trading crypto tokens.
Atomic swaps are automatic exchange contracts that allow two parties to trade tokens from two different blockchains. Sometimes referred to as atomic cross-chain trading, this type of mechanism completely eliminates the need for centralized third-party entities when executing trades. In a way, this system preserves the autonomy of crypto users and enables trustless transactions in which users do not need to know one another and are void of counterparty risks.
Due to the trustless, peer-to-peer nature of atomic swaps, it is widely considered one of the few truly decentralized trading techniques.
How do atomic swaps work?
“Atomic” is a terminology used to connote processes that would either finalize or would not initiate at all. In other words, an atomic swap comes with functionalities that ensure that two sides of trade fulfill all predefined conditions before the trade can be completed. This is made possible by incorporating smart contracts, which are self-initiating programs that enforce the conditions governing the success of a transaction.
To be more specific, an atomic swap uses a Hashed Timelock Contract (HTLC), which functions as a two-way virtual safe. As its name implies, this contract utilizes a sophisticated mathematical-based encryption mechanism called a hash function. Also, it introduces a time constraint such that transactions are reversed when either of the parties involved does not fulfill their sides of the bargain within a predefined time frame.
For example, the two parties involved may agree to set a two-hour time constraint for the atomic swap. In this scenario, the contract will return the deposited coins to their original owners when 2 hours elapses and not all of the trading conditions have been met.
Another important detail you need to know about the HTLC is that it requires two cryptography or encrypted keys. They are:
- Hashlock key: This key ensures that trades are only finalized when both parties submit cryptographic proofs (more on this later) that they have fulfilled their sides of the transaction.
- Timelock key: This is designed as a safety mechanism that helps traders set a deadline for atomic swaps. The mechanism ensures that deposited coins are returned to traders when the swap is not completed for one reason or the other before the deadline elapses.
How are they executed?
To best explain the procedure for initiating atomic swaps, let’s assume Bob and Alice have agreed to execute a trade involving bitcoin and ethereum. Here, Bob has decided to trade 1 BTC in exchange for Alice’s 15 ETH. The first thing Bob needs to do is create a contract address where he will send his 1 BTC. Once he’s deposited his funds, the contract automatically generates a special key that only Bob can access. Think of this key as the password that unlocks the funds Bob just sent to the smart contract.
The contract uses this key to generate a hashed representation or an encrypted form of the key. Next, Bob sends the hash to Alice. By doing so, Alice only has access to the hashed form of the passcode used to lock Bob’s 1 BTC. In essence, she can confirm that he has locked the funds in the contract, but she cannot access or withdraw the funds – at least not yet.
After receiving the hashed key, Alice uses the key to generate a contract address of her own, where she can deposit her 15 ETH. Since both parties have locked their funds on the smart contract, all that is left is for Bob to do is claim the 15 ETH. He can do this because he has access to the passcode that unlocks the key used by Alice to lock her coins on the smart contract. Interestingly, in the process of unlocking Alice’s contract address, Bob will also reveal the passcode to Alice. As such, Alice can use this passcode to claim the 1 BTC and finalize the trade.
In the end, you will notice this process all boils down to the capability of both parties to submit cryptographic proofs. Here, cryptography connotes the process of encrypting and decrypting keys. Note that Bob had to initially encrypt a key then send the encrypted key to Alice. Since he possessed the original key, he can claim the coins that Alice used the encrypted key to lock. As one of the conditions for unlocking such coins though, Bob has to submit the original key to Alice. By doing so, Alice can then access the key and use it to claim the 1 BTC.
Why are atomic swaps needed?
Atomic swap is considered a critical blockchain mechanism because it eliminates the need for intermediaries such as crypto exchanges. With this, traders can execute cross-chain trades without relying on the infrastructures of centralized trading platforms. Since intermediaries are sidelined while using atomic swaps, the transactions are fast, more affordable, and void of security incidents associated with custodial-based exchanges. All these benefits allude to the autonomy that atomic swap provides. In other words, users have more control over their assets since all trades are executed directly from their personal wallets.
Furthermore, the cross-chain trading mechanisms of atomic swaps promote a more interoperable crypto ecosystem. Thanks to atomic swaps, it is becoming easier to conduct transactions across multiple blockchains. Lastly, atomic swaps eliminate counterparty risk since trades are either completed or never happen.
The history of atomic swaps
The techniques involved were first described by Sergio Demian Lerner in 2012. Although the idea was appealing at the time, certain processes had not been fleshed out. A year later, Tier Nolan developed a more robust procedure for initiating atomic swaps. But it wasn’t until four years later when the Decred team completed an atomic swap between Decred and Litecoin that it was first successfully implemented. A few days later, Charlie Lee, the founder of Litecoin, tweeted the process had been successfully replicated for a litecoin-to-bitcoin trade.
Note that the original design of the atomic swap required both parties involved in the transaction to download the entire blockchains of the coins they planned to trade. This is what we call an on-chain atomic swap. A month after Decred and Charlie Lee executed the first sets of atomic swaps, Komodo introduced a “light mode” version that only requires special payment channels, which are offshoots of blockchains. With this, traders do not have to download the entirety of the blockchains of their preferred coins or wait on blockchain validators to finalize transactions.
The leader in news and information on cryptocurrency, digital assets and the future of money, CoinDesk is a media outlet that strives for the highest journalistic standards and abides by a strict set of editorial policies. CoinDesk is an independent operating subsidiary of Digital Currency Group, which invests in cryptocurrencies and blockchain startups. As part of their compensation, certain CoinDesk employees, including editorial employees, may receive exposure to DCG equity in the form of stock appreciation rights, which vest over a multi-year period. CoinDesk journalists are not allowed to purchase stock outright in DCG.
Learn more about Consensus 2023, CoinDesk’s longest-running and most influential event that brings together all sides of crypto, blockchain and Web3. Head to consensus.coindesk.com to register and buy your pass now.