Skip to content

Commit

Permalink
Merge pull request #7 from vansergen/generating
Browse files Browse the repository at this point in the history
Generating
  • Loading branch information
vansergen authored Oct 24, 2019
2 parents 0092257 + d9d11dd commit b1ff6cc
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 2 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,30 @@ const result = await client.stop();
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
const nblocks = 1;
const maxtries = 10000;
const address = "1HLoD9E4SDFFPDiYfNYnkBLQ85Y51J3Zb1";
const wallet = "bitcoin-core-wallet.dat";
const result = await client.generatetoaddress(
{ nblocks, address, maxtries },
wallet
);
```

### ZMQ

- [`getzmqnotifications`](https://bitcoincore.org/en/doc/0.17.0/rpc/zmq/getzmqnotifications/)
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.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rpc-bitcoin",
"version": "1.4.0",
"version": "1.5.0",
"description": "A TypeScript library to make RPC and HTTP REST requests to Bitcoin Core",
"main": "build/index.js",
"type": "module",
Expand Down
33 changes: 33 additions & 0 deletions src/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,15 @@ export type LoggingParams = {
exclude?: string[] | "all" | "none" | 0 | 1;
};

export type GenerateParams = {
nblocks: number;
maxtries?: number;
};

export type GenerateToAddressParams = GenerateParams & {
address: string;
};

export class RPCClient extends RESTClient {
wallet?: string;
fullResponse?: boolean;
Expand Down Expand Up @@ -307,6 +316,30 @@ export class RPCClient extends RESTClient {
return this.rpc("uptime");
}

/**
* @description Mine up to nblocks blocks immediately (before the RPC call returns) to an address in the wallet.
*/
async generate(
{ nblocks, maxtries = 1000000 }: GenerateParams,
wallet?: string
) {
return this.rpc("generate", { nblocks, maxtries }, wallet || this.wallet);
}

/**
* @description Mine blocks immediately to a specified address (before the RPC call returns)
*/
async generatetoaddress(
{ nblocks, address, maxtries = 1000000 }: GenerateToAddressParams,
wallet?: string
) {
return this.rpc(
"generatetoaddress",
{ nblocks, maxtries, address },
wallet || this.wallet
);
}

/**
* @description Returns information about the active ZeroMQ notifications.
*/
Expand Down
95 changes: 95 additions & 0 deletions test/rpc.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1010,6 +1010,101 @@ suite("RPCClient", () => {
});
});

suite("Generating", () => {
test(".generate()", async () => {
const params = { nblocks: 1, maxtries: 10000 };
const request = { params, method: "generate", id, jsonrpc };
const result: any[] = [];
const response = { result, error, id };
nock(uri)
.post("/", request)
.times(1)
.basicAuth(auth)
.reply(200, response);
const data = await client.generate(params);
assert.deepStrictEqual(data, result);
});

test(".generate() (multi-wallet)", async () => {
const params = { nblocks: 1, maxtries: 10000 };
const wallet = "bitcoin-core-wallet.dat";
const request = { params, method: "generate", id, jsonrpc };
const result: any[] = [];
const response = { result, error, id };
nock(uri)
.post("/wallet/" + wallet, request)
.times(1)
.basicAuth(auth)
.reply(200, response);
const data = await client.generate(params, wallet);
assert.deepStrictEqual(data, result);
});

test(".generate() (default wallet)", async () => {
const wallet = "bitcoin-core-wallet.dat";
const client = new RPCClient({ port, timeout, pass, wallet });
const params = { nblocks: 1, maxtries: 10000 };
const request = { params, method: "generate", id, jsonrpc };
const result: any[] = [];
const response = { result, error, id };
nock(uri)
.post("/wallet/" + wallet, request)
.times(1)
.basicAuth(auth)
.reply(200, response);
const data = await client.generate(params);
assert.deepStrictEqual(data, result);
});

test(".generatetoaddress()", async () => {
const address = "tb1qc4gce3kvc8px505r4wurwdytqclkdjta68qlh4";
const params = { nblocks: 1, maxtries: 10000, address };
const request = { params, method: "generatetoaddress", id, jsonrpc };
const result: any[] = [];
const response = { result, error, id };
nock(uri)
.post("/", request)
.times(1)
.basicAuth(auth)
.reply(200, response);
const data = await client.generatetoaddress(params);
assert.deepStrictEqual(data, result);
});

test(".generatetoaddress() (multi-wallet)", async () => {
const address = "tb1qc4gce3kvc8px505r4wurwdytqclkdjta68qlh4";
const params = { nblocks: 1, maxtries: 10000, address };
const wallet = "bitcoin-core-wallet.dat";
const request = { params, method: "generatetoaddress", id, jsonrpc };
const result: any[] = [];
const response = { result, error, id };
nock(uri)
.post("/wallet/" + wallet, request)
.times(1)
.basicAuth(auth)
.reply(200, response);
const data = await client.generatetoaddress(params, wallet);
assert.deepStrictEqual(data, result);
});

test(".generatetoaddress() (default wallet)", async () => {
const wallet = "bitcoin-core-wallet.dat";
const client = new RPCClient({ port, timeout, pass, wallet });
const address = "tb1qc4gce3kvc8px505r4wurwdytqclkdjta68qlh4";
const params = { nblocks: 1, maxtries: 10000, address };
const request = { params, method: "generatetoaddress", id, jsonrpc };
const result: any[] = [];
const response = { result, error, id };
nock(uri)
.post("/wallet/" + wallet, request)
.times(1)
.basicAuth(auth)
.reply(200, response);
const data = await client.generatetoaddress(params);
assert.deepStrictEqual(data, result);
});
});

suite("Zmq", () => {
test(".getzmqnotifications()", async () => {
const params = {};
Expand Down

0 comments on commit b1ff6cc

Please sign in to comment.