npm install bitcoin-ts
The API is purely-functional, fully-typed and documented with TypeScript, and accepts and returns native ES2015 typed arrays.
Below is an example of SHA-256 and RIPEMD-160 being used to validate part of a Bitcoin/Bitcoin Cash transaction. We instantiate the WebAssembly versions of SHA-256 and RIPEMD-160, then we hash
pubkey, first with SHA-256, then with RIPEMD-160 to get the correct
Thoroughly tested against existing implementations
Each hash function implementation is compiled from the excellent, well-reviewed, Rust Crypto library. In addition, the WebAssembly versions are tested against
bcrypto (the library used in
bcoin), and the built-in Node.js hash function implementations (which use OpenSSL).
The generated WebAssembly binary is also auditable and deterministic — you can rebuild it easily yourself and verify that the NPM-distributed version is correct. Simply clone the repo from GitHub and follow the directions there.
You can see the benchmarks on GitHub. Here are some notable observations.
These benchmarks are all over the place
Performance varies drastically between applications. While there are clear themes in the above benchmarks, in many cases, there’s no obvious winner.
If hashing is a performance bottleneck in your application, you’ll need to benchmark the options specifically for your workload.
Bcoin is the fastest on Node.js
If you need the very best performance for Node.js, Bcoin is probably your answer. It outperforms the Node.js built-in methods in nearly every case.
Particularly for the SHA functions, Bcoin and the Node.js built-in implementations perform more than 2x better than WebAssembly. This appears to be a result of their access to hardware SHA256 acceleration (RIPEMD-160 for large inputs is 50% faster in WebAssembly than native).
WebAssembly is extremely fast everywhere
WebAssembly is also remarkably consistent — while other implementations rise and fall in the rankings based on workload, the WebAssembly implementations perform fairly consistently in all cases.
With this addition,
bitcoin-ts now includes all the cryptographic primitives used in most Bitcoin and Bitcoin Cash nodes and wallets. If you have any questions or feedback, or if you’re using
bitcoin-ts in your own projects, please let me know on Twitter.