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 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
@@ -327,12 +304,12 @@ async function main() {
327
304
let txid = "" ;
328
305
let txInfoSigned ;
329
306
{
330
- let utxosResult = await rpc . getaddressutxos ( {
307
+ let utxosResult = await rpc ( "getaddressutxos" , {
331
308
addresses : [ burnAddr ] ,
332
309
} ) ;
333
310
// TODO make sure there's just 1
334
311
// @type {Array<DashTx.TxInput> } */
335
- let inputs = [ utxosResult . result [ 0 ] ] ;
312
+ let inputs = [ utxosResult [ 0 ] ] ;
336
313
// TODO the hash bytes may be reversed
337
314
// @type {Array<DashTx.TxOutput> } */
338
315
let outputs = [ { memo : gobjIdForward , satoshis : 100000000 } ] ;
@@ -352,24 +329,19 @@ async function main() {
352
329
}
353
330
354
331
async function check ( ) {
355
- let gobjResult = await rpc
356
- . request ( "/" , {
357
- method : "gobject" ,
358
- params : [ "check" , gobj . dataHex ] ,
359
- } )
360
- . catch (
361
- /** @param {any } err */ function ( err ) {
362
- console . error ( err . message ) ;
363
- console . error ( err . code ) ;
364
- console . error ( err ) ;
365
- // invalid burn hash
366
- return null ;
367
- } ,
368
- ) ;
332
+ let gobjResult = await rpc ( "gobject" , "check" , gobj . dataHex ) . catch (
333
+ /** @param {any } err */ function ( err ) {
334
+ console . error ( err . message ) ;
335
+ console . error ( err . code ) ;
336
+ console . error ( err ) ;
337
+ // invalid burn hash
338
+ return null ;
339
+ } ,
340
+ ) ;
369
341
370
342
// { result: { 'Object status': 'OK' }, error: null, id: 5542 }
371
- if ( gobjResult ?. result ?. [ "Object status" ] !== "OK" ) {
372
- throw new Error ( `gobject failed: ${ gobjResult . result . error } ` ) ;
343
+ if ( gobjResult ?. [ "Object status" ] !== "OK" ) {
344
+ throw new Error ( `gobject failed: ${ gobjResult . error } ` ) ;
373
345
}
374
346
return gobjResult ;
375
347
}
@@ -379,40 +351,29 @@ async function main() {
379
351
// ./bin/gobject-prepare.js 1 3 100 https://example.com/proposal-00 proposal-00 yPPy7Z5RQj46SnFtuFXyT6DFAygxESPR7K ./yjZxu7SJAwgSm1JtWybuQRYQDx34z8P2Z7.wif
380
352
// set to false to short circuit for testing
381
353
if ( true ) {
382
- let txResult = await rpc . request ( "/" , {
383
- method : "sendrawtransaction" ,
384
- params : [ txInfoSigned . transaction ] ,
385
- } ) ;
354
+ let txResult = await rpc ( "sendrawtransaction" , txInfoSigned . transaction ) ;
386
355
console . log ( "" ) ;
387
356
console . log ( "Transaction sent:" ) ;
388
357
console . log ( txResult ) ;
389
358
}
390
359
391
360
for ( ; ; ) {
392
- let txResult = await rpc
393
- . request ( "/" , {
394
- method : "gettxoutproof" ,
395
- params : [ [ txid ] ] ,
396
- } )
397
- . catch (
398
- /** @param {Error } err */ function ( err ) {
399
- const E_NOT_IN_BLOCK = - 5 ;
400
- // @ts -ignore - code exists
401
- let code = err . code ;
402
- if ( code === E_NOT_IN_BLOCK ) {
403
- return null ;
404
- }
405
- throw err ;
406
- } ,
407
- ) ;
361
+ let txResult = await rpc ( "gettxoutproof" , [ txid ] ) . catch (
362
+ /** @param {Error } err */ function ( err ) {
363
+ const E_NOT_IN_BLOCK = - 5 ;
364
+ // @ts -ignore - code exists
365
+ let code = err . code ;
366
+ if ( code === E_NOT_IN_BLOCK ) {
367
+ return null ;
368
+ }
369
+ throw err ;
370
+ } ,
371
+ ) ;
408
372
if ( txResult ) {
409
373
console . log ( "" ) ;
410
374
console . log ( `TxOutProof` ) ;
411
375
console . log ( txResult ) ;
412
- let jsonResult = await rpc . request ( "/" , {
413
- method : "getrawtransaction" ,
414
- params : [ txid , 1 ] ,
415
- } ) ;
376
+ let jsonResult = await rpc ( "getrawtransaction" , txid , 1 ) ;
416
377
console . log ( "" ) ;
417
378
console . log ( `Tx` ) ;
418
379
console . log ( jsonResult ) ;
@@ -437,7 +398,7 @@ async function main() {
437
398
} ;
438
399
let args = req . params . join ( " " ) ;
439
400
console . log ( `${ req . method } ${ args } ` ) ;
440
- let gobjResult = await rpc . request ( "/ ", req ) . catch (
401
+ let gobjResult = await rpc ( "gobject ", ... req . params ) . catch (
441
402
/** @param {Error } err */ function ( err ) {
442
403
const E_INVALID_COLLATERAL = - 32603 ;
443
404
// @ts -ignore - code exists
0 commit comments