# bcrpc

> Bitcoin RPC wrapper

Latest version **0.4.0** (published 2021-09-19) · ISC license · 0 weekly downloads

## Install

```sh
npm install bcrpc
pnpm add bcrpc
yarn add bcrpc
bun add bcrpc
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.4.0 |
| Published | 2021-09-19 |
| First published | 2016-12-26 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 15.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Karl-Johan Alm |
| Maintainers | kjalm |
| Keywords | bitcoin, rpc, module, utility |

## Links

- npm: https://www.npmjs.com/package/bcrpc
- Repository: https://github.com/dgarage/bcrpc
- Homepage: https://github.com/dgarage/bcrpc#readme
- Issues: https://github.com/dgarage/bcrpc/issues
- npm.io page: https://npm.io/package/bcrpc

## Alternatives

- [lodash.assign](https://npm.io/package/lodash.assign.md) — 2.3M weekly downloads
- [lodash.chunk](https://npm.io/package/lodash.chunk.md) — 1.8M weekly downloads
- [react-native-ios-utilities](https://npm.io/package/react-native-ios-utilities.md) — 138.5K weekly downloads
- [@technically/lodash](https://npm.io/package/@technically/lodash.md) — 50.9K weekly downloads
- [@fluid-topics/ft-icon](https://npm.io/package/@fluid-topics/ft-icon.md) — 20.6K weekly downloads

## Recent versions

- 0.4.0 (latest) — 2021-09-19
- 0.2.7 — 2021-08-05
- 0.2.6 — 2021-08-05
- 0.2.5 — 2021-08-05
- 0.2.4 — 2021-08-05
- 0.2.3 — 2020-04-10
- 0.3.1 — 2020-03-17
- 0.3.0 — 2019-11-16
- 0.2.2 — 2019-11-13
- 0.2.1 — 2019-07-04
- 0.2.0 — 2019-07-03
- 0.1.1 — 2019-01-21
- 0.1.0 — 2018-08-29
- 0.0.6 — 2017-12-18
- 0.0.5 — 2016-12-26
- … 3 more at https://npm.io/package/bcrpc/versions

## 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:

```
> myproject@1.0.0 start /home/user/myproject
> node server.js

1768837
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:

```
> bcrpc@0.2.2 test /home/user/bcrpc
> mocha tests.js



  BitcoinD
    ✓ is running

  bcrpc
    ✓ can get info


  2 passing (37ms)
```

---
_Source: https://npm.io/package/bcrpc · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
