hotshot

@cryptoshot

So How Do I Really Do An Atomic Swap

A Real Atomic Swap Between Viacoin and Litecoin

Crypto, Crypto, Crypto ,this is what most talk about nowadays , the demise of crypto, the revolution of crypto, but in the post we are not going to talk about it but rather do it , use it .

I will show you how atomic swap works in practice , the logic behind them from code point of view , and the flow of execution to achieve the end result which is :

Alice owns Viacoin , Bob on the other hand is a strong Litecoin advocate but he wantsViacoin because he believes it has a bright future , how can Alice & Bob exchange an amount of their stash for the other without going trough a third party ?

The answer is Hashed Time-locked Contracts, HTLC in short .

Put it simply, a Hashed Time-lock Contract is a non-Turing complete contract , that works like a puzzle with a secret key but requires the solver (in this case Bob ) to provide the key before X time is elapsed otherwise he forfeits the ability to claim the reward,returning it to the the initiator of the contract .

A Hash-lock is a “transaction puzzle” that satisfies a condition , the input data hash is equal to a hash value from a previous block , in Script (Bitcoin Scripting Language) it looks like this :

An atomic cross-chain swap is a hash-locked contract that happens on both blockchains , an initiator will create a contract and lock an amount X of coin Z in it ,the contract is valid for a time period T if a key provides the valid secret it can move the locked coins if this is not done by the end of T the contract is canceled .

If you think about it in terms of bitcoin primitives it make sense and it also shows how cryptography makes this operation secure-proof in easy logic .

Alice locks some Viacoin in a contract A,Bob locks some Litecoin in a contract B , Alice gives Bob the secret to unlock the locked Viacoins and Bob does the same at the end both Alice and Bob are incentivized to share the secret since if it’s not done by both of them the coins go back to their respective wallets and no exchange happens .

Let’s do an atomic swap

Requirements:

  • 2x VPS (Digital Ocean,Vultr …) or 2 Machines .
  • Running Full Nodes of Both Blockchains in Each Machine
  • The atomicswap package
  • Supported Coins (Bitcoin ,Litecoin,Viacoin,Decred,Particl…) All UXTO based protocols .

First of all you will need to install both nodes and configure them to use testnet as described here .

Scenario :

On your first machine you initiate the contract using the initiator blockchain (Say Alice’s Viacoin) .

Alice initiates the swap

Once the contract is created , Bob must Audit it to check if :

  • The amount locked is the amount agreed on by both parties
  • The Address is Bob’s address
  • The contract is published on the blockchain (Immutable)
Bob audits the contract

If everything is correct Bob moves to lock his coins by participating in the contract .

Bob participates in the swap by creating a new contract

Alice on her turn audits the contract

If everything is checked (conditions agreed upon) Alice goes on to redeem her Litecoins

Alice redeeming the litecoins from Bob’s contract

Now for Bob, once this transaction is actually published , Bob can extract the secret that was known to Alice at the initiation ,the secret will unlock the coins locked on the Viacoin Blockchain that Bob can redeem

Bob now can extract the secret to redeem his Viacoins

That’s it at that point the coins are unlocked and the atomic swap is complete .

This Technology will truly enable decentralized exchanging,Atomic Swap are the equivalent of a Forex Exchange but trust-less and decentralized .

The road is still long to achieve mainstream usability , I hope this will be helpful to build things on, this is the time of new protocols .

More by hotshot

Topics of interest

More Related Stories