Drain the Swap: Lessons on Loving the Atomic Swap
Drain the Swap: Lessons on Loving the Atomic Swap
CoinMarketCap currently lists over 1,600 different cryptocurrencies, and the number of new coins is expected to increase, and probably increase dramatically.
If an individual owns only one cryptocurrency and will only ever trade in that one cryptocurrency throughout his entire life, then all trades are simple transactions on that one blockchain. (After all, many people over the world may well go their whole lives only using the fiat currency of their given nation.)
However, in the borderless and dynamic world of cryptocurrency, people often own more than one type of coin. A person might have a portfolio of many coins. A person might have a varied portfolio that includes Bitcoin, Ether, Litecoin, Monero, and maybe some more obscure coins as speculative investments. Each of these coins exist on their own blockchain, and none of these blockchains talk to each other.
At some point, you might want to trade your ether for bitcoin, your Monero for Litecoin, your Litecoin for Ether, your Bitcoin for something, or your something for something else altogether. In a centralized context, you could do this on an exchange and trust the exchange to handle the details of the transaction.
This approach is no different than processing a transaction through a bank or making a payment with a credit card. The buyer and the seller both trust the centralized bank to lock the funds in the process and to ensure that all parties end up with the correct assets, or that a refund is processed if the transaction cannot complete to everyone’s satisfaction.
Centralized exchanges function in exactly the same way as centralized banks.
But a number of good reasons exist why you might not want to do your trades through a centralized authority. You might not trust the centralized authority. The fees the centralized authority charges might be too high.
The website of the centralized authority might go down or have other accessibility issues. You might sacrifice a certain amount of privacy by using a centralized authority.
In a trustless, decentralized environment, a cross-chain atomic swap would do everything a centralized authority would do. The “cross-chain” nature provides a bridge between separate blockchains. The “atomic” nature ensures that the trade (the “swap”) would complete successfully or that all assets would be returned to their original owners.
Cross-Chain Atomic Swaps 101
Cross-chain atomic swaps can be implemented on any blockchain that supports hashlocks and timelocks. Suppose Alice has a-coins, and Bob has b-coins, and they want to trade. They do not have any reason to trust each other, and they do not want to use a centralized exchange. A high-level overview of how this would work in a cross-chain atomic swap is as follows.
1) Alice chooses a random value X and hashes it to create the private hashlock to lock the transaction. She keeps this value to herself for now and posts Transaction 1 to send her a-coins to Bob. These funds are currently unspendable because they are locked with the hashlock.
2) Bob waits for Transaction 1 to be confirmed, then posts Transaction 2 sending his b-coin to Alice. He does not yet know the value of X to unlock the hashlock, so all funds are unspendable at this point.
3) Alice waits for Transaction 2 to be confirmed, after which she posts Transaction 3, in which shares the value of X with Bob.
4) Bob posts Transaction 4 which unlocks the funds. Bob now has coins from Alice he can spend, and Alice has coins from Bob she can spend.
All four transactions must complete for the overall transaction to complete; this is what is meant by the word “atomic” to describe this process.
Proper time management is required for the above protocol to work.
For instance, if for any reason Alice never posts Transaction 3, all funds could be frozen and lost forever. This is where a timelock is useful. If Transaction 3 does not occur within a specified time frame, the timelock cancels the transaction, and all funds are reverted to their original owners.
And since Alice has the value of X to begin with, the implementation must be very careful to prevent any shenanigans Alice might attempt. The time between Transaction 3 and Transaction 4 is also important in this regard. If the algorithm is not implemented correctly, exploits exist where Alice could share the value of X but not give Bob time to retrieve his coins from the transaction.
The above approach describes the TierNolan algorithm (for a discussion of the topic, see this thread: https://bitcointalk.org/index.php?topic=193281.msg2224949). This protocol has different variations. For example, if Alice and Bob both trust each other, the number of transactions can be reduced and simplified.
One convenient place to locate this protocol would be to take a lot of the processing off the primary blockchains and implement the logic in a side chain like the Lightning Network.This would reduce the burden of miners having to process all the transactions on the primary blockchains, and it would speed up the overall process. You can find an explanation of the Lightning Network here: https://coincentral.com/lightning-network-beginners-guide/
Other algorithms exist, and for good reasons. One entirely different way of doing cross-chain atomic swaps separate from the TierNolan protocol would be to create a whole new blockchain outside both the a-coin blockchain and the b-coin blockchain.
This new blockchain would be an intermediary between the two separate cryptocurrencies. This new blockchain would understand both a-coins and b-coins and be able to process transactions between them. This approach would require network nodes for this new blockchain and miners willing to invest the resources to process the transactions.
The new network would require a governance model to ensure that transactions were processed fairly and securely and that none of the parties would have any advantage over the other.
Regardless of the method used, in a decentralized, trustless world with a multitude of cryptocurrencies in use, cross-chain atomic swaps will inevitably become an important component of cryptocurrency trading.
With the prices of Bitcoin (BTC) and other altcoins (alternative cryptocurrencies) surging as cryptos become more mainstream,…
Editor’s note: Bitcoin’s price has since broken a new all-time high of $22.3k. This article is still…
As more people enter the cryptocurrency ecosystem than ever, it’s important to rehash a few common concerns about keeping cryptocurrency safe. Many beginner to intermediate-level users wonder whether or not they should have a dedicated cryptocurrency wallet for each digital asset. One of the best ways to guarantee security, beyond taking the usual precautions like…
ABOUT THE AUTHOR
ABOUT THE AUTHOR
Wilton Thornburg is a software engineer, currently based in the greater Boston area.