-
Notifications
You must be signed in to change notification settings - Fork 11
/
assets_js_bitrequest_sockets.js
1106 lines (1079 loc) · 43 KB
/
assets_js_bitrequest_sockets.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
let glob_lnd_confirm = false,
glob_ndef_processing,
glob_ndef_timer = 0,
glob_ws_timer = 0,
glob_l2s = {};
$(document).ready(function() {
//init_socket
//blockcypherws
//lightning_socket
//ln_ndef
//ndef_apifail
//ndef_errormg
//ndef_controller
//abort_ndef
//blockcypher_websocket
//blockchain_btc_socket
//blockchain_bch_socket
//mempoolspace_btc_socket
//nano_socket
//kaspa_websocket
//kaspa_fyi_websocket
//handle_socket_fails
//handle_socket_close
//ws_recon
//try_next_socket
//current_socket
//socket_info
//set_l2_status
});
// Websockets / Pollfunctions
// Initializes a socket connection based on the payment type and node configuration
function init_socket(socket_node, address, swtch, retry) {
if (glob_offline) {
notify(translate("youareoffline") + ". " + translate("notmonitored"));
return
}
glob_api_attempts = {};
const payment = request.payment,
rq_init = request.rq_init,
request_ts_utc = rq_init + glob_timezone,
request_ts = request_ts_utc - 30000;
let socket_name;
if (socket_node) {
socket_name = socket_node.name;
if (socket_name === "poll_fallback") {
init_account_polling();
return
} else {
glob_socket_attempt[sha_sub(socket_node.url + "l1", 15)] = true;
}
}
if (payment === "bitcoin") {
if (address === "lnurl") {
// lightning only
} else {
if (socket_name === "mempool.space websocket" || socket_node.default === false) {
mempoolspace_btc_socket(socket_node, address);
} else if (socket_name === "blockcypher wss") {
blockcypher_websocket(socket_node, address);
} else if (socket_name === "blockcypher ws") {
blockcypherws(socket_node, address);
} else if (socket_name === "blockchain.info websocket") {
blockchain_btc_socket(socket_node, address);
} else {
blockcypher_websocket(socket_node, address);
}
}
if (helper.lnd_status) {
if (retry) {
return;
}
lightning_socket(helper.lnd);
}
return
}
if (payment === "litecoin") {
if (socket_name === "mempool.space websocket" || socket_node.default === false) {
mempoolspace_btc_socket(socket_node, address);
return
}
if (socket_name === "blockcypher wss") {
blockcypher_websocket(socket_node, address);
return
}
if (socket_name === "blockcypher ws") {
blockcypherws(socket_node, address);
return
}
blockcypher_websocket(socket_node, address);
return
}
if (payment === "dogecoin") {
if (socket_name === "mempool.space websocket" || socket_node.default === false) {
mempoolspace_btc_socket(socket_node, address);
return
}
if (socket_name === "blockcypher wss") {
blockcypher_websocket(socket_node, address);
return
}
if (socket_name === "blockcypher ws") {
blockcypherws(socket_node, address);
return
}
if (socket_name === "dogechain api") {
dogechain_info_socket(socket_node, address);
return
}
blockcypher_websocket(socket_node, address);
return
}
if (payment === "dash") {
if (socket_name === "dash.org") {
dashorg_poll();
return
}
if (socket_name === "blockcypher wss") {
blockcypher_websocket(socket_node, address);
return
}
if (socket_name === "blockcypher ws") {
blockcypherws(socket_node, address);
return
}
return
}
if (payment === "bitcoin-cash") {
if (socket_name === "mempool.space websocket" || socket_node.default === false) {
mempoolspace_btc_socket(socket_node, address);
return
}
if (socket_name === "blockchain.info websocket") {
blockchain_bch_socket(socket_node, address);
return
}
blockchain_bch_socket(socket_node, address);
return
}
if (payment === "nano") {
nano_socket(socket_node, address);
return
}
if (payment === "ethereum" || request.erc20) {
init_eth_sockets(payment, socket_node, address, request_ts, request.token_contract, retry);
return
}
if (payment === "monero") {
const vk = swtch ? get_vk(address) : request.viewkey;
if (vk) {
trigger_requeststates(); // update outgoing
const account = vk.account || address,
viewkey = vk.vk;
request.monitored = true;
if (swtch) {
request.viewkey = vk;
}
closenotify();
init_xmr_node(9, account, viewkey, request_ts);
return
}
request.monitored = false;
request.viewkey = false;
notify(translate("notmonitored"), 500000, "yes");
return
}
if (payment === "nimiq") {
nimiq_poll();
return
}
if (payment === "kaspa") {
if (socket_name === glob_main_kas_wss) {
kaspa_websocket(socket_node, address);
return
}
if (socket_name === glob_sec_kas_wss) {
kaspa_fyi_websocket(socket_node, address);
return
}
kaspa_websocket(socket_node, address);
return
}
notify(translate("notmonitored"), 500000, "yes")
}
// Handles BlockCypher WebSocket connection, falling back to WebSocket if local
function blockcypherws(socket_node, address) {
if (glob_local === true) {
blockcypher_websocket(socket_node, address);
return
}
handle_socket_fails(socket_node, address);
}
// Sets up a Lightning Network socket connection
function lightning_socket(lnd) {
glob_lnd_confirm = false;
const p_arr = lnurl_deform(lnd.proxy_host),
proxy_host = p_arr.url,
pk = (lnd.pw) ? lnd.pw : p_arr.k,
pid = lnd.pid,
nid = lnd.nid,
imp = lnd.imp;
if (glob_sockets[pid]) {
return
}
const socket = glob_sockets[pid] = new WebSocket(glob_ln_socket);
socket.onopen = function(e) {
console.log("Connected: " + glob_ln_socket);
glob_paymentpopup.addClass("live");
const ping_event = JSON.stringify({
"id": pid
});
socket.send(ping_event);
glob_pinging[pid] = setInterval(function() {
socket.send(ping_event);
poll_animate();
}, 55000);
};
socket.onmessage = function(e) {
const result = JSON.parse(e.data);
if (result.pid == pid) {
if (result.status === "pending" && result.bolt11) {
clearpinging(pid);
closesocket(pid);
lnd_poll_invoice(proxy_host, pk, imp, result, pid, nid);
glob_pinging[result.hash] = setInterval(function() {
lnd_poll_invoice(proxy_host, pk, imp, result, pid, nid);
}, 5000);
}
if (result.status === "confirm" && !glob_lnd_confirm) {
glob_lnd_confirm = true;
glob_paymentdialogbox.addClass("accept_lnd");
notify(translate("acceptthepayment"), 500000);
vibrate();
playsound(glob_blip);
}
set_request_timer();
return
}
};
socket.onclose = function(e) {
console.log("Disconnected");
};
socket.onerror = function(e) {
glob_lnd_confirm = false;
glob_paymentpopup.addClass("live");
glob_pinging[pid] = setInterval(function() {
lnd_poll_data(proxy_host, pk, pid, nid, imp);
}, 5000);
};
ln_ndef(proxy_host, pk, pid, nid, imp);
}
// Handles NFC (Near Field Communication) functionality for Lightning Network payments
async function ln_ndef(proxy_host, pk, pid, nid, imp) {
if (!glob_ndef) return;
glob_ndef_processing = false;
try {
ndef_controller();
await glob_ndef.scan({
"signal": glob_ctrl.signal
});
glob_ndef.onreading = event => {
if ((now() - 6000) < glob_ndef_timer) { // prevent too many taps
playsound(glob_funk);
notify(translate("ndeftablimit"), 6000);
return;
}
glob_ndef_timer = now();
closenotify();
const message = event.message;
if (message) {
const records = message.records;
if (records) {
const first_record = records[0];
if (first_record) {
const data = first_record.data;
if (data) {
const lnurlw = utf8Decoder.decode(data);
if (lnurlw) {
if (lnurlw.indexOf("p=") && lnurlw.indexOf("c=")) {
const prefix = lnurlw.split("urlw://");
if (prefix[0] == "ln") {
const amount_rel = $("#open_wallet").attr("data-rel"),
ccraw = amount_rel.length ? parseFloat(amount_rel) : 0,
milli_sats = (ccraw * 100000000000).toFixed(0);
if (ccraw <= 0) {
playsound(glob_funk);
notify(translate("enteramount"), 5000);
return
}
if (glob_ndef_processing) {
playsound(glob_funk);
console.log("already processing");
return
}
playsound(glob_blip);
notify("Processing...", 50000);
glob_paymentdialogbox.addClass("accept_lnd");
set_request_timer();
const lnurl_http = "https://" + prefix[1];
glob_ndef_processing = true;
api_proxy({
"api_url": lnurl_http,
"params": {
"method": "GET",
"cache": false
}
}, proxy_host).done(function(e) {
const result = br_result(e).result;
if (!result) { // catch lightning node connection failure
playsound(glob_funk);
notify(translate("unabletoconnectln"), 5000);
glob_paymentdialogbox.removeClass("accept_lnd");
glob_ndef_processing = false;
return
}
if (result.status === "ERROR") {
playsound(glob_funk);
const error_message = result.reason;
notify(error_message, 5000);
glob_paymentdialogbox.removeClass("accept_lnd");
glob_ndef_processing = false;
return
}
if (result.error) {
playsound(glob_funk);
api_eror_msg(null, get_api_error_data(result.error));
glob_paymentdialogbox.removeClass("accept_lnd");
closenotify();
glob_ndef_processing = false;
return
}
if (milli_sats > result.maxWithdrawable) {
playsound(glob_funk);
notify(translate("cardmax"), 5000);
glob_paymentdialogbox.removeClass("accept_lnd");
glob_ndef_processing = false;
return
}
if (milli_sats < result.minWithdrawable) {
playsound(glob_funk);
notify(translate("minamount", {
"min": result.minWithdrawable
}), 5000);
glob_paymentdialogbox.removeClass("accept_lnd");
glob_ndef_processing = false;
return
}
const callback = result.callback;
if (callback) {
const k1 = result.k1;
if (k1) {
const descr = $("#paymentdialog input#requesttitle").val(),
final_descr = (descr && descr.length > 1) ? descr + " (Boltcard)" :
(result.defaultDescription) ? result.defaultDescription : "bitrequest " + pid + " (Boltcard)",
rqtype = request.requesttype,
postdata = {
"imp": imp,
"fn": "ln-create-invoice",
"amount": milli_sats,
"memo": final_descr,
"id": pid,
"nid": nid,
"expiry": 60,
"x-api": pk
};
if (rqtype === "incoming") {
postdata.b11 = true;
}
$.ajax({
"method": "POST",
"cache": false,
"timeout": 5000,
"url": proxy_host + "proxy/v1/ln/api/",
"data": postdata
}).done(function(inv1) {
const invoice = inv1.bolt11;
if (invoice) {
glob_paymentdialogbox.addClass("transacting blockd").attr("data-status", "pending");
$("#paymentdialogbox .brstatuspanel #confnumber").text("1");
notify("Monitoring...", 50000);
const ampersand = callback.includes("?") ? "&" : "?",
cb_url = callback + ampersand + "k1=" + k1 + "&pr=" + invoice;
api_proxy({
"proxy": false,
"api_url": cb_url,
"params": {
"method": "GET",
"cache": false,
"timeout": 15000
}
}, proxy_host).done(function(e) {
const result = br_result(e).result;
if (result.status === "ERROR") {
ndef_errormg(result.reason);
return
}
if (result.status === "OK") {
clearpinging(pid);
closesocket(pid);
abort_ndef();
lnd_poll_invoice(proxy_host, pk, imp, inv1, pid, nid);
glob_pinging[inv1.hash] = setInterval(function() {
lnd_poll_invoice(proxy_host, pk, imp, inv1, pid, nid);
}, 3000);
return
}
}).fail(function(jqXHR, textStatus, errorThrown) {
ndef_apifail(jqXHR, textStatus, errorThrown);
});
return
}
ndef_errormg("failed to create invoice");
}).fail(function(jqXHR, textStatus, errorThrown) {
ndef_apifail(jqXHR, textStatus, errorThrown);
}).always(function() {
glob_ndef_processing = false;
});
return
}
}
glob_ndef_processing = false;
}).fail(function(jqXHR, textStatus, errorThrown) {
ndef_apifail(jqXHR, textStatus, errorThrown);
});
return;
}
}
notify("invalid lnurlw", 5000);
return
}
}
}
}
}
notify("lnurlw not found", 5000);
}
} catch (error) {
notify(error, 5000);
}
}
// Handles API failure for NFC operations
function ndef_apifail(jqXHR, textStatus, errorThrown) {
const error_object = errorThrown || jqXHR;
api_eror_msg(null, get_api_error_data(error_object));
glob_paymentdialogbox.removeClass("accept_lnd transacting");
closenotify();
glob_ndef_processing = false;
}
// Displays error messages for NFC operations
function ndef_errormg(message) {
const pmd = $("#paymentdialogbox"),
brstatuspanel = pmd.find(".brstatuspanel"),
brheader = brstatuspanel.find("h2");
brheader.text(message);
pmd.addClass("accept_lnd transacting pd_error");
playsound(glob_funk);
closenotify();
setTimeout(function() {
pmd.removeClass("accept_lnd transacting pd_error");
brheader.text(translate("waitingforpayment"));
}, 5000);
}
// Sets up the NFC controller for scanning
function ndef_controller() {
glob_ctrl = new AbortController();
console.log("Waiting for NDEF messages.");
glob_ctrl.signal.onabort = () => {
console.log("Done waiting for NDEF messages.");
};
}
// Aborts the NFC operation
function abort_ndef() {
if (glob_ndef && glob_ctrl) {
glob_ctrl.abort();
glob_ctrl = null;
}
}
// Polls Lightning Network data for payment status
function lnd_poll_data(proxy_host, pk, pid, nid, imp) {
if (isopenrequest()) { // only when request is visible
const default_error = translate("unabletoconnect");
$.ajax({
"method": "POST",
"cache": false,
"timeout": 5000,
"url": proxy_host + "proxy/v1/ln/api/",
"data": {
"fn": "ln-request-status",
"id": pid,
"x-api": pk
}
}).done(function(e) {
poll_animate();
const error = e.error;
if (error) {
const message = error?.message ?? (typeof e.error === "string" ? error : default_error);
}
const version = e.version;
if (version < glob_proxy_version) {
proxy_alert(version);
}
if (e.pid == pid) {
if (e.status == "pending" && e.bolt11) {
clearpinging(pid);
set_request_timer();
glob_pinging[e.hash] = setInterval(function() {
lnd_poll_invoice(proxy_host, pk, imp, e, pid, nid);
}, 5000);
return
}
if (e.status == "confirm" && !glob_lnd_confirm) {
glob_lnd_confirm = true;
glob_paymentdialogbox.addClass("accept_lnd");
notify(translate("acceptthepayment"), 500000);
playsound(glob_blip);
}
return
}
lnd_poll_data_fail(pid);
}).fail(function(jqXHR, textStatus, errorThrown) {
lnd_poll_data_fail(pid);
});
return
}
forceclosesocket();
}
// Polls Lightning Network invoice status
function lnd_poll_invoice(proxy_host, pk, imp, inv, pid, nid) {
if (isopenrequest()) { // only when request is visible
const default_error = "unable to connect";
$.ajax({
"method": "POST",
"cache": false,
"timeout": 5000,
"url": proxy_host + "proxy/v1/ln/api/",
"data": {
"fn": "ln-invoice-status",
"imp": imp,
"hash": inv.hash,
"id": pid,
"nid": nid,
"callback": "yes",
"type": request.requesttype,
"x-api": pk
}
}).done(function(e) {
poll_animate();
const status = e.status;
if (status) {
request.address = "lnurl"; // make it a lightning request
notify(translate("waitingforpayment"), 500000);
helper.lnd.invoice = e;
const txd = lnd_tx_data(e);
confirmations(txd, true, true);
glob_paymentdialogbox.removeClass("blockd");
if (status === "paid") {
clearpinging(inv.hash);
helper.currencylistitem.removeData("url");
br_remove_local("editurl");
br_remove_session("lndpid");
closenotify();
return
}
}
}).fail(function(jqXHR, textStatus, errorThrown) {
console.log(jqXHR);
console.log(textStatus);
console.log(errorThrown);
});
return
}
forceclosesocket();
}
// Handles failure in polling Lightning Network data
function lnd_poll_data_fail(pid) {
clearpinging(pid);
notify(translate("notmonitored"), 500000, "yes");
}
// Websockets
// Initializes and manages BlockCypher WebSocket connection
function blockcypher_websocket(socket_node, thisaddress) {
if (glob_sockets[thisaddress]) {
return
}
const provider = socket_node.url + request.currencysymbol + "/main",
websocket = glob_sockets[thisaddress] = new WebSocket(provider);
websocket.onopen = function(e) {
socket_info(socket_node, true);
const ping_event = JSON.stringify({
"event": "tx-confirmation",
"address": thisaddress,
"token": get_blockcypher_apikey()
});
websocket.send(ping_event);
glob_pinging[thisaddress] = setInterval(function() {
websocket.send(ping_event);
poll_animate();
}, 55000);
};
websocket.onmessage = function(e) {
const data = JSON.parse(e.data);
if (data.event === "pong") return;
const txhash = data.hash;
if (!txhash) return;
closesocket();
const set_confirmations = request.set_confirmations || 0,
txd = blockcypher_poll_data(data, set_confirmations, request.currencysymbol, thisaddress);
if (txd.double_spend) {
const content = "<h2 class='icon-warning'>Double spend detected</h2>";
popdialog(content, "canceldialog");
return
}
pick_monitor(txd);
};
websocket.onclose = function(e) {
handle_socket_close(socket_node);
};
websocket.onerror = function(e) {
handle_socket_fails(socket_node, thisaddress);
return
};
}
// Initializes and manages Blockchain.info WebSocket for Bitcoin
function blockchain_btc_socket(socket_node, thisaddress) {
if (glob_sockets[thisaddress]) {
return
}
const provider = socket_node.url,
websocket = glob_sockets[thisaddress] = new WebSocket(provider);
websocket.onopen = function(e) {
socket_info(socket_node, true);
const ping_event = JSON.stringify({
"op": "addr_sub",
"addr": thisaddress
});
websocket.send(ping_event);
glob_pinging[thisaddress] = setInterval(function() {
websocket.send(ping_event);
poll_animate();
}, 55000);
};
websocket.onmessage = function(e) {
try {
const json = JSON.parse(e.data).x,
txhash = json.hash;
if (!txhash) return;
const set_confirmations = request.set_confirmations || 0,
txd = blockchain_ws_data(json, set_confirmations, request.currencysymbol, thisaddress);
if (txd) {
closesocket();
pick_monitor(txd);
}
} catch (error) {
console.error("Error parsing WebSocket message:", error);
}
};
websocket.onclose = function(e) {
handle_socket_close(socket_node);
};
websocket.onerror = function(e) {
handle_socket_fails(socket_node, thisaddress);
return
};
}
// Initializes and manages Blockchain.info WebSocket for Bitcoin Cash
function blockchain_bch_socket(socket_node, thisaddress) {
if (glob_sockets[thisaddress]) {
return
}
const provider = socket_node.url,
websocket = glob_sockets[thisaddress] = new WebSocket(provider);
websocket.onopen = function(e) {
socket_info(socket_node, true);
const c_address = (thisaddress.indexOf("bitcoincash:") > -1) ? thisaddress.split("bitcoincash:").pop() : thisaddress,
ping_event = JSON.stringify({
"op": "addr_sub",
"addr": "bitcoincash:" + c_address
});
websocket.send(ping_event);
glob_pinging[thisaddress] = setInterval(function() {
websocket.send(ping_event);
poll_animate();
}, 55000);
};
websocket.onmessage = function(e) {
try {
const json = JSON.parse(e.data).x,
txhash = json.hash;
if (!txhash) return;
const legacy = bch_legacy(thisaddress),
set_confirmations = request.set_confirmations || 0,
txd = blockchain_ws_data(json, set_confirmations, request.currencysymbol, thisaddress, legacy);
if (txd) {
closesocket();
pick_monitor(txd);
}
} catch (error) {
console.error("Error parsing WebSocket message:", error);
}
};
websocket.onclose = function(e) {
handle_socket_close(socket_node);
};
websocket.onerror = function(e) {
handle_socket_fails(socket_node, thisaddress);
return
};
}
// Initializes and manages mempool.space WebSocket for Bitcoin
function mempoolspace_btc_socket(socket_node, thisaddress) {
if (glob_sockets[thisaddress]) {
return
}
const provider = socket_node.url,
mps_websocket = glob_sockets[thisaddress] = new WebSocket(provider);
mps_websocket.onopen = function(e) {
socket_info(socket_node, true);
const ping_event = JSON.stringify({
"track-address": thisaddress
});
mps_websocket.send(ping_event);
glob_pinging[thisaddress] = setInterval(function() {
mps_websocket.send(ping_event);
poll_animate();
}, 55000);
};
mps_websocket.onmessage = function(e) {
try {
const result = JSON.parse(e.data),
result2 = result["address-transactions"];
if (result2) {
const json = result2[0];
if (json) {
const txhash = json.txid;
if (!txhash) return;
const set_confirmations = request.set_confirmations || 0,
txd = mempoolspace_ws_data(json, set_confirmations, request.currencysymbol, thisaddress);
if (txd) {
closesocket();
pick_monitor(txd);
}
}
}
} catch (error) {
console.error("Error parsing WebSocket message:", error);
}
};
mps_websocket.onclose = function(e) {
handle_socket_close(socket_node);
};
mps_websocket.onerror = function(e) {
handle_socket_fails(socket_node, thisaddress);
return
};
}
// Initializes and manages dogechain.info WebSocket for Dogecoin
function dogechain_info_socket(socket_node, thisaddress) {
if (glob_sockets[thisaddress]) {
return
}
const provider = socket_node.url,
websocket = glob_sockets[thisaddress] = new WebSocket(provider);
websocket.onopen = function(e) {
socket_info(socket_node, true);
const ping_event = JSON.stringify({
"op": "addr_sub",
"addr": thisaddress
});
websocket.send(ping_event);
glob_pinging[thisaddress] = setInterval(function() {
websocket.send(ping_event);
poll_animate();
}, 55000);
};
websocket.onmessage = function(e) {
try {
const json = JSON.parse(e.data),
data = json.x;
if (data) {
const txhash = data.hash;
if (!txhash) return;
const set_confirmations = request.set_confirmations || 0,
txd = dogechain_ws_data(data, set_confirmations, request.currencysymbol, thisaddress);
if (txd) {
closesocket();
pick_monitor(txd);
}
}
} catch (error) {
console.error("Error parsing WebSocket message:", error);
}
};
websocket.onclose = function(e) {
handle_socket_close(socket_node);
};
websocket.onerror = function(e) {
handle_socket_fails(socket_node, thisaddress);
return
};
}
// Initializes and manages WebSocket for Nano cryptocurrency
function nano_socket(socket_node, thisaddress) {
if (glob_sockets[thisaddress]) {
return
}
const address_mod = (thisaddress.match("^xrb")) ? "nano_" + thisaddress.split("_").pop() : thisaddress, // change nano address prefix xrb_ to nano untill websocket support
provider = socket_node.url,
websocket = glob_sockets[thisaddress] = new WebSocket(provider);
websocket.onopen = function(e) {
socket_info(socket_node, true);
const ping_event = JSON.stringify({
"action": "subscribe",
"topic": "confirmation",
"all_local_accounts": true,
"options": {
"accounts": [address_mod]
},
"ack": true
});
websocket.send(ping_event);
glob_pinging[thisaddress] = setInterval(function() {
websocket.send(ping_event);
poll_animate();
}, 55000);
};
websocket.onmessage = function(e) {
try {
const utc = now_utc(),
json = JSON.parse(e.data),
data = (json.message) ? json.message : (json.account) ? json : null;
if (data) {
if (data.account === thisaddress) {
return // block outgoing transactions
}
if (!data.hash) return;
const txd = nano_scan_data(data, undefined, request.currencysymbol),
tx_timestamp = txd.transactiontime,
timestamp_difference = Math.abs(tx_timestamp - utc);
if (timestamp_difference < 60000) { // filter transactions longer then a minute ago
closesocket();
pick_monitor(txd);
}
}
} catch (error) {
console.error("Error parsing WebSocket message:", error);
}
};
websocket.onclose = function(e) {
handle_socket_close(socket_node);
};
websocket.onerror = function(e) {
handle_socket_fails(socket_node, thisaddress);
return
};
}
// Initializes and manages Kaspa WebSocket
function kaspa_websocket(socket_node, thisaddress) {
if (glob_sockets[thisaddress]) {
return
}
const provider = socket_node.url + "/ws/socket.io/?EIO=4&transport=websocket&sid=" + now(),
websocket = glob_sockets[thisaddress] = new WebSocket(provider);
websocket.onopen = function(e) {
glob_ws_timer = now();
socket_info(socket_node, true);
websocket.send("2probe");
};
websocket.onmessage = function(e) {
const dat = e.data;
if (!dat) return;
const datid = dat.slice(0, 2);
if (dat === "3probe") {
websocket.send('42["join-room","blocks"]');
return;
}
if (datid === "42") {
const newdat = dat.slice(2),
data = JSON.parse(newdat),
contents = data[1];
if (contents) {
const txs = contents.txs;
if (txs) {
$.each(txs, function(dat, value) {
const txd = kaspa_ws_data(value, thisaddress);
if (txd.ccval) {
closesocket();
pick_monitor(txd);
return
}
});
}
}
}
};
websocket.onclose = function(e) {
ws_recon({ // reconnect if ws closes
"function": kaspa_websocket,
"node": socket_node,
"address": thisaddress,
"trigger": e.code
});
handle_socket_close(socket_node);
};
websocket.onerror = function(e) {
handle_socket_fails(socket_node, thisaddress);
return
};
}
// Initializes and manages Kaspa FYI WebSocket
function kaspa_fyi_websocket(socket_node, thisaddress) {
if (glob_sockets[thisaddress]) {
return
}
const provider = socket_node.url + "/ws/socket.io/?EIO=4&transport=websocket",
websocket = glob_sockets[thisaddress] = new WebSocket(provider);
websocket.onopen = function(e) {
socket_info(socket_node, true);
websocket.send("40");
};
websocket.onmessage = function(e) {
const dat = e.data;
if (!dat) return;
const datid = dat.slice(0, 2);
if (datid == "40") {
websocket.send('42["join-room","blocks"]');
return;
}
if (datid == "42") {
try {
const newdat = dat.slice(2),
data = JSON.parse(newdat),
contents = data[1];
if (contents) {
const txs = contents.transactions;
if (txs) {
$.each(txs, function(dat, value) {
const txd = kaspa_fyi_ws_data(value, thisaddress);
if (txd.ccval) {
closesocket();
pick_monitor(txd);
return
}
});
}
}
} catch (error) {
console.error("Error processing Kaspa FYI message:", error);
}
}
};
websocket.onclose = function(e) {
ws_recon({ // reconnect if ws closes
"function": kaspa_fyi_websocket,
"node": socket_node,
"address": thisaddress,
"trigger": e.code
});
handle_socket_close(socket_node);
};
websocket.onerror = function(e) {
handle_socket_fails(socket_node, thisaddress);
return
};
}
// Handles WebSocket connection failures
function handle_socket_fails(socket_node, thisaddress, socketid, l2) {
if (glob_paymentdialogbox.hasClass("transacting")) { // temp fix for bch socket
return
}
if (isopenrequest()) { // only when request is visible
const next_socket = try_next_socket(socket_node, l2),
wsid = (socketid) ? socketid : thisaddress;
forceclosesocket(wsid);
if (next_socket) {
init_socket(next_socket, thisaddress, null, true);
return
}
if (l2) {