Crypto Alphabet

What is an atomic swap?

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.

«So like, I can’t trust atomic swaps?» – somebody can ask.

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 instance, 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 Kate and John wanted to swap currencies.  Kate has 57 LTC, but she wants to own 1 BTC to know how it feels.  Turns out, John has 1 BTC and he wants 57 LTC, so Kate and John 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.

They can use atomic swaps for this.  With an atomic swap, Kate and John 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.

How do atomic swaps work?

As you probably understood, 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.

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 deal. 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.

In practice, an atomic swap would look like this:

Kate and John 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, Kate 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, Kate 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, Kate sends the hash to John.

John receives the hash and produces a similar contract address with it. In creating his own contract address, John deposits his 1 BTC into a safe (contract address) that has the same key as Kate’s own. Thus, for Kate to retrieve her 1 BTC, she’ll need to sign a transaction for John’s contract address, and for John to retrieve his 57 LTC, he’ll need to sign a transaction for Kate’s contract address.

To do this, Kate signs John’s contract address with the value (key) that was used to produce the hash that John used for his contract address.  In doing so, Kate unlocks the safe that has her funds while also revealing this key to John. So now that John has this key, he can sign off on the transaction for Kate’s contract address and retrieve his 1 BTC.

Basically, when Kate creates the contract address, deposits her money, and creates a value for this address, she sends John the hash for the value to say, «If you want these 57 LTC, you need to reproduce the hash’s key». So John 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».

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.

On-chain and off-chain atomic swaps

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. In Septemeber of 2017, Litecoin and Decred conducted a successful on-chain atomic swap, the first of its kind.

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 in November of 2017.

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. 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.