bitpay / chainlib-bitcoin

A Bitcoin blockchain library for Node.js
MIT License
6 stars 7 forks source link

ChainLib Bitcoin

Build Status Coverage Status

A library for building Bitcoin chain based databases.

Chainlib-bitcoin is not actively maintained anymore. Please check out bitcoind.js, which is similar, but runs bitcoind in the same node process and uses it to query blocks, transactions, unspent outputs and the mempool.

Getting Started

Install

git clone git@github.com:bitpay/chainlib-bitcoin.git
cd chainlib-bitcoin
npm install

Example Usage


var BitcoinNode = require('chainlib-bitcoin').RPCNode;

var privkey = 'tprv8ZgxMBicQKsPdj1QowoT9z1tY5Et38qaMjCHZVoPdPFb6narfmYkqTygEVHfUmY78k3HcaEpkyNCAQDANaXtwNe1HLFvcA7nqYj1B7wTSTo';

var configuration = {
  db: {
    xprivkey: privkey,
    path: './bitcoin-testnet.db'
  },
  p2p: {
    addrs: [
      {
        ip: {
          v4: '127.0.0.1'
        },
        port: 18333
      }
    ],
    dnsSeed: false
  },
  rpc: {
    host: 'localhost',
    port: 18332,
    user: 'user',
    pass: 'password',
    ssl: false,
    sslStrict: false
  },
  testnet: true
};

var node = new BitcoinNode(configuration);

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) {
  //...
});

Tests and Coverage

To run all of the tests:

npm run test
npm run coverage

To run a single test file in watch mode (useful for developing):

mocha -w -R spec test/db.unit.js