bitcoin-script
Advanced tools
Comparing version 0.0.2 to 0.0.3
@@ -26,3 +26,3 @@ { | ||
"homepage": "crmarsh.com/script/", | ||
"version": "0.0.2" | ||
"version": "0.0.3" | ||
} |
@@ -1,18 +0,39 @@ | ||
script | ||
bitcoin-script | ||
====== | ||
JavaScript implementation of Script, Bitcoin's scripting language, along with a Script Playground, deployed [here](http://www.crmarsh.com/script-playground/). The original ES6 source can be found on [GitHub](https://github.com/crm416/script) with the compiled ES5 package available on [npm](https://www.npmjs.com/package/bitcoin-script). | ||
JavaScript implementation of Script, Bitcoin's scripting language, along with a Script Playground, deployed [here](http://www.crmarsh.com/script-playground/). See my [blog post](crmarsh.com/script/) for more. | ||
## How It Works | ||
The original ES6 source can be found on [GitHub](https://github.com/crm416/script). | ||
Script programs are compiled to JavaScript using [Jison](http://zaach.github.io/jison/), with behavior following the specification outlined on the [Bitcoin Wiki](https://en.bitcoin.it/wiki/Script). BigInteger and elliptical curve operations are off-loaded to the [big-integer](https://www.npmjs.org/package/big-integer) and [ecdsa](https://www.npmjs.org/package/ecdsa) packages, respectively. | ||
## Usage | ||
The live editor is based off [Joel Burget's](http://joelburget.com/) [react-live-editor](https://github.com/joelburget/react-live-editor/). | ||
This package can be used to evaluate Bitcoin scripts as follows: | ||
### Core Behavior | ||
```js | ||
var evaluate = require('bitcoin-script').evaluate; | ||
var script = 'OP_1 OP_VERIFY'; | ||
evaluate(script); | ||
// => true | ||
``` | ||
Script is primarily a stack-based programming language. That is, it operates by maintaining a stack, onto which it pushes and pops as necessary. It is relatively simple, containing if-statements, but no looping. The standard library includes functions for performing basic arithmetic and cryptographic operations, all of which can be found on the [Wiki](https://en.bitcoin.it/wiki/Script). | ||
Alternatively, you can use the lock-unlock paradigm, which concatenates the scripts before evaluating: | ||
When the compiler runs over a Script program, it returns an object of the following form: | ||
```js | ||
var unlock = require('bitcoin-script').unlock; | ||
var scriptSig = 'OP_1'; | ||
var scriptPubKey = 'OP_VERIFY'; | ||
unlock(scriptSig, scriptPubKey); | ||
// => true | ||
``` | ||
Further [examples](https://github.com/crm416/script/tree/master/src/examples) and [tests](https://github.com/crm416/script/tree/master/src/__tests__) are available in the [GitHub repo](https://github.com/crm416/script/). | ||
## How It Works | ||
(A longer explanation of Script and this implementation can be found in my [blog post](crmarsh.com/script/).) | ||
Script programs are compiled to JavaScript using [Jison](http://zaach.github.io/jison/), with behavior following the specification outlined on the [Bitcoin Wiki](https://en.bitcoin.it/wiki/Script). For those unfamiliar, Script is a simple, stack-based programming language used to validate transactions in the Bitcoin protocol. | ||
When the parser runs over a Script program, it returns an object of the following form: | ||
```js | ||
@@ -25,6 +46,4 @@ { | ||
This allows for inspection into the compiled code, as well as the return value (`true` or `false`) of the Script. | ||
This allows for inspection into the compiled code (e.g., for use in the live editor), as well as the return value (`true` or `false`) of the Script. | ||
(Note that the function exported by `index.js` abstracts this object away, simply returning the result of executing the script, i.e., the `value` field.) The compiler throws if the Script does not terminate with a proper terminating operation, e.g., `OP_RETURN`, `OP_VERIFY`, etc. | ||
### Deviations From the Spec | ||
@@ -36,38 +55,8 @@ | ||
- Signatures aren't generated by hashing transaction inputs and outputs (as the snippets only exist in isolation); instead, the protocol expects users to sign a simple nonce (in this case, the string 'Secure'). | ||
- Any hex data is pushed to the state with no limitations. | ||
Operations labeled as _Disabled_ are not implemented. | ||
## Usage | ||
This repository can be roughly split into two parts: | ||
1. The core Script-to-JavaScript compiler. | ||
2. The Script live editor. | ||
### Compiler | ||
In its simplest form, usage of (1) might look like the following: | ||
```js | ||
var evaluate = require('./index.js').evaluate; | ||
var script = 'OP_0 OP_1ADD OP_VERIFY'; | ||
evaluate(script); | ||
``` | ||
In this case, `evaluate` would return `true`. Additional examples can be found in `__tests__/test-stack.js` and `__tests__/test-crypt.js`. | ||
If you edit the Jison script, you can re-build the compiler with `npm run compile`. The compiler can also be bundled for usage in the browser with `npm run bundle`, which produces a bundle at `bundle.js`. | ||
### Live Editor | ||
To build the live editor, run `make` from the `live-editor` directory, followed by `python -m SimpleHTTPServer`. Once running, you can head to `localhost:8000/script-compiler.html`. | ||
(You will need to run `npm run compile` before you can run the live editor, as the compiler must be generated from the Jison file, but it's unnecessary to bundle it, since the live editor takes care of that.) | ||
### Testing | ||
Unit tests can be run with `npm run test`, which will execute the [Jest](https://facebook.github.io/jest/) test runner. | ||
## License | ||
MIT. |
42405
8
61