Skip to content

Commit

Permalink
Merge pull request #19 from vansergen/v2
Browse files Browse the repository at this point in the history
V2
  • Loading branch information
vansergen authored Mar 26, 2020
2 parents dac2f00 + 76803ec commit b1b8cff
Show file tree
Hide file tree
Showing 12 changed files with 428 additions and 736 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
{
"files": ["./build/**/*.js"],
"rules": {
"@typescript-eslint/camelcase": "off"
"@typescript-eslint/explicit-function-return-type": "off"
}
}
],
Expand Down
25 changes: 25 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Publish

on:
release:
types: [published]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- uses: actions/setup-node@master
with:
node-version: 12
registry-url: "https://registry.npmjs.org"
- name: Node.js version
run: node -v
- name: npm version
run: npm -v
- name: Install dependencies
run: npm ci
- name: Publish to npm
run: npm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.npm_token }}
7 changes: 7 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
src/
test/
tsconfig.json
index.ts
.github/
.eslintrc.json
.travis.yml
8 changes: 6 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
language: node_js
node_js:
- stable
- lts/*
- 10
- 11
- 12
- 13
script:
- npm run prettier
- npm run lint
- npm test
- npm run prepublishOnly
- npm run postpublish
36 changes: 26 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# rpc-bitcoin [![Build Status](https://travis-ci.com/vansergen/rpc-bitcoin.svg?token=cg5dVMovG8Db6p5Qzzps&branch=master)](https://travis-ci.com/vansergen/rpc-bitcoin)
# rpc-bitcoin [![Build Status](https://travis-ci.com/vansergen/rpc-bitcoin.svg?token=cg5dVMovG8Db6p5Qzzps&branch=master)](https://travis-ci.com/vansergen/rpc-bitcoin) [![Known Vulnerabilities](https://snyk.io/test/github/vansergen/rpc-bitcoin/badge.svg)](https://snyk.io/test/github/vansergen/rpc-bitcoin) ![NPM license](https://img.shields.io/npm/l/rpc-bitcoin) ![npm downloads](https://img.shields.io/npm/dt/rpc-bitcoin) ![GitHub top language](https://img.shields.io/github/languages/top/vansergen/rpc-bitcoin)

A [TypeScript](https://www.typescriptlang.org) library to make RPC and HTTP REST requests to [Bitcoin Core](https://bitcoin.org/en/bitcoin-core/).

Expand Down Expand Up @@ -59,6 +59,15 @@ const info = await client.getblockchaininfo();
const count = await client.getblockcount();
```

- [`getblockfilter`](https://bitcoin.org/en/developer-reference#getblockfilter)

```javascript
const blockhash =
"00000000000000dfffa1954693ba3f79813909dbcdedfe05eccb9829e828c141";
const filtertype = "basic";
const result = await client.getblockfilter({ blockhash, filtertype });
```

- [`getblockhash`](https://bitcoin.org/en/developer-reference#getblockhash)

```javascript
Expand Down Expand Up @@ -263,15 +272,6 @@ const result = await client.uptime();

### Generating

- [`generate`](https://bitcoin.org/en/developer-reference#generate)

```javascript
const nblocks = 1;
const maxtries = 10000;
const wallet = "bitcoin-core-wallet.dat";
const result = await client.generate({ nblocks, maxtries }, wallet);
```

- [`generatetoaddress`](https://bitcoin.org/en/developer-reference#generatetoaddress)

```javascript
Expand Down Expand Up @@ -838,6 +838,13 @@ const include_watchonly = true;
const result = await client.getbalance({ minconf, include_watchonly }, wallet);
```

- [`getbalances`](https://bitcoin.org/en/developer-reference#getbalances)

```javascript
const wallet = "bitcoin-core-wallet.dat";
const result = await client.getbalances(wallet);
```

- [`getnewaddress`](https://bitcoin.org/en/developer-reference#getnewaddress)

```javascript
Expand Down Expand Up @@ -1228,6 +1235,15 @@ const amount = 0.00002;
const result = await client.settxfee({ amount }, wallet);
```

- [`setwalletflag`](https://bitcoin.org/en/developer-reference#setwalletflag)

```javascript
const wallet = "bitcoin-core-wallet.dat";
const flag = "avoid_reuse";
const value = false;
const result = await client.setwalletflag({ flag, value }, wallet);
```

- [`signmessage`](https://bitcoin.org/en/developer-reference#signmessage)

```javascript
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
{
"name": "rpc-bitcoin",
"version": "1.11.1",
"version": "2.0.0",
"description": "A TypeScript library to make RPC and HTTP REST requests to Bitcoin Core",
"main": "build/index.js",
"type": "module",
"type": "commonjs",
"engines": {
"node": ">=10.16.3"
"node": ">=10.19.0"
},
"types": "build/index.d.ts",
"scripts": {
"build": "rm -fr build && tsc",
"fresh-install": "rm -fr node_modules && rm -f package-lock.json && npm install",
"prettier": "prettier -c **/*.{md,ts,json}",
"prettier:build": "prettier -c **/*.{js} --write",
"prettier": "prettier -c \"**/*.{md,ts,json}\"",
"prettier:build": "prettier -c \"./build/**/*.{js,d.ts}\" --write",
"prettier-write": "npm run prettier -- --write",
"lint": "eslint --ext .ts ./",
"lint:build": "eslint --ext .js ./ --fix",
"lint-fix": "npm run lint -- --fix",
"test": "mocha -r ts-node/register --full-trace --ui tdd --bail --extension ts",
"prepublishOnly": "npm run prettier && npm run lint && npm run build && prettier:build && lint:build",
"prepublishOnly": "npm run prettier && npm run lint && npm run build && npm run prettier:build && npm run lint:build",
"postpublish": "rm -fr build"
},
"repository": {
Expand Down
65 changes: 23 additions & 42 deletions src/rest.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,21 @@
import { RPC, RPCOptions } from "rpc-request";

export type formatParam = {
format?: "json" | "hex" | "bin";
};
export type formatParam = { format?: "json" | "hex" | "bin" };

export type BlockParams = formatParam & {
hash: string;
};
export type BlockParams = formatParam & { hash: string };

export type BlockHeightParams = formatParam & {
height: number;
};
export type BlockHeightParams = formatParam & { height: number };

export type Outpoint = {
txid: string;
n: number;
};
export type Outpoint = { txid: string; n: number };

export type UtxosParams = formatParam & {
checkmempool?: boolean;
outpoints: Outpoint[] | Outpoint;
};

export type HeaderParams = BlockParams & {
count?: number;
};
export type HeaderParams = BlockParams & { count?: number };

export type TxParams = formatParam & {
txid: string;
};
export type TxParams = formatParam & { txid: string };

export type RESTIniOptions = RPCOptions & { url?: string };

Expand All @@ -46,7 +33,7 @@ export class RESTClient extends RPC {
timeout = 30000,
...options
}: RESTIniOptions = {}) {
super({ ...options, baseUrl: url + ":" + port, timeout, json: true });
super({ ...options, baseUrl: `${url}:${port}`, timeout, json: true });
}

/**
Expand All @@ -55,8 +42,8 @@ export class RESTClient extends RPC {
* @param {string} params.hash - The hash of the header of the block to get
* @param {string} [params.format='json'] - Set to 'json' for decoded block contents in JSON, or 'bin' or 'hex' for a serialized block in binary or hex
*/
async getBlock({ hash, format = "json" }: BlockParams) {
return this.get({ uri: "rest/block/" + hash + "." + format });
getBlock({ hash, format = "json" }: BlockParams) {
return this.get({ uri: `/rest/block/${hash}.${format}` });
}

/**
Expand All @@ -65,8 +52,8 @@ export class RESTClient extends RPC {
* @param {string} params.hash - The hash of the header of the block to get
* @param {string} [params.format='json'] - Set to 'json' for decoded block contents in JSON, or 'bin' or 'hex' for a serialized block in binary or hex
*/
async getBlockNoTxDetails({ hash, format = "json" }: BlockParams) {
return this.get({ uri: "rest/block/notxdetails/" + hash + "." + format });
getBlockNoTxDetails({ hash, format = "json" }: BlockParams) {
return this.get({ uri: `/rest/block/notxdetails/${hash}.${format}` });
}

/**
Expand All @@ -75,15 +62,14 @@ export class RESTClient extends RPC {
* @param {number} params.height - The height of the block
* @param {string} [params.format='json'] - Set to `json`, `bin` or `hex`.
*/
async getBlockHashByHeight({ height, format = "json" }: BlockHeightParams) {
const uri = "/rest/blockhashbyheight/" + height + "." + format;
return this.get({ uri });
getBlockHashByHeight({ height, format = "json" }: BlockHeightParams) {
return this.get({ uri: `/rest/blockhashbyheight/${height}.${format}` });
}

/**
* @description Get information about the current state of the block chain.
*/
async getChainInfo() {
getChainInfo() {
return this.get({ uri: "rest/chaininfo.json" });
}

Expand All @@ -94,15 +80,11 @@ export class RESTClient extends RPC {
* @param {Object|Object[]} params.outpoints - The list of outpoints to be queried.
* @param {string} [params.format='json'] - Set to `json`, `bin` or `hex`.
*/
async getUtxos({
checkmempool = true,
outpoints,
format = "json",
}: UtxosParams) {
let uri = "rest/getutxos" + (checkmempool ? "/checkmempool" : "");
getUtxos({ checkmempool = true, outpoints, format = "json" }: UtxosParams) {
let uri = `rest/getutxos${checkmempool ? "/checkmempool" : ""}`;
outpoints = !Array.isArray(outpoints) ? [outpoints] : outpoints;
for (const { txid, n } of outpoints) {
uri += "/" + txid + "-" + n;
uri += `/${txid}-${n}`;
}
return this.get({ uri: uri + "." + format });
}
Expand All @@ -114,22 +96,21 @@ export class RESTClient extends RPC {
* @param {string} params.hash - The hash of the header of the block to get
* @param {string} [params.format='json'] - Set to `json`, `bin` or `hex`.
*/
async getHeaders({ count, hash, format = "json" }: HeaderParams) {
const uri = "rest/headers/" + count + "/" + hash + "." + format;
return this.get({ uri });
getHeaders({ count, hash, format = "json" }: HeaderParams) {
return this.get({ uri: `rest/headers/${count}/${hash}.${format}` });
}

/**
* @description Get all transaction in the memory pool with detailed information.
*/
async getMemPoolContents() {
getMemPoolContents() {
return this.get({ uri: "rest/mempool/contents.json" });
}

/**
* @description Get information about the node’s current transaction memory pool.
*/
async getMemPoolInfo() {
getMemPoolInfo() {
return this.get({ uri: "rest/mempool/info.json" });
}

Expand All @@ -139,7 +120,7 @@ export class RESTClient extends RPC {
* @param {string} params.txid - The TXID of the transaction to get.
* @param {string} [params.format='json'] - Set to `json`, `bin` or `hex`.
*/
async getTx({ txid, format = "json" }: TxParams) {
return this.get({ uri: "rest/tx/" + txid + "." + format });
getTx({ txid, format = "json" }: TxParams) {
return this.get({ uri: `rest/tx/${txid}.${format}` });
}
}
Loading

0 comments on commit b1b8cff

Please sign in to comment.