bitcore-node-zcash/README.md

347 lines
9.3 KiB
Markdown
Raw Normal View History

2014-08-12 12:03:04 -07:00
# bitcoind.js
__bitcoind.js__ as a node.js module which dynamically loads a node.js C++
modules which links to libbitcoind.{so|dylib}. Unix/Linux use the file extension "so" whereas Mac OSX uses "dylib" (bitcoind compiled as a shared library),
making all useful bitcoind functions asynchronous (with the exception of the wallet functionality).
2014-08-20 14:51:07 -07:00
## Building
### libbitcoind.{so|dylib}
2014-08-20 14:51:07 -07:00
2014-12-07 00:21:24 -08:00
#### Compiling bitcoind as a library
2014-08-20 14:51:07 -07:00
2014-09-12 14:27:25 -07:00
##### Dependencies
- Boost
2015-07-07 10:47:22 -07:00
- Boost Header Files (`/usr/include/boost`)
- The Boost header files can be from your distro (like Debian or Ubuntu), just be sure to install the "-dev" versions of Boost.
2014-09-12 14:27:25 -07:00
2015-07-07 10:47:22 -07:00
- OpenSSL headers and libraries (-lcrypto and -lssl), this is used to compile Bitcoin.
2014-12-01 23:56:54 -08:00
- If target platform is Mac OS X, then OS X >= 10.9, Clang and associated linker.
2014-12-01 23:56:54 -08:00
2014-12-07 00:21:24 -08:00
##### Building
2014-10-02 14:29:38 -07:00
2014-09-11 17:18:36 -07:00
``` bash
2014-12-07 00:21:24 -08:00
$ cd ~/node_modules/bitcoind.js
$ ./bin/build-libbitcoind
2014-08-20 14:51:07 -07:00
```
2014-12-09 14:17:39 -08:00
NOTE: This script will run automatically on an `npm install`, along with the
compilation below.
The first argument can also be a bitcoin repo directory you already have on your disk, otherwise
it will check for ~/bitcoin by default. The `PATCH_VERSION` file dictates what version/tag the patch goes clean against.
2014-12-07 00:21:24 -08:00
###### In the build-libbitcoind.sh script:
2014-10-02 14:29:38 -07:00
`--enable-daemonlib` will compile all object files with `-fPIC` (Position
2014-09-11 17:18:36 -07:00
Independent Code - needed to create a shared object).
`make` will then compile `./src/libbitcoind.{so|dylib}` (with `-shared -fPIC`), linking
2014-10-02 14:29:38 -07:00
to all the freshly compiled PIC object files. This will completely ignore
compiling tests, QT object files and the wallet features in bitcoind/libbitcoind.{so|dylib}.
2014-09-11 17:18:36 -07:00
2014-10-02 14:29:38 -07:00
Without `--enable-daemonlib`, the Makefile with compile bitcoind with -fPIE
2014-09-11 17:18:36 -07:00
(Position Independent for Executable), this allows compiling of bitcoind.
2014-12-07 00:21:24 -08:00
### bitcoind.js
2014-08-20 14:51:07 -07:00
``` bash
2015-07-07 10:47:22 -07:00
$ cd bitcoind.js
$ node-gyp rebuild
2014-08-20 14:51:07 -07:00
```
2014-08-12 12:03:04 -07:00
2014-09-11 17:18:36 -07:00
#### Running bitcoind.js
You can run bitcoind.js to start downloading the blockchain by doing:
``` bash
2014-10-09 03:35:37 -07:00
$ node example --on-block &
2014-09-11 17:18:36 -07:00
bitcoind: status="start_node(): bitcoind opened."
2014-10-09 03:35:37 -07:00
...
[should see full javascript blocks here]
2014-09-11 17:18:36 -07:00
```
2014-12-07 00:21:24 -08:00
You can also look at the blocks come in through the bitcoind log file:
2014-09-11 17:18:36 -07:00
``` bash
2014-12-07 00:21:24 -08:00
$ tail -f ~/.libbitcoind-example/debug.log
2014-09-11 17:18:36 -07:00
```
^C (SIGINT) will call `StartShutdown()` in bitcoind on the node thread pool.
##### Example Usage
2014-09-26 15:50:05 -07:00
bitcoind.js has direct access to the global wallet:
2014-09-26 15:50:05 -07:00
``` js
2014-10-20 09:38:35 -07:00
var bitcoind = require('bitcoind.js')({
2014-12-07 00:21:24 -08:00
directory: '~/.libbitcoind-example',
testnet: false,
rpc: false
2014-10-20 09:38:35 -07:00
});
2014-12-07 00:21:24 -08:00
bitcoind.on('block', function(block) {
console.log('Found Block:');
console.log(block);
});
bitcoind.on('addr', function(addr) {
console.log('Found more peers to connect to:');
console.log(addr);
});
bitcoind.on('open', function() {
console.log('Whatever you want from the open signal');
});
2014-12-07 00:21:24 -08:00
bitcoind.start();
2014-09-26 15:50:05 -07:00
```
``` bash
$ node ./my-example.js
2014-09-26 15:50:05 -07:00
bitcoind.js: status="start_node(): bitcoind opened."
2014-10-06 12:58:37 -07:00
^C
2014-09-26 15:50:05 -07:00
bitcoind.js: stop_node(): bitcoind shutdown.
bitcoind.js: shutting down...
bitcoind.js: shut down.
```
## Documentation
**bitcoind.js** is a node.js module which links to libbitcoind.{so|dylib} (bitcoind
complied as a shared library).
### Javascript API
#### Bitcoin Object/Class
Bitcoind in javascript. Right now, only one object can be instantiated.
2014-12-07 00:21:24 -08:00
##### `Bitcoin::start([options], [callback])`
Start the javascript bitcoin node.
##### `Bitcoin::getBlock(blockHash, callback)`
Get any block asynchronously by reading it from disk.
2014-12-07 00:21:24 -08:00
##### `Bitcoin::getTransaction(txid, blockhash, callback)`
Get any tx asynchronously by reading it from disk.
##### `Bitcoin::log(), Bitcoin::info()`
Log to standard output.
##### `Bitcoin::error()`
Log to stderr.
##### `Bitcoin::stop, Bitcoin::close(callback)`
Stop the javascript bitcoin node safely. This will be done automatically on
`process.exit` also. It also takes the bitcoin node off the libuv event loop.
If the bitcoin object is the only thing on the event loop. Node will simply
close.
##### Bitcoin Object Events
Note: Any event that requires polling will only start the polling once the
event is bound.
###### `open(bitcoind)`
bitcoind has opened and loaded the blockchain.
###### `close(bitcoind)`
bitcoind has shutdown.
###### `block(block)`
A block has been received an accepted by bitcoind.
###### `tx(tx)`
A confirmed or unconfirmed transaction has been received by bitcoind.
###### `mptx(tx)`
A tx from the mempool has been addded. Most likely not included in a block yet.
#### Block Object
A block (CBlock) represented in javascript. It is a full block containing all
transactions in `block.tx`.
##### `Block::_blockFlag`
Internal non-enumerable property to check whether the object is a block.
##### `Block.isBlock(block)`
Static method to check whether object is a block.
##### `Block.fromHex(hex)`
Create a js block from a hex string.
##### `Block::getHash(enc)`
Get the block's hash. Return the correct encoding. `hex` is most likely what
you want. If no encoding is provided, a buffer will be returned.
##### `Block::verify()`
Verify whether the block is valid.
##### `Block::toHex()`
Convert the block to a hex string.
##### `Block.toHex(block)`
Static method to convert any block-like object to a hex string.
##### `Block::toBinary()`
Convert the block to a binary buffer.
##### `Block.toBinary(block)`
Static method to convert a block-like object to a hex string.
#### Transaction Object/Class
##### `Transaction::_txFlag`
Internal non-enumerable property to check whether the object is a transaction.
##### `Transaction.isTransaction(tx), Transaction.isTx(tx)`
Static method to check whether object is a transaction.
##### `Transaction.fromHex(hex)`
Create a js transaction from a hex string.
##### `Transaction::verify()`
Verify whether the transaction is valid.
##### `Transaction::sign(), Transaction::fill(options)`
Fill the raw transaction with available unspent outputs and sign them.
##### `Transaction.sign(tx, options), Transaction.fill(tx, options)`
Static method to fill a tx-like object.
##### `Transaction::getHash(enc)`
Get the hash of a Transaction object. Encoding is usually `hex`. If no encoding
is provided, a Buffer will be returned.
##### `Transaction::isCoinbase()`
Check whether the Transaction is a coinbase tx.
##### `Transaction::toHex()`
Convert the transaction to a hex string.
##### `Transaction.toHex(tx)`
Static method to convert a transaction-like object to a hex string.
##### `Transaction::toBinary()`
Convert the transaction to a binary buffer.
##### `Transaction.toBinary(tx)`
Static method to convert a transaction-like object to a binary buffer.
##### `Transaction::broadcast(options, callback)`
Broadcast a raw transaction that has not been included in a block yet. This can
be your own transaction or a transaction relayed to you.
##### `Transaction.broadcast(tx, options, callback)`
Static method to broadcast a transaction.
#### Utils Object (Singleton)
##### `utils.forEach(obj, iter, done)`
Asynchronous parallel forEach function.
##### `utils.NOOP()`
A simple NOP function.
#### Exposed Objects
NOTE: All exposed objects will also be exposed on any instantiated `Bitcoin` object.
##### `bitcoin.Bitcoin, bitcoin.bitcoin, bitcoin.bitcoind`
The bitcoin object.
##### `bitcoin.native, bitcoin.bitcoindjs`
The native C++ bitcoindjs object.
##### `bitcoin.Block, bitcoin.block`
The bitcoind.js Block object.
##### `bitcoin.Transaction, bitcoin.transaction, bitcoin.tx`
The bitcoind.js Transaction object.
##### `bitcoin.Wallet, bitcoin.wallet`
The bitcoind.js Wallet singleton.
##### `bitcoin.utils`
The bitcoind.js utils object.
## Discussion about the patch to Bitcoin to allow the shared library creation
To provide native bindings to JavaScript (or any other language for that matter), Bitcoin code, itself, must be linkable. Currently, Bitcoind achieves this by providing an JSON RPC interface to bitcoind. The major drawbacks to this interface are:
1. JSON RPC interfaces are much slower than linking natively to the C++ code.
2. There can be errors in the interface that prevent clients from using bitcoind's functionality.
3. Functionality can be limited or otherwise unavailable to clients through this interface.
Linking C++ binding code directly to bitcoind can mitigate all of the above disadvantage, but has its own disadavantages:
1. The original authors are not explicitly (or implicitly) providing ANY API support to the C++ bindings written here. This means that in subsequent releases of bitcoind, the bindings could fail and this project's authors will need to update the bindings retroactively.
2. As such, there is likely a lag in support for newer versions of bitcoind.
Due to the pros and cons listed above. The patch to Bitcoin will not be merged by the core devs due to the attitude that the cons outweigh the pros. Every effort will be made to ensure that this project stays up-to-date with the latest release of Bitcoin. At the very least, this project began supporting Bitcoin v0.10.2.
2014-08-12 12:03:04 -07:00
## Contribution and License Agreement
If you contribute code to this project, you are implicitly allowing your code
to be distributed under the MIT license. You are also implicitly verifying that
all code is your original work. `</legalese>`
2014-08-12 12:03:04 -07:00
## License
- bitcoind.js: Copyright (c) 2015, BitPay (MIT License).
- bitcoin: Copyright (c) 2009-2015 Bitcoin Core Developers (MIT License)
- bcoin (some code borrowed temporarily): Copyright Fedor Indutny, 2014.