What Is a Hashed Timelock Contract (HTLC)?

A Hashed Timelock Contract (HTLC) is a type of smart contract used in blockchain applications to eliminate counterparty risk by enabling the implementation of time-bound transactions. In practical terms, this means that recipients of a transaction have to acknowledge payment by generating cryptographic proof within a certain timeframe. Otherwise, the transaction does not take place.

Atomic swaps, cross-chain trading between cryptocurrencies is often achieved using HTLCs. In addition, Bitcoin’s lightning network (LN) also employs HTLCs.

Key Takeaways

  • A hashed timelock contract (HTLC) reduces counterparty risk in decentralized smart contracts by effectively creating a time-based escrow.
  • This type of smart contract requires the receiver of a payment to acknowledge it within a certain period of time or forfeit it.
  • Payments using HTLCs are conditional and so have efficiency benefits for blockchain transactions. This property makes HTLCs a fundamental tool used by the lightning network.

How a Hashed Timelock Contract Works

A Hashed Timelock Contract (HTLC) uses several elements from existing cryptocurrency transactions. For example, HTLC transactions use multiple signatures, which consist of a private-public key, to verify and validate transactions. But there are two elements that distinguish HTLC from standard cryptocurrency transactions or ordinary smart contracts.

The first element is the hashlock. A hashlock is a hashed, or cryptographically scrambled version of a public key generated by the originator of a transaction. The associated private key is then used to unlock the original hash. In HTLC, the originating party generates a key and hashes it. The hash is stored in a pre-image that is subsequently revealed during the final transaction. HTLCs are programmed to expire after a certain period of time or number of blocks generated, creating a known termination date.

The second important element of HTLC is a timelock. Two different timelocks are used to set time constraints on contracts generated using HTLC. The first one is CheckLockTimeVerify (CLTV). It uses a time base to lock and release bitcoins. This means that time constraints are hardcoded and coins are released only at a specific time and date or a specific height of block size.

The second one is CheckSequenceVerify (CSV). It is not dependent on time. Instead, it uses the number of blocks generated as a measure to keep track of when to finalize a transaction.

To conduct a transaction using HTLC, interested parties need to open channels with each other. 

Real-World Examples of a Hashed Timelock

Suppose Alice wants to exchange her Bitcoin for Litecoin from Bob. A typical HTLC transaction between them takes place as follows:

  1. Alice generates a hash from her private key and sends it to Bob on Litecoin blockchain. She also generates a pre-image of the hash by creating a nominal transaction. This pre-image will help her validate and finalize the transaction. 
  2. Bob also generates a hash from his key and sends it to Alice. In addition to this, he creates a pre-image by conducting a nominal transaction (in Litecoin) with Alice. 
  3. Once Alice receives Bob’s Litecoin transaction, she signs it using the original key that is already available with her in the pre-image. Bob does likewise on his end, using his private key to unlock Alice’s transaction.