Atomic swap is cryptographically secured smart contract cross-chain transection between one crypto to another. It is first introduced by Komodo Platform. Atomic swaps are one of the most important things when it comes to exchanging cryptocurrencies. Atomic swaps allows individuals to trade crypto without having any third party involvement this is the main highlight of atomic swap.
How it works
Let suppose Alice have 1 Bitcoin and she wants it to exchange with 10 ethereum, in traditional way she has to go on centralized exchanges which’s involves high risk of fraudulent but she only wants to exchange it in decentralized manner so let suppose another random guy Bob have 10 Ethereum and he also wants to convert his 10 ETH to 1 BTC so Alice and Bob could make a tarde so there are two scenarios might happen.
- Alice sends her 1 btc to Bob but Bob never sends his 10 Eth back to Alice.
- Alice receives Bob’s 10 Eth but she never sends back her 1 btc to Bob.
In order to prevent these kind of scenarios atomic swap uses something called Hash Time Locked contracts. It ensures that transaction between them are completely trustless. For Example – If Alice receives Bob’s 10 eth and she failed to send her 1 btc to Bob or vice versa then Hash Time Locked contract prevent these kind of fraudulent situation.
How Hash Time Locked contracts works (HTLCs)
In HTLCs there are two concept
- Hash Locked
- Time Locked
So, in order to have a trade between Alice and Bob, both must have to submit their transaction to their respective blockchain, Alice on the Bitcoin blockchain and Bob on the Ethereum blockchain. First Alice must have to generate a private key that only she knows then using her private key she’ll have to sign a 1 BTC transaction to Bob then she sends the hash of the transaction to Bob, then Bob’ll sign his 10 Eth transaction back to Alice using Alice’s hash sent so after signing the transaction now Alice is able to redeem her 10 Ethereum sent by Bob then after redeeming Bob’s 10 Eth now Bob also can redeem his 1 bitcoin sent from Alice. So even Alice has her private key before Bob there is no way to fraud to Bob because Alice transaction have already committed on the blockchain. She can’t reverse her 1 btc. There is one thing to keep in mind Bob can’t redeem his 1 BTC sent by Alice before redeeming Alice her 10 Eth sent by Bob first.
So there is a problem in between
What if Alice signed a 1 btc transaction on the blockchain to Bob but Bob’d never send his 10 Eth transaction using Alice transaction hash. Here’s Time Lock contracts comes into play. What it does is basically sets a time limit for transaction. When blockchain will not get Bob’s transaction back to Alice under a fixed period of time then Alice transaction which is already committed on the blockchain will reverse to her wallet and this same applies for Bob as well.