The following article can be viewed as a blockchain development manual with significant immersion in the subject of Atomic Swaps and requires at least basic knowledge of the topic.
ACCS - Atomic cross-chain swap - allows two parties to exchange cryptocurrencies between two blockchains without intermediaries.
Technology is under development. Only part of the blockchains is currently supported. To implement the exchange, blockchain support is not required for classic smart contracts. The problem of performing related operations in unrelated blockchains is solved, when both parties must complete their set of actions under the contract, otherwise the whole transaction will not take place.
Implementations, that are theoretically possible:
- Directly on the blockchain (on-chain) - part of the blockchains are already supported. (described in more detail below)
- Using some payment channel (off-chain) - development is currently underway only for the Lightning Network and Raiden. There are no ready-made implementations even for test networks. (described in more detail below)
The technology is based on a tool called the Hashed Time-Locked Contract (HTLC). This is a scheme of cryptographic confirmation of the legality of the actions of participants, while the actions themselves are separated in time. As the name implies, this tool combines two mechanisms for blocking the exit of a transaction: by time (time lock) and by a secret number, the hash of which is recorded in the blockchain (hash lock).
Such a scheme ensures the reversibility of the operation when one of the parties “disappears” (does not perform the required part of the actions).
HTLC is a proven tool that is used, in particular, in the Lightning network. Mandatory conditions for the possibility of an atomic swap on a particular pair of blockchains is to support HTLC in it, as well as to implement the calculation of the same hash function.
To exclude the possibility of fraud, the blockchain should not allow transaction plasticity. The transaction ID cannot be arbitrarily changed, because this may mislead the exchange partner. Activation of Segwit on the blockchain closes this potential vulnerability and is also one of the necessary conditions for supporting ACCS.
Implementation of ACCS on payment channels
Although the concepts underlying ACCS and payment channels are common, one should distinguish between these two definitions. A pair of participants have the opportunity to make many transactions by opening a payment channel between themselves. For the exchange between blockchains, you will need an open payment channel in each of them. After that, participants close the payment channel, thus recording the fact of the exchange directly on the blockchain.
The option of exchange through a payment channel is faster because it eliminates the need for a long wait in the absence of actions on the part of the partner.
The first exchange of this kind was made between Bitcoin and Litecoin using the Lightning network .
Performing an Atomic cross-chain swap operation on a blockchain
The concept was first described in 2013 by Tier Nolan . The first Proof-of-Concept (POC) was created by Decred: a blog , source code and instructions . The first exchange was made in September 2017 between Decred and Litecoin . Since then, the code in the repository has been changed. We will consider the modern version here.
The two parties (Alice and Bob) agree on an exchange. Alice pays Bob 1 BTC, Bob pays Alice 10 LTC. The parties agreed to pay the blockchain commissions on their own.
Step 1. Alice creates a new address. So does Bob. Then they share it. Alice sends Bob the AA address in the LTC blockchain, Bob sends Alice the AB address in the BTC blockchain.
Step 2. Alice generates the secret key K and its one-way hash KX. Alice creates a transaction - a TA contract worth 1 BTC. TA gets to the blockchain at the time of Tta.
Step 3. Alice tells Bob the details of the TA, from which Bob recognizes the KX hash, and also checks the contract.
Step 4. If everything is fine, Bob creates a transaction - a TB contract worth 10 LTC. TB falls into the blockchain at Ttb time. Bob tells Alice the details of TB.
Step 5. Alice checks the TB contract. She needs to make sure that her “Recipient address” is set as her LTC address, which she generated in the first step and sent to Bob. The value of the contract must be in accordance with the original agreement, 10 LTC. The value of the “Secret hash” field should be equal to the hash of the KX secret key that Alice sent to Bob in step 2. It is also necessary that the contract is still valid (the established Locktime should allow completing the exchange).
If everything is fine, Alice transfers 10 LTC from the TB transaction to another address of her wallet. With this action, Alice tells Bob key K. Now that Bob has recognized the key, he has access to transaction TA.
Bob transfers 1 BTC from transaction TA to another address of his wallet. This completes the exchange process.
As a result, Alice got 10 LTC minus LTC transaction fee, Alice spent 1 BTC plus BTC transaction fee. Bob had 1 BTC minus the BTC transaction fee, he spent 10 LTC plus the LTC transaction fee.
Thus, due to the use of delays in contracts, each party has enough time to get its amount as in the contract. If Bob does not create a TB contract, Alice can get her BTC back in 48 hours.
If Alice does not receive funds from the TB contract, Bob will be able to receive LTC back after 24 hours. Alice in this case, after 48 hours, will take back the BTC, to which Bob will not get access.
Therefore, this transaction, consisting of two contracts, is called atomic. Each receives his agreed amount of currency, or both parties remain with their initial amount, minus a commission.
These shown exchange methods need full blockchain nodes, where the exchange could be made. Let’s consider services that simplify the exchange operation.
There are not so many exchangers that implement reliable ACCS. Since the Lightning Network is still far from widespread adoption, we consider only on-chain solutions.
Swap.Online - a cryptocurrency web wallet with the ability to exchange via atomic swap . User keys are stored on the client’s side. In addition to Bitcoin, Ethereum and many ERC-20 tokens, in the fall of 2018 the app has the ability to exchange USDT, EOS, Litecoin.
It is possible to create a new exchange request or to choose from already created ones (via Orderbook). When creating an order, you can specify that you want a partial redemption. When the counter application is found, an exchange is made. It is carried out automatically if the wallet is currently open.
Both users create transactions to each other's addresses, then take the received funds one by one, by sending a second transaction. The time for the exchange transaction is 3 hours, after which it is possible to return your funds.
If there are not enough funds for an exchange in the wallet, a dialogue is displayed inviting you to deposit the missing amount into your wallet (including the transaction fee). As a result of the exchange, the user receives the exchanged amount minus the commission for the second transaction.
Atomic Wallet is a mobile and desktop application - a wallet with an exchange function . It has a wide range of cryptocurrencies (25) and tokens (over 300). The application supports any standard tokens, just transfer them to your Ethereum address. This app performed the ACCS exchange of Qtum and Bitcoin first . In addition to the usual exchange through partner exchangers, there is a section with atomic swaps where you can view existing orders, place your own and track the history of your exchange operations. The exchange between Bitcoin, Litecoin and Qtum is supported. (UPDATE 01.02.2020: Atomic wallet no longer support atomic swaps transactions)
Another service, Altcoin.io offers a decentralized exchange solution. A month after the first exchange between Decred and Litecoin, Altcoin.io enthusiasts announced the first ACCS operation between Ethereum and Bitcoin . After the first tests of ACCS, in early 2018 they launched a wallet with atomic swap function in testnet . In the summer of that year, the wallet code was posted publicly, as its release scheduled for the end of the year was cancelled. The company focused on a decentralized exchange in the Plasma network . There are open-source website written in Angular and a wallet, which is a set of libraries for implementing wallet and atomic swap functions. The wallet code is quite crude, apparently, the project requires serious revision before launching in mainnet.
- Instant Cross-Chain Transactions On Lightning announcement
- ACCS description, 2013
- Decred about the first implementation of ACCS
- Initial texts of POC
- First Successful QTUM-BTC atomic swap
- The First Ethereum <> Bitcoin Atomic Swap
- Atomic Swap Wallet release announcement
- Atomic Swap Wallet Now Open-Sourced
- Atomic Swap Wallet description