Skip to content

Commit afac272

Browse files
committed
feat: include DashTx.utils.rpc(basicAuthUrl, method, arg1, arg2, ...)
1 parent 1906118 commit afac272

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,9 @@ Tx.appraise({ inputs, outputs });
396396
397397
Tx.parseUnknown(serializedHex);
398398
399+
// RPC, for 'sendrawtransaction' broadcast
400+
Tx.utils.rpc(basicAuthUrl, method, arg1, arg2, ...);
401+
399402
// Byte-level helpers
400403
Tx.utils.toVarInt(n);
401404
Tx.utils.toVarIntSize(n);
@@ -573,6 +576,19 @@ Tx.doubleSha256(txBytes);
573576
Tx.parseUnknown(serializedHex);
574577
```
575578

579+
### RPC Helper
580+
581+
```js
582+
/**
583+
* Make RPC calls to a web service, masternode, or full node
584+
*
585+
* ex:
586+
* - https://api:[email protected]/
587+
* - http://user:pass@localhost:19998/wallet/foo
588+
*/
589+
Tx.utils.rpc(basicAuthUrl, method, arg1, arg2, ...);
590+
```
591+
576592
### Utility Functions
577593

578594
```js

dashtx.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1691,6 +1691,40 @@ var DashTx = ("object" === typeof module && exports) || {};
16911691
}
16921692
}
16931693

1694+
/**
1695+
* @param {String} basicAuthUrl - ex: https://api:[email protected]/
1696+
* http://user:pass@localhost:19998/
1697+
* @param {String} method - the rpc, such as 'getblockchaininfo',
1698+
* 'getaddressdeltas', or 'help'
1699+
* @param {...any} params - the arguments for the specific rpc
1700+
* ex: rpc(url, 'help', 'getaddressdeltas')
1701+
*/
1702+
TxUtils.rpc = async function rpc(basicAuthUrl, method, ...params) {
1703+
let url = new URL(basicAuthUrl);
1704+
let baseUrl = `${url.protocol}//${url.host}${url.pathname}`;
1705+
let basicAuth = btoa(`${url.username}:${url.password}`);
1706+
1707+
// typically http://localhost:19998/
1708+
let payload = JSON.stringify({ method, params });
1709+
let resp = await fetch(baseUrl, {
1710+
method: "POST",
1711+
headers: {
1712+
Authorization: `Basic ${basicAuth}`,
1713+
"Content-Type": "application/json",
1714+
},
1715+
body: payload,
1716+
});
1717+
1718+
let data = await resp.json();
1719+
if (data.error) {
1720+
let err = new Error(data.error.message);
1721+
Object.assign(err, data.error);
1722+
throw err;
1723+
}
1724+
1725+
return data.result;
1726+
};
1727+
16941728
/**
16951729
* @param {String} hex
16961730
*/

tests/rpc-getblockchaininfo.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
"use strict";
2+
3+
let Zora = require("zora");
4+
5+
let DashTx = require("../dashtx.js");
6+
7+
Zora.test("rpc 'getblockchaininfo'", async function (t) {
8+
let rpcUrl = "https://user:[email protected]/";
9+
let info = await DashTx.utils.rpc(rpcUrl, "getblockchaininfo");
10+
t.equal(info.chain, "test", `trpc 'chain' should be 'test'`);
11+
});

0 commit comments

Comments
 (0)