Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,9 @@ Tx.appraise({ inputs, outputs });

Tx.parseUnknown(serializedHex);

// RPC, for 'sendrawtransaction' broadcast
Tx.utils.rpc(basicAuthUrl, method, arg1, arg2, ...);

// Byte-level helpers
Tx.utils.toVarInt(n);
Tx.utils.toVarIntSize(n);
Expand Down Expand Up @@ -573,6 +576,19 @@ Tx.doubleSha256(txBytes);
Tx.parseUnknown(serializedHex);
```

### RPC Helper

```js
/**
* Make RPC calls to a web service, masternode, or full node
*
* ex:
* - https://api:[email protected]/
* - http://user:pass@localhost:19998/wallet/foo
*/
Tx.utils.rpc(basicAuthUrl, method, arg1, arg2, ...);
```

### Utility Functions

```js
Expand Down
34 changes: 34 additions & 0 deletions dashtx.js
Original file line number Diff line number Diff line change
Expand Up @@ -1691,6 +1691,40 @@ var DashTx = ("object" === typeof module && exports) || {};
}
}

/**
* @param {String} basicAuthUrl - ex: https://api:[email protected]/
* http://user:pass@localhost:19998/
* @param {String} method - the rpc, such as 'getblockchaininfo',
* 'getaddressdeltas', or 'help'
* @param {...any} params - the arguments for the specific rpc
* ex: rpc(url, 'help', 'getaddressdeltas')
*/
TxUtils.rpc = async function rpc(basicAuthUrl, method, ...params) {
let url = new URL(basicAuthUrl);
let baseUrl = `${url.protocol}//${url.host}${url.pathname}`;
let basicAuth = btoa(`${url.username}:${url.password}`);

// typically http://localhost:19998/
let payload = JSON.stringify({ method, params });
let resp = await fetch(baseUrl, {
method: "POST",
headers: {
Authorization: `Basic ${basicAuth}`,
"Content-Type": "application/json",
},
body: payload,
});

let data = await resp.json();
if (data.error) {
let err = new Error(data.error.message);
Object.assign(err, data.error);
throw err;
}

return data.result;
};

/**
* @param {String} hex
*/
Expand Down
4 changes: 2 additions & 2 deletions 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": "dashtx",
"version": "0.18.3",
"version": "0.18.4",
"description": "Create DASH Transactions with Vanilla JS (0 deps, cross-platform)",
"main": "dashtx.js",
"module": "dashtx.mjs",
Expand Down
11 changes: 11 additions & 0 deletions tests/rpc-getblockchaininfo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"use strict";

let Zora = require("zora");

let DashTx = require("../dashtx.js");

Zora.test("rpc 'getblockchaininfo'", async function (t) {
let rpcUrl = "https://user:[email protected]/";
let info = await DashTx.utils.rpc(rpcUrl, "getblockchaininfo");
t.equal(info.chain, "test", `trpc 'chain' should be 'test'`);
});
Loading