Blockchains like Bitcoin, Ethereum, and others are decentralized systems to maintain crypto tokens. But when you own some BTC and want to buy some ETH with it, you do this through an exchange. The common exchanges in the market are Coinbase and Binance or if we look at Indian crypto exchanges, there are WazirX, CoinSwitch, CoinDCX, etc. The majority of these exchanges are centralized, maintained by individual companies. Many are custodial wallets, owning your funds, and are vulnerable to security attacks. These are limited by their centralized infrastructure and often unable to handle some heavy influx of user activities.
Most importantly, they defy the whole concept of introducing blockchain in the ecosystem in the first place, by bringing in elements of centralization. As per the government norms, in most countries they require government-issued provable identities of the users, making the reach of crypto difficult to each one. This also curtails the anonymization claims of blockchain, putting at risk user privacy.
Atomic swap is the way to decentralize the cross-chain exchange. It brings in the required trust in the process by using cryptographic constructs. You no longer need to send your tokens and trust any individual or any centralized service to exchange them or return them to you in case of failure. This is all possible using the technology behind atomic swaps. In the following section, let's review how this is done.
Let Alice and Bob be two parties who want to exchange their tokens. Alice has A BTCs and wants to exchange them with B ETHs owned by Bob. As the funds are on different blockchains, there can’t be a direct transaction. Now, in order to exchange funds, Alice and Bob exchange the addresses they want to receive funds on and an exchange rate. Alice initiates the transfer and sends her money to Bob. Bob claims the A BTCs on his Bitcoin address. Now it's his turn to send the ETHs to Alice, but he may choose not to. There is no contract binding them, and they merely trusted each other. The transactions are non-refundable, so Alice would not be able to do anything in this case.
Another way could be to use an exchange that supports the desired trading pair. This also exposes both users to significant risks.
This is a way to support cross-blockchain operability, secured by cryptography. The participants do not have to trust each other or any third party with their funds. It is supported by the use of smart contracts. When done, an atomic swap results in either successfully swapping the funds, or it fails with both participants keeping their original tokens. It either happens completely or none at all.
The atomic swaps are the peer-to-peer exchange of funds across blockchains achieved by the use of programmable escrows called Hash Time-Locked Contracts (HTLCs). These are hash and time-bound conditional payments and do not require any third party. There are two types of conditions enforced through these locks, which are defined using the scripting language:
When put together in a transaction using scripts, this contract ensures that the funds are transferred if a specific preimage is provided within a deadline, otherwise funds are refunded.
The typical scenario of an HTLC is as follows:
HTLCs are supported by blockchains that have the Segregated Witness (SegWit) capability, like Bitcoin, Ethereum, Litecoin, etc.
The first atomic swap was done between Decred coin and Litecoin on Sept 20, 2017. Both cryptocurrencies are forks of the Bitcoin blockchain. Since then, many decentralized exchanges have been set up using atomic swap technology to achieve cross-chain interoperability.
As the number of networks increases, there arises a need for operability between them to exchange tokens and data. Today, the DeFi sector is completely built on the exchange of different crypto tokens. Atomic swaps provide a secure and trustless way to easily trade using these applications. They use complex algorithms but provide a cryptographically secure method to trade between blockchains.