Skip to content

Atomic Swaps: Unveiling Trustless, Peer-to-Peer Cross-Chain Trading

Atomic swaps are presented as a decentralized and trustless solution for exchanging cryptocurrencies between different blockchains. This mechanism eliminates the need to rely on intermediaries or custodians, using smart contracts called Hash Time-Locked Contracts (HTLC) to facilitate automated and secure transactions.

In this article, we take a closer look at the technical aspects behind this technology, explaining how HTLC contracts work and their impact on exchanges.

What are atomic swaps?

In essence, atomic swaps allow cryptocurrencies to be exchanged between different blockchains without the need for intermediaries. To achieve this, they use smart contracts that ensure the atomicity of transactions. Technically speaking, atomicity ensures that a process is completed in its entirety or not executed at all, eliminating the possibility of an incomplete or failed transaction.

The decentralization inherent in atomic swaps ensures that users do not have to relinquish control of their private keys to centralized platforms such as exchanges, which greatly enhances security. Unlike these centralized platforms that hold users’ assets, atomic swaps are executed directly between users without relying on a third party to complete the transaction. This eliminates risks such as exchange hacking or platform insolvency.

Beginnings of the technology

Although the concept of atomic swaps has been around for years, its practical implementation is relatively recent. In 2013, Tier Nolan pioneered the description of a complete protocol for such swaps, but the first attempts at decentralized swaps date back to 2012, when Daniel Larimer laid the groundwork for the development of atomic swaps with the P2PTradeX protocol.

Since then, it took several years before we could witness one of the first major milestones in the implementation of atomic swaps, so it was not until 2017 that Decred and Litecoin performed a successful swap without the intervention of a third party or the use of fiat currencies. Subsequently, swaps were performed between Litecoin and Bitcoin, consolidating this technology as a pillar of exchange between blockchains.

Compatibility between blockchains

In order for a blockchain to support atomic swaps, it must meet a number of strict technical requirements, the most important of which are as follows

  • Compatibility with HTLC contracts: The participating blockchains must be able to execute HTLC contracts, which use hash functions and time limits to block and release funds under certain conditions.
  • Shared hash function: It is imperative that both blockchain networks use the same hash algorithm (e.g., SHA-256) to ensure that the same hash value can be verified on both chains.
  • Programmability of smart contracts: Atomic swaps rely on the ability of blockchains to execute complex smart contracts that govern the terms of the exchange.
  • Multisig transaction support: This ensures that funds are released only if all parties involved comply with the terms of the contract. This adds an extra layer of security to the exchange process.
  • Timelocks and fast completion: Timelocks ensure that transactions are completed or canceled within a specified timeframe, while blockchains with fast completion times are preferable for the efficient execution of swaps.

How do atomic swaps work?

As mentioned above, the operation of atomic swaps is a highly technical process that relies on HTLC smart contracts and the interaction between different blockchains. Below we describe the process:

Step 1 – Asset lock: First, each participant locks their cryptocurrencies to a multi-signature address controlled by an HTLC contract on the respective blockchains. This means that funds cannot move without the consent of both parties. The HTLC contract ensures that both parties are committed to the exchange without either party being able to divert the funds.

Step 2 – Creation of Hash Time-Locked Contracts (HTLC): HTLCs are smart contracts that use both hashlocks and timelocks to ensure that the terms of the exchange are met. This mechanism ensures that cryptocurrencies are only released if both parties submit the correct hash preimage within a specified time. The two main components of HTLC are:

  • Hashlock: Locks funds until a secret key is revealed that corresponds to the hash stored in the contract.
  • Timelock: Sets a time limit within which the secret key must be presented. If the deadline is not met, the funds are automatically returned to their owners.

Step 3 – Blockchain verification: Each participant verifies that the counterparty’s funds are correctly locked in their respective blockchains by reviewing the blocks. This step is essential, as it ensures that the cryptocurrencies are secure and cannot be moved without the required secrecy.

Step 4 – Exchange of secrets: Each party exchanges their cryptographic key, which allows them to unlock the funds locked in the HTLC contracts. This exchange ensures that funds can only be transferred if both parties comply with the terms of the contract.

Step 5 – Perform the atomic swap: When both parties reveal their secret keys, HTLCs are unlocked on both blockchains simultaneously. If either party defaults, the HTLC contract automatically reverses the transaction and returns the funds to their original owners. This mechanism protects both parties by ensuring that neither can keep the other’s funds without fulfilling the agreed terms.

This process ensures that exchanges are secure and reliable by eliminating the need for intermediaries and reducing counterparty risk. Each step is designed to ensure that both parties comply with the terms of the swap and that the transaction is completed efficiently and effectively.

Technical components required for atomic swaps

The success and security of atomic swaps depends on several key technical components that ensure their proper functioning. These elements allow transactions to be atomic, secure, and efficient:

  • Hash Time-Locked Contracts (HTLC): HTLCs are the linchpin of atomic swaps because they ensure that both parties provide a secret key within a specified time frame before funds are released. If this condition is not met, the transaction will be reversed, so HTLCs protect against the possibility of one party failing to live up to their end of the bargain, thus eliminating the risk of fraud.
  • Check-Lock-Time-Verify (CLTV): Introduced to Bitcoin by BIP-65 in 2015, CLTV is a mechanism that imposes time limits on transactions. In the context of atomic swaps, CLTV ensures that funds are returned to their owners if one of the parties fails to fulfill its part of the exchange within the agreed time frame. This prevents funds from being blocked indefinitely.
  • Check-Sequence-Verify (CSV): Similar to CLTV, CSV allows transactions to become valid only after a certain number of blocks or time has elapsed. In atomic swaps, this mechanism acts as an additional timer to ensure that transactions are automatically executed or canceled within the specified time.
  • Pay-to-Script-Hash (P2SH): This technique allows funds to be sent to a script-generated address instead of a standard address. In atomic swaps, P2SH allows for the creation of multi-signature or complex contracts where funds cannot be released until the conditions of the HTLC are met.
  • Hashlocks: Hashlocks are cryptographic mechanisms that block funds until a secret key or “pre-image” matching the stored hash is revealed.
  • Timelocks: Timelocks define a certain amount of time within which the transaction must take place. If the conditions are not met within that time, the funds are automatically returned to their owners.
  • Multisig transactions: Multisig transactions require more than one party to sign the transaction to authorize the release of funds. In atomic swaps, multisig addresses ensure that both parties must comply with the terms of the contract before funds can be moved.
  • Compatible hash algorithms: For atomic swaps to be effective, the participating blockchains must be compatible with the same hash algorithm. This compatibility is essential so that both blockchains can verify the hashes and ensure interoperability.

Types of atomic swaps

Atomic swaps can be classified into two main categories according to the layer at which the transactions are executed: on-chain and off-chain. Each type has particular characteristics, advantages and disadvantages.

On-chain swaps

On-chain swaps take place directly within the blockchains of the cryptocurrencies involved. In this type of swap, transactions are recorded and verified on the main blockchain, ensuring high security and transparency. However, due to the nature of blockchains, these swaps can be slower and more costly, especially in congested networks such as Bitcoin or Ethereum.

Below, we mention the most relevant wallets that allow fully decentralized on-chain swaps:

  • AtomicDEX (new Komodo Wallet): AtomicDEX is a decentralized platform developed by Komodo that enables atomic swaps without the need for intermediaries. The platform enables direct swaps between multiple blockchains through integrated HTLC contracts.
  • Liquality Wallet: This wallet supports atomic swaps between cryptocurrencies such as Bitcoin, Ethereum and Polygon. Liquality allows swaps to be executed directly between cryptocurrencies using HTLC contracts without the need for additional platforms.
  • Decred Wallet: Although mainly focused on swaps between Decred and other cryptocurrencies, it also allows atomic swaps with other currencies. However, for swaps between assets such as bitcoin and litecoin, specialized wallets such as AtomicDEX and Liquality may be more suitable.

Off-chain swaps

Off-chain swaps take place outside the main blockchain, usually using second-layer solutions. These swaps allow for faster and cheaper transactions by avoiding the need to record each transaction on the main blockchain. An example of an off-chain swap would be the use of a Lightning Network channel between two users, where assets are exchanged off-chain and only recorded when the channel is closed.

The Atomic Wallet, on the other hand, is a wallet that focuses primarily on off-chain swaps and facilitates access to different cryptocurrencies through a single interface.

Each type of atomic swap offers advantages depending on the context of use, and the choice between on-chain and off-chain depends on factors such as the need for speed, security, and the characteristics of the blockchains involved.

Conclusion

Atomic swaps represent a breakthrough in the cryptocurrency arena, providing a secure, reliable, and decentralized method of exchanging digital assets. By eliminating reliance on centralized exchanges, they allow users to maintain full control over their funds, which is in line with the fundamental principles of blockchain technology.

Although still an underutilized technology, its potential is beginning to be recognized with the growing development of decentralized exchanges. These platforms eschew liquidity pools and use decentralized order books to facilitate trading, highlighting the relevance and value of this technology today.

Next week, we will publish the second part of this article, in which we will compare atomic swaps to cross-chain bridges, concluding our exploration of this fascinating technology.


Want to learn more about decentralization? Don’t miss these resources!


At Block&Capital, we strive to create an environment where growth and success are accessible to all. If you’re ready to take your career to the next level, we encourage you to join us.