-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlitewallet.js
executable file
·1074 lines (886 loc) · 37.9 KB
/
litewallet.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
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const native = require("./native.node");
const axios = require('axios');
const { TxDetail, Transaction, TotalBalance, Address, AddressBalance, WalletSettings, Info } = require('./utils/classes');
class LiteWallet {
constructor(url, chain) {
this.url = url;
this.chain = chain || "main";
this.lastSyncId;
this.lastBlockHeight;
this.lastTxId;
this.transactionsList;
this.walletSettings;
this.lastZecPrice;
this.lastPriceFetch;
this.syncTimerID;
this.refreshTimerId;
this.updateTimerId;
this.updateDataLock = false;
}
restore(mnemonic, birthday, allowOverwrite) {
return new Promise(async (resolve, reject) => {
if(mnemonic) {
const birth = birthday || 0;
const result = await native.zingolib_initialize_new_from_phrase(this.url, mnemonic, birth, allowOverwrite, this.chain);
if (result.startsWith("Error")) {
reject(result);
}
resolve('success');
}
});
}
init() {
return new Promise(async (resolve, reject) => {
if(!native.zingolib_wallet_exists(this.chain)) {
console.log('Wallet not configured, creating new one!');
const res = native.zingolib_initialize_new(this.url, this.chain);
if(res.toString().toLowerCase().startsWith('error')) {
reject("Error: Couldn't create a wallet");
}
else {
const seed = await native.zingolib_execute_async("seed", "");
console.log("Wallet created! Please save the wallet seed:\n" + seed);
}
}
let res = native.zingolib_initialize_existing(this.url, this.chain);
if(res !== 'OK') {
reject('Something went wrong while initializing the wallet. \n'+ res + '\nQuitting ...');
return;
}
native.zingolib_execute_spawn('sync', '');
console.log('Starting wallet synchronization ...');
this.lastSyncId = JSON.parse(await native.zingolib_execute_async('syncstatus','')).sync_id;
const syncPoller = setInterval(async () => {
var syncStatus = JSON.parse(await native.zingolib_execute_async('syncstatus', ''));
if(!syncStatus.in_progress && syncStatus.sync_id > this.lastSyncId) {
clearInterval(syncPoller);
this.lastBlockHeight = await this.fetchLatestBlockHeight();
this.lastTxId = this.fetchLastTxId();
await native.zingolib_execute_async('save','');
// this.fetchTandZTransactions(this.lastBlockHeight);
await this.fetchTandZandOTransactionsSummaries(this.lastBlockHeight);
this.doRefreshAndUpdateData();
resolve('OK');
}
}, 3 * 1000);
});
}
doRefreshAndUpdateData() {
this.refresh();
if(!this.refreshTimerId) {
this.refreshTimerId = setInterval(() => {
this.refresh();
}, 75 * 1000); // refresh wallet every 75 seconds (average block)
}
if(!this.updateTimerId) {
this.updateTimerId = setInterval(() => {
this.updateData();
}, 3 * 1000); // update data every 3 seconds
}
}
async createNewAddress(type) {
const ua = JSON.parse(await native.zingolib_execute_async('new', type));
if(ua.error) return ua.error; // maybe return new Address() ?
const addrDetail = await this.fetchAllAddresses().filter((addr) => addr.address === ua[0]);
const addr = new Address(addrDetail[0].address, addrDetail[0].receivers);
return addr;
}
async zingolibTxSummaries() {
// fetch transaction summaries
const txSummariesStr = await native.zingolib_execute_async("summaries", "");
if(txSummariesStr) {
if (txSummariesStr.toLowerCase().startsWith('error')) {
console.log(`Error txs summaries ${txSummariesStr}`);
return {};
}
}
else {
console.log('Internal Error txs summaries');
return {};
}
const txSummariesJSON = JSON.parse(txSummariesStr);
return txSummariesJSON;
}
// Fetch all T and Z and O transactions
async fetchTandZandOTransactionsSummaries(latestBlockHeight) {
const summariesJSON = await this.zingolibTxSummaries();
//console.log('summaries antes', summariesJSON);
let txList = [];
summariesJSON
//.filter(tx => tx.kind !== 'Fee')
.forEach((tx) => {
let currentTxList = txList.filter(t => t.txid === tx.txid);
if (currentTxList.length === 0) {
currentTxList = [{}];
currentTxList[0].txDetails = [];
}
let restTxList = txList.filter(t => t.txid !== tx.txid);
const type = tx.kind === 'Fee' ? 'Sent' : tx.kind;
if (!currentTxList[0].type && !!type) {
currentTxList[0].type = type;
}
// unconfirmed means 0 confirmations, the tx is mining already.
if (tx.unconfirmed) {
currentTxList[0].confirmations = null;
}
else if (!currentTxList[0].confirmations) {
currentTxList[0].confirmations = latestBlockHeight
? latestBlockHeight - tx.block_height + 1
: 0;
}
if (!currentTxList[0].txid && !!tx.txid) {
currentTxList[0].txid = tx.txid;
}
if (!currentTxList[0].time && !!tx.datetime) {
currentTxList[0].time = tx.datetime;
}
if (!currentTxList[0].zec_price && !!tx.price && tx.price !== 'None') {
currentTxList[0].zec_price = tx.price;
}
//if (tx.txid.startsWith('426e')) {
// console.log('tran: ', tx);
// console.log('--------------------------------------------------');
//}
let currenttxdetails = {};
if (tx.kind === 'Fee') {
currentTxList[0].fee = (currentTxList[0].fee ? currentTxList[0].fee : 0) + tx.amount / 10 ** 8;
if (currentTxList[0].txDetails.length === 0) {
// when only have 1 item with `Fee`, we assume this tx is `SendToSelf`.
currentTxList[0].type = 'SendToSelf';
currenttxdetails.address = '';
currenttxdetails.amount = 0;
currentTxList[0].txDetails.push(currenttxdetails);
}
}
else {
currenttxdetails.address = !tx.to_address || tx.to_address === 'None' ? '' : tx.to_address;
currenttxdetails.amount = tx.amount / 10 ** 8;
currenttxdetails.memos = !tx.memos ? undefined : tx.memos;
currenttxdetails.pool = !tx.pool || tx.pool === 'None' ? undefined : tx.pool;
currentTxList[0].txDetails.push(currenttxdetails);
}
//currentTxList[0].txDetails.forEach(det => console.log(det.memos));
//console.log(currentTxList[0]);
txList = [...currentTxList, ...restTxList];
});
//console.log('summaries despues', txList);
// Now, combine the amounts and memos
const combinedTxList = [];
txList.forEach((txns) => {
//console.log(txns.txDetails.length);
const combinedTx = txns;
if (txns.type === 'Sent' || txns.type === 'SendToSelf') {
// using address for `Sent` & `SendToSelf`
combinedTx.txDetails = this.combineTxDetailsByAddress(txns.txDetails);
}
else {
// using pool for `Received`
combinedTx.txDetails = this.combineTxDetailsByPool(txns.txDetails);
}
//combinedTx.txDetails.forEach(det => console.log(det.memos));
//console.log(combinedTx);
combinedTxList.push(combinedTx);
});
//console.log(combinedTxList);
this.transactionsList = combinedTxList;
}
// We combine detailed transactions if they are sent to the same outgoing address in the same txid. This
// is usually done to split long memos.
// Remember to add up both amounts and combine memos
combineTxDetailsByAddress(txdetails) {
// First, group by outgoing address.
//console.log(txdetails);
const m = new Map();
txdetails
.filter(i => i.address !== undefined)
.forEach(i => {
const coll = m.get(i.address);
if (!coll) {
m.set(i.address, [i]);
}
else {
coll.push(i);
}
});
//console.log(m);
// Reduce the groups to a single TxDetail, combining memos and summing amounts
const reducedDetailedTxns = [];
m.forEach((txns, toaddr) => {
const totalAmount = txns.reduce((sum, i) => sum + i.amount, 0);
const memos = txns
.filter(i => i.memos && i.memos.length > 0)
.map(i => {
const combinedMemo = i.memos
?.filter(memo => memo)
.map(memo => {
const rex = /\((\d+)\/(\d+)\)((.|[\r\n])*)/;
const tags = memo.match(rex);
if (tags && tags.length >= 4) {
return { num: parseInt(tags[1], 10), memo: tags[3] };
}
// Just return as is
return { num: 0, memo };
})
.sort((a, b) => a.num - b.num)
.map(a => a.memo);
return combinedMemo && combinedMemo.length > 0 ? combinedMemo.join('') : undefined;
})
.map(a => a);
const detail = {
address: toaddr,
amount: totalAmount,
memos: memos && memos.length > 0 ? [memos.join('')] : undefined,
};
//console.log(detail);
reducedDetailedTxns.push(detail);
});
//console.log(reducedDetailedTxns);
return reducedDetailedTxns;
}
// We combine detailed transactions if they are received to the same pool in the same txid. This
// is usually done to split long memos.
// Remember to add up both amounts and combine memos
combineTxDetailsByPool(txdetails) {
// First, group by pool.
const m = new Map();
txdetails
.filter(i => i.pool !== undefined)
.forEach(i => {
const coll = m.get(i.pool);
if (!coll) {
m.set(i.pool, [i]);
}
else {
coll.push(i);
}
});
// Reduce the groups to a single TxDetail, combining memos and summing amounts
const reducedDetailedTxns = [];
m.forEach((txns, pool) => {
const totalAmount = txns.reduce((sum, i) => sum + i.amount, 0);
const memos = txns
.filter(i => i.memos && i.memos.length > 0)
.map(i => {
const combinedMemo = i.memos
?.filter(memo => memo)
.map(memo => {
const rex = /\((\d+)\/(\d+)\)((.|[\r\n])*)/;
const tags = memo.match(rex);
if (tags && tags.length >= 4) {
return { num: parseInt(tags[1], 10), memo: tags[3] };
}
// Just return as is
return { num: 0, memo };
})
.sort((a, b) => a.num - b.num)
.map(a => a.memo);
return combinedMemo && combinedMemo.length > 0 ? combinedMemo.join('') : undefined;
})
.map(a => a);
const detail = {
address: '',
amount: totalAmount,
memos: memos && memos.length > 0 ? [memos.join('')] : undefined,
pool: pool,
};
reducedDetailedTxns.push(detail);
});
return reducedDetailedTxns;
}
async fetchTandZTransactions(latestBlockHeight) {
const list = await this.fetchTxList();
let txList = list.map((tx) => {
const transaction = new Transaction();
const type = tx.outgoing_metadata ? "sent" : "receive";
transaction.address = type === 'sent' ? (tx.outgoing_metadata.length > 0 ? tx.outgoing_metadata[0].address : '') : tx.address;
transaction.type = type;
transaction.amount = tx.amount / 10 ** 8;
transaction.confirmations = tx.unconfirmed ? 0 : latestBlockHeight - tx.block_height + 1;
transaction.txid = tx.txid;
transaction.zecPrice = tx.zec_price;
transaction.time = tx.datetime;
transaction.position = tx.position;
if(tx.outgoing_metadata) {
const dts = tx.outgoing_metadata.map((o) => {
const detail = new TxDetail();
detail.address = o.address;
detail.amount = (o.value / 10 ** 8).toFixed(8);
detail.memo = o.memo;
return detail;
});
transaction.detailedTxns = this.combineTxDetails(dts);
}
else {
transaction.detailedTxns = [ new TxDetail() ];
transaction.detailedTxns[0].address = tx.address;
transaction.detailedTxns[0].amount = (tx.amount / 10 ** 8).toFixed(8);
transaction.detailedTxns[0].memo = tx.memo;
}
return transaction;
});
// If you send yourself transactions, the underlying SDK doesn't handle it very well, so
// we supress these in the UI to make things a bit clearer.
txList = txList.filter((tx) => !(tx.type === "sent" && tx.amount < 0 && tx.detailedTxns.length === 0));
// We need to group transactions that have the same (txid and send/recive), for multi-part memos
const m = new Map();
txList.forEach((tx) => {
const key = tx.txid + tx.type;
const coll = m.get(key);
if (!coll) {
m.set(key, [tx]);
} else {
coll.push(tx);
}
});
let combinedTxList = [];
m.forEach((txns) => {
// Get all the txdetails and merge them
// Clone the first tx into a new one
const combinedTx = Object.assign({}, txns[0]);
combinedTx.detailedTxns = this.combineTxDetails(txns.flatMap((tx) => tx.detailedTxns));
combinedTxList.push(combinedTx);
});
// Sort the list by confirmations
combinedTxList.sort((t1, t2) => t1.confirmations - t2.confirmations);
this.transactionsList = combinedTxList;
return this.transactionsList;
}
combineTxDetails(txdetails) {
// First, group by outgoing address.
const m = new Map();
txdetails.forEach((i) => {
const coll = m.get(i.address);
if (!coll) {
m.set(i.address, [i]);
} else {
coll.push(i);
}
});
// Reduce the groups to a single TxDetail, combining memos and summing amounts
let reducedDetailedTxns = [];
m.forEach((txns, toaddr) => {
const totalAmount = txns.reduce((p, td) => p + parseFloat(td.amount), 0);
const memos = txns
.filter((i) => i.memo)
.map((i) => {
const rex = /\((\d+)\/(\d+)\)((.|[\r\n])*)/;
const tags = i.memo?.match(rex);
if (tags && tags.length >= 4) {
return { num: parseInt(tags[1], 10), memo: tags[3] };
}
// Just return as is
return { num: 0, memo: i.memo };
})
.sort((a, b) => a.num - b.num)
.map((a) => a.memo);
const detail = new TxDetail();
detail.address = toaddr;
detail.amount = totalAmount.toFixed(8);
detail.memo = memos.length > 0 ? memos.join("") : null;
reducedDetailedTxns.push(detail);
});
return reducedDetailedTxns;
}
async updateData() {
if(this.updateDataLock) return;
this.updateDataLock = true;
const latestTxId = await this.fetchLastTxId();
if(this.lastTxId !== latestTxId) {
this.lastBlockHeight = await this.fetchLatestBlockHeight();
this.lastTxId = latestTxId;
// await this.fetchTandZTransactions(this.lastBlockHeight);
await this.fetchTandZandOTransactionsSummaries(this.lastBlockHeight);
}
this.updateDataLock = false;
}
async refresh(fullRefresh) {
if (this.syncTimerID) {
console.log("Already have a sync process launched", this.syncTimerID);
return;
}
const latestBlockHeight = await this.fetchLatestBlockHeight();
const walletHeight= await this.fetchWalletHeight();
if(!this.lastBlockHeight || this.lastBlockHeight < latestBlockHeight || walletHeight < latestBlockHeight || fullRefresh) {
console.log('Refreshing wallet: ' + (latestBlockHeight - this.lastBlockHeight) + ' new blocks.');
this.updateDataLock = true;
native.zingolib_execute_spawn('sync', '');
let retryCount = 0;
this.syncTimerID = setInterval(async () => {
const walletHeight = this.fetchWalletHeight();
// retryCount ++;
if(retryCount > 30 || walletHeight >= latestBlockHeight) {
clearInterval(this.syncTimerID);
this.syncTimerID = undefined;
console.log('Wallet is up to date!');
// await this.fetchTandZTransactions(latestBlockHeight);
await this.fetchTandZandOTransactionsSummaries(latestBlockHeight);
this.lastBlockHeight = latestBlockHeight;
await native.zingolib_execute_async('save','');
this.updateDataLock = false;
}
else {
const ssStr = await native.zingolib_execute_async("syncstatus", "");;
const ss = JSON.parse(ssStr);
if (!ss.in_progress) {
clearInterval(this.syncTimerID);
this.syncTimerID = undefined;
// await this.fetchTotalBalance();
// this.fetchTandZTransactions(latestBlockHeight);
await this.fetchTandZandOTransactionsSummaries(latestBlockHeight);
this.lastBlockHeight = latestBlockHeight;
await native.zingolib_execute_async('save','');
this.updateDataLock = false;
}
}
}, 2000);
}
else console.log('no new blocks');
}
async sendTransaction(sendJson) {
// First, get the previous send progress id, so we know which ID to track
const prevProgress = await this.getSendProgress();
const prevSendId = prevProgress.id;
try {
await native.zingolib_execute_async("send", JSON.stringify(sendJson));
}
catch(err) {
console.log(err);
throw err;
}
// The send command is async, so we need to poll to get the status
const sendTxPromise = new Promise((resolve, reject) => {
const intervalID = setInterval(async () => {
const progress = await this.getSendProgress();
if(progress.id === prevSendId) {
// Still not started, so wait for more time
console.log('waiting')
return;
}
if (!progress.txid && !progress.error) {
// Still processing
return;
}
// Finished processing
clearInterval(intervalID);
if(progress.txid && !progress.error) {
// And refresh data (full refresh)
this.refresh(true);
resolve(progress.txid);
}
else {
reject(progress.error);
}
}, 2 * 1000) // Every 2 seconds
});
return sendTxPromise;
}
async getSyncStatus() {
const ssStr = await native.zingolib_execute_async('syncstatus','');
const ss = JSON.parse(ssStr);
return ss;
}
async getInfoObject() {
const infostr = await native.zingolib_execute_async("info", "");
try {
const infoJSON = JSON.parse(infostr);
const info = new Info();
info.testnet = infoJSON.chain_name === "test";
info.latestBlock = infoJSON.latest_block_height;
info.connections = 1;
info.version = `${infoJSON.vendor}/${infoJSON.git_commit.substring(0, 6)}/${infoJSON.version}`;
// info.zcashdVersion = infoJSON.zcashd_version;
info.verificationProgress = 1;
info.currencyName = info.testnet ? "TAZ" : "ZEC";
info.solps = 0;
info.zecPrice = await this.getZecPrice();
// const encStatus = native.zingolib_execute_spawn("encryptionstatus", "");
// const encJSON = JSON.parse(encStatus);
// info.encrypted = encJSON.encrypted;
// info.locked = encJSON.locked;
const walletHeight = await this.fetchWalletHeight();
info.walletHeight = walletHeight;
return info;
}
catch(err) {
console.log("Failed to parse info", err);
return new Info();
}
}
async fetchInfo() {
const info = await this.getInfoObject();
this.info = info;
return info.latestBlock;
}
async fetchSeed() {
const seedStr = await native.zingolib_execute_async("seed", "");
const seedJSON = JSON.parse(seedStr);
return seedJSON.seed;
}
async getWalletBirthday() {
const birthdayStr = await native.zingolib_execute_async("get_birthday", "");
return birthdayStr;
}
async exportUfvkAsString() {
const ufvkStr = await native.zingolib_execute_async("exportufvk", "");
const ufvkJSON = JSON.parse(ufvkStr);
return ufvkJSON.ufvk;
}
async fetchTotalBalance() {
const balanceJSON = JSON.parse(await native.zingolib_execute_async('balance',''));
const balance = new TotalBalance();
console.log(balanceJSON)
// Every sapling balance
balance.sapling_balance = balanceJSON.sapling_balance / 10 ** 8;
balance.verified_sapling_balance = balanceJSON.verified_sapling_balance / 10 ** 8;
balance.spendable_sapling_balance = balanceJSON.spendable_sapling_balance / 10 ** 8;
balance.unverified_sapling_balance = balanceJSON.unverified_sapling_balance / 10 ** 8;
// Every orchard balance
balance.orchard_balance = balanceJSON.orchard_balance / 10 ** 8;
balance.verified_orchard_balance = balanceJSON.verified_orchard_balance / 10 ** 8;
balance.spendable_orchard_balance = balanceJSON.spendable_orchard_balance / 10 ** 8;
balance.unverified_orchard_balance = balanceJSON.unverified_orchard_balance / 10 ** 8;
// Transparent balance
balance.transparent_balance = balanceJSON.transparent_balance / 10 ** 8;
balance.total = balance.sapling_balance + balance.orchard_balance + balance.transparent_balance;
return balance;
}
async fetchNotes() {
const notes = await native.zingolib_execute_async("notes", "");
const notesJSON = JSON.parse(notes);
return notesJSON;
}
async fetchUnspentNotes() {
const unspentNotes = await native.zingolib_execute_async("notes", "");
const unspentJSON = JSON.parse(unspentNotes);
const addresses = await this.fetchAllAddresses();
const addressBalances = {
orchard: [],
sapling: [],
transparent: []
};
// Process orchard notes
unspentJSON.unspent_orchard_notes.forEach((s) => {
addressBalances.orchard.push({address: s.address, value: s.value});
});
// Process sapling notes
unspentJSON.unspent_sapling_notes.forEach((s) => {
let z_address = addresses.filter((addr) => s.address === addr.address);
addressBalances.sapling.push({address: z_address[0].receivers.sapling, value: s.value});
});
// Process UTXOs
unspentJSON.utxos.forEach((s) => {
let t_address = addresses.filter((addr) => s.address === addr.address);
addressBalances.transparent.push({address: t_address[0].receivers.transparent, value: s.value});
});
return addressBalances;
}
async fetchPendingNotes() {
const pendingNotes = await native.zingolib_execute_async("notes", "");
const pendingJSON = JSON.parse(pendingNotes);
const addresses = await this.fetchAllAddresses();
const pendingAddressBalances = {
orchard: [],
sapling: [],
transparent: []
};
// Process orchard notes
pendingJSON.pending_orchard_notes.forEach((s) => {
pendingAddressBalances.orchard.push({address: s.address, value: s.value});
});
// Process sapling notes
pendingJSON.pending_sapling_notes.forEach((s) => {
let z_address = addresses.filter((addr) => s.address === addr.address);
pendingAddressBalances.sapling.push({address: z_address[0].receivers.sapling, value: s.value});
});
// Process UTXOs
pendingJSON.pending_utxos.forEach((s) => {
let t_address = addresses.filter((addr) => s.address === addr.address);
pendingAddressBalances.transparent.push({address: t_address[0].receivers.transparent, value: s.value});
});
return pendingAddressBalances;
}
async fetchAllAddresses() {
const addressesJSON = JSON.parse(await native.zingolib_execute_async("addresses", ""));
return addressesJSON;
}
groupNotes(notes) {
let result = notes.reduce((acc, obj) => {
let key = obj['address'];
if (!acc[key]) {
acc[key] = obj.value;
} else {
acc[key] += obj.value;
}
return acc;
}, {});
return Object.entries(result);
}
async fetchAddressesWithBalance() {
const addressBalancesJSON = await this.fetchUnspentNotes();
const pendingAddressBalances = await this.fetchPendingNotes();
const balanceJSON = {
ua_addresses: [],
z_addresses: [],
t_addresses: []
};
// group orchard balances
const o_notes = this.groupNotes(addressBalancesJSON.orchard)
o_notes.forEach(([el, val]) =>{
balanceJSON.ua_addresses.push({
address: el,
balance: val
});
});
// group sapling balances
const z_notes = this.groupNotes(addressBalancesJSON.sapling)
z_notes.forEach(([el, val]) =>{
balanceJSON.z_addresses.push({
address: el,
balance: val
});
});
// group transparent balances
const t_notes = this.groupNotes(addressBalancesJSON.transparent)
t_notes.forEach(([el, val]) =>{
balanceJSON.t_addresses.push({
address: el,
balance: val
});
});
// return balanceJSON
// Addresses with Balance. The lite client reports balances in zatoshi, so divide by 10^8;
const pendingOrchardNotes = new Map();
pendingAddressBalances.orchard.forEach((note) => {
pendingOrchardNotes.set(note.address, note.value);
});
const oaddresses = balanceJSON.ua_addresses
.map((o) => {
// If this has any unconfirmed txns, show that in the UI
const ab = new AddressBalance(o.address, o.balance / 10 ** 8);
if (pendingOrchardNotes.has(ab.address)) {
ab.containsPending = true;
}
return ab;
});
const pendingSaplingNotes = new Map();
pendingAddressBalances.sapling.forEach((note) => {
pendingSaplingNotes.set(note.address, note.value);
});
const zaddresses = balanceJSON.z_addresses
.map((o) => {
// If this has any unconfirmed txns, show that in the UI
const ab = new AddressBalance(o.address, o.balance / 10 ** 8);
if (pendingSaplingNotes.has(ab.address)) {
ab.containsPending = true;
}
return ab;
});
const pendingUtxos = new Map();
pendingAddressBalances.sapling.forEach((note) => {
pendingUtxos.set(note.address, note.value);
});
const taddresses = balanceJSON.t_addresses
.map((o) => {
// If this has any unconfirmed txns, show that in the UI
const ab = new AddressBalance(o.address, o.balance / 10 ** 8);
if (pendingUtxos.has(ab.address)) {
ab.containsPending = true;
}
return ab;
});
const addresses = oaddresses.concat(zaddresses.concat(taddresses));
return addresses;
}
async getDefaultFee() {
const fee = JSON.parse(await native.zingolib_execute_async('defaultfee',''));
return parseFloat(fee.defaultfee);
}
async getZecPrice() {
const res = await native.zingolib_execute_async('updatecurrentprice','');
if(res.toString().toLowerCase().startsWith('error')) return 0;
return parseFloat(res);
// const now = new Date().getTime();
// if(this.lastPriceFetch && (now - this.lastPriceFetch) < 5 * 60 * 1000) {
// // console.log(this.lastZecPrice)
// return this.lastZecPrice;
// }
// await axios.get('https://api.coingecko.com/api/v3/simple/price?ids=zcash&vs_currencies=usd').then(res => {
// this.lastZecPrice = res.data.zcash.usd;
// }).catch(err => {
// console.log(`Coundn't get ZEC price: ${err}`);
// if(!this.lastZecPrice) this.lastZecPrice = 0;
// })
// // console.log(this.lastZecPrice)
// this.lastPriceFetch = now;
// return parseFloat(this.lastZecPrice);
}
async getSendProgress() {
const res = JSON.parse(await native.zingolib_execute_async('sendprogress', ''));
return res;
}
async getTransactionsList() {
await this.fetchLatestBlockHeight();
await this.refresh(true);
// await this.fetchTandZTransactions(this.lastBlockHeight);
await this.fetchTandZandOTransactionsSummaries(this.lastBlockHeight);
return this.transactionsList;
}
async fetchWalletHeight() {
const res = await native.zingolib_execute_async('height','');
return JSON.parse(res).height;
}
async fetchLatestBlockHeight() {
const res = await this.fetchInfo();
return res;
}
async fetchLastTxId() {
const txListStr = await native.zingolib_execute_async("summaries", "");
const txListJSON = JSON.parse(txListStr);
// console.log('=============== get Last TX ID', txListJSON.length);
if (txListJSON && txListJSON.length && txListJSON.length > 0) {
return txListJSON[txListJSON.length - 1].txid;
}
else {
return "0";
}
}
async fetchTxList() {
const res = await native.zingolib_execute_async('list','');
return JSON.parse(res);
}
async fetchWalletSettings() {
const download_memos_str = await native.zingolib_execute_async("getoption", "download_memos");
const download_memos = JSON.parse(download_memos_str).download_memos;
let spam_filter_threshold = "0";
try {
const spam_filter_str = await native.zingolib_execute_async("getoption", "spam_filter_threshold");
spam_filter_threshold = JSON.parse(spam_filter_str).spam_filter_threshold;
// console.log(`Spam filter threshold: ${spam_filter_threshold}`);
// If it is -1, i.e., it was not set, then set it to 50
if (spam_filter_threshold === "-1") {
await this.setWalletSettingOption("spam_filter_threshold", "50");
}
} catch (e) {
console.log(`Error getting spam filter threshold: ${e}`);
}
const wallet_settings = new WalletSettings();
wallet_settings.download_memos = download_memos;
wallet_settings.spam_filter_threshold = parseInt(spam_filter_threshold);
this.walletSettings = wallet_settings;
return wallet_settings;
}
async setWalletSettingOption(name, value) {
return new Promise(async(resolve, reject) => {
try {
const r = await native.zingolib_execute_async("setoption", `${name}=${value}`);
if(r.toLowerCase().startsWith("error")) reject(r)
native.zingolib_execute_spawn('save','');
resolve(r)
}
catch(err) {
reject(err);
throw(err);
}
});
}
// async encryptWallet(password) {
// return new Promise((resolve, reject) => {
// const resultStr = native.zingolib_execute_spawn("encrypt", password);
// const resultJSON = JSON.parse(resultStr);
// // And save the wallet
// native.zingolib_execute_spawn('save','');
// if (resultJSON.result === "success") resolve("success");
// else reject(`Error! ${resultJSON.error}`);
// });
// }
// async decryptWallet(password) {
// return new Promise((resolve, reject) => {
// const resultStr = native.zingolib_execute_spawn("decrypt", password);
// const resultJSON = JSON.parse(resultStr);
// // And save the wallet
// native.zingolib_execute_spawn('save','');
// if (resultJSON.result === "success") resolve("success");
// else reject(`Error! ${resultJSON.error}`);
// });
// }
// async lockWallet() {
// return new Promise((resolve, reject) => {
// const resultStr = native.zingolib_execute_spawn("lock", "");
// const resultJSON = JSON.parse(resultStr);
// if (resultJSON.result === "success") resolve("success");
// else reject(`Error! ${resultJSON.error}`);
// });
// }
// async unlockWallet(password) {
// return new Promise((resolve, reject) => {
// const resultStr = native.zingolib_execute_spawn("unlock", password);
// const resultJSON = JSON.parse(resultStr);
// if (resultJSON.result === "success") resolve("success");
// else reject(`Error! ${resultJSON.error}`);
// });
// }