-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
828 lines (653 loc) · 35.9 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
const request = require('request');
const bodyParser = require('body-parser');
const express = require('express');
var app = express();
const http = require('http');
const server = http.createServer(app);
//const uuid = require('uuid);
// add sessions / express-sessions to log in user / cache data?
// https://hacks.mozilla.org/2012/12/using-secure-client-side-sessions-to-build-simple-and-scalable-node-js-applications-a-node-js-holiday-season-part-3/
// https://hackernoon.com/how-to-use-session-in-nodejs
// replace request with axios
const axios = require('axios');
const { Server } = require("socket.io");
const io = new Server(server);
// const async = require('async');
const us = new Intl.NumberFormat('en-us');
const os = require('os');
// load up .env file vars. accessible with process.env.variable
const dotenv = require('dotenv');
dotenv.config();
app.set('views', __dirname + '/views');
app.use(express.static('public'));
app.set('view engine', 'ejs');
// Need bodyParser?
// configure body-parser for express
app.use(bodyParser.urlencoded({extended:false}));
app.use(bodyParser.json());
// User and password specified like so: node index.js username password PIN - OLD
//let username = process.argv.length < 2 ? "default-username" : process.argv[2];
//let password = process.argv.length < 3 ? "default-password" : process.argv[3];
//let PIN = process.argv.length < 4 ? "default-PIN" : process.argv[4];
// Using .env file, safer than entering passwords in plain text on the app start
const username = process.env.rpcuser;
const password = process.env.rpcpassword;
const PIN = process.env.PIN;
// check if env file vars are empty. If so, let user know and exit app.
if(!username){
console.log('rpcuser is not configured in .env file. Create a file named .env and add rpcuser=rpcusername');
process.exit(1);
}
if(!password){
console.log('rpcpassword is not configured in .env file. Create a file named .env and add rpcpassword=rpcpassword123');
process.exit(1);
}
if(!PIN){
console.log('PIN is not configured in .env file. Create a file named .env and add PIN=1234 (up to 9999) to configure your PIN');
process.exit(1);
}
// init global vars
let walletInfo;
let zAccounts;
let zUsd;
let systemInfo;
let zInfo;
let miningInfo;
let addresses;
let options = {
method: 'post',
url: "http://localhost:8232",
withCredentials: true,
responseType: 'json',
headers:
{
"content-type": "text/plain"
},
auth: {
user: username,
pass: password
}
};
// AXIOSconfig
let axiosConfig = {
method: 'POST',
url: "http://localhost:8232",
auth: {
username: username,
password: password
},
data: {
"jsonrpc": "1.0",
"id": "curltest",
"method": "getwalletinfo",
"params": []
}
};
let optionsCoinGecko = {
url: "https://api.coingecko.com/api/v3/simple/price?ids=zcash&vs_currencies=usd",
method: "get",
headers:
{
"content-type": "application/json'"
}
}
const waitFor = (ms) => new Promise(r => setTimeout(r, ms));
// initial page load at localhost:8081
app.get('/', (req, res) => {
console.log("Got a GET request for the homepage for zcashdxpress");
// Get latest ZEC price from CoinGecko, then returning the page.
//axios.get(optionsCoinGecko.url).then(function (response) {
//console.log(response.data)
//// no need to parse gecko resp.
//zUsd = response.data.zcash.usd
//// if we have a user connected, send updates for the price of ZEC
//}).catch(function (error) {
//// handle error
//console.log(error);
//}).then(function () {
//})
// Gather data from RPC calls zcashd. Currently caching responses and then serving that to the webpage.
// https://axios-http.com/docs/api_intro
if(addresses && walletInfo){
walletInfo.addresses = addresses;
}
let sendSuccess = false;
let loggedIn = false;
//TODO: combine all these objects into one master obj.
res.render('index', { wallet: walletInfo, zAccounts: zAccounts, zUsd: zUsd, zInfo: zInfo, systemInfo: systemInfo, sendSuccess: sendSuccess, loggedIn: loggedIn, errormsg: null, successmsg: null});
})
app.get('/accountz', (req, res) => {
console.log('Got a request for the /accountz page, serving json data');
res.send(JSON.stringify(zAccounts, null, 2));
//zAccounts.forEach(account => {
//account.forEach(
//})
})
app.get('/addresses', (req, res) => {
console.log('Got a request for the /addresses page, serving json data');
res.send(JSON.stringify(addresses, null, 10));
// "<br />"));
//zAccounts.forEach(account => {
//account.forEach(
//})
})
app.post('/login', (req, res) => {
let loggedIn = false;
let sendSuccess = false;
let errormsg;
// console.log('PIN entered = ' + req.body.userPIN);
//PIN submission, handle it here.
let userPIN = req.body.userPIN;
// if the PIN was entered, check if it's valid. If it is, log them in.
if(userPIN) {
if(userPIN == PIN){
console.log('user PIN entered matches PIN on file');
loggedIn = true;
}else{
console.log('User entered PIN did not match. Re-enter PIN');
errormsg = 'User entered PIN did not match. Re-enter PIN';
loggedIn = false;
}
}
res.render('index', { wallet: walletInfo, zAccounts: zAccounts, zUsd: zUsd, zInfo: zInfo, systemInfo: systemInfo, sendSuccess: sendSuccess, loggedIn: loggedIn, errormsg: errormsg, successmsg: null});
})
// HANDLE ALL FORM SUBMISSIONS
app.post('/', (req, res) => {
console.log('form submitted, determining which form that is - test form or sendZEC form');
// var init - user is not yet logged in, sendSuccess is false, errormsg is null;
let loggedIn = false;
let sendSuccess = false;
let errormsg;
// Validate user entered PIN
if(req.body.userPIN){
console.log('userPIN form submitted');
//PIN submission, handle it here.
let userPIN = req.body.userPIN;
// if the PIN was entered, check if it's valid. If it is, log them in.
if(userPIN) {
if(userPIN == PIN){
console.log('user PIN entered matches PIN on file');
loggedIn = true;
}else{
console.log('User entered PIN did not match. Re-enter PIN');
errormsg = 'User entered PIN did not match. Re-enter PIN';
loggedIn = false;
}
}
res.render('index', { wallet: walletInfo, zAccounts: zAccounts, zUsd: zUsd, zInfo: zInfo, systemInfo: systemInfo, sendSuccess: sendSuccess, loggedIn: loggedIn, errormsg: errormsg, successmsg: null});
}
if(req.body.fromAddress){
console.log('submitZEC form submitted');
//user is already logged in if they're submitting this form. Keep user logged in?
// send request, handle it here.
let fromAddress = req.body.fromAddress.trim();
let toAddress = req.body.toAddress.trim();
let amountZEC = req.body.amount;
let allowRevealedAmountsCheck = req.body.allowRevealedAmounts;
let errormsg;
let continu = true;
let allowRevealedAmounts;
if(allowRevealedAmountsCheck){
allowRevealedAmounts = true;
}else{
allowRevealedAmounts = false;
}
//TODO: add in MEMO functionality. Take in text from user, trim whitespace, convert to HEX?, then include in z_sendmany RPC call.
let sendObj = {
"address": toAddress,
"amount": amountZEC
}
// DEBUG MOCK SEND. COMMENT ALL BELOW
//console.log('Mock send success');
//console.log('fromAddress - '+ fromAddress);
//console.log('toAddress - '+ toAddress);
//console.log('amountZEC - '+ amountZEC);
//console.log('allowRevealedAmounts - '+ allowRevealedAmounts);
//setTimeout(function () {
//let successmsg = 'MOCK ZEC SEND TX SUCCESS';
//sendSuccess = true;
////return with user logged in so they can see the successmsg, txid, etc.
//loggedIn = true;
//res.render('index', { wallet: walletInfo, zAccounts: zAccounts, zUsd: zUsd, zInfo: zInfo, systemInfo: systemInfo, sendSuccess: sendSuccess, loggedIn: loggedIn, successmsg: successmsg, errormsg: null});
//}, 20000);
// DEBUG MOCK SEND. COMMENT ALL ABOVE.
// SEND ZEC
// set axiosConfig.data for validating z addr.
// z_validateaddress
axiosConfig.data = {
"jsonrpc": "1.0",
"id": "curltest",
"method": "z_validateaddress",
"params": [fromAddress]
}
// VALIDATE THE 'FROM ADDRESS' - RPC CALL
axios(axiosConfig).then(function (response) {
//console.log(response.data.result);
addrValidation = response.data.result;
if(addrValidation.isvalid === false){
console.log('The "From Address" is not a valid z address');
errormsg = 'The "From Address" is not a valid address';
// return with errorMsg, stay logged in.
loggedIn = true;
sendSuccess = false;
res.render('index', { wallet: walletInfo, zAccounts: zAccounts, zUsd: zUsd, zInfo: zInfo, systemInfo: systemInfo, sendSuccess: sendSuccess, loggedIn: loggedIn, errormsg: errormsg, successmsg: null});
continu = false;
}
}).catch(function (error) {
// handle error
if(error.response) {
console.log(error.response.data);
let errormsg = error.response.data;
}else{
console.log(error);
let errormsg = error;
}
loggedIn = true;
sendSuccess = false;
res.render('index', { wallet: walletInfo, zAccounts: zAccounts, zUsd: zUsd, zInfo: zInfo, systemInfo: systemInfo, sendSuccess: sendSuccess, loggedIn: loggedIn, errormsg: errormsg, successmsg: null});
continu = false;
})
.then(function () {
// VALIDATE THE 'TO ADDRESS' - RPC CALL
if(continu === true) {
axiosConfig.data = {
"jsonrpc": "1.0",
"id": "curltest",
"method": "z_validateaddress",
"params": [toAddress]
}
axios(axiosConfig).then(function (response) {
addrValidation = response.data.result;
if(addrValidation.isvalid === false){
console.log('The "To Address" is not a valid z or ua address');
errormsg = 'The "To Address" is not a valid z or ua address';
// return with errorMsg, stay logged in.
loggedIn = true;
sendSuccess = false;
res.render('index', { wallet: walletInfo, zAccounts: zAccounts, zUsd: zUsd, zInfo: zInfo, systemInfo: systemInfo, sendSuccess: sendSuccess, loggedIn: loggedIn, errormsg: errormsg, successmsg: null});
return;
}
}).catch(function (error) {
// handle error
if(error.response) {
console.log(error.response.data);
let errormsg = error.response.data;
}else{
console.log(error);
let errormsg = error;
}
loggedIn = true;
sendSuccess = false;
res.render('index', { wallet: walletInfo, zAccounts: zAccounts, zUsd: zUsd, zInfo: zInfo, systemInfo: systemInfo, sendSuccess: sendSuccess, loggedIn: loggedIn, errormsg: errormsg, successmsg: null});
return;
})
.then(function () {
if(continu === true) {
// Both addresses have been validated, assume the amount is correct and continue sending.
// if there is an issue with the amount, or other params, it will fail and report the error
// .THEN THE z_sendmany CALL HERE
console.log('Addresses have been validated, continuing to send ZEC');
// Check if we're allowing revealed amounts / migrating pools.
let sendObjArray = new Array();
sendObjArray.push(sendObj);
if(allowRevealedAmounts){
axiosConfig.data = {
"jsonrpc": "1.0",
"id": "curltest",
"method": "z_sendmany",
"params": [fromAddress, sendObjArray, 1, 0.00001, "AllowRevealedAmounts"]
}
}else{
axiosConfig.data = {
"jsonrpc": "1.0",
"id": "curltest",
"method": "z_sendmany",
"params": [fromAddress, sendObjArray]
}
}
axios(axiosConfig).then(function (response) {
resp = response.data.result;
// resp = opid-xxxx-xxx-xxxx-xxxx
// can use opid to get status/result but it usually takes about ~25 sec for tx to finish.
//response is success, giving back an operation id (tx is submitted but not yet included in a block).
console.log('tx broadcast successfully... ', resp);
opid = resp;
}).catch(function (error) {
// handle error elegantly, showing the actual RPC error instead of the axios error.
if(error.response) {
console.log(error.response.data);
let errormsg = error.response.data;
}else{
console.log(error);
let errormsg = error;
}
//console.log(response.
loggedIn = true;
sendSuccess = false;
res.render('index', { wallet: walletInfo, zAccounts: zAccounts, zUsd: zUsd, zInfo: zInfo, systemInfo: systemInfo, sendSuccess: sendSuccess, loggedIn: loggedIn, errormsg: errormsg, successmsg: null});
}).then(function() {
let sendObjArray = new Array();
sendObjArray.push(opid);
// wait for 25 seconds, then check status and respond.
setTimeout(function () {
let successmsg;
//check status of opid after waiting for 25 sec
axiosConfig.data = {
"jsonrpc": "1.0",
"id": "curltest",
"method": "z_getoperationresult",
"params": [sendObjArray]
}
axios(axiosConfig).then(function (response) {
resp = response.data.result;
//TODO: save this info somewhere in mem to be able to display later? Otherwise, once this RPC call is executed, the opid & txid are wiped from mem.
// only responding with txid info here then it's gone forever. (it is in the logs though)
// DEBUG
// console.log('response.data - ', response.data);
// console.log('response.data.result - ', response.data.result);
sendSuccess = true;
// RESPONSE IS AN ARRAY. WE ARE ONLY looking up ONE TX SO JUST GRAB THE FIRST one [0]
let txResult = response.data.result[0];
if(txResult){
console.log('The transaction was '+ txResult.status + ' with txid: ' + txResult.result.txid + '. Execution time was '+ txResult.execution_secs + '. The opid is '+ opid);
if(txResult.status === 'success'){
successmsg = 'The send transaction was successful! Txid: ' + txResult.result.txid + '. Execution time: '+ txResult.execution_secs;
}
}else {
successmsg = 'Transaction was submitted successfully with '+ opid + ' but it is still pending inclusion in a block (took longer than 35sec). You can search the zecwallet/debug.log with the opid to see the result or use RPC call: ./zcash-cli z_getoperationresult [opid].';
}
//return with user logged in so they can see the successmsg, txid, etc.
loggedIn = true;
res.render('index', { wallet: walletInfo, zAccounts: zAccounts, zUsd: zUsd, zInfo: zInfo, systemInfo: systemInfo, sendSuccess: sendSuccess, loggedIn: loggedIn, successmsg: successmsg, errormsg: null});
}).catch(function (error) {
// handle error elegantly, showing the actual RPC error instead of the axios error.
if(error.response) {
console.log(error.response.data);
let errormsg = error.response.data;
}else{
console.log(error);
let errormsg = error;
}
})
}, 35000);
})
}
});
}
});
}
})
////SOCKETio new socket connection
//io.on('connection', async (socket) => {
//// Generate a v4 (random) id
//// let userId = uuid.v4();
//userConnected = true;
//console.log('a new user connected');
//// console.log('user id: '+ userId);
//// setInterval(getZusd, 25000, socket);
//let x = 0;
//while(userConnected){
////setInterval(getZusd, 25000, socket);
//console.log("we're at # " + x);
//await waitFor(5000);
//x++;
//}
//socket.on('disconnect', function () {
//console.log('A user disconnected');
//userConnected = false;
//});
//})
// SOCKETio docs - https://socket.io/docs/v4/server-socket-instance/
function getWalletInfo(){
//options.body = JSON.stringify( {"jsonrpc": "1.0", "id": "curltest", "method": "getwalletinfo", "params": [] })
//options.data = {"jsonrpc": "1.0", "id": "curltest", "method": "getwalletinfo", "params": [] }
axiosConfig.data = {
"jsonrpc": "1.0",
"id": "curltest",
"method": "getwalletinfo",
"params": []
}
axios(axiosConfig).then(function (response) {
//console.log(response.data.result);
walletInfo = response.data.result;
// JSON.parse(walletInfo);
// console.log(walletInfo)
}).catch(function (error) {
// handle error
console.log(error);
})
.then(function () {
// always executed
});
}
// fetches all accounts associated with wallet
// then fetches all addresses associated with each account.
// then fetches address balance associated with each address.
// If there is a large number of Accounts & addresses, this will take some time. We are caching responses to serve when homepage is hit.
function getAccounts(){
console.log('getting accounts');
axiosConfig.data = {
"jsonrpc": "1.0",
"id": "curltest",
"method": "z_listaccounts",
"params": []
}
axios(axiosConfig).then(function (response) {
console.log(response.data.result);
zAccounts = response.data.result;
if(zAccounts){
zAccounts.forEach(zacnt => {
// console.log('zacnt - ', zacnt);
// console.log(zacnt.addresses);
// RPC call with zAccounts to get balances for each account - z_getbalanceforaccount
axiosConfig.data = {
"jsonrpc": "1.0",
"id": "curltest",
"method": "z_getbalanceforaccount",
"params": [zacnt.account, 1]
}
axios(axiosConfig).then(function (response) {
//set response within each zacnt cached in the system
zacnt.pools = response.data.result;
}).catch(function (error) {
// handle error
console.log(error);
}).then(function () {
//zacnt.addresses.forEach(addr => {
//// zcash-cli z_getbalance "address"
//axiosConfig.data = {
//"jsonrpc": "1.0",
//"id": "curltest",
//"method": "z_getbalance",
//"params": [addr.ua]
//}
//axios(axiosConfig).then(function (response) {
//// console.log(response.data);
//// add response to the zAccount
//addr.bal = response.data.result;
//}).catch(function (error) {
//console.log(error);
//})
//})
}) // end of then() func.
//create address array to use in rpc call to find all unspent utxo's for each Zaccount
// for some reason with z_listunspent, using one of the accounts ua's pulls info for all of them.
// if you were to pass in all (x) addresses, then you get the unspent tx's multiplied by (x).
// will probably have to fix this later.
// TODO: SCRAP UNSPENT AND SWITCH TO Z_GETBALANCE (which works correctly although is/will be deprecated?).
let addressList = new Array();
addressList.push(zacnt.addresses[0].ua);
// console.log('addressList - ', addressList);
// RPC call with zacnt.addresses[] using z_listunspent (returns all unspent tx for addr) - https://zcash.github.io/rpc/z_listunspent.html
// RPC call with zAccounts to get balances for each account - z_getbalanceforaccount
axiosConfig.data = {
"jsonrpc": "1.0",
"id": "curltest",
"method": "z_listunspent",
"params": [1, 99999999, false, addressList]
}
axios(axiosConfig).then(function (response) {
// console.log(response.data);
// add response to the zAccount
zacnt.unspentList = response.data.result;
//var txids = new Array();
//var result = response.data.result;
//var uniqueResult = new Array();
//let x = 0;
//let y = 0;
//result.forEach(unspent => {
//if(txids.indexOf(unspent.txid) == -1){
//uniqueResult.push(unspent);
//txids.push(unspent.txid);
//y++;
//}
//x++;
//})
//console.log('total length of unspent list = '+x);
//console.log('total length of unspent list unique = '+y);
//zacnt.unspentList = uniqueResult;
//Result (output indices for only one value pool will be present):
//(array of json object)
//{
//"txid" : "txid", (string) the transaction id
//"pool" : "sprout|sapling|orchard", (string) The shielded value pool
//"jsindex" (sprout) : n, (numeric) the joinsplit index
//"jsoutindex" (sprout) : n, (numeric) the output index of the joinsplit
//"outindex" (sapling, orchard) : n, (numeric) the Sapling output or Orchard action index
//"confirmations" : n, (numeric) the number of confirmations
//"spendable" : true|false, (boolean) true if note can be spent by wallet, false if address is watchonly
//"account" : n, (numeric, optional) the unified account ID, if applicable
//"address" : "address", (string, optional) the shielded address, omitted if this note was sent to an internal receiver
//"amount": xxxxx, (numeric) the amount of value in the note
//"memo": xxxxx, (string) hexadecimal string representation of memo field
//"change": true|false, (boolean) true if the address that received the note is also one of the sending addresses
//}
// try converting hex to readable so I can "de-code" the memos
// hex.toString('utf-8')
}).catch(function (error) {
// handle error
console.log(error);
}) //end catch err
}) // end zAccounts.forEach(zacnt)
}
}).catch(function (error) {
// handle error
console.log(error);
})
}
// GET USD for ZEC from coinGecko and display
function getZusd(){
// options.body = JSON.stringify( {"jsonrpc": "1.0", "id": "curltest", "method": "z_listaccounts", "params": [] })
axios.get(optionsCoinGecko.url).then(function (response) {
console.log(response.data)
// no need to parse gecko resp.
zUsd = response.data.zcash.usd
// if we have a user connected, send updates for the price of ZEC
}).catch(function (error) {
// handle error
console.log(error);
})
// messari API ?
// curl --compressed "https://data.messari.io/api/v1/assets?fields=id,slug,symbol,metrics/market_data/price_usd"
}
// get basic Zcashd info, cache response.
function getZinfo(){
axiosConfig.data = {
"jsonrpc": "1.0",
"id": "curltest",
"method": "getinfo",
"params": []
}
axios(axiosConfig).then(function (response) {
//console.log(response.data);
zInfo = response.data.result;
}).catch(function (error) {
// handle error
console.log(error);
})
.then(function () {
// always executed
});
}
// get basic system info, cache response.
function getSystemInfo(){
let arch = os.arch();
let freemem = os.freemem();
let loadavg = os.loadavg();
let release = os.release();
let totalmem = os.totalmem();
systemInfo = {'cpu': (arch.length - 1)};
console.log(' // SYSTEM INFO // ');
console.log('Free Memory: ' + freemem);
console.log('Load Avg: ' + loadavg);
console.log((arch.length - 1) + ' CPUs in system');
console.log('Release: ' + release);
console.log('Total Mem: ' + totalmem);
//console.log('os.arch() - ' + arch);
//options.body = JSON.stringify( {"jsonrpc": "1.0", "id": "curltest", "method": "getmininginfo", "params": [] })
//request(options, (error, response, body) => {
//if (error) {
//console.error('An error has occurred - getinfo: ', error);
//} else {
//console.log('Post successful - getinfo');
//// res.send(body);
//// parse the response json
//res = JSON.parse(body);
//miningInfo = res.result;
//console.log('miningInfo - ', miningInfo);
//systemInfo.miningInfo = miningInfo;
//}
//})
}
// listaddresses - gets all addresses known to wallet and how they were derived (source)
function listAddresses(){
axiosConfig.data = {
"jsonrpc": "1.0",
"id": "curltest",
"method": "listaddresses",
"params": []
}
axios(axiosConfig).then(function (response) {
//console.log(response.data);
addresses = response.data.result;
}).catch(function (error) {
// handle error
console.log(error);
}) // always executed
.then(function () {
// filter the addresses and check for balances.
if(walletInfo.balance){
}
// get balances of each address
});
}
// TODO: enable z_sendmany directly from an address
// TODO: get address balances and populate page
// Function to sendZEC
function zSendMany(fromAddress, toAddress, amount, allowRevealedAmounts) {
// send zec => z_sendmany
// '{"jsonrpc": "1.0", "id":"curltest", "method": "z_sendmany", "params": ["t1M72Sfpbz1BPpXFHz9m3CdqATR44Jvaydd", [{"address": "ztfaW34Gj9FrnGUEf833ywDVL62NWXBM81u6EQnM6VR45eYnXhwztecW1SjxA7JrmAXKJhxhj3vDNEpVCQoSvVoSpmbhtjf", "amount": 5.0}]] }' -H 'content-type: text/plain;' http://127.0.0.1:8232/
// check status/result of opid and update.
// ./zcash-cli z_getoperationresult '["opid..."]'
}
// run RPC call functions on app start and then run every x seconds to keep info updated and cached.
getWalletInfo();
getAccounts();
getZusd();
getSystemInfo();
getZinfo();
listAddresses();
// 1000ms is 1 sec
setInterval(getWalletInfo, 10000); // run every 10 sec to cache recent main balances
setInterval(getZusd, 150000); // run every 150 sec to cach ZEC price from coingecko
setInterval(getAccounts, 300000); // run every 300 sec to cache any new accounts created (these shouldn't change often unless creating new accounts/addresses
setInterval(getZinfo, 19000); // cache new zcash blockchain info every 19 sec
setInterval(listAddresses, 21600000); // get New entire wallet address list every 12 hours
function timeout(ms) { //pass a time in milliseconds to this function
return new Promise(resolve => setTimeout(resolve, ms));
}
server.listen(8081, () => {
var port = server.address().port;
console.log("zcashd-fullnodejs is available at http://localhost:"+port);
console.log("Any RPC errors will be logged here.");
})