diff --git a/README.md b/README.md index f9d0eea..e49d1dd 100644 --- a/README.md +++ b/README.md @@ -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/) diff --git a/package-lock.json b/package-lock.json index 4d0d2d1..e3e09e9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "rpc-bitcoin", - "version": "1.4.0", + "version": "1.5.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index b40878b..7f83af4 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/rpc.ts b/src/rpc.ts index 2a8e275..b819b8f 100644 --- a/src/rpc.ts +++ b/src/rpc.ts @@ -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; @@ -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. */ diff --git a/test/rpc.spec.ts b/test/rpc.spec.ts index f6b0b80..0587993 100644 --- a/test/rpc.spec.ts +++ b/test/rpc.spec.ts @@ -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 = {};