btcscript – the script package from btcd

Following up on our previous releases (btcwire, btcjson, btcutil, and btcdb) we are happy to announce btcscript, the script package from btcd:

btcscript provides code and data structures to parse and execute bitcoin scripts. The scripting system bitcoin uses is a stack-based, FORTH-like language. It is not turing complete by design (as adding a full turing complete language opens up all sorts of potential complications and problems), but it still provides a fair amount of power.

Bitcoin Scripts

Without looking a little closer into how bitcoin works, it is not immediately clear why it would even need a scripting language. As we saw in btcdb, the basic piece of the bitcoin system is the block chain. The block chain is essentially just a list of transactions organized together in blocks (where each block has a hash of the previous block).

Each block is made up a number of transactions (tx). Txs primarily consist of a list of inputs and a list of outputs, each of which has an attached script. The script from a tx’s output usually contains instructions that can verify that the person attempting to spend the transaction has the correct private key. This will usually be via verifying a signature, or providing a piece of data with the correct hash. The input script on a tx which is spending the output of a previous tx will contain a script adding the required signatures and public keys to prove that they are the owners of that output and thus may spend it. The script providing the keys (the scriptPubkey) is executed first in full, followed by the tx from the previous output (the scriptSig), which operates on the stack left by the scriptPubKey to verify the credentials provided.

The simplest tx will transfer the output of a previous tx to a different address. More complex transactions will split the money into more than one part, giving some to one address and some to another, or may contain several inputs that are transferred to one or more outputs. Others may require multiple signatures in order to implement split wallets (where you need verification from say a phone and a computer to spend a coin) or escrow services where two out of three parties need to agree in order to transfer funds.

A description of the script language can be found on the bitcoin wiki. All defined instructions should be supported.


We are also releasing btcec:

btcec is a package to provide the elliptical curve cryptographic functions needed for the bitcoin scripts. It is partially based on work by ThePiachu (whom we would like to thank for making public code we could start with).


Currently the test coverage for btcscript is around 90%. As with the other packages, we hope to increase this in the coming weeks with the ultimate goal of 100% coverage (or as close to that as is practical). Along with this type of automated testing, for btcscript, the block chain itself provides a wealth of test cases. The ability to validate all transactions in the blockchain (over 18 million at last count) provides us with additional confidence that btcscipt follows the specification for the transaction scripts and the behavior of the standard client.

Coming Up Next

We have several additional components for btcd which we will release in this fashion once their completeness and test coverage reach the same standards as the current public components. We also plan on continuing to improve the test coverage (except for btcwire which is already at 100%!) and add additional features for the public components. Stay tuned for the next piece and please let us know what you think or are doing with the pieces that are already out there.

Leave a Reply

Your email address will not be published. Required fields are marked *