Home

Awesome

blockchainjs

Version build status Coverage Status Dependency status

A pure JavaScript library for node.js and browsers for easy data exchange between wallets and bitcoin network.

What is include blockchainjs?

blockchainjs have two abstraction level: Connector and Blockchain

Connector implements a common interface for remote service. For now available only one provider: chromanode.

Blockchain implements a common interface between connector and your wallet. You can use Naive (trust all data from remove service) or Verified (SPV implementation).

In addition to Verified blockchainjs has Storage interface for store headers. Memory and LocalStorage available for now.

API

Examples

Show UTXO on address touched

var blockchainjs = require('blockchainjs')
var connector = new blockchainjs.connector.Chromanode({networkName: 'testnet'})
var address = 'mp8XoMWnJzQwovninMdChQutPuhyHokJNc'

function showUTXO(address) {
  connector.addressesQuery([address], {status: 'unspent'})
    .then(function (result) {
      console.log('UTXO for ' + address + ':')
      result.transactions.forEach(function (unspent) {
        // var txOut = unspent.txid + ':' + unspent.outIndex
        // console.log(txOut + ' has ' + unspent.value + ' satoshi')
        // sorry, only txid and height available now
        console.log('Unspent in txid: ' + unspent.txid)
      })
      if (result.transactions.length === 0) {
        console.log('nothing...')
      }
      console.log('')
    })
}

connector.on(address, showUTXO)
connector.connect()
connector.subscribe({event: 'touchAddress', address: address})
showUTXO(address)

Show last header upon completion of sync process

var blockchainjs = require('blockchainjs')

var connector = new blockchainjs.connector.Chromanode({networkName: 'testnet'})
connector.connect()

var storage = new blockchainjs.storage.Memory({
  networkName: 'testnet',
  compactMode: true
})

var blockchain = new blockchainjs.blockchain.Verified(connector, {
  storage: storage,
  networkName: 'testnet',
  testnet: true,
  compactMode: true,
  chunkHashes: blockchainjs.chunkHashes.testnet
})

blockchain.on('syncStop', blockchainjs.util.makeSerial(function () {
  return blockchain.getHeader(blockchain.latest.hash)
    .then(function (header) {
      console.log('Current header: ', header)
    })
}))

License

Code released under the MIT license.

Copyright 2015 Chromaway AB

Todo