Genesis Block
Home » Crypto Classroom Lesson #10: Atomic Swaps

Crypto Classroom Lesson #10: Atomic Swaps

What are Atomic Swaps?

Atomic Swaps are the peer-to-peer exchange of two different cryptocurrencies. They can be executed on off-chain channels or separate blockchains with different native coins. Currently, there are 10 cryptocurrencies that support atomic swaps including BTC, BCH, ETH, LTC, etc.

Why Atomic Swaps?

One might wonder why we should use Atomic Swaps when there are numerous exchanges providing crypto-to-crypto pairs. The main reason is to mitigate counter-party risk with respect to centralized exchanges. There have been many cases of funds lost due to fraud, mismanagement and cyber attacks. Notable examples include Mt. Gox, Coincheck and Zaif. Besides the crypto assets, user personal data is also at risk.

Atomic Swaps provide a solution for users to exchange cryptocurrencies without having to trust a third-party.

 

How does it work?


Suppose Alice has 10 BTC and Bob has 100 LTC, and they want to swap their coins. Alice can generate a smart contract, tx1, with hash function H(x), where x is a random number. Her 10 BTC is conditionally locked in the smart contract.

 

        tx1:

        [pay 10 BTC to Bob’s public key if
                i) x for H(x) is known and signed by Bob or
                ii) signed by both Alice and Bob.]

 

Since her BTC is locked in tx1 she needs to generate another smart contract, tx2, to retrieve her BTC if Bob defaults.

 

        tx2:

        [pay 10 BTC from tx1 to Alice’s public key, locked for 48 hours, signed by Alice]

 

tx1 and tx2 are also called Hashed Timelock Contracts (HTLCs).

 


Alice then sends tx2 to Bob. Once Bob has signed tx2,, Alice will then broadcast tx1 to the network or send it to Bob directly.

 


Bob creates smart contracts, tx3 with hash function H(x), and tx4

 

        tx3:

        [pay 100 LTC to Alice’s public key if
               
i) x for H(x) is known and signed by Alice or
               
ii) signed by both Alice and Bob.]

 

        tx4:

        [pay 100 LTC from tx3 to Bob’s public key, locked for 24 hours, signed by Bob]

 

Bob will then send tx4 to Alice which Alice will sign and return it to Bob. Bob will then broadcast tx3 to the network or send it to Alice directly.

 


Alice then signs tx3 and unlocks 100 LTC from the contract to her address, at the same time revealing the value of x to Bob. Bob can now sign off tx1 knowing the value of x and receive the BTC.
The hash function H(x) can be thought of as a lock while x is the key. Since both tx1 and tx3 were created with H(x), the value of x can be seen as the key to unlock the contracts.

 

The Future of Atomic Swaps

Besides eliminating third-party risk, Atomic Swaps also allows users to exchange cryptocurrency pairs without the need of an intermediate coin. For example, if Bob wants to swap his LTC for VTC on exchanges, he may need to trade LTC to BTC and BTC to VTC if there is no direct pair. Thus, Atomic Swaps can be a tool to lower transaction costs.

At the moment, the technology still has a long way to go before mass adoption. To initiate Atomic Swaps, both cryptocurrencies must be capable of executing Hashed Timelock Contracts (tx1 & tx2 in above example). There are other technical hurdles including the hash algorithm and public preimages.

Atomic Swaps are most notable on the Lightning Network and we hope to see adoption grow linearly for these two technologies.

 

Written by Peter Chan
Trader @Genesis Block
For any queries please contact [email protected]