As of now, there are thousands of blockchains either operational or in development. Public, permissionless blockchains for the peer-to-peer transfer of assets and information, and private enterprise-grade blockchains for industrial applications.
As more and more project teams pursue innovation in just about every field imaginable, the question of blockchain interoperability becomes more pressing. If we have different networks for different use cases, how can we connect them all together? How can blockchain build the architecture for the next iteration of the internet, “Web 3.0,” if we can’t even trade assets like Bitcoin and Ethereum for one another without going through an intermediary like an exchange?
The issue isn’t just an inability to exchange cryptocurrency directly, but an inability to allow blockchains dealing in information regarding healthcare, digital identity, etc, to communicate with each other. However, exchanging crypto tokens is the first step to achieving interoperability – and that’s something that has already been achieved.
Atomic Swaps Instead of Crypto Exchanges
The phrase “atomic swaps” essentially refers to the act of trading different digital assets directly, wallet to wallet, without needing a trusted third party like an exchange to gain power over the assets and generate unnecessary revenue that doesn’t benefit the user.
Exchanges, while of crucial importance to early adoption, have major flaws that impact cryptocurrency development. They’re vulnerable to hacking and mismanagement, like Mt. Gox. They can struggle to keep up with volume demands and tank asset prices, and they’re also subject to government regulation and censorship, adding too many centralized elements to what is intended to be a decentralized movement.
After years of research, the first ever atomic swap was carried out between Decred and Litecoin in September 2017.
— Decred (@decredproject) September 19, 2017
By carrying out a cross-chain atomic swap of two different digital assets, developers achieved something that blockchain is all about – cutting out the middleman.
How it Works: HTLCs
Let’s look at an analogy of how this works before getting into the nuts and bolts of it all.
To carry out cross-chain atomic swaps peer-to-peer, both parties must decide on a shared secret. The only way to access the assets being traded is via this secret, meaning unwelcome parties cannot try to enter the transaction and hijack funds.
Essentially one user can reveal their secret in exchange for the assets being transferred, and once it is confirmed that the secrets match, the assets are unlocked. Sounds a bit vague? Here’s how it works exactly.
This system of secret exchange relies on mathematics. Hashed Timelock Contracts (HTLCs) are what enable cross-chain atomic swaps, and play a fundamental role in the Lightning Network as well.
A HTLC is essentially a state payment channel which allows multiple transactions to take place off-chain or on a side chain. This is useful because it reduces congestion on the main blockchain and aids scalability, allowing more people to use the blockchain and send transactions at higher speeds and volumes.
For example, in a situation where a user knows they’ll be carrying out multiple transactions with the same party (paying rent, buying coffee in their local coffee shop), a payment channel can take a lot of pressure off the blockchain. That user can open a channel and make, for example, dozens of transactions over the course of a month.
Instead of making all those transactions on the main blockchain, they can open the channel, handle all the purchases in-channel, and then close the channel again. The act of opening and closing the channel only requires two transactions on the main blockchain instead of dozens, hundreds, thousands – there is theoretically no limit.
A segment of blockchain state is locked in a smart-contract or multi-sig. Participants agree on the conditions of the smart contract and then sign transactions to each other through the channel without requiring miners to validate anything – then the entire set of transactions is sent for validation when the channel is closed, which happens when a certain time limit is reached or when a certain amount of funds or data have been transferred.
HTLCs always work on a time deadline (hence “time locked”). Payments are acknowledged via cryptographic proofs, in this case, the hash or “secret” we mentioned earlier. It’s possible for one party to refuse the payment they’ve received and returned it to the sender, which is an important feature enabled by the multi-signature transaction system which allows for both traders to be held accountable for a swap to be successful.
The ability to refuse and return a payment if certain conditions haven’t been met on the sender’s side is atypical to current blockchain systems and was a significant obstacle to overcome in the design of atomic swaps and interoperable systems overall.
Atomic Swaps Broken Down
So, Hashed Time Locked Contracts enable atomic swaps to take place – but what’s actually happening when we transfer funds this way?
Let’s take an example of two parties swapping Decred with Litecoin. Alice and Bob both want to exchange funds directly, so they open a payment channel and Bob creates a contact address which is a multi-lock address that will handle both the Decred and the Litecoin. Bob creates a value (or secret) and deposits his Decred. The value is essentially a key and the hash generated from the key will be the password for the multi-sig address.
Bob sends the hash to Alice who creates a contract address using the hash – only Bob and Alice know the hash, and only Alice knows her new address. She deposits her Litecoin to the address.
Now only Bob can unlock it because he has the value which generates that hash. For Bob to get his new Litecoin funds, he needs to sign a transaction for Alice’s contract address. For Alice to get her Decred, she has to do the same – but she doesn’t know the value that generates the hash yet.
However, when Bob signs Alice’s contract address with the value, he unlocks the address and Alice receives the value in the process. Now that she has the value she can also sign and receive her funds, and the swap is complete.
- Bob creates a value, generates its hash, creates a contract address with the hash, and deposits his funds.
- Bob sends the hash to Alice.
- Alice generates her contract address with the hash and deposits her funds.
- Bob accesses the funds Alice sent by unlocking the address using the value, which Alice can then use to unlock the other address to access her new funds.
Atomic swaps can take place on-chain if they support HTLCs and have the same hashing algorithm (like Litecoin and Decred). Otherwise, they can take place off-chain in a second layer like the Lightning Network.
The Importance of Atomic Swaps and Interoperability
Atomic swaps enable users to transfer totally different cryptocurrencies directly, peer-to-peer, with no centralization, greatly furthering the concept and ideology of the cryptocurrency movement as a whole. Ironically, for Decred and Litecoin to be traded without atomic swaps, typically Bitcoin will act as the “middleman” as users sell their Decred for Bitcoin to buy Litecoin, or the reverse. Cutting out unnecessary steps and increasing efficiency is what cryptocurrency and blockchain are all about.
Atomic has a huge use case for decentralized exchanges which can now act as a decentralized platform through which people can retain their own keys and control of their own funds without making any external deposits or needing to trust any third party.
However, the big picture is that this technology enables us to experiment with forging preliminary connections between multiple blockchain networks. In 2008, nobody had even mentioned the concept of having multiple blockchains and interoperability was not an issue on anyone’s mind. Ten years later, the community is looking to ways we can create a new, decentralized internet.
Decentralized application platforms that can be connected to different cryptocurrency and information blockchain networks to seamlessly share funds and data with no central authority, censorship, downtime, or security breaches. At least, that’s the dream! Interoperability is the goal of forward-thinking blockchain developers, and atomic swaps are the first step forward.