Skip to content

Commit b9334bb

Browse files
committed
ref: replace DashRPC with DashTx.util.rpc
1 parent b90aaf6 commit b9334bb

File tree

3 files changed

+55
-102
lines changed

3 files changed

+55
-102
lines changed

bin/gobject-prepare.js

Lines changed: 49 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
11
#!/usr/bin/env node
22
"use strict";
33

4-
import DotEnv from "dotenv";
5-
64
import DashGov from "dashgov";
7-
import DashRpc from "dashrpc";
85
import DashKeys from "dashkeys";
96
import DashTx from "dashtx";
107
import * as Secp256k1 from "@dashincubator/secp256k1";
118

129
import Fs from "node:fs/promises";
1310

14-
DotEnv.config({ path: ".env" });
15-
DotEnv.config({ path: ".env.secret" });
16-
1711
async function main() {
1812
/* jshint maxcomplexity: 100 */
1913
/* jshint maxstatements: 1000 */
@@ -32,8 +26,14 @@ async function main() {
3226

3327
/** @type {"mainnet"|"testnet"} */
3428
let network = "mainnet";
29+
let rpcBasicAuth = `api:null`;
30+
let rpcBaseUrl = `https://${rpcBasicAuth}@rpc.digitalcash.dev/`;
31+
let rpcExplorer = "https://rpc.digitalcash.dev/";
32+
3533
let isTestnet = takeFlag(process.argv, ["--testnet"]);
3634
if (isTestnet) {
35+
rpcBaseUrl = `https://${rpcBasicAuth}@trpc.digitalcash.dev/`;
36+
rpcExplorer = "https://trpc.digitalcash.dev/";
3737
network = "testnet";
3838
}
3939

@@ -50,54 +50,31 @@ async function main() {
5050
burnWif = burnWif.trim();
5151
}
5252

53-
let rpcConfig = {
54-
protocol: process.env.DASHD_RPC_PROTOCOL || "",
55-
user: process.env.DASHD_RPC_USER || process.env.DASHD_RPC_USERNAME || "",
56-
pass: process.env.DASHD_RPC_PASS || process.env.DASHD_RPC_PASSWORD || "",
57-
host: process.env.DASHD_RPC_HOST || process.env.DASHD_RPC_HOSTNAME || "",
58-
port: parseInt(process.env.DASHD_RPC_PORT || "", 10),
59-
onconnected: function () {
60-
console.info(
61-
`[dashrpc] connected to '${rpcConfig.host}:${rpcConfig.port}'`,
62-
);
63-
},
64-
};
65-
66-
if (!rpcConfig.protocol) {
67-
throw new Error(`not set: export DASHD_RPC_PROTOCOL=`);
68-
}
69-
if (!rpcConfig.user) {
70-
throw new Error(`not set: export DASHD_RPC_USERNAME=`);
71-
}
72-
if (!rpcConfig.pass) {
73-
throw new Error(`not set: export DASHD_RPC_PASSWORD=`);
74-
}
75-
if (!rpcConfig.host) {
76-
throw new Error(`not set: export DASHD_RPC_HOSTNAME=`);
77-
}
78-
if (!rpcConfig.port) {
79-
throw new Error(`not set: export DASHD_RPC_PORT=`);
53+
/**
54+
* @param {String} method
55+
* @param {...any} params
56+
*/
57+
async function rpc(method, ...params) {
58+
let result = await DashTx.utils.rpc(rpcBaseUrl, method, ...params);
59+
return result;
8060
}
8161

82-
let rpc = DashRpc.create(rpcConfig);
83-
void (await rpc.init());
84-
85-
let tipsResult = await rpc.getBestBlockHash();
86-
let blockInfoResult = await rpc.getBlock(tipsResult.result, 1);
87-
let blockHeight = blockInfoResult.result.height;
88-
let blockMs = blockInfoResult.result.time * 1000;
89-
// console.log(rootInfoResult.result, blockInfoResult.result, blockMs);
62+
let tipsResult = await rpc("getbestblockhash");
63+
let blockInfoResult = await rpc("getblock", tipsResult, 1);
64+
let blockHeight = blockInfoResult.height;
65+
let blockMs = blockInfoResult.time * 1000;
66+
// console.log(rootInfoResult, blockInfoResult, blockMs);
9067
// let blockTime = new Date(blockMs);
9168

9269
// for testnet
9370
let blockDelta = 25000;
94-
let rootHeight = blockInfoResult.result.height - blockDelta;
95-
let rootResult = await rpc.getBlockHash(rootHeight);
96-
let rootInfoResult = await rpc.getBlock(rootResult.result, 1);
71+
let rootHeight = blockInfoResult.height - blockDelta;
72+
let rootResult = await rpc("getblockhash", rootHeight);
73+
let rootInfoResult = await rpc("getblock", rootResult, 1);
9774

9875
let root = {
9976
block: rootHeight,
100-
ms: rootInfoResult.result.time * 1000,
77+
ms: rootInfoResult.time * 1000,
10178
};
10279
// let rootTime = new Date(root.ms);
10380

@@ -326,12 +303,12 @@ async function main() {
326303
let txid = "";
327304
let txInfoSigned;
328305
{
329-
let utxosResult = await rpc.getaddressutxos({
306+
let utxosResult = await rpc("getaddressutxos", {
330307
addresses: [burnAddr],
331308
});
332309
// TODO make sure there's just 1
333310
// @type {Array<DashTx.TxInput>} */
334-
let inputs = [utxosResult.result[0]];
311+
let inputs = [utxosResult[0]];
335312
// TODO the hash bytes may be reversed
336313
// @type {Array<DashTx.TxOutput>} */
337314
let outputs = [{ memo: gobjIdForward, satoshis: 100000000 }];
@@ -351,24 +328,19 @@ async function main() {
351328
}
352329

353330
async function check() {
354-
let gobjResult = await rpc
355-
.request("/", {
356-
method: "gobject",
357-
params: ["check", gobj.dataHex],
358-
})
359-
.catch(
360-
/** @param {any} err */ function (err) {
361-
console.error(err.message);
362-
console.error(err.code);
363-
console.error(err);
364-
// invalid burn hash
365-
return null;
366-
},
367-
);
331+
let gobjResult = await rpc("gobject", "check", gobj.dataHex).catch(
332+
/** @param {any} err */ function (err) {
333+
console.error(err.message);
334+
console.error(err.code);
335+
console.error(err);
336+
// invalid burn hash
337+
return null;
338+
},
339+
);
368340

369341
// { result: { 'Object status': 'OK' }, error: null, id: 5542 }
370-
if (gobjResult?.result?.["Object status"] !== "OK") {
371-
throw new Error(`gobject failed: ${gobjResult.result.error}`);
342+
if (gobjResult?.["Object status"] !== "OK") {
343+
throw new Error(`gobject failed: ${gobjResult.error}`);
372344
}
373345
return gobjResult;
374346
}
@@ -378,40 +350,29 @@ async function main() {
378350
// ./bin/gobject-prepare.js 1 3 100 https://example.com/proposal-00 proposal-00 yPPy7Z5RQj46SnFtuFXyT6DFAygxESPR7K ./yjZxu7SJAwgSm1JtWybuQRYQDx34z8P2Z7.wif
379351
// set to false to short circuit for testing
380352
if (true) {
381-
let txResult = await rpc.request("/", {
382-
method: "sendrawtransaction",
383-
params: [txInfoSigned.transaction],
384-
});
353+
let txResult = await rpc("sendrawtransaction", txInfoSigned.transaction);
385354
console.log("");
386355
console.log("Transaction sent:");
387356
console.log(txResult);
388357
}
389358

390359
for (;;) {
391-
let txResult = await rpc
392-
.request("/", {
393-
method: "gettxoutproof",
394-
params: [[txid]],
395-
})
396-
.catch(
397-
/** @param {Error} err */ function (err) {
398-
const E_NOT_IN_BLOCK = -5;
399-
// @ts-ignore - code exists
400-
let code = err.code;
401-
if (code === E_NOT_IN_BLOCK) {
402-
return null;
403-
}
404-
throw err;
405-
},
406-
);
360+
let txResult = await rpc("gettxoutproof", [txid]).catch(
361+
/** @param {Error} err */ function (err) {
362+
const E_NOT_IN_BLOCK = -5;
363+
// @ts-ignore - code exists
364+
let code = err.code;
365+
if (code === E_NOT_IN_BLOCK) {
366+
return null;
367+
}
368+
throw err;
369+
},
370+
);
407371
if (txResult) {
408372
console.log("");
409373
console.log(`TxOutProof`);
410374
console.log(txResult);
411-
let jsonResult = await rpc.request("/", {
412-
method: "getrawtransaction",
413-
params: [txid, 1],
414-
});
375+
let jsonResult = await rpc("getrawtransaction", txid, 1);
415376
console.log("");
416377
console.log(`Tx`);
417378
console.log(jsonResult);
@@ -436,7 +397,7 @@ async function main() {
436397
};
437398
let args = req.params.join(" ");
438399
console.log(`${req.method} ${args}`);
439-
let gobjResult = await rpc.request("/", req).catch(
400+
let gobjResult = await rpc("gobject", ...req.params).catch(
440401
/** @param {Error} err */ function (err) {
441402
const E_INVALID_COLLATERAL = -32603;
442403
// @ts-ignore - code exists

package-lock.json

Lines changed: 5 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@
4747
"@dashincubator/secp256k1": "^1.7.1-5",
4848
"@types/node": "^22.1.0",
4949
"dashkeys": "^1.1.5",
50-
"dashrpc": "^20.0.0",
51-
"dashtx": "^0.18.2",
50+
"dashtx": "^0.20.1",
5251
"dotenv": "^16.4.5"
5352
}
5453
}

0 commit comments

Comments
 (0)