dashhive / DashTx.js

Create TX hex for payments and such
MIT License
3 stars 0 forks source link

feat: include DashTx.utils.rpc(basicAuthUrl, method, arg1, arg2, ...) #79

Closed coolaj86 closed 2 months ago

coolaj86 commented 2 months ago

No need for insight, DashSight, dashd-rpc, or DashRPC.js.

This is the real deal, right here:

 /**
   * @param {String} basicAuthUrl - ex: https://api:token@trpc.digitalcash.dev/
   *                                    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;
  };