Latest

What are atomic swaps and how are they used

Atomic swaps – a mechanism for enabling true P2P trading of crypto-assets directly via blockchain. How do atomic swaps work and what is the future of this technology?

Atomic swaps are automatic contracts for exchanging crypto-assets between blockchains. The mechanism of smart contracts completely eliminates the need for centralized third parties (exchanges or exchangers) in transactions and relieves the risk of the counterparty. It is one of the few truly decentralized methods of exchanging cryptoassets.

How atomic swaps work

“Atomic” (indivisible) is the term for processes that are either executed entirely or not at all. In other words, an atomic swap has features that ensure that both sides of the transaction meet all predetermined conditions to complete the exchange. Fulfillment of conditions is guaranteed by smart contracts. If one of the parties fails or there are technical problems, the entire transaction is cancelled.

Atomic swaps use smart contracts with hash locking and timestamping (HTLC). These contracts use a mechanism for creating and comparing data fingerprints – the hash function. In addition, the contract sets a time limit – transactions are canceled if at least one of the parties has not fulfilled the terms of the transaction within a predetermined period.

For example, two parties may 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 have passed and not all the conditions of the deal have been 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. This is a security mechanism that helps participants in a transaction set a deadline for committing an atomic swap. The mechanism ensures that deposited coins will be returned to the deal participants if the swap is not completed for one reason or another before the deadline.

How atomic swaps are executed

Suppose User A and User B agree to make 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 where he will send 1 BTC. After he performs the transaction, the contract automatically generates a special key, which only User B has access to.. This key unlocks the money that the user sent to the smart contract.

The contract uses this key to create a hashed representation or encrypted form of the key. Then User B sends the key hash 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 is to confirm that User B has locked the money in the contract, but User A cannot access it or withdraw it from the contract yet.

Once User A receives the hashed key, he 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 because he has access to the password that reveals the key that User A used to lock the coins in the smart contract.

In the process of unlocking User A’s contract address, User B reveals his password to the other party to the transaction. User A uses it to get 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 first encrypt the key and then send it to User A. Because he had the original key, he can demand the crypto-assets that User A locked into the smart contract with the encrypted key. However, as one of the conditions for unlocking crypto-assets, User B must provide User A with the original key. After that, User A can access the key and use it to get 1 BTC.

Why we need atomic swaps

Atomic swaps are an important blockchain mechanism because they eliminate the need for intermediaries, such as cryptocurrency exchanges, in transactions involving cryptoassets, enabling traders to make cross-network transactions without relying on the infrastructure of centralized trading platforms.

Because intermediaries are not involved in atomic swaps, transactions are faster, more accessible, and eliminate the security incidents associated with custodial-based exchanges. Users retain control over their crypto-assets, as all transactions are made from their personal wallets.

Additionally, cross-network trading via atomic swaps promotes an interoperable cryptocurrency ecosystem. Atomic swaps make it easier to conduct transactions across multiple blockchains. In addition, atomic swaps eliminate counterparty risk – the transaction is either done in full or not done at all.

The development of atomic swaps

The atomic swap mechanism was first described by developer Sergio Lerner in 2012. The idea interested the community, but some swap processes were not specified. A year later, Tier Nolan developed a more robust atomic swap procedure. But it wasn’t until four years later, when the Decred team made an
atomic swap between Decred and Litecoin, the mechanism was first successfully tested in a real blockchain. A few days later, Litecoin founder Charlie Lee tweeted that the process was successfully replicated for the LTC and BTC swap.

The original design of the atomic swap required both sides of the transaction to upload to their device the full version of the cryptocurrency blockchain they planned to exchange. A month after Decred and Litecoin conducted their first atomic swaps, the developers of the Komodo project introduced a “light version” of atomic swaps, requiring only the opening of special payment channels. Transaction participants do not need to upload crypto-asset blockchains or wait for validators to validate transactions.

In 2017, Zcash developers demonstrated a
a working version of the XCAT (cross-chain atomic trades) tool, which can be used to swap BTC to ZEC and back. A few months later, the first atomic swap
the first atomic swap between Bitcoin Cash and Decred.

In 2019, the Qtum platform ran
atomic swaps on the main Bitcoin network, and Blockstream launched
atomic swaps on the Liquid sidechain.. There have also been attempts to create trading floors that support atomic swaps. In 2019, a
Sparkswap decentralized exchange for bitcoin with Lightning and atomic swaps support. A year later, however, the site closed down
due to low user numbers and low liquidity.

Perhaps the development of the DeFi industry, the general course of the community towards decentralization, and the support of atomic swaps by major blockchains will give a boost to the technology in the coming years. As of 2021, however, 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 crypto assets.