Bitcoin as programmable money: Hashed timelock contracts

Matt ฿
ChainRift Research
Published in
3 min readNov 19, 2018

Bitcoin detractors enjoy gloating about the lack of sophistication in Bitcoin BScript logic, which foregoes the flexibility of full Turing-completeness for a robust security-oriented infrastructure. As we explored in last week’s post on multisig transactions, however, the protocol incorporates a number of features that extend its functionality and allow users to craft complex transactions. This week, we’ll take a look at Hashed Timelock Contracts (or HTLCs).

A HTLC is a particular kind of smart contract — it enables a person A to generate and hash a key, which is then sent to a person B. Person B can subsequently construct a transaction incorporating the hash, using a timelock to specify a window in which the funds can be redeemed by person A, or indeed, by anyone holding the key (note that such timelocks can also be used to make UTXOs unspendable prior to a given block height — worth exploring if you’re looking to up your HODL game). As such, if the key is not presented within a specified timeframe, the funds are returned to person B.

HTLCs have numerous applications, the most interesting of which is perhaps in building payment channels — though bidirectional payment channels are possible without their incorporation, they’re vital to scaling channels to work across a network with multiple hops, in such a way that person A could transact with person B even if not directly connected (as outlined in the Lightning Network whitepaper).

These kinds of applications are pivotal to the off-chain scaling of the Bitcoin protocol, seeing as competition over block space will likely lead to increased fees and delays in on-chain settlement (layer two solutions like LN allow near-instant and incredibly cheap transfers, albeit with some security/finality tradeoffs).

Of course, this is just one of the use cases that can be achieved. Another one has huge implications for the wider multicoin paradigm and sidechain/drivechain interconnectivity — cross-chain atomic swaps.

“Yet another blockchain for this ultra-niche use case? Why not?”

Many are beginning to catch on to the fact that blockchains existing in a void are going to suffer going forward. The dream of a permissionless peer-to-peer universe of self-sovereign finance quickly unravels when each trade needs to be executed through KYC on and off-ramps.

Developments in cross-chain atomic swaps are a memento mori to centralised exchanges, which may very well be eradicated as atomic swap solutions proliferate. Individuals have little choice when it comes to exchanging, say, Dentacoin for Bitcoin, without exposing themselves to counterparty risk, but this is poised to end. In order to trustlessly swap one coin for another, persons C and D need only leverage HTLCs:

  • C takes their pre-image and hashes it, publishing a transaction (intended for D) to the Bitcoin blockchain, with a set expiry time.
  • On their end, D does the same thing on the Dentacoin network, for the agreed-upon exchange rate, payable to the hash published by C.
  • When D’s transaction has been pushed to the Dentacoin blockchain, C can claim it. While C acquires the Dentacoin, the script forces the reveal of the pre-image, which D can then use to unlock the Bitcoin.

Hashed timelock contracts are yet another example of a remarkably powerful function bundled into the Bitcoin protocol, and are a crucial building block for developers wishing to anchor scalable layers into the blockchain or trustlessly conduct swaps with other networks.

As UI/UX improves, it’s clear that these smart contracts will pose a serious threat to the centralised trading hubs of today, spearheading a shift back to true peer-to-peer transactions and providing greater privacy, anonymity and independence to cryptocurrency users.

Cover photo modified from Pexels and comic strip from xkcd.

--

--