📜 ⬆️ ⬇️

Atomic Swaps In A Nutshell

As of July 2019, there are about 2000 cryptocurrencies, but there is no single exchange mechanism between different blockchains. In this article, we will simplifyly consider how one of the exchange technologies without intermediaries is arranged - atomic exchanges (atomic swaps).
Enjoy reading!


logo


Atomic Swaps: what is it and what is it eaten with?


Atomic swaps are a series of transactions on both blockchains that allow you to exchange coins without the participation of a third party and trust in the second participant in the exchange. In essence, a transaction is created, the output of which can be spent either with a certain key or after a time out.
Not all blockchains allow you to implement AtomicSwap. There are two main requirements:



Smart contracts? No, not heard

Smart contracts - in a broad sense, a "non-oblique" algorithm that performs actions under certain conditions and not otherwise known. In our case, a smart contract can be considered as a large black box, which is fed the recipient's address, the sender's address, the amount sent, the money hold time and assign a unique identifier (hash). The output is the desired conditional transaction.


Hash and his friends

The hash function is another character from a tale of higher mathematics. Converts a stream of input data to another value. The same data give the same result. Typically, the input is a large number, referred to as a secret key, and the output is a hash. Converting a hash back to a secret key is impossible.


When everything goes smoothly


Consider the exchange between Alice (yes, yes, Alice is no longer the same) and Bob. Alice has Bitcoin (btc), Bob has Ether (eth). Both characters have addresses on both blockchains. Prm .: large signs cryptocurrency symbolize blockchains.



Alice generates a key (a large number) and modestly tells her no one:



Based on the key, it considers the hash:



Creates a contract with a unique identifier equal to the hash:



Sends money to a contract. Now she can not spend them a certain time (timelock):


Time is up, or timelock

Timelock - the time after which the contract creator will be able to return the money if he sends a transaction of a certain type (let's call it "refund"). This transaction does not need a secret key. Another user can withdraw money to the recipient specified in the contract (important: not to his own) if he sends a transaction of another type (let's call it redeem) with the secret key.


In general, the transaction processing mechanism for a contract is as follows:


if (transaction_type == "refund" and sender == contract_creater and timelock < now()) { send_to(creator_address); } else if (transaction_type == "redeem" and hash(secret_key) == contract_id) { send_to(receiver_address); } 


Alice tells the hash to Bob. Now he can audit (check) all fields of the contract: make sure the addresses are correct, the amount sent, the time the money is stored on the contract (the period during which Alice will not be able to remove them):



If everything is good, Bob creates a contract on the second blockchain similar to Aliceen (with the same identifier). The recipient changes and the amount is possible (if the rate is not 1 to 1).



Alice is auditing the contract:



If it triples, it creates a transaction on Ethereum that spends money on Bob's contract. The only way to do this is to provide the key. Now he is known:



Alice gets eth:



Bob looks at the key and creates a transaction that spends money on Alice’s contract, using her key:



Success! The wolves are fed, the sheep are safe!


When the world is not perfect


However, sometimes something can go wrong. There are two places where the exchange can break. For this there is a mechanism that allows you to return the money to their rightful owners.


Suppose Alice as a decent girl sent the money to the contract, but after the audit, Bob changed his mind to part with the ethers:



Alice waits for the timelock specified in the contract to expire (say, 48 hours) and returns the bloodlock:



Another story tells about the fickle Alice, who did not want to continue the atomic exchange after Bob made a contract. Alternative scenario: Alice lost the key and cannot do it:



Bob waits for the time of blocking money (it is usually less than Alicin’s, so that Alice does not withdraw Bob’s money at the last minute, and then does not return her money; let's say 24 hours). Alice returns her money at the expiration of her timelock.



Conclusion


Atomic exchanges are very convenient for exchanging point-to-point, bypassing the exchanges and intermediaries. Participants undertake to pay only the transaction fee.


Summing up, in the exchange between A and C on the blockchains B1 and B2, we can distinguish the following steps:


  1. initialization of the contract for B1 by party A;
  2. listening to a contract on B1 with participant C;
  3. initialization of the contract for B2 by party C;
  4. listening to a contract for B2 by participant A;
  5. provision of a key and withdrawal of money for B2 by participant A;
  6. obtaining a key on B2 by party C;
  7. withdrawal of money for B1 by S.

The technology is new, but it is already being actively implemented. Perhaps in the near future she will be able to replace centralized and so ideologically strangers for the blockchain exchange.


')

Source: https://habr.com/ru/post/458646/


All Articles