Say, you want to trade one cryptocurrency for another. How do you do that? You can either find someone you know and trust to do it in person or, a more common scenario, you go through a centralized exchange such as Shapeshift.io
The latter involves risk, because it means taking your funds off the blockchain and putting them in the hands of an unknown third party. If the exchange fails, or simply decides to hold on to your money, for whatever reason, you lose your coins.
But Lightning Network, an off-chain scaling solution originally intended for bitcoin, is setting the stage for a decentralized option – one that does not require a third party – called atomic swaps.
Also known as atomic cross-chain swaps, the technology essentially allows two people holding tokens on two different blockchains to trade directly – and instantly – without the risk of one party running off with the other’s money before the trade is complete.
That is where the word ‘atomic’ comes in. It means that either the trade happens in its entirety, or it doesn’t happen at all. So, if a Lightning node goes offline or the third-party reneges on his end of the deal, everyone gets their money back.
So far, so good. But there is a catch.
In order for atomic swaps to work, Lightning has to be up and running on at least two different blockchains. Right now, it is just starting on one: litecoin. But, the hope is, it will be running on multiple chains soon.
As it stands, several development teams are currently testing their implementations of Lightning on the litecoin blockchain. Further, SegWit (the protocol upgrade that is a prerequisite for Lightning) has been activated on a second blockchain, vertcoin, opening the doors to Lightning on that chain as well.
According to Charlie Lee, the founder of litecoin, who is committed to atomic swaps, all that is left is to get Lightning fully operational on litecoin, and then begin testing it on vertcoin. Once those steps are complete, we may see the the first atomic swaps as soon as this year.
Certainly, that is the idea Lee has been kicking around for some time. He wrote about his plans for atomic swaps in a blog post back in January. And, the 2017 litecoin roadmap clearly specifies a plan for atomic swaps between litecoin and vertcoin.
Going back to the start, the idea of atomic swaps is nothing new – other ways of doing cross change trades have been proposed in the past. But with Lightning, the building blocks are already in place. (Another benefit of Lightning is that it allows for instant clearing, meaning transactions occur on the spot, with no waiting involved.)
So how does it work?
Essentially, atomic swaps make use of a scheme known as a hash timelock contract (HTLC). Lightning already uses this same technology to establish bidirectional payment channels on top of a single blockchain, so it is no stretch to open channels across two chains.
HTLC is a merger of two other technologies, a hashlock and a timelock. Both of which set conditions on a multi-signature (or multisig) transaction, which acts like a type of escrow.
For example, a hashlock uses a cryptographic puzzle to ensure one party cannot release their funds without the other doing the same. And a timelock acts like a safety net if nothing happens, routing funds back to the senders after a certain amount of time.
You can think of all this as a way of putting funds aside and then using ‘if/then’ conditions to stipulate their output.
So, in short, an atomic swap looks like this:
Alice (on bitcoin) agrees to give Bob (on litecoin) 1 BTC in exchange for 50 LTC. To do this, Alice opens up a payment channel to Bob, and Bob opens up a payment channel to Alice on the other blockchain.
Both parties set ‘conditions’ on each channel. The first condition uses a timelock to ensure that, essentially, if the trade is unsuccessful, funds will be returned after a certain period of time. The second makes use of a hashlock. Alice generates a piece of data and its hash. She then pays Bob 1 BTC, but includes the hash, and says essentially, “If Bob wants to claim the payment, he has to provide the pre-image of the hash.”
Bob uses his payment channel to pay Alice 50 LTC, but to collect the funds, he stipulates that Alice needs to produce the pre-image of the hash.
By claiming the 50 LTC from Bob, Alice reveals the data, allowing Bob to grab the counterpart.
Now, if Bob or Alice fail to follow through, the first condition ensures that they get their money back. In this sense, you don’t risk losing your money, and more important, you don’t hand ownership of your money over to a third party escrow service.
Of course, Lightning won’t work out of the box for atomic swaps. But, developers on the project say modifying the code would not require an undue amount of work.
“We don’t have a standard for doing atomic swaps yet,” said Christian Decker, a Blockstream developer working on an implementation of Lightning for the programming language C. But he made it clear that, even though they don’t have the specification laid out, doing so should be pretty straightforward.
Meanwhile, in a recent blog post, Lightning Labs developer Olaoluwa Osuntokun, who is working on an implementation of Lightning for Go called lnd (stands for Lightning Network Daemon), suggested that upcoming releases of lnd would be “multi-chain aware”.
Another point to note: now that SegWit has activated on litecoin, users can begin sending test cross-chain transactions from litecoin to the bitcoin testnet.
Once Lightning Network is up and running successfully on a worthwhile number of blockchains, we can start thinking about decentralized exchanges.
To give more detail, these are exchanges that involve no central custodian. Other exchanges, like 0x (pronounced ‘zero-ex’) and EtherDelta, only allow you to exchange tokens that are supported by the ERC20 token standard on the ethereum blockchain. Whereas, atomic swaps allow payments across two different chains – an important distinction.
But, keep in mind, atomic swaps only do one thing: execute the trade. A true decentralized exchange also needs to match up traders (so that Alice can find Bob) and aggregate trades to determine a market value (so Alice knows what sort of deal she can reasonably offer Bob).
The good news is, those things don’t require a trustless service like the actual exchange of funds does.
As Decker explained, you can solve the other problems by simply having websites collect the orders and present them in some fashion, or by creating a broadcast network that announces trade opportunities.
But, as Decker said:
“Just like in a centralized exchange, everyone makes their own decision about what their coins are worth and sets their own value. So, basically if Alice announces she’s willing to exchange 1 BTC for 50 LTC, then Bob can either take it or leave it.”
Decentralized exchanges are a big part of the decentralized dream. They point to a future where individuals take control of their own money, and cryptocurrencies interoperate.
In other words, one day, it may be possible to pay a merchant who only accepts bitcoin in litecoin, monero, zcash, or whatever. It wouldn’t matter, because behind the scenes, your money is instantly converted to bitcoin.
And, from there, it is not hard to envision a time where centralized exchanges serve only as an onramp to the cryptocurrency world – but everything after that happens on the blockchain.