1
1
#!/usr/bin/env node
2
2
"use strict" ;
3
3
4
- import DotEnv from "dotenv" ;
5
-
6
4
import DashGov from "dashgov" ;
7
- import DashRpc from "dashrpc" ;
8
5
import DashKeys from "dashkeys" ;
9
6
import DashTx from "dashtx" ;
10
7
import * as Secp256k1 from "@dashincubator/secp256k1" ;
11
8
12
9
import Fs from "node:fs/promises" ;
13
10
14
- DotEnv . config ( { path : ".env" } ) ;
15
- DotEnv . config ( { path : ".env.secret" } ) ;
16
-
17
11
async function main ( ) {
18
12
/* jshint maxcomplexity: 100 */
19
13
/* jshint maxstatements: 1000 */
@@ -32,8 +26,14 @@ async function main() {
32
26
33
27
/** @type {"mainnet"|"testnet" } */
34
28
let network = "mainnet" ;
29
+ let rpcBasicAuth = `api:null` ;
30
+ let rpcBaseUrl = `https://${ rpcBasicAuth } @rpc.digitalcash.dev/` ;
31
+ let rpcExplorer = "https://rpc.digitalcash.dev/" ;
32
+
35
33
let isTestnet = takeFlag ( process . argv , [ "--testnet" ] ) ;
36
34
if ( isTestnet ) {
35
+ rpcBaseUrl = `https://${ rpcBasicAuth } @trpc.digitalcash.dev/` ;
36
+ rpcExplorer = "https://trpc.digitalcash.dev/" ;
37
37
network = "testnet" ;
38
38
}
39
39
@@ -50,54 +50,31 @@ async function main() {
50
50
burnWif = burnWif . trim ( ) ;
51
51
}
52
52
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 ;
80
60
}
81
61
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);
90
67
// let blockTime = new Date(blockMs);
91
68
92
69
// for testnet
93
70
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 ) ;
97
74
98
75
let root = {
99
76
block : rootHeight ,
100
- ms : rootInfoResult . result . time * 1000 ,
77
+ ms : rootInfoResult . time * 1000 ,
101
78
} ;
102
79
// let rootTime = new Date(root.ms);
103
80
@@ -326,12 +303,12 @@ async function main() {
326
303
let txid = "" ;
327
304
let txInfoSigned ;
328
305
{
329
- let utxosResult = await rpc . getaddressutxos ( {
306
+ let utxosResult = await rpc ( "getaddressutxos" , {
330
307
addresses : [ burnAddr ] ,
331
308
} ) ;
332
309
// TODO make sure there's just 1
333
310
// @type {Array<DashTx.TxInput> } */
334
- let inputs = [ utxosResult . result [ 0 ] ] ;
311
+ let inputs = [ utxosResult [ 0 ] ] ;
335
312
// TODO the hash bytes may be reversed
336
313
// @type {Array<DashTx.TxOutput> } */
337
314
let outputs = [ { memo : gobjIdForward , satoshis : 100000000 } ] ;
@@ -351,24 +328,19 @@ async function main() {
351
328
}
352
329
353
330
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
+ ) ;
368
340
369
341
// { 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 } ` ) ;
372
344
}
373
345
return gobjResult ;
374
346
}
@@ -378,40 +350,29 @@ async function main() {
378
350
// ./bin/gobject-prepare.js 1 3 100 https://example.com/proposal-00 proposal-00 yPPy7Z5RQj46SnFtuFXyT6DFAygxESPR7K ./yjZxu7SJAwgSm1JtWybuQRYQDx34z8P2Z7.wif
379
351
// set to false to short circuit for testing
380
352
if ( true ) {
381
- let txResult = await rpc . request ( "/" , {
382
- method : "sendrawtransaction" ,
383
- params : [ txInfoSigned . transaction ] ,
384
- } ) ;
353
+ let txResult = await rpc ( "sendrawtransaction" , txInfoSigned . transaction ) ;
385
354
console . log ( "" ) ;
386
355
console . log ( "Transaction sent:" ) ;
387
356
console . log ( txResult ) ;
388
357
}
389
358
390
359
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
+ ) ;
407
371
if ( txResult ) {
408
372
console . log ( "" ) ;
409
373
console . log ( `TxOutProof` ) ;
410
374
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 ) ;
415
376
console . log ( "" ) ;
416
377
console . log ( `Tx` ) ;
417
378
console . log ( jsonResult ) ;
@@ -436,7 +397,7 @@ async function main() {
436
397
} ;
437
398
let args = req . params . join ( " " ) ;
438
399
console . log ( `${ req . method } ${ args } ` ) ;
439
- let gobjResult = await rpc . request ( "/ ", req ) . catch (
400
+ let gobjResult = await rpc ( "gobject ", ... req . params ) . catch (
440
401
/** @param {Error } err */ function ( err ) {
441
402
const E_INVALID_COLLATERAL = - 32603 ;
442
403
// @ts -ignore - code exists
0 commit comments