Cross-chain atomic swaps, as explained in this Medium post by Xiaohui Liu, refer to a method that allows for the exchange of cryptocurrencies between two different blockchains. The concept of atomic swaps was introduced in 2013 on the BitcoinTalk forums and has since become an essential feature in the blockchain space.
In these swaps, the process is atomic, meaning either both parties involved in the exchange receive the other’s coins, or they both keep their respective coins. The protocol ensures there is no possibility of cheating or any need for a trusted third party, thus eliminating counterparty risk.
The basic idea of atomic swaps relies on the use of different types of smart contracts, which act as “locks” to secure the coins during the exchange process. Two common types of locks are “hash locks” and “time locks.”
- Hash locks: These locks can be opened using a secret code or preimage that hashes to a specific value. The smart contract acts as a hash puzzle, and once the preimage is used to unlock the coins, it becomes publicly visible on the blockchain.
- Time locks: These locks remain closed until a predetermined time has passed, either defined by a specific date (UNIX epoch time) or a block height. The corresponding smart contract is known as “CheckLockTimeVerify.”
The atomic swap process involves the following steps:
- Setup: Alice and Bob each deposit their respective cryptocurrencies into smart contracts associated with each other. They share a joint hash lock and keep the secret preimage hidden from each other.
- Swap: Alice initiates the process by using her private key and the secret preimage to unlock Bob’s coins, while Bob observes the revealed preimage on the blockchain. He then uses the same preimage, along with his private key, to unlock Alice’s coins. The swap is completed without the need for a third party.
- Abortion Protection: To prevent one party from not fulfilling their part of the swap, time locks are implemented. If one party fails to deposit their coins or complete the swap, a time-based refund mechanism allows the other party to retrieve their coins.
The use of Hash Time-Locked Contracts (HTLCs) enables atomic swaps since they combine both hash locks and time locks. HTLCs can be implemented using different programming languages like sCrypt (for BSV) and Script (for BTC).
Furthermore, cross-chain atomic swaps are not limited to specific blockchain pairs; they can be achieved between any two blockchains supporting HTLCs with compatible hash functions and time lock mechanisms.
Overall, atomic swaps provide a secure and trustless way to exchange cryptocurrencies across different blockchains, promoting interoperability and decentralization in the cryptocurrency ecosystem.