Atomic swaps are a mechanism for enabling true P2P trading of cryptoassets directly through the blockchain. How do atomic swaps work and what is the future of this technology?
Atomic swaps are automatic contracts for exchanging cryptoassets between blockchains. The mechanism of smart contracts completely eliminates the need for centralized third-party organizations (exchanges or exchangers) when making transactions and frees the counterparty from risk. This is one of the few truly decentralized methods of conducting cryptoasset exchange transactions.
How atomic swaps work
“Atomic” (indivisible) is a term for processes that either run entirely or do not run at all. In other words, an atomic swap has functions that ensure that both parties to a transaction meet all predefined conditions to complete the exchange. The fulfillment of the conditions is guaranteed by smart contracts. If one of the parties refuses or technical problems, the entire operation is completely canceled.
Atomic swaps use hash-locked and time-bound smart contracts (HTLC). These contracts use a mechanism for creating and comparing data fingerprints – a hash function. In addition, the contract sets a time limit – transactions are canceled if at least one of the parties does not fulfill the terms of the transaction within a predetermined period.
For example, two parties can agree to set a two-hour time limit for an atomic swap. In this scenario, the contract will return the deposited coins to their original owners when two hours pass and not all of the terms of the transaction are met. The HTLC smart contract requires two cryptographic keys:
Hashlock key. This key ensures that the transaction is completed only when both parties provide cryptographic evidence that they have complied with the terms of the transaction.
Timelock key. It is a security mechanism that helps participants in a transaction set a deadline for an atomic swap. The mechanism ensures that the deposited coins will be returned to the participants in the transaction if the swap is not completed for one reason or another before the deadline expires.
How atomic swaps are executed
Let’s assume that User A and User B agree to do an atomic swap for BTC and ETH. User B wants to exchange his 1 BTC for 15 ETH of User A.
First, User B needs to create a contract address to which he will send 1 BTC. After he has made a transaction, the contract automatically generates a special key, to which only User B has access. This key unlocks the money that the user has sent to the smart contract.
The contract uses this key to create a hashed representation or encrypted form of the key. User B then sends the hash of the key to User A. Thus, User A has access only to the hashed form of the access code used to block 1 BTC of User B. This hash serves as a confirmation that he has locked the money in the contract, but User A is still cannot access them or take them out of the contract.
After receiving the hashed key, User A uses it to create his own contract address, to which he deposits his 15 ETH. After both parties have locked their money in the smart contract, User B can claim 15 ETH as he has access to a password that reveals the key that User A uses to lock the coins in the smart contract.
In the process of unlocking the address of the contract of User A, User B discloses his password to the second party to the transaction. User A uses it to receive 1 BTC and complete the transaction. In essence, the entire atomic swap process boils down to the ability of both parties to provide cryptographic evidence of their actions.
User B had to encrypt the key first and then send it to User A. Since he had the original key, he can claim the cryptoassets that User A locked in the smart contract with the encrypted key. However, as one of the conditions for unlocking cryptoassets, User B must provide User A with the original key. After that, User A can access the key and use it to receive 1 BTC.
Why atomic swaps are needed
Atomic swap is an important mechanism for blockchain interoperability as it eliminates the need for intermediaries, such as cryptocurrency exchanges, when dealing with cryptoassets.This allows traders to make cross-chain transactions without relying on the infrastructures of centralized trading platforms.
Since intermediaries are not involved in atomic swaps, transactions are faster, more accessible, and avoid security incidents associated with custodial exchanges. Users retain control over their cryptoassets as all transactions are performed from their personal wallets.
In addition, cross-chain trading through atomic swaps contributes to the creation of an interoperable cryptocurrency ecosystem. Atomic swaps make it easier to transact across multiple blockchains. In addition, atomic swaps eliminate counterparty risk – the transaction is completed either in full or not at all.
Development of atomic swaps
Atomic swaps were first described by developer Sergio Lerner in 2012. The community was interested in the idea, but some of the swap processes were not fleshed out. A year later, Tier Nolan developed a more robust atomic swap process. But only four years later, when the Decred team performed an atomic swap between Decred and Litecoin, the mechanism was successfully tested for the first time on a real blockchain. A few days later, Litecoin founder Charlie Lee tweeted that the process was successfully replicated for an LTC and BTC swap.
The original design of the atomic swap required both parties to a transaction to download to their device the full version of the blockchains of the cryptocurrencies they planned to exchange. A month after Decred and Litecoin conducted the first atomic swaps, the Komodo project developers presented a “lightweight version” of atomic swaps, requiring only the opening of special payment channels. At the same time, the participants in the transaction do not need to download the blockchains of cryptoassets or wait for the validators to confirm the transactions.
In 2017, the Zcash developers demonstrated a working version of the XCAT tool (cross-chain atomic trades – atomic transactions between blockchains), with which you can swap BTC to ZEC and vice versa. A few months later, the first atomic swap took place between Bitcoin Cash and Decred.
In 2019, the Qtum platform ran atomic swaps on the Bitcoin mainnet, and Blockstream launched atomic swaps on the Liquid sidechain. There have been attempts to create trading platforms with support for atomic swaps. In 2019, the Sparkswap decentralized exchange for Bitcoin was launched with support for Lightning and atomic swaps. However, a year later, the site closed due to a small number of users and low liquidity.
Perhaps the development of the DeFi industry, the general course of the community towards decentralization, as well as the support of atomic swaps by the largest blockchains will give an impetus to the development of technology in the coming years. However, as of 2021, atomic swaps are still a convenient and secure, but rare form of transaction, eliminating the need for counterparties and allowing users to retain control over their cryptoassets.