Atomic Swaps

Since the activation of SegWit, it is possible to perform cross-chain atomic swaps on the Lightning Network. The first Lightning swap has now been performed on the Bitcoin and Litecoin testnets. Because we are not really that far for these swaps on the mainnet, the atomic swap seemed to be a dream for the time being. However, developers have now presented another way to perform on-chain atomic swaps.

An atomic swap is a digital exchange that can be carried out in confidence, without trust in a counterparty. This is also possible between different types of blockchains (cross-chain) when other cryptocurrencies have activated SegWit. This becomes possible without the need for trust in the other party. In addition, the use of an exchange or exchange service is no longer necessary.

Problem with trust

To understand what an atomic swap is, we must first deal with the problem that exists in trading bitcoins. We do this on the basis of an exchange between Alice and Bob.

Imagine that Alice wants to buy something from Bob, for example a didgeridoo. Alice and Bob live very far apart, so it is difficult to make this exchange in person. In addition, the two do not know each other, so there is no foundation for trust. So how can Alice and Bob do business with each other without one of the parties leaving the other with a pig in a poke?

When Alice sends money to Bob first, Bob could then neglect to send the didgeridoo, leaving Bob with the money and leaving Alice empty-handed. This can also be a problem the other way around, when Bob sends the didgeridoo, it arrives at Alice’s and Alice then refuses to transfer the agreed amount to Bob.

With such an agreement, you ideally want both parties to fulfill the transfer simultaneously, so that the other party cannot subsequently fail to comply with the agreement.

Possible solutions

Several possible solutions are available. First, Alice and Bob could actually meet in person and then do the handover. However, this is often impossible because today, with the convenience of the internet, people order products from all over the world. Booking a return ticket from Amsterdam to Sydney to ensure that the 50 euro didgeridoo will actually be in the living room is of course not desirable.

Another possibility is to look for a trusted intermediary. The intermediary in question can then first take possession of the money and the didgeridoo from Alice and Bob and then forward it on. Here again the danger of trust comes into play; how can one be sure that the middleman is not running away with both the money and the didgeridoo? And don’t the costs of a reputable intermediary exceed the entire agreement?

For bitcoins, this also happens in these ways. People do business directly with each other on marketplaces like LocalBitcoins; the other must therefore be trusted. Or you buy the bitcoins on an exchange or from an intermediary; these companies should be trusted.

Virtual exchange

It is slightly different with virtual exchanges. Because everything goes through the computer, there are also more options. In this way, the two parts of an exchange can be made dependent on each other. For example, conditions can be set for the transfer; the money should only be sent when the didgeridoo is at the post office and the package should only be sent when the money has arrived in good order. In this way you guarantee that either the agreement will be fully complied with, or that both parties can waive the agreement if the other fails to comply; so no one will be left empty-handed.

This can be thought of as the atomic swap; it is one transfer that is executed when the conditions are met by both parties.

On-chain Atomic Swaps

Developers of Decred, a fork of Bitcoin, have been working on a SCRIPT smart contract that now also enables on-chain atomic swaps. SCRIPT is a language that Bitcoin, Litecoin, Viacoin and Decred use for the smart contracts, among others. This new kind of smart contract makes it possible to have an on-chain exchange, i.e. on the blockchain itself, take place between these currencies without trust.

How does it work?

Imagine that Alice and Bob want to make an agreement for the exchange of Alphacoin and Betacoin. These coins both use a different blockchain. A construction must be set up with which this exchange can take place between the two blockchains, without the possibility that Alice or Bob will only half comply with the agreement. That’s where the virtual vault comes in.

Alice creates a virtual vault on the blockchain and gives the specifications for the lock, such as what kind of (as yet unknown) key will be needed to open it, to Bob. She then puts her Alphacoin in the safe, which can only be opened with Alice’s key and Bob’s (digital) signature.

Bob then in turn creates a similar vault containing his Betacoin. He uses the same kind of key that he will receive from Alice. The type of key is therefore known to both parties, so that the safe of the other could also be opened by both parties, provided that the correct signature is provided.

When Alice opens Bob’s vault, the key used is made public on the Betacoin blockchain. Bob can then use this disclosed key to open Alice’s vault and get the Alphacoin.

This key system therefore makes it possible that when one party opens the safe of the other, the other immediately obtains the correct information to open the safe intended for him. Because a signature is also required to open the safe, no arbitrary party that searches the blockchain for safe keys can open one of the safes; after all, the required signatures of Alice and Bob are only in the possession of themselves. The signature is created from the private keys of Alice’s Alphacoin address or Bob’s Betacoin address.

If something goes wrong, or one of the parties refuses to fulfill the agreement, the safes are arranged in such a way that the contents are returned to the relevant owner after a certain time, for example 24 or 48 hours.

Off-chain Atomic Swaps

Cross-chain atomic swaps over the Lightning Network, also known as off-chain atomic swaps, have both advantages and disadvantages compared to these on-chain swaps.

On-chain swaps require confirmation of the transactions made. A confirmation takes on average 10 minutes, but in the worst case can take up to an hour. With Lightning swaps, these transactions are instant; these do not need to be confirmed on the blockchain.

For Lightning swaps, (bit)coins must first be anchored in the Lightning Network before they can be sent over the network through channels. Only when these channels are closed, and the Lightning Network can no longer be used, are the (bit)coins returned to the blockchain.

Lightning transactions are generally cheaper and faster; ideal for small transactions and possibly better for privacy, because not all transactions are confirmed on the blockchain. For larger transactions, which may use offline signatures, the on-chain atomic swap is again more favorable. Speed is often not important here and direct confirmation on the blockchain has priority.

Application in practice

This is ideal for OTC exchanges (Over The Counter, where large transactions are facilitated directly between parties). No intermediary is needed anymore to arrange the trade between Alphacoin and Betacoin; you can exchange coins directly and without trust.

It also creates opportunities for trustless trading exchanges, where each party retains ownership of funds and does not entrust them to a third party. The exchanges then take place by means of atomic swaps, without having any risk of failing third parties. Think of malicious services that run away with all credits, or hackers who target the servers of the service used.

Privacy

It should be noted that on-chain atomic swaps may not benefit any privacy. Since the transactions on the blockchain need to be confirmed, and the vault keys on these blockchains are made public, the exchanges on both blockchains can be linked together. This way you can see with which coin, and the corresponding address, the exchange has taken place.

In conclusion

Atomic swaps make coin interchangeability a lot easier. This way you can immediately switch between coins with different use cases. Consider, for example, coins that are focused on privacy. Bitcoins can be exchanged for that currency in question, in order to subsequently make a transaction with it, and later exchange this privacy coin for bitcoins without the intervention of a third party. Even though Bitcoin does not (yet) have these and other functions itself, using the applications of other blockchains is made a lot easier by atomic swaps.

Meanwhile, Charlie Lee, the creator of Litecoin, has already performed an atomic swap between Litecoin and Bitcoin.

This article was written based on articles published by Jimmy Song. Jimmy Song is a Bitcoin developer, entrepreneur and former developer for bitcoinwallet Armory. In his Bitcoin Tech Talk series, he lets the reader watch the developments surrounding the technical aspects of Bitcoin.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

© 2024 Cryptocoin Budisma.net