Note, bitcoin-ts was renamed to Libauth in June 2020.

Evaluate and Debug Bitcoin Cash Scripts in JavaScript

A full TypeScript implementation of the BCH virtual machine is now available in bitcoin-ts, including both standard evaluation and a…

A full TypeScript implementation of the BCH virtual machine is now available in bitcoin-ts, including both standard evaluation and a step-by-step debugging mode. The VM passes Bitcoin ABC’s script_tests.json test vectors, as well as some additional testing done by bitcoin-ts.

What is Bitcoin Script?

Bitcoin transactions move money from inputs to outputs. For a transaction to be valid, every input must be checked by a “virtual machine which executes instructions called Bitcoin Script. For more information see the video: Bitcoin Script For Beginners.

Interactive Demo: Using the New VM

The example below fetches a transaction from the blockchain and checks the validity of the first input. At each step, it logs the opcode being executed and the contents of the stack. Click run to see the results.

Inspectable Program State

Because the program state is inspectable at each step, it’s easy to understand what’s happening. Errors are clear and helpful, and the library includes full opcode descriptions, including the new BCH opcodes.

Stateless VM

The virtual machine implementation is “stateless” — all methods are pure functions which accept an input and return a new output. Opcodes are also defined as simple functions which accept a program state and return the resulting state. This makes it easier to re-use elements of the codebase when trying new ideas (e.g. prototyping a new opcode).

Universal, Modular, No Dependencies

As with the rest of bitcoin-ts, the code is fully portable — only universal APIs are used, so the same code runs on both Node.js and browsers. Because the library is fully modular, it can be easily tree-shaken to reduce bundle-size in web applications. bitcoin-ts also has no dependencies, so it’s easier to audit and maintain.

See it in Action

This implementation powers Bitauth IDE, an interactive Bitcoin script editor and debugging environment.

You can find the code for Bitauth IDE on GitHub.

Please give the new APIs a try and let me know what you think. If you find this useful, please star the bitcoin-ts repo on GitHub. Thanks!