bitcore-node-zcash/docs/services/bitcoind.md

317 lines
8.5 KiB
Markdown
Raw Normal View History

# Bitcoin Service
The Bitcoin Service is a Node.js interface to [Bitcoin Core](https://github.com/bitcoin/bitcoin) for querying information about the bitcoin block chain. It will manage starting and stopping `bitcoind` or connect to several running `bitcoind` processes. It uses a branch of a [branch of Bitcoin Core](https://github.com/bitpay/bitcoin/tree/0.12-bitcore) with additional indexes for querying information about addresses and blocks. Results are cached for performance and there are several additional API methods added for common queries.
## Configuration
The default configuration will include a "spawn" configuration in "bitcoind". This defines the location of the block chain database and the location of the `bitcoind` daemon executable. The below configuration points to a local clone of `bitcoin`, and will start `bitcoind` automatically with your Node.js application.
```json
"servicesConfig": {
"bitcoind": {
"spawn": {
"datadir": "/home/bitcore/.bitcoin",
"exec": "/home/bitcore/bitcoin/src/bitcoind"
}
}
}
```
It's also possible to connect to separately managed `bitcoind` processes with round-robin quering, for example:
```json
"servicesConfig": {
"bitcoind": {
"connect": [
{
"rpchost": "127.0.0.1",
"rpcport": 30521,
"rpcuser": "bitcoin",
"rpcpassword": "local321",
"zmqpubrawtx": "tcp://127.0.0.1:30611"
},
{
"rpchost": "127.0.0.1",
"rpcport": 30522,
"rpcuser": "bitcoin",
"rpcpassword": "local321",
"zmqpubrawtx": "tcp://127.0.0.1:30622"
},
{
"rpchost": "127.0.0.1",
"rpcport": 30523,
"rpcuser": "bitcoin",
"rpcpassword": "local321",
"zmqpubrawtx": "tcp://127.0.0.1:30633"
}
]
}
}
```
**Note**: For detailed example configuration see [`regtest/cluster.js`](regtest/cluster.js)
## API Documentation
Methods are available by directly interfacing with the service:
2015-09-22 08:38:14 -07:00
```js
node.services.bitcoind.<methodName>
```
### Chain
**Getting Latest Blocks**
```js
// gives the block hashes within a range of timestamps
var high = 1460393372; // Mon Apr 11 2016 12:49:25 GMT-0400 (EDT)
var low = 1460306965; // Mon Apr 10 2016 12:49:25 GMT-0400 (EDT)
node.services.bitcoind.getBlockHashesByTimestamp(high, low, function(err, blockHashes) {
//...
});
// get the current tip of the chain
node.services.bitcoind.getBestBlockHash(function(err, blockHash) {
//...
})
```
**Getting Synchronization and Node Status**
```js
// gives a boolean if the daemon is fully synced (not the initial block download)
node.services.bitcoind.isSynced(function(err, synced) {
//...
})
// gives the current estimate of blockchain download as a percentage
node.services.bitcoind.syncPercentage(function(err, percent) {
//...
});
// gives information about the chain including total number of blocks
node.services.bitcoind.getInfo(function(err, info) {
//...
});
```
**Generate Blocks**
```js
// will generate a block for the "regtest" network (development purposes)
var numberOfBlocks = 10;
node.services.bitcoind.generateBlock(numberOfBlocks, function(err, blockHashes) {
//...
});
```
### Blocks and Transactions
2015-09-22 08:38:14 -07:00
**Getting Block Information**
It's possible to query blocks by both block hash and by height. Blocks are given as Node.js Buffers and can be parsed via Bitcore:
2015-09-22 08:38:14 -07:00
```js
var blockHeight = 0;
node.services.bitcoind.getRawBlock(blockHeight, function(err, blockBuffer) {
2015-09-22 08:38:14 -07:00
if (err) {
throw err;
}
var block = bitcore.Block.fromBuffer(blockBuffer);
console.log(block);
};
// get a bitcore object of the block (as above)
node.services.bitcoind.getBlock(blockHash, function(err, block) {
//...
};
2015-09-22 08:38:14 -07:00
// get only the block header and index (including chain work, height, and previous hash)
node.services.bitcoind.getBlockHeader(blockHeight, function(err, blockHeader) {
//...
});
2015-09-22 08:38:14 -07:00
```
**Retrieving and Sending Transactions**
Get a transaction asynchronously by reading it from disk:
2015-09-22 08:38:14 -07:00
```js
var txid = '7426c707d0e9705bdd8158e60983e37d0f5d63529086d6672b07d9238d5aa623';
node.services.bitcoind.getRawTransaction(txid, function(err, transactionBuffer) {
2015-09-22 08:38:14 -07:00
if (err) {
throw err;
}
var transaction = bitcore.Transaction().fromBuffer(transactionBuffer);
});
// get a bitcore object of the transaction (as above)
node.services.bitcoind.getTransaction(txid, function(err, transaction) {
//...
});
2015-09-22 08:38:14 -07:00
// also retrieve the block timestamp and height
node.services.bitcoind.getTransactionWithBlockInfo(txid, function(err, transaction) {
console.log(transaction.__blockHash);
console.log(transaction.__height);
console.log(transaction.__timestamp); // in seconds
2015-09-22 08:38:14 -07:00
});
```
Send a transaction to the network:
```js
var numberOfBlocks = 3;
node.services.bitcoind.estimateFee(numberOfBlocks, function(err, feesPerKilobyte) {
//...
});
2015-09-22 08:38:14 -07:00
node.services.bitcoind.sendTransaction(transaction.serialize(), function(err, hash) {
//...
});
```
### Addresses
**Get Unspent Outputs**
One of the most common uses will be to retrieve unspent outputs necessary to create a transaction, here is how to get the unspent outputs for an address:
```js
var address = 'mgY65WSfEmsyYaYPQaXhmXMeBhwp4EcsQW';
node.services.bitcoind.getAddressUnspentOutputs(address, options, function(err, unspentOutputs) {
// see below
});
```
The `unspentOutputs` will have the format:
```js
[
{
address: 'mgY65WSfEmsyYaYPQaXhmXMeBhwp4EcsQW',
txid: '9d956c5d324a1c2b12133f3242deff264a9b9f61be701311373998681b8c1769',
outputIndex: 1,
height: 150,
satoshis: 1000000000,
script: '76a9140b2f0a0c31bfe0406b0ccc1381fdbe311946dadc88ac',
confirmations: 3
}
]
2015-09-22 08:38:14 -07:00
```
**View Balances**
2015-09-22 08:38:14 -07:00
```js
var address = 'mgY65WSfEmsyYaYPQaXhmXMeBhwp4EcsQW';
node.services.bitcoind.getAddressBalance(address, options, function(err, balance) {
// balance will be in satoshis with "received" and "balance"
});
```
**View Address History**
This method will give history of an address limited by a range of block heights by using the "start" and "end" arguments. The "start" value is the more recent, and greater, block height. The "end" value is the older, and lesser, block height. This feature is most useful for synchronization as previous history can be omitted. Furthermore for large ranges of block heights, results can be paginated by using the "from" and "to" arguments.
```js
var addresses = ['mgY65WSfEmsyYaYPQaXhmXMeBhwp4EcsQW'];
var options = {
start: 345000,
end: 344000,
queryMempool: true
};
node.services.bitcoind.getAddressHistory(addresses, options, function(err, history) {
// see below
});
```
The history format will be:
```js
{
totalCount: 1, // The total number of items within "start" and "end"
items: [
{
addresses: {
'mgY65WSfEmsyYaYPQaXhmXMeBhwp4EcsQW': {
inputIndexes: [],
outputIndexes: [0]
}
},
satoshis: 1000000000,
height: 150, // the block height of the transaction
confirmations: 3,
timestamp: 1442948127, // in seconds
fees: 191,
tx: <Transaction> // the populated transaction
}
]
2015-09-22 08:38:14 -07:00
}
```
**View Address Summary**
2015-09-22 08:38:14 -07:00
```js
var address = 'mgY65WSfEmsyYaYPQaXhmXMeBhwp4EcsQW';
var options = {
noTxList: false
};
node.services.bitcoind.getAddressSummary(address, options, function(err, summary) {
// see below
});
2015-09-22 08:38:14 -07:00
```
The `summary` will have the format (values are in satoshis):
```js
{
totalReceived: 1000000000,
totalSpent: 0,
balance: 1000000000,
unconfirmedBalance: 1000000000,
appearances: 1, // number of transactions
unconfirmedAppearances: 0,
txids: [
'3f7d13efe12e82f873f4d41f7e63bb64708fc4c942eb8c6822fa5bd7606adb00'
]
}
```
2015-09-22 08:38:14 -07:00
## Events
The Bitcoin Service exposes two events via the Bus, and there are a few events that can be directly registered:
2015-09-22 08:38:14 -07:00
```js
node.services.bitcoind.on('tip', function(blockHash) {
// a new block tip has been added, if there is a rapid update (with a second) this will not emit every tip update
2015-09-22 08:38:14 -07:00
});
node.services.bitcoind.on('tx', function(transactionBuffer) {
// a new transaction has entered the mempool
});
node.services.bitcoind.on('block', function(blockHash) {
// a new block has been added
2015-09-22 08:38:14 -07:00
});
```
For details on instantiating a bus for a node, see the [Bus Documentation](../bus.md).
- Name: `bitcoind/rawtransaction`
- Name: `bitcoind/hashblock`
**Examples:**
2015-10-19 10:59:58 -07:00
2015-09-22 08:38:14 -07:00
```js
bus.subscribe('bitcoind/rawtransaction');
bus.subscribe('bitcoind/hashblock');
bus.on('bitcoind/rawtransaction', function(transactionHex) {
//...
});
bus.on('bitcoind/hashblock', function(blockhashHex) {
//...
});
```