Off Chain Explained

Atomic swaps and HTLC

True peer-to-peer trading

An atomic swap is a type of trade that allows for the exchange of one cryptocurrency for another without the need for a centralized intermediary or third party. The trade is called “atomic” because it is indivisible, meaning that either all of the steps in the swap occur or none of them do. This ensures that both parties in the trade receive their expected funds without the risk of one party defaulting on the trade. 

Atomic swaps are typically realized by Hashed Time-Lock Contracts (HTCLs), a type of contract used in cryptocurrency transactions to ensure that a payment is made only if specific conditions are met. HTCLs combines two main concepts - Hashlocks and Timelocks.

  • Hashlocks: A hashlock is a cryptographic condition that requires the recipient to provide a specific piece of data known as the “preimage” (a secret) to unlock the funds in the contract. 
  • Timelocks: A timelock is a condition that restricts the contract based on time. It specifies that if the preimage is not provided within a certain time frame, the contract will expire, and the funds in the contract can be refunded to the sender.

Together, these two mechanisms ensure that the exchange of assets between two parties is secure and trustless, meaning that neither party needs to rely on the other’s goodwill or a third party to complete the transaction. To see how these concepts work in practice, let’s consider an example of a cross-chain atomic swap involving Alice and Bob. Alice wants to exchange her Bitcoin (BTC) for Bob’s Litecoin (LTC). By using HTLCs on both the Bitcoin and Litecoin blockchains, they can safely execute this swap without the need for a centralized exchange. Here’s how it unfolds:

  1. Alice creates a preimage (secret) consisting of a random number and computes its hash.
  2. Alice creates an HTLC on the Bitcoin blockchain in which she locks her BTC. Bob can claim the BTC in the contract if he provides the preimage that matches the hash within a specific time frame. If he doesn't provide the preimage within that time, Alice can reclaim her BTC.
  3. Bob creates a similar HTLC on the Litecoin blockchain after seeing Alice’s HTLC. When setting up the HTLC, Bob uses the same hash that Alice is using in her HTLC on the Bitcoin blockchain. 
  4. Alice claims the LTC from Bob’s HTLC by using the preimage. By doing so, she publicly reveals the preimage to the Litecoin blockchain.
  5. Bob sees the preimage that Alice revealed on the Litecoin blockchain and uses it to claim the BTC from Alice’s HTLC on the Bitcoin blockchain.
  6. The swap is completed successfully. Alice now has Bob’s LTC, and Bob has Alice’s BTC. If either party had failed to fulfill their part of the contract within the specified time frame, the timelock would have ensured that the funds locked in both Alice’s and Bob’s HTLC were returned to their original owners.

While atomic swaps are a powerful tool for trustless exchange of cryptocurrencies, they are most commonly associated with cross-chain swaps. Atomic swaps within the same blockchain are less frequent, as modern Automated Market Makers (AMMs) like Uniswap handle intra-chain swaps more efficiently. The multiple steps involved in atomic swaps - such as coordinating hashlocks, timelocks, setting up contracts on the blockchain(s), and revealing preimages - can lead to higher transaction fees and longer processing times, making on-chain atomic swaps relatively rare. This complexity is what projects like Hydranet aim to overcome by developing off-chain solutions that streamline atomic swaps. Hydranet's approach of using off-chain protocols seeks to retain the security and trustless nature of atomic swaps while enhancing speed and efficiency, providing a more practical solution for decentralized exchanges.


Copyright © 2024