An atomic swap is a cryptocurrency trade that can be executed instantaneously without the need for a third party service to facilitate the exchange. It’s another stage in the evolution of decentralized exchange, an attempt to make trading safer, more efficient, and trustless.
“Wait, so like, I can’t trust atomic swaps?” No no, that’s not what we mean when we say trustless–quite the opposite, in fact. Ironically, a trustless exchange is more trustworthy than a trusted one. For example, when you use a centralized exchange like Binance, you’re involving yourself in a trusted transaction. Namely, you’re trusting Binance with your funds. You relinquish your private keys to the exchange with the belief that its operators will not misuse them, and in return, you’re given access to Binance’s trading platform.
To give another example, let’s say Molly and Steve wanted to swap currencies. Molly has 57 LTC, but she wants to own 1 BTC to know how it feels. Turns out, Steve has 1 BTC and he wants 57 LTC, so Molly and Steve decide to make the trade. But neither wants to send the funds to the other first, out of fear that s/he might be cheated. They also don’t want to go through an escrow service (another example of a trusted, third-party intermediary) to execute the trade for them.
Luckily, they can use atomic swaps for this. With an atomic swap, Molly and Steve could conduct the transaction without having to go through a third party, like Binance or an escrow service, and without having to worry about being screwed over by the other.
Magical, right? Well, if you keep reading, we’ll go into more details below.
Atomic Swaps: How they Work
As you probably figured, atomic swaps are designed with two different currencies in mind. They’re a method by which you can trade coins cross-chain directly without going through an exchange. Additionally, atomic swaps can be conducted on-chain or off-chain, but more on this after we’ve gone over how they work.
Like some Lightning Network transactions, an atomic swap employs a hashed timelock contract (HTLC) to ensure that both parties hold up their ends of the bargain. These contracts make use of a multisignature transaction system that holds both traders accountable for a swap to be successful. In order to make this possible, a hashlock uses a cryptographic algorithm that only allows users to access funds once both parties have signed off on their respective transactions, and a timelock is like an insurance policy that ensures that both users will have their funds returned to them if the trade is not successful under a specific timeframe.
Source: Hacker Noon
So in practice, an atomic swap would go down like this:
Molly and Steve open up payment channels to each other to swap 57 LTC for 1 BTC. The instigator of the transaction must then make a contract address. In this example, Molly is the instigator–she’s forward like that–so she creates a contract address.
The contract address functions like a safe for funds. In creating the address, Molly deposits her 57 LTC and produces a value to go with it (a secret string of data that functions like a key). Essentially, the hash is like a lock for safe, while the value is the key. To facilitate the transaction, Molly sends the hash to Steve.
Steve receives the hash and produces a similar contract address with it. In creating his own contract address, Steve deposits his 1 BTC into a safe (contract address) that has the same key as Molly’s own. Thus, for Molly to retrieve her 1 BTC, she’ll need to sign a transaction for Steve’s contract address, and for Steve to retrieve his 57 LTC, he’ll need to sign a transaction for Molly’s contract address.
To do this, Molly signs Steve’s contract address with the value (key) that was used to produce the hash that Steve used for his contract address. In doing so, Molly unlocks the safe that has her funds while also revealing this key to Steve. So now that Steve has this key, he can sign off on the transaction for Molly’s contract address and retrieve his 1 BTC, and everyone’s happy.
Basically, when Molly creates the contract address, deposits her money, and creates a value for this address, she sends Steve the hash for the value to say, “If you want these 57 LTC, you need to reproduce the hash’s key.” So Steve then creates his own contract address with this hash to say, “Ok, but I can’t have the key until you give it to me, so use it to unlock your 1 BTC and then I’ll be able to take it to unlock my 57 LTC.”
Pretty cool, right? We think so. The HTLC structures the transaction in such a way that both parties are dependant on the other to make sure the swap is a success. If for whatever reason the exchange fails (e.g., network failure or one party not holding up his/her end of the deal), then the timelock returns all funds to their rightful owners–no harm done.
Atomic Swaps: On-chain vs. Off-chain
It’s important to note that atomic swaps can be executed either as on-chain or off-chain functions.
On-chain atomic swaps, as the name suggests, take place on either currency’s blockchain. Currently, for these swaps to work, both currencies must use the same hashing algorithm, and they also must support HTLC. Back in Septemeber of 2017, Litecoin and Decred conducted a successful on-chain atomic swap, the first of its kind.
— Decred (@decredproject) September 19, 2017
Off-chain atomic swaps allow for, you guessed it, off-blockchain exchange of the currencies in question. This takes place on a secondary layer of nodes, and at this point, off-chain atomic swaps are an extension of the Bitcoin Lightning Network. Bitcoin and Litecoin executed the first ever off-chain atomic swap using the Lightning Network back in November of 2017.
Today we’re excited to announce the first ever Lightning cross-chain swap from Bitcoin to Litecoin!⚡️⛓️?
— Lightning⚡️ (@lightning) November 16, 2017
Wrapping Up: Why Atomic Swaps Matter
Atomic swaps could bring greater security to an exchange atmosphere ridden with risks, including funding misallocation and hacking attacks. There’s something to be said about a trading feature that allows you to conduct trades without having to go through a centralized intermediary.
On this point, atomic swaps streamline the entire trading process to a point of complete convenience. Theoretically, atomic swaps could be developed to provide universal trading pairs from currency to currency. Want to trade ARK for LINK? You got it. How about IOTA for DASH? As long as users make asks and bids and are creating a market for these swaps, you can trade in these pairs. Moreover, atomic swaps could see wallet integration, allowing you to trade currencies directly from your software, mobile, or web wallet–no more moving funds to and from exchanges.
As I mentioned earlier in the article, atomic swaps could be next evolution in decentralized trading. There are a few projects out there looking to turn this dream into a decentralized reality. Altcoin.io, for example, is building a decentralized exchange and wallet with built-in atomic swap features. The project currently has a beta test net live for its atomic swaps, and the team hopes to have a fully functioning platform live soon. Komodo is also building its own decentralized exchange with on-chain atomic swaps enabled, an integral piece of its all-in-one blockchain solutions.
Hopefully, projects such as these can deliver on their promises and fulfill a much-needed trading solution to the cryptocurrency realm. If they do, look for atomic swaps to disrupt the current exchange model, as they’ll likely unearth the potential for a complete revolution in how currencies change hands.