-
Notifications
You must be signed in to change notification settings - Fork 11
/
assets_js_bitrequest_coin_settings.js
1362 lines (1312 loc) · 52.5 KB
/
assets_js_bitrequest_coin_settings.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_ap_id,
glob_test_rpc_call,
glob_is_erc20t,
glob_is_btc;
$(document).ready(function() {
// ** Currency Settings **
// Confirmations
edit_confirmations();
submit_confirmations();
// Reuse addresses
reuse_address_trigger();
cc_switch();
// Blockexplorer
edit_blockexplorer();
submit_blockexplorer();
// RPC settings
edit_rpcnode();
//get_rpc_placeholder
//test_append_rpc
//rpc_option_li
test_rpcnode();
submit_rpcnode();
//test_rpc
//pass_rpc_submit
remove_rpcnode();
//get_rpc_url
// Xpub settings
edit_xpub_trigger();
delete_xpub();
//delete_xpub_cb
//add_xpub_cb
xpub_cc_switch();
//edit_xpub
xpub_change();
submit_xpub_trigger();
//validate_xpub
//xpub_fail
//clear_xpub_inputs
//clear_xpub_checkboxes
//check_xpub
// Key Management
key_management();
segwit_switch();
//xpub_info_pu
// Add apikey
trigger_apikey();
//add_apikey;
submit_apikey();
reset_coinsettings();
//reset_coinsettings_function
});
// ** Currency Settings **
// Function to handle editing confirmations for cryptocurrency transactions
function edit_confirmations() {
$(document).on("click", ".cc_settinglist li[data-id='confirmations'] .edit_trigger", function() {
const thistrigger = $(this),
thiscurrency = thistrigger.attr("data-currency"),
thisli = thistrigger.closest("li"),
confsrc = thisli.data("selected"),
confirmationOptions = [{
conf: 0,
emoji: "☕"
},
{
conf: 1,
emoji: "🍷 🍽"
},
{
conf: 2,
emoji: "📱"
},
{
conf: 3,
emoji: "🖥"
},
{
conf: 4,
emoji: "🚗"
},
{
conf: 5,
emoji: "🏠"
},
{
conf: 6,
emoji: "🛥 💎"
}
],
confOptionsHtml = confirmationOptions.map(function(option) {
return "<li><div class='pick_conf'><div class='radio icon-radio-unchecked'></div><span>" + option.conf + "</span><div class='conf_emoji'>" + option.emoji + "</div></div></li>";
}).join(""),
ddat = [{
"ul": {
"class": "conf_options noselect",
"content": confOptionsHtml
},
"div": {
"class": "popform",
"content": [{
"input": {
"attr": {
"type": "hidden",
"value": confsrc
}
}
},
{
"input": {
"class": "submit",
"attr": {
"type": "submit",
"value": translate("okbttn"),
"data-currency": thiscurrency
}
}
}
]
}
}],
content = template_dialog({
"id": "conf_formbox",
"icon": "icon-clock",
"title": translate("confirmations"),
"elements": ddat
});
popdialog(content, "triggersubmit");
const currentli = $("#conf_formbox ul.conf_options li").filter(function() {
return $(this).find("span").text() === confsrc;
});
currentli.find(".radio").removeClass("icon-radio-unchecked").addClass("icon-radio-checked2");
})
}
// Function to handle submission of confirmation settings
function submit_confirmations() {
$(document).on("click", "#conf_formbox input.submit", function(e) {
e.preventDefault();
const thistrigger = $(this),
thiscurrency = thistrigger.attr("data-currency"),
thisvalue = thistrigger.prev("input").val(),
csnode = cs_node(thiscurrency, "confirmations");
if (csnode) {
csnode.data("selected", thisvalue).find("p").html(thisvalue);
canceldialog();
notify(translate("datasaved"));
save_cc_settings(thiscurrency, true);
}
})
}
// Function to handle the reuse address toggle switch
function reuse_address_trigger() {
$(document).on("mouseup", ".cc_settinglist li[data-id='Reuse address'] .switchpanel.custom", function() {
const this_switch = $(this),
thislist = this_switch.closest("li"),
thisliwrap = this_switch.closest(".liwrap"),
thiscurrency = thisliwrap.attr("data-currency"),
warning = thislist.data("warning");
if (this_switch.hasClass("true")) {
let xresult = true;
if (warning) {
xresult = confirm(translate("reusewarningalert", {
"thiscurrency": thiscurrency
}));
}
if (xresult) {
thislist.data("selected", false);
this_switch.removeClass("true").addClass("false");
save_cc_settings(thiscurrency, false);
}
return
}
const result = confirm(translate("reusealert", {
"thiscurrency": thiscurrency
}));
if (result) {
thislist.data("selected", true);
this_switch.removeClass("false").addClass("true");
save_cc_settings(thiscurrency, true);
}
})
}
// Function to handle generic cryptocurrency switch toggles
function cc_switch() {
$(document).on("mouseup", ".cc_settinglist li .switchpanel.bool", function() {
const thistrigger = $(this),
thislist = thistrigger.closest("li"),
thisliwrap = thistrigger.closest(".liwrap"),
thiscurrency = thisliwrap.attr("data-currency"),
thisvalue = thistrigger.hasClass("true");
thislist.data("selected", thisvalue);
save_cc_settings(thiscurrency, false);
})
}
// Function to handle editing block explorer settings
function edit_blockexplorer() {
$(document).on("click", ".cc_settinglist li[data-id='blockexplorers']", function() {
const current_li = $(this),
this_data = current_li.data(),
options = this_data.options;
if (options) {
const thiscurrency = current_li.children(".liwrap").attr("data-currency"),
selected = this_data.selected,
choosebe = translate("chooseblockexplorer"),
options_li = options.map(function(value) {
return "<span data-pe='none'>" + value + "</span>";
}).join(""),
ddat = [{
"div": {
"class": "popform",
"content": [{
"div": {
"class": "selectbox",
"content": [{
"input": {
"attr": {
"type": "text",
"value": selected,
"placeholder": choosebe,
"readonly": "readonly"
},
"close": true
},
"div": {
"class": "selectarrows icon-menu2",
"attr": {
"data-pe": "none"
}
}
},
{
"div": {
"class": "options",
"content": options_li
}
}
]
},
"input": {
"class": "submit",
"attr": {
"type": "submit",
"value": translate("okbttn"),
"data-currency": thiscurrency
}
}
}]
}
}],
content = template_dialog({
"id": "be_formbox",
"icon": "icon-eye",
"title": choosebe,
"elements": ddat
});
popdialog(content, "triggersubmit");
}
})
}
// Function to handle submission of block explorer settings
function submit_blockexplorer() {
$(document).on("click", "#be_formbox input.submit", function(e) {
e.preventDefault();
const thiscurrency = $(this).attr("data-currency"),
thisvalue = $("#be_formbox").find("input:first").val(),
csnode = cs_node(thiscurrency, "blockexplorers");
if (csnode) {
csnode.data("selected", thisvalue).find("p").html(thisvalue);
canceldialog();
notify(translate("datasaved"));
save_cc_settings(thiscurrency, true);
}
})
}
// Function to handle editing RPC node settings for APIs and WebSockets
function edit_rpcnode() {
$(document).on("click", ".cc_settinglist li[data-id='apis'], .cc_settinglist li[data-id='websockets']", function() {
const current_li = $(this),
this_data = current_li.data(),
options = this_data.options,
api_list = this_data.apis;
if (!exists(options) && !exists(api_list)) {
return
}
const thiscurrency = current_li.children(".liwrap").attr("data-currency");
glob_ap_id = current_li.attr("data-id"),
glob_test_rpc_call = this_data.rpc_test_command,
glob_is_erc20t = ($("#" + thiscurrency + "_settings").attr("data-erc20") == "true"),
glob_is_btc = is_btchain(thiscurrency) === true;
const h_hint = glob_is_btc ? "mempool.space" : (thiscurrency === "ethereum" || glob_is_erc20t === true) ? "Infura" : "",
header_text = glob_ap_id === "websockets" ? translate("addwebsocket", {
"h_hint": h_hint
}) : translate("addapi", {
"h_hint": h_hint
}),
currencycode = (thiscurrency === "ethereum" || glob_is_erc20t === true) ? "eth" : thiscurrency,
placeholder_id = glob_ap_id + currencycode + getrandomnumber(1, 3),
getplaceholder = get_rpc_placeholder(thiscurrency)[placeholder_id],
placeholder = getplaceholder || "eg: some.local-or-remote.node:port",
api_form = options ? "<div id='rpc_input_box' data-currency='" + thiscurrency + "' data-erc20='" + glob_is_erc20t + "'>\
<h3 class='icon-plus'>" + header_text + "</h3>\
<div id='rpc_input'>\
<input type='text' value='' placeholder='" + placeholder + "' id='rpc_url_input'/>\
<div class='c_stat icon-wifi-off'></div>\
<div class='c_stat icon-connection'></div>\
</div>\
<input type='text' value='' placeholder='Username (optional)' id='rpc_username_input'/>\
<input type='password' value='' placeholder='Password (optional)' id='rpc_password_input'/>\
</div>" : "",
selected = this_data.selected,
selected_title = selected.name || selected.url,
content = "\
<div class='formbox' id='settingsbox' data-id='" + glob_ap_id + "'>\
<h2 class='icon-sphere'>" + translate("choose") + " " + thiscurrency + " " + glob_ap_id + "</h2>\
<div class='popnotify'></div>\
<div class='popform'>\
<div class='selectbox'>\
<input type='text' value='" + selected_title + "' placeholder='Choose RPC node' readonly='readonly' id='rpc_main_input'/>\
<div class='selectarrows icon-menu2' data-pe='none'></div>\
<div class='options'>\
</div>\
</div>" +
api_form +
"<input type='submit' class='submit' value='" + translate("okbttn") + "' data-currency='" + thiscurrency + "'/>\
</div>\
</div>";
popdialog(content, "triggersubmit");
const optionlist = $("#settingsbox").find(".options");
$.each(api_list, function(key, value) {
if (value.display === true) {
let selected = value.url === selected_title || value.name === selected_title;
if (thiscurrency === "nano") {
test_append_rpc(thiscurrency, optionlist, key, value, selected);
} else {
rpc_option_li(optionlist, true, key, value, selected, false);
}
}
});
$.each(options, function(key, value) {
let selected = value.url === selected_title || value.name === selected_title;
test_append_rpc(thiscurrency, optionlist, key, value, selected);
});
$("#rpc_main_input").data(selected);
setTimeout(function() {
closesocket();
}, 5000);
})
}
// Function to get RPC placeholders for different currencies and API types
function get_rpc_placeholder(currency) {
return {
"apisnano1": "eg: http://127.0.0.1:7076",
"apisnano2": "eg: http://some.local-or-remote.node:7076",
"apisnano3": "eg: http://localhost:7076",
"websocketsnano1": "eg: ws://127.0.0.1:7078",
"websocketsnano2": "eg: ws://some.local-or-remote.node:7078",
"websocketsnano3": "eg: ws://localhost:7078",
"apiseth1": "eg: http://localhost:8545",
"apiseth2": "eg: http://some.local-or-remote.node:8546",
"apiseth3": "eg: https://mainnet.infura.io/v3/YOUR-PROJECT-ID",
"websocketseth1": "eg: ws://localhost:8545",
"websocketseth2": "eg: ws://some.local-or-remote.node:8546",
"websocketseth3": "eg: wss://mainnet.infura.io/ws/v3/YOUR-PROJECT-ID"
}
}
// Function to test and append RPC options for different cryptocurrencies
function test_append_rpc(thiscurrency, optionlist, key, value, selected) {
if (glob_ap_id === "apis") {
if (thiscurrency === "ethereum" || glob_is_erc20t === true) {
const txhash = "0x919408272d05b3fd7ccfa1f47c10bea425891c8aa47ba7309dc3beb0b89197f1", // random tx
payload = {
"jsonrpc": "2.0",
"id": 2,
"method": "eth_getTransactionByHash",
"params": [txhash]
};
api_proxy({
"api_url": value.url,
"params": {
"method": "POST",
"data": JSON.stringify(payload),
"headers": {
"Content-Type": "application/json"
}
}
}).done(function(e) {
const data = br_result(e),
rhash = q_obj(data, "result.result.hash");
rpc_option_li(optionlist, rhash === txhash, key, value, selected, true);
}).fail(function(e) {
rpc_option_li(optionlist, false, key, value, selected, true);
});
return
}
const rpcurl = get_rpc_url({
"url": value.url,
"username": value.username,
"password": value.password
});
const pload = glob_is_btc ? { // mempoolspace API
"api_url": value.url + "/api/v1/difficulty-adjustment",
"proxy": false,
"params": {
"method": "GET"
}
} : {
"api": thiscurrency,
"search": "test",
"cachetime": 25,
"cachefolder": "1h",
"api_url": rpcurl,
"params": {
"method": "POST",
"data": JSON.stringify(glob_test_rpc_call),
"headers": {
"Content-Type": "text/plain"
}
}
}
api_proxy(pload).done(function(e) {
const data = br_result(e),
result = data.result,
live = $.isEmptyObject(result) ? false : (thiscurrency === "nano" ? result.network === "live" : true);
rpc_option_li(optionlist, live, key, value, selected, true);
}).fail(function(e) {
rpc_option_li(optionlist, false, key, value, selected, true);
});
return
}
if (glob_ap_id === "websockets") {
let provider = value.url,
provider_name = value.name || provider,
ping_event = "heartbeat";
if (provider_name === "blockcypher wss") {
provider = value.url + "btc/main";
}
if (glob_is_btc) {
ping_event = JSON.stringify({
"action": "want",
"data": ["stats"]
});
}
if (thiscurrency === "nano") {
const naddr = "nano_1hedzz9g3oq1pw49hf9u9koqgwwg8in49o73xwrnfu9j43qk533r7hhuratx"; // random xno address for testing
ping_event = JSON.stringify({
"action": "subscribe",
"topic": "confirmation",
"all_local_accounts": true,
"options": {
"accounts": [naddr]
},
"ack": true
});
}
if (thiscurrency === "ethereum" || glob_is_erc20t === true) {
const if_id = get_infura_apikey(provider);
provider = provider + if_id,
ping_event = JSON.stringify({
"jsonrpc": "2.0",
"id": 1,
"method": "eth_subscribe",
"params": ["logs", {
"address": glob_expected_eth_address,
"topics": []
}]
});
}
let web_socket = glob_sockets["ws_test"] = new WebSocket(provider);
web_socket.onopen = function(e) {
web_socket.send(ping_event);
console.log("Connected: " + provider);
};
web_socket.onmessage = function(e) {
rpc_option_li(optionlist, true, key, value, selected, true);
console.log("socket test success");
web_socket.close();
web_socket = null;
closesocket("ws_test");
};
web_socket.onclose = function(e) {
console.log("End socket test");
};
web_socket.onerror = function(e) {
rpc_option_li(optionlist, false, key, value, selected, true);
console.log("socket test failed");
web_socket.close();
web_socket = null;
closesocket("ws_test");
};
}
}
// Function to create and append an RPC option list item
function rpc_option_li(optionlist, live, key, value, selected, checked) {
const liveclass = live ? " live" : " offline",
selected_class = selected ? " rpc_selected" : "",
icon = live ? "connection" : "wifi-off",
datakey = checked ? " data-key='" + key + "'" : "",
default_class = value.default !== false ? " default" : "",
node_name = value.name || value.url,
option = $("<div class='optionwrap" + liveclass + selected_class + default_class + "' style='display:none' data-pe='none'><span" + datakey + " data-value='" + value.url + "' data-pe='none'>" + node_name + "</span><div class='opt_icon_box' data-pe='none'><div class='opt_icon c_stat icon-" + icon + "' data-pe='none'></div><div class='opt_icon icon-bin' data-pe='none'></div></div>");
option.data(value).appendTo(optionlist);
option.slideDown(500);
}
// Function to handle RPC node selection
function test_rpcnode() {
$(document).on("click", "#settingsbox .selectbox .options > div", function(e) {
const target = $(e.target);
if (target.hasClass("icon-bin")) {
return // prevent selection when deleting
}
const thisoption = $(this),
thisdata = thisoption.data();
if (thisoption.hasClass("offline")) {
playsound(glob_funk);
topnotify(translate("unabletoconnect"));
return
}
const settingsbox = $("#settingsbox"),
rpc_main_input = settingsbox.find("#rpc_main_input");
rpc_main_input.removeData().data(thisdata);
settingsbox.find(".options .optionwrap").removeClass("rpc_selected");
thisoption.addClass("rpc_selected");
})
}
// Function to handle RPC node submission
function submit_rpcnode() {
$(document).on("click", "#settingsbox input.submit", function(e) {
e.preventDefault();
const settingsbox = $("#settingsbox"),
thiscurrency = $(this).attr("data-currency"),
rpc_main_input = settingsbox.find("#rpc_main_input"),
setvalue = rpc_main_input.data(),
rpc_input_box = settingsbox.find("#rpc_input_box");
if (rpc_input_box.length) {
const rpc_url_input_val = rpc_input_box.find("#rpc_url_input").val();
if (rpc_url_input_val.length > 5) {
const optionsbox = settingsbox.find(".options"),
duplicates = optionsbox.find("span[data-value='" + rpc_url_input_val + "']"),
indexed = duplicates.length > 0;
if (indexed || rpc_url_input_val.indexOf("mempool.space") > -1 ||
rpc_url_input_val.indexOf("litecoinspace.org") > -1) {
popnotify("error", translate("nodealreadyadded"));
return
}
const rpc_username_input_val = rpc_input_box.find("#rpc_username_input").val(),
rpc_password_input_val = rpc_input_box.find("#rpc_password_input").val(),
rpc_data = {
"url": rpc_url_input_val,
"username": rpc_username_input_val,
"password": rpc_password_input_val,
"default": false
};
test_rpc(rpc_input_box, rpc_data, thiscurrency);
return
}
}
pass_rpc_submit(thiscurrency, setvalue, false)
})
}
// Function to test RPC connection for various cryptocurrencies
function test_rpc(rpc_input_box, rpc_data, currency) {
const cant_connect = translate("unabletoconnect");
if (glob_ap_id === "apis") {
if (currency === "ethereum" || glob_is_erc20t === true) {
const txhash = "0x919408272d05b3fd7ccfa1f47c10bea425891c8aa47ba7309dc3beb0b89197f1", // random tx
payload = {
"jsonrpc": "2.0",
"id": 2,
"method": "eth_getTransactionByHash",
"params": [txhash]
};
api_proxy({
"api_url": rpc_data.url,
"params": {
"method": "POST",
"data": JSON.stringify(payload),
"headers": {
"Content-Type": "application/json"
}
}
}).done(function(e) {
const data = br_result(e),
rhash = q_obj(data, "result.result.hash");
if (rhash === txhash) {
rpc_input_box.addClass("live").removeClass("offline");
pass_rpc_submit(currency, rpc_data, true);
return
}
rpc_input_box.addClass("offline").removeClass("live");
popnotify("error", cant_connect);
}).fail(function(e) {
rpc_input_box.addClass("offline").removeClass("live");
popnotify("error", cant_connect);
});
return
}
const rpcurl = get_rpc_url(rpc_data),
testadress = {
"bitcoin": glob_expected_bech32,
"litecoin": glob_expected_ltc_address,
"dogecoin": glob_expected_doge_address,
"bitcoin-cash": glob_expected_bch_cashaddr
} [currency] || "",
pload = glob_is_btc ? {
"api_url": rpcurl + "/api/address/" + testadress + "/txs",
"proxy": false,
"params": {
"method": "GET"
}
} : {
"api": currency,
"search": "test",
"cachetime": 25,
"cachefolder": "1h",
"api_url": rpcurl,
"params": {
"method": "POST",
"data": JSON.stringify(glob_test_rpc_call),
"headers": {
"Content-Type": "text/plain"
}
}
}
api_proxy(pload).done(function(e) {
const data = br_result(e),
rpc_result = data.result;
const error = data.error || rpc_result.error;
if (error) {
rpc_input_box.addClass("offline").removeClass("live");
topnotify(cant_connect);
const error_message = error.error_message || error.message;
if (error_message) {
popnotify("error", error_message);
}
return
}
if (rpc_result && (br_issar(rpc_result) || rpc_result.rpc_version)) {
rpc_input_box.addClass("live").removeClass("offline");
pass_rpc_submit(currency, rpc_data, true);
}
}).fail(function(e) {
rpc_input_box.addClass("offline").removeClass("live");
topnotify(cant_connect);
});
return
}
if (glob_ap_id === "websockets") {
let provider = rpc_data.url,
ping_event;
if (glob_is_btc) {
ping_event = JSON.stringify({
"action": "ping"
});
}
if (currency === "nano") {
let naddr = "nano_1hedzz9g3oq1pw49hf9u9koqgwwg8in49o73xwrnfu9j43qk533r7hhuratx"; // random xno address for testing
ping_event = JSON.stringify({
"action": "subscribe",
"topic": "confirmation",
"all_local_accounts": true,
"options": {
"accounts": [naddr]
},
"ack": true
});
}
if (currency === "ethereum" || glob_is_erc20t === true) {
ping_event = JSON.stringify({
"jsonrpc": "2.0",
"id": 1,
"method": "eth_subscribe",
"params": ["logs", {
"address": glob_expected_eth_address,
"topics": []
}]
});
}
let w_socket = glob_sockets["ws_submit"] = new WebSocket(provider);
w_socket.onopen = function(e) {
w_socket.send(ping_event);
console.log("Connected: " + provider);
};
w_socket.onmessage = function(e) {
rpc_input_box.addClass("live").removeClass("offline");
pass_rpc_submit(currency, rpc_data, true);
console.log("socket test success");
w_socket.close();
w_socket = null;
closesocket("ws_submit");
};
w_socket.onclose = function(e) {
console.log("End socket test");
};
w_socket.onerror = function(e) {
rpc_input_box.addClass("offline").removeClass("live");
popnotify("error", cant_connect);
console.log("socket test failed");
w_socket.close();
w_socket = null;
closesocket("ws_submit");
};
setTimeout(function() {
closesocket();
}, 5000);
}
}
// Function to finalize RPC submission and update settings
function pass_rpc_submit(thiscurrency, thisvalue, newnode) {
const rpc_setting_li = cs_node(thiscurrency, glob_ap_id),
options = rpc_setting_li.data("options"),
node_name = thisvalue.name || thisvalue.url;
rpc_setting_li.data("selected", thisvalue).find("p").html(node_name);
if (newnode === true) {
if ($.isEmptyObject(options)) {
rpc_setting_li.data("options", [thisvalue]);
} else {
options.push(thisvalue);
}
}
canceldialog();
notify(translate("datasaved"));
save_cc_settings(thiscurrency, true);
}
// Function to handle removal of RPC nodes
function remove_rpcnode() {
$(document).on("click", "#settingsbox .options .opt_icon_box .icon-bin", function(e) {
e.preventDefault();
const thistrigger = $(this),
settingsbox = $("#settingsbox"),
thiscurrency = settingsbox.find("#rpc_input_box").attr("data-currency"),
rpc_setting_li = cs_node(thiscurrency, glob_ap_id),
options = rpc_setting_li.data("options");
if (options && options.length) {
const thisoption = thistrigger.closest(".optionwrap"),
thisdata = thisoption.data(),
thisurl = thisdata.url,
default_node = thisdata.default !== false,
optionsbox = settingsbox.find(".options"),
duplicates = optionsbox.find("span[data-value='" + thisurl + "']"),
is_duplicate = duplicates.length > 1;
if (default_node === true && !is_duplicate) {
playsound(glob_funk);
topnotify(translate("removedefaultnode"));
return
}
const thisname = thisdata.name || thisurl,
result = confirm(translate("confirmremovenode", {
"thisname": thisname
}));
if (result) {
const new_array = options.filter(option => option.url !== thisurl);
thisoption.slideUp(500, function() {
$(this).remove();
});
rpc_setting_li.data("options", new_array);
notify(translate("rpcnoderemoved"));
$("#rpc_url_input").val("");
save_cc_settings(thiscurrency, true);
}
}
return
})
}
// Function to construct RPC URL with optional authentication
function get_rpc_url(rpc_data) {
if (rpc_data === false) {
return false;
}
const url = rpc_data.url,
username = rpc_data.username,
password = rpc_data.password,
login_param = (username && password) ? username + ":" + password + "@" : "",
hasprefix = url.includes("http"),
urlsplit = hasprefix ? url.split("://") : url;
return hasprefix ? urlsplit[0] + "://" + login_param + urlsplit[1] : url;
}
// Function to handle editing of Xpub settings
function edit_xpub_trigger() {
$(document).on("click", ".cc_settinglist li[data-id='Xpub'] .atext", function() {
if (!glob_test_derive) {
playsound(glob_funk)
return
}
const thisnode = $(this),
thislist = thisnode.closest("li"),
thisdat = thislist.data();
if (!thisdat.selected || !thisdat.key) {
return
}
const thisliwrap = thislist.find(".liwrap"),
thiscurrency = thisliwrap.attr("data-currency"),
coindat = getcoindata(thiscurrency),
xpub = thisdat.key,
cc_icon = getcc_icon(coindat.cmcid, coindat.ccsymbol + "-" + thiscurrency, coindat.erc20),
content = $("<div id='ad_info_wrap'><h2>" + cc_icon + " " + translate("bip32xpub") + "</h2>\
<div class='d_ulwrap'>\
<ul>\
<li><strong>Key: </strong><span class='adbox adboxl select'>" + xpub + "</span>\
<div id='qrcodexp' class='qrwrap flex'><div class='qrcode'></div>" + cc_icon + "</div>\
</li>\
<li><strong>" + translate("derivationpath") + ":</strong> M/0/</li>\
</ul>\
</div>\
<div id='backupactions'>\
<div id='delete_xpub' data-currency='" + thiscurrency + "' class='util_icon icon-bin'></div>\
<div id='backupcd'>" + cancelbttn + "</div>\
</div>\
</div>");
popdialog(content, "triggersubmit", null, true);
$("#qrcodexp .qrcode").qrcode(xpub);
})
}
// Function to handle deletion of Xpub
function delete_xpub() {
$(document).on("click", "#delete_xpub", function() {
const result = confirm(translate("delete") + " " + translate("bip32xpub") + "?");
if (result) {
const currency = $(this).attr("data-currency"),
xpubli = cs_node(currency, "Xpub"),
x_pubid = xpubli.data("key_id");
delete_xpub_cb(currency, x_pubid, true);
saveaddresses(currency, false);
check_currency(currency);
xpubli.data({
"selected": false,
"key": null,
"key_id": null
}).find(".switchpanel").removeClass("true").addClass("false");
xpubli.find("p").html("false");
save_cc_settings(currency, true);
canceldialog();
}
})
}
// Function to handle callback after Xpub deletion
function delete_xpub_cb(currency, x_pubid, uncheck) {
const xpublist = filter_addressli(currency, "xpubid", x_pubid);
xpublist.each(function() {
const thisli = $(this);
thisli.removeClass("xpubv").addClass("xpubu");
if (uncheck) {
thisli.attr("data-checked", "false").data("checked", false);
}
});
}
// Function to handle callback after adding Xpub
function add_xpub_cb(currency, x_pubid) {
const xpublist = filter_addressli(currency, "xpubid", x_pubid);
xpublist.each(function() {
$(this).addClass("xpubv").removeClass("xpubu").attr("data-checked", "true").data("checked", true);
});
}
// Function to handle Xpub switch in currency settings
function xpub_cc_switch() {
$(document).on("mouseup", ".cc_settinglist li[data-id='Xpub'] .switchpanel.custom", function() {
if (glob_test_derive !== true) {
playsound(glob_funk);
return
}
const this_switch = $(this),
thislist = this_switch.closest("li"),
thisliwrap = this_switch.closest(".liwrap"),
thiscurrency = thisliwrap.attr("data-currency"),
thisdat = thislist.data();
if (this_switch.hasClass("true")) {
const result = confirm(translate("disablexpub"));
if (result) {
thislist.data("selected", false).find("p").html("false");
this_switch.removeClass("true").addClass("false");
save_cc_settings(thiscurrency, true);
delete_xpub_cb(thiscurrency, thisdat.key_id);
}
return
}
if (thisdat.key) {
thislist.data("selected", true).find("p").text("true");
this_switch.removeClass("false").addClass("true");
save_cc_settings(thiscurrency, true);
add_xpub_cb(thiscurrency, thisdat.key_id);
saveaddresses(thiscurrency, false);
currency_check(thiscurrency);
return
}
const cd = getcoindata(thiscurrency),
ad = {
"currency": thiscurrency,
"ccsymbol": cd.ccsymbol,
"cmcid": cd.cmcid,
"erc20": cd.erc20
}
edit_xpub(ad);
})
}
// Function to handle editing of Xpub
function edit_xpub(ad) {
const currency = ad.currency,
cpid = ad.ccsymbol + "-" + currency,
address = ad.address || "",
scanqr = (glob_hascam === true) ? "<div class='qrscanner' data-currency='" + currency + "' data-id='address' title='scan qr-code'><span class='icon-qrcode'></span></div>" : "",
addxpub = translate("addxpub", {
"currency": currency
}),
content = $("<div class='formbox form add' id='xpubformbox'>\
<h2>" + getcc_icon(ad.cmcid, cpid, ad.erc20) + " " + addxpub + "</h2>\
<div class='popnotify'></div>\
<form class='addressform popform'>\
<div class='inputwrap'><input type='text' id='xpub_input' class='address' value='" + address + "' placeholder='" + addxpub + "' data-currency='" + currency + "'>" + scanqr + "</div>\
<div id='ad_info_wrap' style='display:none'>\
<ul class='td_box'>\
</ul>\
<div id='pk_confirm' class='noselect'>\
<div id='matchwrap' class='cb_wrap' data-checked='false'><span class='checkbox'></span></div><span>" + translate("xpubmatch", {
"currency": currency
}) + "</span><br/>\
<div id='pk_confirmwrap' class='cb_wrap' data-checked='false'><span class='checkbox'></span></div><span>" + translate("xpubkeys") + "</span>\
</div>\
</div>\
<input type='submit' class='submit' value='" + translate("okbttn") + "'></form>").data(ad);
popdialog(content, "triggersubmit");
if (!glob_supportsTouch) {
$("#popup input.address").focus();
}
}
// Function to handle Xpub input changes
function xpub_change() {
$(document).on("input", "#xpub_input", function(e) {
const thisnode = $(this),
addressinputval = thisnode.val(),
currency = thisnode.attr("data-currency"),
valid = check_xpub(addressinputval, xpub_prefix(currency), currency);
if (valid) {
clear_xpub_checkboxes();
validate_xpub(thisnode.closest("#xpubformbox"));
return
}
xpub_fail(currency);
})
}
// Function to trigger Xpub submission
function submit_xpub_trigger() {
$(document).on("click", "#xpubformbox input.submit", function(e) {
e.preventDefault();
validate_xpub($(this).closest("#xpubformbox"));
})
}
// Function to validate Xpub
function validate_xpub(thisnode) {
const this_data = thisnode.data(),
currency = this_data.currency,
addressfield = thisnode.find(".address"),
addressinputval = addressfield.val();
if (!addressinputval) {
xpub_fail(currency);
addressfield.focus();
return
}
const valid = check_xpub(addressinputval, xpub_prefix(currency), currency),
tdbox = $("#ad_info_wrap .td_box"),
dp_body = $("#ad_info_wrap");
if (valid !== true) {
const errormessage = translate("invalidxpub", {
"currency": currency
});
popnotify("error", errormessage);
setTimeout(function() {
addressfield.select();