Go to file
Ryan X. Charles 8a41414114 add caching feature for bitcore.X requires
Normally when one loads the bitcore object, all classes are loaded into memory,
even if they are not used. This comment updates this so that the classes are
only required when they are actually used, preserving memory and making bitcore
as small and light for each project. This code is only relevant for node, since
the browser will have the ability to compile only the selected components.
Also, the Key class does not yet work with this, but everything else does.
2014-03-10 16:47:52 -04:00
browser merge master changes 2014-03-10 15:00:25 -03:00
examples support soop with browser all test passing 2014-03-08 18:42:08 -03:00
src Rename compiled binding to KeyModule. 2013-07-10 15:32:38 -04:00
test merge master changes 2014-03-10 15:00:25 -03:00
util Merge remote-tracking branch 'maraoz/feature/add-bitcoin-core-tests' 2014-03-09 12:07:11 -04:00
.gitignore load test data from files 2014-02-24 11:59:43 -03:00
.jshintrc tests work in browser and node 2014-02-04 16:37:34 -03:00
Address.js all classes working with soop and test passing 2014-03-05 16:11:16 -03:00
Block.js all classes working with soop and test passing 2014-03-05 16:11:16 -03:00
Bloom.js all classes working with soop and test passing 2014-03-05 16:11:16 -03:00
Buffers.monkey.js add some needed monkey patches 2013-07-18 12:01:12 -04:00
Connection.js all classes working with soop and test passing 2014-03-05 16:11:16 -03:00
Deserialize.js replace tabs with 2 spaces using "expand" 2014-01-16 06:21:35 -03:00
Gruntfile.js fix gruntfile 2014-03-10 14:55:30 -03:00
Key.js remove constructor params in browser version of KeyModule 2014-03-04 17:45:27 -03:00
LICENSE Update LICENSE 2014-02-07 21:08:22 -05:00
Number.monkey.js add some needed monkey patches 2013-07-18 12:01:12 -04:00
Opcode.js all classes working with soop and test passing 2014-03-05 16:11:16 -03:00
Peer.js all classes working with soop and test passing 2014-03-05 16:11:16 -03:00
PeerManager.js fix mocha tests 2014-03-10 13:10:52 -04:00
PrivateKey.js all classes working with soop and test passing 2014-03-05 16:11:16 -03:00
README.md make browser tests work by including testdata 2014-03-08 21:34:21 -05:00
RpcClient.js all classes working with soop and test passing 2014-03-05 16:11:16 -03:00
SIN.js all classes working with soop and test passing 2014-03-05 16:11:16 -03:00
SINKey.js all classes working with soop and test passing 2014-03-05 16:11:16 -03:00
Script.js Merge remote-tracking branch 'maraoz/feature/add-bitcoin-core-tests' 2014-03-09 12:07:11 -04:00
ScriptInterpreter.js Merge remote-tracking branch 'maraoz/feature/add-bitcoin-core-tests' 2014-03-09 12:07:11 -04:00
Sign.js replace tabs with 2 spaces using "expand" 2014-01-16 06:21:35 -03:00
Transaction.js Merge remote-tracking branch 'matiu/feature/support-soop-browser' 2014-03-08 19:30:21 -05:00
Wallet.js all classes working with soop and test passing 2014-03-05 16:11:16 -03:00
WalletKey.js all classes working with soop and test passing 2014-03-05 16:11:16 -03:00
binding.gyp Specify OpenSSL lib path so that bitcore references to OpenSSL will link to separate OpenSSL DLL in Windows (following bignum@1.6 pattern). Fixes issue #106 2014-02-27 16:54:21 -08:00
bitcore.js add caching feature for bitcore.X requires 2014-03-10 16:47:52 -04:00
config.js fix logger variable name 2014-01-14 15:59:38 -03:00
const.js replace tabs with 2 spaces using "expand" 2014-01-16 06:21:35 -03:00
networks.js fix buffertool's fill calls 2014-02-18 16:03:44 -03:00
package.json add grunt browserify package so "npm install" works 2014-03-08 21:52:47 -05:00

README.md

Bitcore

A pure, powerful core for your bitcoin project.

Bitcore is a complete, native interface to the Bitcoin network, and provides the core functionality needed to develop apps for bitcoin.

#Principles Bitcoin is a powerful new peer-to-peer platform for the next generation of financial technology. The decentralized nature of the Bitcoin network allows for highly resilient bitcoin infrastructure, and the developer community needs reliable, open-source tools to implement bitcoin apps and services.

Bitcore unchains developers from fallible, centralized APIs, and provides the tools to interact with the real Bitcoin network.

#Get Started

Bitcore runs on node, and can be installed via npm:

npm install bitcore

It is a collection of objects useful to bitcoin applications; class-like idioms are enabled via Soop. In most cases, a developer will require the object's class directly:

var Address = require('bitcore/Address');

#Examples

Some examples are provided at the examples path. Here are some snippets:

Validating an address

Validating a Bitcoin address:

var Address = require('bitcore/Address');

var addrStrings = [
  "1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa",
  "1A1zP1eP5QGefi2DMPTfTL5SLmv7Dixxxx",
  "A1zP1eP5QGefi2DMPTfTL5SLmv7Dixxxx",
  "1600 Pennsylvania Ave NW",
].map(function(addr) {
  return new Address(addr);
});

addrStrings.forEach(function(addr) {

  try {
    addr.validate();
    console.log(addr.data + ": is valid");
  } catch(e) {
    console.log(addr.data + ": is not a valid address. " + e);
  }

});

Monitoring Blocks and Transactions

For this example you need a running bitcoind instance with RPC enabled.

var util        = require('util');
var networks    = require('bitcore/networks');
var Peer        = require('bitcore/Peer');
var PeerManager = require('soop').load('bitcore/PeerManager',
  {network: networks.testnet});

var handleBlock = function(info) {

  console.log('** Block Received **');
  console.log(info.message);

};

var handleTx = function(info) {

  var tx = info.message.tx.getStandardizedObject();

  console.log('** Block TX **');
  console.log(tx);

};

var handleInv = function(info) {

  console.log('** Block Inv **');
  console.log(info.message);

  var invs = info.message.invs;
  info.conn.sendGetData(invs);

};

var peerman = new PeerManager();

peerman.addPeer( new Peer('127.0.0.1', 18333) );
  
peerman.on('connection', function(conn) {
  conn.on('inv',   handleInv);
  conn.on('block', handleBlock);
  conn.on('tx',    handleTx);
});

peerman.start();

PeerManager will emit the following events: 'version', 'verack', 'addr', 'getaddr', 'error' 'disconnect'; and will relay events like: 'tx', 'block', 'inv'. Please see PeerManager.js, Peer.js and Connection.js

Creating and sending a Transaction through P2P

For this example you need a running bitcoind instance with RPC enabled.

var networks    = require('bitcore/networks');
var Peer        = require('bitcore/Peer');
var Transaction = require('bitcore/Transaction');
var Address     = require('bitcore/Address');
var Script      = require('bitcore/Script');
var coinUtil    = require('bitcore/util/util');
var PeerManager = require('soop').load('bitcore/PeerManager',
  {network: networks.testnet});

var createTx = function() {

  var TXIN   = 'd05f35e0bbc495f6dcab03e599c8f5e32a07cdb4bc76964de201d06a2a7d8265';
  var TXIN_N = 0;
  var ADDR   = 'muHct3YZ9Nd5Pq7uLYYhXRAxeW4EnpcaLz';
  var VAL    = '1.234';

  var txobj = {
    version:   1,
    lock_time: 0,
    ins:       [],
    outs:      []
  }

  var txin = {
    s: coinUtil.EMPTY_BUFFER, // Add signature
    q: 0xffffffff
  };

  var hash = new Buffer(TXIN.split('').reverse(), 'hex');

  var vout    = parseInt(TXIN_N);
  var voutBuf = new Buffer(4);

  voutBuf.writeUInt32LE(vout, 0);
  txin.o = Buffer.concat([hash, voutBuf]);
  txobj.ins.push(txin);

  var addr     = new Address(ADDR);
  var script   = Script.createPubKeyHashOut(addr.payload());
  var valueNum = coinUtil.parseValue(VAL);
  var value    = coinUtil.bigIntToValue(valueNum);

  var txout = {
    v: value,
    s: script.getBuffer(),
  };
  txobj.outs.push(txout);

  return new Transaction(txobj);

};

var peerman = new PeerManager();
peerman.addPeer(new Peer('127.0.0.1', 18333));

peerman.on('connect', function(conn) {

  var conn = peerman.getActiveConnection();

  if (conn) {
    conn.sendTx(createTx());
  }

  conn.on('reject', function () {
    console.log('Transaction Rejected'); 
  });

});

peerman.start();

Consuming bitcoind RPC

For this example you need a running bitcoind instance with RPC enabled.

var util      = require('util');
var RpcClient = require('bitcore/RpcClient');
var hash      = process.argv[2] || '0000000000b6288775bbd326bedf324ca8717a15191da58391535408205aada4';

 var config =  {   
   protocol: 'http',
   user:     'user',
   pass:     'pass',
   host:     '127.0.0.1',
   port:     '18332',
};
 
var rpc   = new RpcClient(config);

rpc.getBlock(hash, function(err, ret) {

  if(err) {
    console.error("An error occured fetching block", hash);
    console.error(err);
    return;
  }

  console.log(ret);

});

Check the list of all supported RPC call at RpcClient.js

Parsing a Script

Gets an address strings from a ScriptPubKey Buffer

  var Address = require('bitcore/Address');
  var coinUtil= require('bitcore/util/util');

  var getAddrStr = function(s) {
    var addrStrs = [];
    var type = s.classify();
    var addr;

    switch (type) {
      case Script.TX_PUBKEY:
        var chunk = s.captureOne();
        addr = new Address(network.addressPubkey, coinUtil.sha256ripe160(chunk));
        addrStrs.push(addr.toString());
        break;
      case Script.TX_PUBKEYHASH:
        addr = new Address(network.addressPubkey, s.captureOne());
        addrStrs.push(addr.toString());
        break;
      case Script.TX_SCRIPTHASH:
        addr = new Address(network.addressScript, s.captureOne());
        addrStrs.push(addr.toString());
        break;
      case Script.TX_MULTISIG:
        var chunks = s.capture();
        chunks.forEach(function(chunk) {
          var a = new Address(network.addressPubkey, coinUtil.sha256ripe160(chunk));
          addrStrs.push(a.toString());
        });
        break;
      case Script.TX_UNKNOWN:
        break;
    }
    return addrStrs;
  };

  var s = new Script(scriptBuffer);
  console.log(getAddrStr(s);
  

#Security Please use at your own risk.

Bitcore is still under heavy development and not quite ready for "drop-in" production use. If you find a security issue, please email security@bitcore.io.

#Contributing Bitcore needs some developer love. Please send pull requests for bug fixes, code optimization, and ideas for improvement.

#Browser support Work to enable Bitcore for use in the browser is ongoing. To build bitcore for the browser:

npm install -g grunt-cli
grunt shell

You can check a usage example at examples/example.html

#License

Code released under the MIT license.

Copyright 2013-2014 BitPay, Inc. Bitcore is a trademark maintained by BitPay, Inc.

Bitdeli Badge