Bitcoin's scripting language

Now, let's have a look at the programmability aspect of Bitcoin. Some blockchain cryptocurrencies are referred to as programmable money.

Bitcoin is indeed a programmable digital currency. It uses a programming language called Script in structuring and processing transactions. As the name suggests, this is a script type of language, which supports small programs called scripts. They automate the execution of a list of tasks, or commands, that otherwise could be executed one-by-one manually by a human operator.

Script is a high-level programming language with limited functionality, which is also domain-specific, meaning it's specialized to its application domain. Bitcoin's transaction script was specifically designed to be limited in scope, in order to require minimal processing and to be executable even on very simple devices. Its limited scope also enhances security because this leaves less space for coding errors. As you can imagine, any errors in a system processing money transfers can be quite costly.

Script is not a Turing-complete language, meaning that it is not general-purpose and cannot be used for programs solving just any problem. We'll talk more about Turing completeness when we discuss Ethereum in the coming chapters.

Bitcoin's transaction script includes functions that drive transaction execution. Each transaction script has the following steps:

  1. When a sender signs a transaction with a cryptographic key, the script locks the coins being sent, which are basically transaction inputs.
  2. These coins or transaction inputs can then be unlocked only by the receiver's private key.
  3. After that, the coins in the form of transaction outputs are transferred to the receiver's account and are controlled by the receiver's private key. Given these two steps of each Bitcoin transaction, the most common scripts used to validate transactions are a locking script and an unlocking script.

This is better illustrated in the following diagram:

Bitcoin transaction scripts can also be a bit more complex, involving several conditions to unlock coins or transaction outputs. In this way, it becomes possible to structure multi-signature transactions requiring more than one private key to unlock the coins. This can be used to send money to multiple receivers in one transaction or to send money to an entity with multiple stakeholders who have joint control over the funds. For instance, a company with several partners, which receives Bitcoin may find multi-signature transactions a useful way to manage such transfers.

Another useful feature for transaction structuring are time locks. Time-bound conditions can be included in a script, so that funds are unlocked at a specific time or after a specific period of time following an event.

There are other useful logical conditions that can be included in Bitcoin transaction scripts. These include conditional clauses, such as IF…THEN…ELSE. You are probably familiar with the IF and other conditional functions, if you have used spreadsheet software, such as Excel, or studied any type of programming. The purpose and effect of such clauses in a Bitcoin script are similar. The funds are unlocked only if certain conditions are present.

The various programming features of the Bitcoin script language can be combined together to produce more complex conditional transactions than a simple payment. For example, the escrow account type of transaction structures can be programmed and automated with Bitcoin scripts.

Traditional escrow accounts are typically created when two parties agree to a payment, but only if certain conditions have been met. The payer creates an escrow account with a respected third party (traditionally a bank) and transfers the funds to the escrow account. Once the necessary condition has been met, and the third party is able to verify that happened, it transfers the money to the intended recipient.

In Bitcoin, payment conditions can be programmed to automatically execute a transaction without any need for a third-party agent.

This logic and functionality is the basis for the smart contracts, which you may have heard of. We'll discuss smart contracts in more detail when we examine Ethereum, which takes smart contracts to the next level.