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 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—the hashlock and the time contract.
Hashlock
The first element is the hashlock. A hashlock is a hashed—or cryptographically scrambled—version of a public key generated by the person that initiated the transaction. The associated private key 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 are generated, creating a known termination date and time.
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—cross-chain trading between cryptocurrencies— are often achieved using HTLCs. Additionally, Bitcoin’s lightning network (LN) 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:
- 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.
- 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).
- 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.
- 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.