Hashed TimeLock Contract (HTLC) interconnection solution between blockchain platforms

Tram Ho

Since the glorious years of Bitcoin has opened a new blockchain era with hundreds of different types of blockchain, typically ETH, NEO, TRON, … Each blockchain was born to improve a certain weakness of the tissue. old blockchain models such as increasing speed, increasing applicability, being programmable, saving resources. Unlocking the promising growth possibilities of blockchain adoption in our daily lives. However, it is development in that direction that leads to blockchains being isolated from each other. Although it is completely possible to put tokens on centralized exchanges for exchange, it is more centralized than accounts because such a management platform is so insecure. The community dreamed of a decentralized exchange where every blockchain platform could be exchanged without any third party intervening, so the Hashed TimeLock Contract was born.

Problem

What if two people want to exchange 2 items? Person A sends person B and person B sends person A. So what if person B receives it and then explodes? Another way is to ask a 3rd person, when both people A and B give the item to the 3rd person then the 3rd person will give it to both people B and A. But what if the third person took it and ran away? And the current situation, the way of trading in some centralized exchanges like Binance Okex is like that. That is why people want a decentralized floor for a long time. Surely some of you will be wondering, does not writing a smart contract on eth solve the 3rd person problem? . Yes, but what if the user wants to exchange between two different types of blockchain? And the hashed timelock contract is an answer to this question.

What is Hashed TimeLock Contract?

Hashed TimeLock Contract (HTLC) is a type of smart contract that is used to exchange different coins and tokens without the need for a third party or worrying about scam risks. By allowing transactions within a certain time limit. More specifically, the recipient of the HTLC transaction must confirm the payment by sending a piece of encryption key to authenticate within a specified timeframe (in block numbers). If the receiver declines or does not request payment, the amount will be returned to the original sender.

There are two important elements in HTLC: Hashlock and timelock:

Hashlock : The person who first creates one creates a secret and then uses an encryption function to hash out a code called a hashlock.

Timelock : is a function that restricts transactions to a specific time (calculated by the number of blocks).

Flow of operation

Our scenario will include two people Alice who holds Ethereum and Bob who holds Bitcoin. These two will exchange Eth and BTC without having to trust each other. This process includes two parties and four accounts, so the process will include:

Alice 2 accounts: An account that sends ETH and an account that receives BTC

Bob 2 accounts: Account sending BTC and account receiving ETH

The process will go as follows:

  • Alice chooses a number or a random string of characters named secret and then uses a hash function to generate the hasklock
  • Create a Lock on Eth contract, including necessary information such as hashlock, timelock, input amount, receiver, sender, …
  • Send Bob that hashlock
  • Bob also creates a Lock on BTC including the hachlock that Alice sent, timelock, input amount, receiver, sender, …
  • Alice will use the secret to decrypt Bob’s Lock to receive BTC and the secret will be publicized on the network and Bob will know
  • Bob uses the public secret to unlock Alice’s Lock and receive ETH

Demo

I will demo one-way code from ETH

Contract will include 3 main functions which are newContract , withdraw , refund and in addition 2 functions check status of Lock

  • newContract: this is the function to create a Lock with the necessary parameters. After the creation is complete, it will create a NewContract public emit on the network
  • withdraw: this is a function for receiving users to enter Lock id and secret. First check the time to see if the time has expired, then use the function sha256 hash and compare vs hashlock if it is correct, the transaction will be done and then public private key via emit.
  • refund: This function is only run when the time is over but when the Lock vs the purpose is created for the creator to withdraw money

Contract will consist of 5 positions

  • INVALID: This is the state in which contracts [id] already existed. Since the id is taken as the hash of sender, receiver, inputAmount, hashLock, expiration , it can still match. And if the same, it will not be able to create Lock
  • ACTIVE: this is the state for which contracts [id] are valid.
  • WITHDRAWN: after the receiver withdraws money, Lock will move to withdrawn
  • EXPIRED: this is the status of the recipient who did not withdraw and has exceeded the specified time period (expiration)
  • REFUNDED: Only if the contract is expired, the creator can go back and withdraw his money and after the withdrawal is completed, the Lock will be refunded.

In addition, there are 2 functions to check status as well as 3 emit to catch the events in the contract. The most important is that 2 emits are NewContract and withdrawn because newContract will public Id of Lock and withdrawn will public secret of the creator on the network.

summary

I just explained a bridge from eth, and the BTC side is likewise still needing 3 basic functions: newContract, withdraw and refund and comply with the rules of time as well as having public events on the network when someone. manipulation . In the next article I will write about Market Maker so that when combined, we have a decentralized floor vs heart, htlc.

Reference

Share the news now

Source : Viblo