bitcore/README.md

287 lines
7.4 KiB
Markdown
Raw Normal View History

2014-01-22 14:12:45 -08:00
Bitcore
2013-07-04 13:02:18 -07:00
=======
2013-07-04 12:55:19 -07:00
2014-01-22 14:12:45 -08:00
A pure, powerful core for your bitcoin project.
2014-01-22 14:12:45 -08:00
Bitcore is a complete, native interface to the Bitcoin network, and provides the core functionality needed to develop apps for bitcoin.
2014-01-22 14:12:45 -08:00
#Principles
2014-02-03 12:54:26 -08:00
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.
2014-01-22 14:12:45 -08:00
**Bitcore unchains developers from fallible, centralized APIs, and provides the tools to interact with the real Bitcoin network.**
2014-01-22 14:12:45 -08:00
#Get Started
2014-01-22 14:12:45 -08:00
Bitcore runs on [node](http://nodejs.org/), and can be installed via [npm](https://npmjs.org/):
```
npm install bitcore
```
2014-02-03 12:54:26 -08:00
It is a collection of objects useful to bitcoin applications; class-like idioms are enabled via [Classtool](https://github.com/gasteve/classtool). In most cases, a developer will require the object's class directly:
2014-01-23 08:17:43 -08:00
```
var Address = require('bitcore/Address').class();
```
2014-01-23 07:26:27 -08:00
#Examples
Some examples are provided at the [examples](/examples) path. Here are some snippets:
2014-02-19 11:49:01 -08:00
## Validating an address
2014-01-23 07:26:27 -08:00
Validating a Bitcoin address:
2014-02-24 04:06:54 -08:00
```js
2014-01-23 08:17:43 -08:00
var Address = require('bitcore/Address').class();
2014-02-24 04:06:54 -08:00
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);
}
});
2014-01-22 14:12:45 -08:00
```
## Monitoring Blocks and Transactions
2014-02-27 07:10:15 -08:00
For this example you need a running bitcoind instance with RPC enabled.
2014-02-24 04:06:54 -08:00
```js
var util = require('util');
var networks = require('bitcore/networks');
var Peer = require('bitcore/Peer').class();
2014-02-20 06:04:02 -08:00
var PeerManager = require('bitcore/PeerManager').createClass({
2014-02-19 11:49:01 -08:00
network: networks.testnet
});
2014-02-19 11:49:01 -08:00
2014-02-24 04:06:54 -08:00
var handleBlock = function(info) {
console.log('** Block Received **');
console.log(info.message);
};
2014-02-19 11:49:01 -08:00
2014-02-24 04:06:54 -08:00
var handleTx = function(info) {
2014-02-20 06:04:02 -08:00
var tx = info.message.tx.getStandardizedObject();
2014-02-24 04:06:54 -08:00
console.log('** Block TX **');
console.log(tx);
2014-02-19 11:49:01 -08:00
};
2014-02-24 04:06:54 -08:00
var handleInv = function(info) {
console.log('** Block Inv **');
console.log(info.message);
2014-02-20 06:04:02 -08:00
var invs = info.message.invs;
info.conn.sendGetData(invs);
2014-02-19 11:49:01 -08:00
2014-02-24 04:06:54 -08:00
};
2014-02-19 11:49:01 -08:00
var peerman = new PeerManager();
2014-02-24 04:06:54 -08:00
peerman.addPeer( new Peer('127.0.0.1', 18333) );
peerman.on('connection', function(conn) {
2014-02-24 04:06:54 -08:00
conn.on('inv', handleInv);
conn.on('block', handleBlock);
2014-02-24 04:06:54 -08:00
conn.on('tx', handleTx);
});
2014-02-24 04:06:54 -08:00
peerman.start();
2014-02-24 04:06:54 -08:00
```
PeerManager will emit the following events: 'version', 'verack', 'addr', 'getaddr', 'error' 'disconnect'; and will relay events like: 'tx', 'block', 'inv'. Please see [PeerManager.js](PeerManager.js), [Peer.js](Peer.js) and [Connection.js](Connection.js)
2014-02-21 20:43:40 -08:00
## Creating and sending a Transaction through P2P
2014-02-27 07:10:15 -08:00
For this example you need a running bitcoind instance with RPC enabled.
2014-02-24 04:06:54 -08:00
```js
var networks = require('bitcore/networks');
var Peer = require('bitcore/Peer').class();
var Transaction = require('bitcore/Transaction').class();
var Address = require('bitcore/Address').class();
var Script = require('bitcore/Script').class();
var coinUtil = require('bitcore/util/util');
var PeerManager = require('bitcore/PeerManager').createClass({
network: networks.testnet
});
var createTx = function() {
2014-02-24 04:06:54 -08:00
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);
2014-02-24 04:06:54 -08:00
voutBuf.writeUInt32LE(vout, 0);
txin.o = Buffer.concat([hash, voutBuf]);
txobj.ins.push(txin);
2014-02-24 04:06:54 -08:00
var addr = new Address(ADDR);
var script = Script.createPubKeyHashOut(addr.payload());
var valueNum = coinUtil.parseValue(VAL);
2014-02-24 04:06:54 -08:00
var value = coinUtil.bigIntToValue(valueNum);
var txout = {
v: value,
s: script.getBuffer(),
};
txobj.outs.push(txout);
return new Transaction(txobj);
2014-02-24 04:06:54 -08:00
};
var peerman = new PeerManager();
2014-02-24 04:06:54 -08:00
peerman.addPeer(new Peer('127.0.0.1', 18333));
peerman.on('connect', function(conn) {
2014-02-24 04:06:54 -08:00
var conn = peerman.getActiveConnection();
2014-02-24 04:06:54 -08:00
if (conn) {
conn.sendTx(createTx());
2014-02-24 04:06:54 -08:00
}
conn.on('reject', function () {
console.log('Transaction Rejected');
});
});
2014-02-24 04:06:54 -08:00
peerman.start();
```
2014-02-19 11:49:01 -08:00
## Consuming bitcoind RPC
2014-02-27 07:10:15 -08:00
For this example you need a running bitcoind instance with RPC enabled.
2014-02-24 04:06:54 -08:00
```js
var util = require('util');
var RpcClient = require('bitcore/RpcClient').class();
var hash = process.argv[2] || '0000000000b6288775bbd326bedf324ca8717a15191da58391535408205aada4';
var config = {
protocol: 'http',
user: 'user',
pass: 'pass',
host: '127.0.0.1',
port: '18332',
};
2014-02-24 04:06:54 -08:00
var rpc = new RpcClient(config);
2014-02-24 04:06:54 -08:00
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](RpcClient.js)
2014-02-19 11:49:01 -08:00
## Parsing a Script
Gets an address strings from a ScriptPubKey Buffer
```
var Address = require('bitcore/Address').class();
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);
```
2014-01-22 14:12:45 -08:00
#Security
2014-01-23 08:17:43 -08:00
Please use at your own risk.
2014-01-22 14:12:45 -08:00
2014-01-23 08:17:43 -08:00
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.
2014-01-22 14:12:45 -08:00
#Contributing
Bitcore needs some developer love. Please send pull requests for bug fixes, code optimization, and ideas for improvement.
#Browser support
2014-02-07 18:08:56 -08:00
Work to enable Bitcore for use in the browser is ongoing. To build bitcore for the browser:
```
2014-01-31 08:13:34 -08:00
npm install -g grunt-cli
grunt browserify
```
2014-02-03 12:33:45 -08:00
2014-02-26 11:16:42 -08:00
You can check a usage example at examples/example.html
2014-02-17 11:39:13 -08:00
2014-02-07 18:08:56 -08:00
#License
**Code released under [the MIT license](https://github.com/bitpay/bitcore/blob/master/LICENSE).**
Copyright 2013-2014 BitPay, Inc. Bitcore is a trademark maintained by BitPay, Inc.
2014-02-03 12:35:51 -08:00
[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/bitpay/bitcore/trend.png)](https://bitdeli.com/free "Bitdeli Badge")
2014-02-03 12:33:45 -08:00