Lcross-chain swaps or atomic cross-chain swaps, are a type of currency exchange that takes place between two different cryptocurrencies that run on their own blockchain. That is, it is a mechanism that allows users to exchange different cryptocurrencies directly between two pairs.
The easiest way to see this type of change is to change the fiat currencies lifelong. In such exchanges, two people (one with euros and the other with dollars) want to exchange their currencies for each other at a certain rate. For this, in fiat, simply knowing the rate and doing the accounts, each of the parties can give the other the amount of coins equivalent to the change. For example, € 100 would be equivalent to about $ 121,55, using an exchange rate of $ 1,2155 per euro. Suffice it to say that this change has been direct and without any intermediary.
However, in this example we are talking about tangible things, it is money that is in the real world. The question here is: How to make such an exchange possible using cryptocurrencies whose systems work on different platforms? Well, the answer to this is cross-chain swaps and you will learn much more about them below.
Beginnings of Cross-Chain Swaps
The first whitepaper that broadly explains the nature of cross-chain swaps is "Atomic Cross-Chain Swaps"by Maurice Herlihy of the Department of Computer Science at Brown University Providence. This document, presented during the 2018 ACM Symposium, clearly explains how an atomic cross-chain swap works, its possibilities and main challenges in creating this type of tool.
Basically, what a cross-chain swap does is; take the money from the parties and deposit it under a special schedule that guarantees that the operation can only be completed with the approval of both parties. And in the event that any party acts maliciously, it cannot take control of our funds.
Now, the programming part has surely caught your attention at this point. Well, first of all, remember that every cryptocurrency transaction occurs thanks to the fact that said currencies respond to a programming or smart contracts that allows to send, take and deliver the control of said currencies. In Bitcoin, for example, we call this programming is possible thanks to Bitcoin Script, And in Ethereum, thanks to Solidity. In general, all cryptocurrencies use this management model for their operations. By the way, if you want to know how to start programming smart contracts for Ethereum, you can check our article How to start programming in Solidity?
Now, taking into account that cryptocurrencies are programmable currencies, we can make a special programming that allows us to exchange them cross-chain with total security. This is what makes this type of exchange possible, and we will talk more about this in the next section.
Programming currencies, the key to cross-chain swaps
Now, let's go a bit to the practical side of cross-chain swaps and for this we will take, for example, Bitcoin. As we already mentioned, in Bitcoin we have the Bitcoin Script programming language. This scripting language allows us to send and receive coins in a fully programmed way. In fact, every transaction in Bitcoin is only possible using such programming.
But there is something very interesting with the capabilities of this language, more specifically with the functions OP_CHECKSEQUENCEVERIFY (CSV) y OP_CHECKLOCKTIMEVERIFY (CLTV). The first is a very useful operation code or OP_CODE, since it allows us to block the execution of a certain script or transaction until certain conditions are met. Thanks to this, things like Lightning Network are possible, as well as decentralized exchanges and secure P2P exchanges. These last two are precisely those that enter the field of cross-chain swap operations. The second code, for its part, allows us to place time locks on our transactions, something that we can use to create security mechanisms for our exchanges.
Thus, if we use a wallet capable of giving us the ability to perform cross-chain swap operations, we can make use of the OP_CHECKSEQUENCEVERIFY and OP_CHECKLOCKTIMEVERIFY functions, and perform cross-chain swap operations with people who have the same capacity. And all, because our wallet will program the coins so that they can only be exchanged under a series of conditions that both parties have decided in advance and without any intermediary.
Of course, this is a very simple explanation of how everything actually happens, so below we will explain how all this actually happens.
How does a cross-chain swap work?
Now let's assume the following scenario. On the one hand, we have Laura who wants to exchange her Bitcoin (BTC) for Monero (XMR). And on the other hand, we have Juan who wants to exchange his Monero for Bitcoin. Both have a need for exchange, and both parties have the currency they need.
Given this, Laura and Juan begin the exchange process having their purses trained for it. First, Laura creates a transaction using the opcode OP_CHECKSEQUENCEVERIFY and blocks the amount of Bitcoin to be exchanged with Juan. Said script can only be unlocked once Juan complies with the exchange conditions, issuing his equivalent transaction in Monero, and knows the secret that protects Laura's transaction. In case something goes wrong, Laura's transaction has a security measure that would allow her to recover the money after a while, using OP_CHECKLOCKTIMEVERIFY for this.
For his part, Juan also issues a transaction in Monero using the corresponding programming. In this case, Monero doesn't support OP_CHECKSEQUENCEVERIFY, but instead uses an operation called DLTC or Discrete-log Timelock Contract, a system that allows conditional payments in this currency. This operation allows Juan to issue a payment in Monero, which will only be effective if Laura meets the conditions. Like Laura, Juan has included conditions that will allow him to recover the funds in case something goes very wrong.
At this point, both parties have issued the payments, but no one has the money. At this point, it is time to check the unlocking conditions, which will allow Laura and Juan to take the money from the exchange effectively. Thus, both Laura and Juan reveal the secrets that protect their transactions. It is when you both know these secrets, when you can use them to unlock the script that holds payments. In the event that one of the parties has acted maliciously, one of the parties can use security measures for their transactions.
These security measures are time locks, and their function is to ensure that the funds are inaccessible to both parties for a certain and prudent period of time. Thus, if after a while, one of the parties does not comply with the conditions, the other can simply withdraw their money from the exchange and get it back, remaining as at the beginning.
Cross-chain swaps with other cryptocurrencies
The example we have given here we have done with Bitcoin and Monero. And, although it is a fictional example, it is close to the reality of this system. In fact, the Monero team is currently working on a cross-chain protocol to allow Bitcoin users to convert their BTC to Monero using this approach.
Of course this is not only limited to cryptocurrencies such as Bitcoin or Monero, all cryptocurrencies have this possibility, as long as they meet these two requirements:
- They must allow blocking at the script level.
- They must allow blocking at the time level.
Both transaction execution locks are intended to add the security necessary for these exchanges to be secure. Thus, for example, Ethereum could implement this system to allow cross-chain with other cryptocurrencies, and the same with the rest of crypto. The only limitation in all this is the one that developers can interpose by not supporting certain functions or equivalents in the blockchain and cryptocurrency.