Go to file
Braydon Fuller bcdf75a5e1 Add environment variable to travis to compile for testing. 2015-07-21 13:49:20 -04:00
benchmarks Regtest testing 2015-07-21 13:06:43 -04:00
bin Fix debug flag and readme formatting. 2015-07-21 13:19:18 -04:00
etc Patch update for wallet inclusion under the test environment 2015-07-21 13:07:23 -04:00
example Removed extra files and updated config options. 2015-07-21 13:23:38 -04:00
integration Removed extra files and updated config options. 2015-07-21 13:23:38 -04:00
lib Update tests to pass network option with regtest option. 2015-07-21 13:47:25 -04:00
platform 1. Updated patch for v0.11.0 2015-07-13 16:34:29 -04:00
src Regtest testing 2015-07-21 13:06:43 -04:00
test Update tests to pass network option with regtest option. 2015-07-21 13:47:25 -04:00
.gitignore 1. Updated patch for v0.11.0 2015-07-13 16:34:29 -04:00
.jshintrc Cleanup configuration options 2015-07-21 11:16:12 -04:00
.npmignore bitcoind.js 2014-08-12 15:04:13 -04:00
.travis.yml Add environment variable to travis to compile for testing. 2015-07-21 13:49:20 -04:00
LICENSE Updated the patch for a better version of LDFLAGS. 2015-06-24 17:06:27 -04:00
Makefile Made the bindings work with node v0.12. There was A LOT of api breakages in node and v8 since! 2015-06-08 17:18:06 -04:00
PATCH_VERSION 1. Updated patch for v0.11.0 2015-07-13 16:34:29 -04:00
README.md Fix debug flag and readme formatting. 2015-07-21 13:19:18 -04:00
binding.gyp Changed the order in which the header files were being included. Due to the nan header using the system version of endian.h, but the bitcoin-related headers need the compat/endian.h. The two (at least on Linux) are not compatible. 2015-07-14 15:54:39 -04:00
index.js rename bitcoind to daemon 2015-07-20 10:39:07 -06:00
index_stripped.js cleaned up the project! 2015-07-02 14:59:14 -04:00
package.json Regtest testing 2015-07-21 13:06:43 -04:00

README.md

bitcoind.js

Build Status Coverage Status

A Node.js module that adds a native interface to Bitcoin Core for querying information about the Bitcoin blockchain. Bindings are linked to Bitcore Core compiled as a shared library.

Install

git clone https://github.com/bitpay/bitcoind.js.git
cd bitcoind.js
npm install

Example Usage


var BitcoinNode = require('bitcoind.js');

var configuration = {
  datadir: '~/.bitcoin',
  network: 'testnet'
};

var node = new BitcoinNode(configuration);

node.on('ready', function() {
  console.log('Bitcoin Node Ready');
});

node.on('error', function(err) {
  console.error(err);
});

node.chain.on('addblock', function(block) {
  console.log('New Best Tip:', block.hash);
});

API Documentation

Get Unspent Outputs

var address = '15vkcKf7gB23wLAnZLmbVuMiiVDc1Nm4a2';
var includeMempool = true;
node.getUnspentOutputs(address, includeMempool, function(err, unspentOutputs) {
  //...
});

View Balances

var address = '15vkcKf7gB23wLAnZLmbVuMiiVDc1Nm4a2';
var includeMempool = true;
node.getBalance(address, includeMempool, function(err, balance) {
  //...
});

Get Outputs

var address = '15vkcKf7gB23wLAnZLmbVuMiiVDc1Nm4a2';
var includeMempool = true;
node.getOutputs(address, includeMempool, function(err, outputs) {
  //...
});

Get Transaction

var txid = 'c349b124b820fe6e32136c30e99f6c4f115fce4d750838edf0c46d3cb4d7281e';
var includeMempool = true;
node.getTransaction(txid, includeMempool, function(err, transaction) {
  //...
});

Get Block

var blockHash = '00000000d17332a156a807b25bc5a2e041d2c730628ceb77e75841056082a2c2';
node.getBlock(blockHash, function(err, block) {
  //...
});

You can log output from the daemon using:

$ tail -f ~/.bitcoin/debug.log

^C (SIGINT) will call StartShutdown() in bitcoind on the node thread pool.

Daemon Documentation

  • daemon.start([options], [callback]) - Start the JavaScript Bitcoin node.
  • daemon.getBlock(blockHash|blockHeight, callback) - Get any block asynchronously by block hash or height as a node buffer.
  • daemon.getTransaction(txid, blockhash, callback) - Get any tx asynchronously by reading it from disk.
  • daemon.log(message), daemon.info(message) - Log to standard output.
  • daemon.error(message) - Log to stderr.
  • daemon.close([callback]) - Stop the JavaScript bitcoin node safely, the callback will be called when bitcoind is closed. This will also be done automatically on process.exit. It also takes the bitcoind node off the libuv event loop. If the daemon object is the only thing on the event loop. Node will simply close.

Building

There are two main parts of the build, compiling Bitcoin Core and the Node.js bindings. You can run both by using npm install and set environment variable, $BITCOINDJS_ENV to 'test' or 'debug'. Both 'test' and 'debug' build libbitcoind with debug symbols whereas 'test' adds wallet capability so that regtest can be used.

Node.js Bindings

$ node-gyp rebuild

And then with debug:

$ node-gyp -d rebuild

To be able to debug you'll need to have gdb and node compiled for debugging with gdb using --gdb (node_g), and you can then run:

$ gdb --args node_g path/to/example.js

To run mocha from within gdb (notice _mocha and not mocha so that the tests run in the same process):

$ gdb --args node /path/to/_mocha -R spec integration/index.js

To run integration tests against testnet or livenet data:

$ cd integration
// modify index.js configuration, and then run mocha
$ mocha -R spec index.js

To run the benchmarks (also with livenet or testnet data):

$ cd benchmarks
$ node index.js

Bitcoin Core

Dependencies

Most of all the dependencies for building Bitcoin Core are needed, for more information please see the build notes for Unix and Mac OS X. These dependencies are needed:

  • Boost

    • Boost Header Files (/usr/include/boost)
    • The Boost header files can be from your distro (like Debian or Ubuntu), just be sure to install the "-dev" versions of Boost (sudo apt-get install libboost-all-dev).
  • OpenSSL headers and libraries (-lcrypto and -lssl), this is used to compile Bitcoin.

  • If target platform is Mac OS X, then OS X >= 10.9, Clang and associated linker.

Shared Library Patch

To provide native bindings to JavaScript (or any other language for that matter), Bitcoin code, itself, must be linkable. Currently, Bitcoin Core provides a JSON RPC interface to bitcoind as well as a shared library for script validation (and hopefully more) called libbitcoinconsensus. There is a node module, node-libbitcoinconsensus, that exposes these methods. While these interfaces are useful for several use cases, there are additional use cases that are not fulfilled, and being able to implement customized interfaces is necessary. To be able to do this a few simple changes need to be made to Bitcoin Core to compile as a shared library.

The patch is located at etc/bitcoin.patch and adds a configure option --enable-daemonlib to compile all object files with -fPIC (Position Independent Code - needed to create a shared object), exposes leveldb variables and objects, exposes the threadpool to the bindings, and conditionally includes the main function.

Every effort will be made to ensure that this patch stays up-to-date with the latest release of Bitcoin. At the very least, this project began supporting Bitcoin Core v0.10.2.

Building

There is a build script that will download Bitcoin Core v0.10.2 and apply the necessary patch, compile libbitcoind.{so|dylib} and copy the artifact into platform/<os_dir>. Unix/Linux uses the file extension "so" whereas Mac OSX uses "dylib" (bitcoind compiled as a shared library).

$ cd /path/to/bitcoind.js
$ ./bin/build-libbitcoind

The PATCH_VERSION file dictates what version/tag the patch goes clean against.

There is a config_options.sh that has the configure options used to build libbitcoind. make will then compile libbitcoind/src/.libs/libbitcoind.{so|dylib}. This will completely ignore compiling tests, QT object files and the wallet features in bitcoind/libbitcoind.{so|dylib}.

Or you can also manually compile using:

configure and make (Linux/Unix)

$ cd libbitcoind
$ ./configure --enable-tests=no --enable-daemonlib --with-gui=no --without-qt --without-miniupnpc --without-bdb --enable-debug --disable-wallet --without-utils
$ make

configure and make (Mac OS X) --note the addition of prefix to the location where the libbitcoind library will be installed.

$ cd libbitcoind
$ ./configure --enable-tests=no --enable-daemonlib --with-gui=no --without-qt --without-miniupnpc --without-bdb --enable-debug --disable-wallet --without-utils --prefix=<os_dir/lib>
$ make

And then copy the files (with Unix/Linux):

$ cp -P libbitcoind/src/.libs/libbitcoind.so* platform/<os_dir>

With Mac OS X:

$ cp -R libbitcoind/src/.libs/libbitcoind.*dylib platform/osx/lib

License

Code released under the MIT license.

Copyright 2013-2015 BitPay, Inc.

  • bitcoin: Copyright (c) 2009-2015 Bitcoin Core Developers (MIT License)
  • bcoin (some code borrowed temporarily): Copyright Fedor Indutny, 2014.