Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dgarage/bcrpc
Tiny Bitcoin RPC wrapper for node.js
https://github.com/dgarage/bcrpc
Last synced: about 1 month ago
JSON representation
Tiny Bitcoin RPC wrapper for node.js
- Host: GitHub
- URL: https://github.com/dgarage/bcrpc
- Owner: dgarage
- License: mit
- Created: 2016-12-26T03:46:35.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2021-09-19T11:14:28.000Z (over 3 years ago)
- Last Synced: 2024-11-18T07:08:07.618Z (3 months ago)
- Language: JavaScript
- Size: 49.8 KB
- Stars: 18
- Watchers: 14
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# bcrpc
Tiny Bitcoin RPC wrapper for Node.js.## Usage
Each rpc function is exposed as a function in an instantiated RpcAgent. The first arguments are parsed as array of arguments, as expected by that specific rpc function, see [Bitcoin Core RPC Docs](https://bitcoincore.org/en/doc). The last argument is an optional callback function, if no callback is provided, it will return a promise.The following program can be saved and and run in the same directory as the ```index.js``` file from bcrpc. It will print out the latest block number along with its hash. Change username and password to those from the ```bitcoin.conf``` file and also change the port number to ```18332``` if you are running Bitcoin on testnet.
```
const RpcAgent = require('./index'); // Change './index' to 'bcrpc' if running outside of bcrpc directory
agent = new RpcAgent({port: 18332, user: 'username', pass: 'password'});// Using Callbacks
agent.getBlockCount(function (err, blockCount) {
if (err)
throw Error(JSON.stringify(err));
console.log(blockCount.result);
agent.getBlockHash(blockCount.result, function (err, hash) {
if (err)
throw Error(JSON.stringify(err));
console.log(hash.result);
})
});// Using Promises
agent.getBlockCount()
.then((blockCount) => {
console.log(blockCount);
return agent.getBlockHash(blockCount);
})
.then((hash) => {
console.log(hash);
})
.catch((err) => {
console.error(err);
return err;
});
```## Example
Make sure a Bitcoin Core node is running on your system already. This example creates a new project and shows bcrpc being used.```
$ mkdir myproject
$ cd myproject
$ npm init
[continue with default options]
$ npm install bcrpc
```Create a new file called ```server.js``` and write the following in it (change username and password to those from the ```bitcoin.conf``` file, also change the port number to ```18332``` if you are running Bitcoin on testnet):
```
const RpcAgent = require('bcrpc');
agent = new RpcAgent({port: 18332, user: 'username', pass: 'password'});agent.getBlockCount(function (err, blockCount) {
if (err)
throw Error(JSON.stringify(err));
console.log(blockCount.result);
agent.getBlockHash(blockCount.result, function (err, hash) {
if (err)
throw Error(JSON.stringify(err));
console.log(hash.result);
})
});
```Then run ```$ npm start```. You should get the latest block number along with its hash printed out:
```
> [email protected] start /home/user/myproject
> node server.js1768837
00000000000009f6c6eba1dde1cf61022ea59d58f31b2e447c25297c29601008
```## Testing
Install mocha first with ```$ sudo npm install mocha -g```. Change username and password to those from the ```bitcoin.conf``` file.```
$ git clone https://github.com/dgarage/bcrpc.git
$ cd bcrpc
$ npm install
$ export BITCOIND_USER=username
$ export BITCOIND_PASS=password
$ export BITCOIND_PORT=18332
$ npm test
```If everything is configured properly you should see this output:
```
> [email protected] test /home/user/bcrpc
> mocha tests.jsBitcoinD
✓ is runningbcrpc
✓ can get info2 passing (37ms)
```