Atomic Swaps Enabling Token Trades Across Blockchains
|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.
A Direct Way to Exchange Your Tokens
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:
- Hash Lock- a hash lock prevents spending locked funds until the preimage for a specified hash which is used to put a lock on the funds is provided by the claiming party.
- Time Lock- a time lock prevents spending funds until a certain time.
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:
- Alice and Bob exchange information and agree on token amounts to be exchanged and their respective addresses they want to receive funds on. This interaction is done off-chain, generally using a side-channel.
- Alice puts her BTCs in an HTLC on the bitcoin blockchain. She puts a lock on it using a secret phase and Bob’s public key, which is a hash lock. The use of Bob’s public key ensures that it can only be claimed by Bob. She also sets an expiry (ex. 6 blocks).
- Bob also puts his ETHs in an HTLC on Ethereum blockchain using Alice’s public key and the hash value of the same secret used by Alice. A shorter deadline for Alice is set here to claim ETHs.
- To claim ETHs, Alice reveals the preimage (secret phrase) to Bob. Using this disclosed secret, Bob can now unlock his BTCs.
- Bob enters the correct secret phrase, unlocking and claiming the funds within the expiry period.
- In case, Bob does not enter the correct secret phase, and/or does not claim the funds within the expiry, the BTCs become available for Alice to claim a refund.
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.
Exchanges Using Atomic Swaps
- Atomex: it is a multi-currency crypto wallet with a built-in atomic swap decentralized exchange. It supports trading between bitcoin, ethereum, tezos, litecoin, tether, tzBTC, tBTC and wBTC. During the entire duration of exchanges, the keys are encrypted and saved on the users’ devices.
- Liquality: this one is a ConsenSys-incubated startup. It is a multi-crypto wallet that also supports atomic swaps. It provides one-click atomic swaps. There is no need to trust a custodian or any counterparty. Users can swap cryptocurrencies using their Liquality, Metamask, or Ledger wallet. Compatible blockchains are bitcoin, ethereum, RSK, and NEAR. Some dApps also use Liquality to facilitate cross-chain swaps, ex. Sovryn, Uniswap and Maker.
- DogeDEX: it is a decentralized exchange from the Komodo platform used for P2P trading of Dogecoin. Atomic swaps of dogecoins with bitcoins, litecoins, ethereum, and many other cryptocurrencies can be carried out. It is also compatible with Binance smart chain.
- Bitcoin<>Monero Atomic Swap: Monero is building a technology to be used for exchanging it with other major cryptocurrencies, like BTC. This is implemented in Monero ecosystem by a project called Farcaster and developed by COMIT team. The HTLC smart contracts used in other exchanges cannot be used in Monero because of incompatibility. It uses Adapter signatures which will be compatible with the bitcoin Taproot upgrade.
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.