# Hashed Timelock Contract (HTLC): Overview and Examples in Crypto

## What Is a Hashed Timelock Contract (HTLC)?

A hashed timelock contract (HTLC) is a type of smart contract used in blockchain applications. It reduces counterparty risk by creating a time-based escrow that requires a cryptographic passphrase for unlocking.

In practical terms, this means that the person receiving the funds in a transaction has to perform two actions to access the funds: enter the correct passphrase and claim payment within a specific timeframe. If they enter an incorrect passphrase or do not claim the funds within the timeframe, they lose access to the payment.

### Key Takeaways

*   A hashed timelock contract (HTLC) reduces counterparty risk in decentralized smart contracts by effectively creating a time-based escrow that utilizes a cryptographic passphrase.
*   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.

## Understanding Hashed Timelock Contracts (HTLCs)

A Hashed Timelock Contract (HTLC) uses several elements from existing [cryptocurrency](https://www.investopedia.com/terms/c/cryptocurrency.asp) transactions. For example, HTLC transactions use multiple signatures consisting of a private and public key to verify and validate transactions. However, two elements distinguish HTLC from standard cryptocurrency transactions with [smart contracts](https://www.investopedia.com/terms/s/smart-contracts.asp)—the hashlock and the time contract.

### Hashlock

The first element is the hashlock. A hashlock is a [hashed](https://www.investopedia.com/terms/h/hash.asp)—or cryptographically scrambled—version of a [public key](https://www.investopedia.com/terms/p/public-key.asp) generated by the person that initiated the transaction. The associated [private key](https://www.investopedia.com/terms/p/private-key.asp) is then used to unlock the original hash.

In HTLC, the initiating party generates a key and hashes it. The hash is stored in a pre-image, which is revealed during the final transaction. HTLCs are programmed to expire after a certain period passes or a specific number of [blocks](https://www.investopedia.com/terms/b/block-bitcoin-block.asp) are generated, creating a known termination date and time.

> **Important**
>
> To conduct a transaction using HTLC, both parties need to open channels with each other. This is generally done using a wallet, an exchange, or both.

### Timelock

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 cryptocurrency. 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.

[Atomic swaps](https://www.investopedia.com/terms/a/atomic-swaps.asp)—cross-chain trading between cryptocurrencies— are often achieved using HTLCs. Additionally, Bitcoin’s [lightning network (LN)](https://www.investopedia.com/terms/l/lightning-network.asp) also employs HTLCs.  

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

## The Process in a Hashed Timelock Contract

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 Litecoin private key and sends it to Bob. 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). 
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.
4.  Once Alice and Bob enter their keys and agree to the transaction, the smart contract executes it. Neither can change the transaction once it executes, which removes the possibility that one could trick the other by quickly canceling the exchange.

## What Is a Timelock Contract?

A timelock contract is a smart contract embedded in a blockchain that executes a transaction at a specific time. They are used in hashed timelock contracts and payment channels where specific payment times are needed.

## Does Bitcoin Have Smart Contracts?

Initially, Bitcoin's blockchain was not able to execute smart contracts. However, the Taproot upgrade in 2021 allowed the blockchain to use smart contracts in transactions.

## What Is a Smart Contract?

A smart contract is a program stored on a blockchain that executes when specific conditions are met.

## How Much Does a Smart Contract Cost?

On the Ethereum blockchain, a smart contract deployment takes gas, which costs Gwei (a lower denomination of ether). Depending on the complexity of the contract, it can cost billions of Gwei to deploy a smart contract. Less complex contracts like a simple exchange are much cheaper.

_Investing in cryptocurrencies and other Initial Coin Offerings (“ICOs”) is highly risky and speculative, and this article is not a recommendation by Investopedia or the writer to invest in cryptocurrencies or other ICOs. Since each individual's situation is unique, a qualified professional should always be consulted before making any financial decisions. Investopedia makes no representations or warranties as to the accuracy or timeliness of the information contained herein._

> **Article Sources**
>
> 1.  Builder's Guide to the LND Galaxy. "[Hashed Timelock Contract (HTLC)](https://docs.lightning.engineering/the-lightning-network/multihop-payments/hash-time-lock-contract-htlc)."
>     
> 2.  Liquality. "[Hash Time Locked Contracts (HTLCs) Explained](https://blog.liquality.io/hash-time-locked-contracts-htlcs-explained/)."
>     
> 3.  Builder's Guide to the LND Galaxy. "[Timelocks](https://docs.lightning.engineering/the-lightning-network/multihop-payments/timelocks)."
>     
> 4.  Monika, Bhatia and et al. "[Hash Time Locked Contract Based Asset Exchange Solution for Probabilistic Public Blockchains](https://link.springer.com/article/10.1007/s10586-022-03643-x)." _Cluster Computing_, vol. 25, December 2022, pp. 4189–4201.
>     
> 5.  Bitcoin Magazine. "[Understanding Taproot in a Simple Way](https://bitcoinmagazine.com/technical/understanding-taproot-in-a-simple-way)."
>     
> 6.  Wood, Gavin. "[Ethereum: A Secure Decentralised Generalised Transaction Ledger](https://ethereum.github.io/yellowpaper/paper.pdf)," Page 27.