Go to file
Ryan X. Charles da7fede2c0 Merge remote-tracking branch 'gordonwritescode/feature/tor' 2014-04-17 14:47:58 -03:00
browser fix Buffers.skip in the browser 2014-04-10 18:52:13 -03:00
examples update example to discover a peer instead of hardcoding arbitrary IP 2014-04-11 14:05:53 -04:00
src remove commented-out code 2014-04-11 14:23:41 -03:00
test Merge remote-tracking branch 'gordonwritescode/feature/tor' 2014-04-17 14:47:58 -03:00
util fixed calcDifficulty by making sure the MAX_TARGET is also locally available. 2014-04-08 10:26:36 +02:00
.gitignore Ingnored .DS_Store file 2014-04-15 09:49:14 -03:00
.jshintrc tests work in browser and node 2014-02-04 16:37:34 -03:00
.travis.yml Added .travis.yml 2014-04-08 14:52:32 -04:00
Address.js update fromPubKeys to use fromScript 2014-04-09 14:07:56 -03:00
BIP32.js network refactors 2014-04-07 18:31:17 -03:00
Block.js fix firefox compatibility issue with buffertools#compare 2014-03-21 18:52:49 -03:00
Bloom.js all classes working with soop and test passing 2014-03-05 16:11:16 -03:00
Buffers.monkey.js fix Buffers.skip in the browser 2014-04-10 18:52:13 -03:00
Connection.js requiring socks5 client conditionally from connection constructor 2014-04-15 10:30:41 -04:00
Deserialize.js replace tabs with 2 spaces using "expand" 2014-01-16 06:21:35 -03:00
Gruntfile.js update examples and readme 2014-03-28 23:39:12 -03:00
Key.js get BIP32 working in the browser by exposing more crypto 2014-03-27 23:34:17 -04: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 random transaction and script generation 2014-03-25 14:37:23 -03:00
Peer.js all classes working with soop and test passing 2014-03-05 16:11:16 -03:00
PeerManager.js resolved merge 2014-04-09 10:49:07 -04:00
Point.js get BIP32 working in the browser by exposing more crypto 2014-03-27 23:34:17 -04:00
PrivateKey.js network refactors 2014-04-07 18:31:17 -03:00
README.md Merge remote-tracking branch 'cimm/fix-p2p-example-in-readme' 2014-04-17 10:43:37 -03: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 update interface, bitcore.KeyModule.Key -> bitcore.Key 2014-03-13 13:31:02 -04:00
Script.js Script: removed dead code this.class = Script 2014-04-11 16:15:54 +08:00
ScriptInterpreter.js remove console.logs and std::couts 2014-04-04 18:24:34 -03:00
Sign.js network refactors 2014-04-07 18:31:17 -03:00
Transaction.js Merge pull request #251 from olalonde/tx-sequence 2014-04-11 13:37:39 -03:00
TransactionBuilder.js remove inputMap from serialization 2014-04-16 10:34:16 -03:00
Wallet.js network refactors 2014-04-07 18:31:17 -03:00
WalletKey.js network refactors 2014-04-07 18:31:17 -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 fix Buffers.skip in the browser 2014-04-10 18:52:13 -03:00
config.js set logger back to normal 2014-04-04 11:53:05 -04:00
const.js replace tabs with 2 spaces using "expand" 2014-01-16 06:21:35 -03:00
networks.js network refactors 2014-04-07 18:31:17 -03:00
package.json Merge remote-tracking branch 'gordonwritescode/feature/tor' 2014-04-17 14:47:58 -03:00

README.md

Bitcore

Build Status

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. For instance:

var bitcore = require('bitcore');
var Address = bitcore.Address;
var Transaction = bitcore.Transaction;
var PeerManager = bitcore.PeerManager;

#Examples

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

Validating an address

Validating a Bitcoin address:

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

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

addrs.forEach(function(addr) {
  var valid = addr.isValid();
  console.log(addr.data + ' is ' + (valid ? '' : 'not ') + 'valid');
});

Monitoring Blocks and Transactions

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

var bitcore = require('bitcore');
var networks = bitcore.networks;
var Peer = bitcore.Peer;
var PeerManager = require('soop').load('../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('** TX Received **');
  console.log(tx);
};

var handleInv = function(info) {
  console.log('** 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

Consuming bitcoind RPC

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

var bitcore = require('bitcore');
var RpcClient = bitcore.RpcClient;
var hash = '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

Creating and sending a Transaction through P2P

The fee of the transaction can be given in opts or it will be determined by the transaction size. Documentation on the parameters of TransactionBuilder can be found on the source file.

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

// Unspent transactions can be found via the insight.bitcore.io or blockchain.info APIs
var unspent = [
  {
    "address": "n4g2TFaQo8UgedwpkYdcQFF6xE2Ei9Czvy",
    "txid": "2ac165fa7a3a2b535d106a0041c7568d03b531e58aeccdd3199d7289ab12cfc1",
    "scriptPubKey": "76a914fe021bac469a5c49915b2a8ffa7390a9ce5580f988ac",
    "vout": 1,
    "amount": 1.0101,
    "confirmations":7
  },
  {
    "address": "mhNCT9TwZAGF1tLPpZdqfkTmtBkY282YDW",
    "txid": "2ac165fa7a3a2b535d106a0041c7568d03b531e58aeccdd3199d7289ab12cfc2",
    "scriptPubKey": "76a9141448534cb1a1ec44665b0eb2326e570814afe3f188ac",
    "vout": 0,
    "confirmations": 1,
    "amount": 10
  }
];

// Private keys in WIF format (see TransactionBuilder.js for other options)
var keys = [
  "cSq7yo4fvsbMyWVN945VUGUWMaSazZPWqBVJZyoGsHmNq6W4HVBV",
  "cPa87VgwZfowGZYaEenoQeJgRfKW6PhZ1R65EHTkN1K19cSvc92G",
  "cPQ9DSbBRLva9av5nqeF5AGrh3dsdW8p2E5jS4P8bDWZAoQTeeKB"
];

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

peerman.on('connect', function() {
  var conn = peerman.getActiveConnection();
  if (conn) {
    var outs = [{address: toAddress, amount: amt}];
    var opts = {remainderOut: {address: changeAddressString}};
    var TransactionBuilder = bitcore.TransactionBuilder;

    var tx = new TransactionBuilder(opts)
                 .setUnspent(unspent)
                 .setOutputs(outs)
                 .sign(keys)
                 .build();

   /* Create and signing can be done in multiple steps:
    *
    *  var builder = new bitcore.TransactionBuilder(opts)
    *                .setUnspent(utxos) 
    *                .setOutputs(outs);
    *
    *  // Sign with the first key
    *  builder.sign(key1);
    *  var tx = builder.build(); // Partially signed transaction
    *
    *  // Sign with the second key
    *  builder.sign(key2);
    *  if (builder.isFullySigned()){
    *   var tx = builder.build();
    *  }
    *
    *  var selectedUnspent = build.getSelectedUnspent(); // Retrieve selected unspent outputs from the transaction
    */
    conn.sendTx(tx.serialize().toString('hex'));
  }
  conn.on('reject', function() {
    console.log('Transaction Rejected');
  });
});

peerman.start();

Parsing a Script

Gets an address strings from a ScriptPubKey Buffer

var bitcore = require('bitcore');
var Address = bitcore.Address;
var coinUtil = bitcore.util;
var Script = bitcore.Script;
var network = bitcore.networks.testnet;

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.addressVersion, coinUtil.sha256ripe160(chunk));
      addrStrs.push(addr.toString());
      break;
    case Script.TX_PUBKEYHASH:
      addr = new Address(network.addressVersion, s.captureOne());
      addrStrs.push(addr.toString());
      break;
    case Script.TX_SCRIPTHASH:
      addr = new Address(network.P2SHVersion, s.captureOne());
      addrStrs.push(addr.toString());
      break;
    case Script.TX_MULTISIG:
      var chunks = s.capture();
      chunks.forEach(function(chunk) {
        var a = new Address(network.addressVersion, coinUtil.sha256ripe160(chunk));
        addrStrs.push(a.toString());
      });
      break;
    case Script.TX_UNKNOWN:
      console.log('tx type unkown');
      break;
  }
  return addrStrs;
};

var script = 'DUP HASH160 0x14 0x3744841e13b90b4aca16fe793a7f88da3a23cc71 EQUALVERIFY CHECKSIG';
var s = Script.fromHumanReadable(script);
console.log(getAddrStr(s)[0]); // mkZBYBiq6DNoQEKakpMJegyDbw2YiNQnHT

#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

Building the browser bundle

To build bitcore full bundle for the browser (this is automatically executed after you run npm install):

node browser/build.js -a

This will generate a browser/bundle.js file which you can include in your HTML to use bitcore in the browser.

##Example browser usage

From example/simple.html

<!DOCTYPE html>
<html>
  <body>
    <script src="../browser/bundle.js"></script>
    <script>
      var bitcore = require('bitcore');
      var Address = bitcore.Address;
      var a = new Address('1KerhGhLn3SYBEQwby7VyVMWf16fXQUj5d');
      console.log('1KerhGhLn3SYBEQwby7VyVMWf16fXQUj5d is valid? '+a.isValid());
    </script>
  </body>
</html>

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

Generating a customized browser bundle

To generate a customized bitcore bundle, you can specify which submodules you want to include in it with the -s option:

node browser/build.js -s Transaction,Address

This will generate a browser/bundle.js containing only the Transaction and Address class, with all their dependencies. Use this option if you are not using the whole bitcore library, to optimize the bundle size, script loading time, and general resource usage.

Tests

Run tests in node:

mocha

Or generate tests in the browser:

grunt shell

And then open test/index.html in your browser.

To run the code coverage report:

npm run-script coverage

And then open coverage/lcov-report/index.html in your browser.

#License

Code released under the MIT license.

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

Bitdeli Badge