-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathassets_js_bitrequest_payments.js
3415 lines (3272 loc) · 150 KB
/
assets_js_bitrequest_payments.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
$(document).ready(function() {
// ** Core Dialog Management: **
wake_panel();
//set_dialog_timeout
//show_paymentdialog
//main_input_focus
// ** Swipe Controls: **
swipe_start();
//swipe
swipe_end();
// ** Flip Controls: **
flip_start();
//flip
flip_end();
//flip_right1
//flip_right2
//flip_left1
//flip_left2
//flip_reset1
//flip_reset2
//add_flip
//remove_flip
//face_front
//face_back
//poll_animate
// ** Payment Flow: **
//load_request
//get_tokeninfo
//get_tokeninfo_local
//continue_request
//lightning_setup
//lnd_put
//test_lnd
//proceed_pf
//get_cc_exchangerates
//cc_fail
//init_exchangerate
//get_fiat_exchangerate
//next_fiat_api
//no_xrate_result
// ** UI State Management: **
//render_currencypool
//get_payment
//update_exchange_rates_text
//update_currency_pool
//refresh_currency_pool
//generate_payment_qr
//set_wallet_uris
//set_lightning_qr
//set_lightning_uris
scan_qr();
show_api_stats();
hide_api_stats();
// ** Input Sync: **
sync_fiat_inputs();
sync_local_currency();
sync_crypto_inputs();
sync_satoshi_input();
//update_fiat_display
//update_local_currency
//update_crypto_display
//update_satoshi_display
//sync_input_values
//calculate_crypto_amount
mirror_input_value();
// ** Lightning & NFC: **
lnd_switch_function();
ndef_switch_function();
//lnd_statusx
lnd_offline();
lnd_ni();
//lnd_popup
// ** Request Management: **
switch_currency();
//validate_request_data
input_requestdata();
validate_steps();
flip_request();
reveal_title();
//pending_request
view_pending_tx();
//update_request_url
//show_pending_status
//update_payment_status
// ** Address Management: **
switch_address();
//new_addresli
copy_address_dblclick();
copy_address();
copy_inputs();
pick_address_from_dialog();
add_address_from_dialog();
add_from_seed();
// ** Sharing: **
share_button();
//is_ln_only
//lightning_mode
//share
//shorten_url
//firebase_shorten
//bitly_shorten
//custom_shorten
//get_saved_shorturl
//random_id
//share_request
//share_fallback
whatsapp_share();
mailto();
copy_url();
gmail_share();
telegram_share();
outlook_share();
//get_share_info
//share_callback
//open_share_url
// ** Transaction Viewing: **
view_tx();
//open_tx
xmr_settings();
// ** Wallet Operations: **
open_wallet();
open_wallet_url();
dw_trigger();
//download_wallet
// ** Request Updates: **
//save_payment_request
//update_request
//get_xmrpid
//xmr_integrated
});
// ** Swipe payment dialog **
// Attaches wake-up event handlers to the payment dialog box
function wake_panel() {
$(document).on("mousedown touchstart", "#paymentdialogbox", function() {
set_dialog_timeout();
})
}
// Sets a 3-minute auto-close timer for the payment request dialog
function set_dialog_timeout() {
// close request dialog after 3 minutes
if (!glob_const.paymentpopup.hasClass("live")) return
clearTimeout(glob_let.request_timer);
glob_let.request_timer = setTimeout(function() {
cpd_pollcheck();
}, 180000, function() {
clearTimeout(glob_let.request_timer);
});
glob_const.paymentdialogbox.removeClass("timer");
setTimeout(function() {
glob_const.paymentdialogbox.addClass("timer");
}, 500);
}
// Activates payment dialog with scroll position preservation and blur effects
function show_paymentdialog() {
glob_let.scrollposition = $(document).scrollTop(); // get scrollposition save as global
toggle_fixed_nav(glob_let.scrollposition); // fix nav position
glob_const.html.addClass("paymode blurmain_payment");
$(".showmain #mainwrap").css("transform", "translate(0, -" + glob_let.scrollposition + "px)"); // fake scroll position
glob_const.paymentpopup.addClass("showpu active");
}
// Sets focus to appropriate amount input field based on dialog state
function main_input_focus() {
const visible_input = glob_const.paymentdialogbox.hasClass("flipped") ? $("#paymentdialog #shareamount input:visible:first") :
$("#paymentdialog #amountbreak input:visible:first");
// hack to move cursor to the end
const amount_value = visible_input.val();
visible_input.val("").val(amount_value).focus();
}
// ** Swipe Controls: **
// Initializes vertical swipe detection for the payment dialog
function swipe_start() {
$(document).on("mousedown touchstart", "#paymentdialog", function(e) {
glob_let.blockswipe = false;
const current_dialog = $(this),
input_fields = current_dialog.find("input");
if (input_fields.is(":focus")) {
glob_let.blockswipe = true;
}
const start_height = e.originalEvent.touches ? e.originalEvent.touches[0].pageY : e.pageY;
startswipetime = now();
swipe(current_dialog.height(), start_height);
})
}
// Processes real-time vertical swipe movements and animations
function swipe(dialog_height, start_height) {
$(document).on("mousemove touchmove", "#payment", function(e) {
if (glob_let.blockswipe === true) {
unfocus_inputs();
return
}
const current_height = e.originalEvent.touches ? e.originalEvent.touches[0].pageY : e.pageY,
drag_distance = current_height - start_height;
if (Math.abs(drag_distance) > 3) { // margin to activate swipe
glob_const.html.addClass("swipemode");
const distance_ratio = drag_distance / dialog_height,
opacity_level = 1 - Math.abs(distance_ratio);
glob_let.percent = distance_ratio * 100;
$(this).addClass("swiping");
$("#paymentdialog").css({
"opacity": opacity_level,
"transform": "translate(0, " + glob_let.percent + "%)"
});
}
})
}
// Handles swipe gesture completion and resets dialog state
function swipe_end() {
$(document).on("mouseup mouseleave touchend", "#payment", function() {
$(document).off("mousemove touchmove", "#payment");
const current_unit = $(this);
if (current_unit.hasClass("swiping")) {
const payment_dialog = $("#paymentdialog"),
swipe_duration = now() - startswipetime,
is_large_swipe = Math.abs(glob_let.percent) > 90,
is_quick_swipe = Math.abs(glob_let.percent) > 25;
if (is_large_swipe || (is_quick_swipe && swipe_duration < 500)) {
current_unit.removeClass("swiping");
payment_dialog.css({
"opacity": "",
"transform": ""
});
cpd_pollcheck();
glob_const.html.removeClass("swipemode");
} else {
current_unit.removeClass("swiping");
payment_dialog.css({
"opacity": "",
"transform": ""
});
glob_const.html.removeClass("swipemode");
}
}
})
}
// ** Flip Controls: **
// Initializes horizontal flip gesture detection with payment state validation
function flip_start() {
$(document).on("mousedown touchstart", "#paymentdialog", function(e) {
if (glob_const.paymentdialogbox.hasClass("norequest")) {
if (glob_const.offline === true) {
return
}
const is_lightning = lightning_mode();
if (glob_const.paymentdialogbox.attr("data-pending") === "ispending" && !is_lightning) {
return
}
if (is_lightning && glob_const.paymentdialogbox.hasClass("accept_lnd")) {
return
}
}
const start_width = e.originalEvent.touches ? e.originalEvent.touches[0].pageX : e.pageX;
flip($(this).width(), start_width);
})
}
// Processes real-time horizontal flip movements and animations
function flip(dialog_width, start_width) {
$(document).on("mousemove touchmove", "#payment", function(e) {
glob_const.html.addClass("flipmode");
const current_width = e.originalEvent.touches ? e.originalEvent.touches[0].pageX : e.pageX,
drag_distance = current_width - start_width;
if (Math.abs(drag_distance) > 3) { // margin to activate flip (prevent sloppy click)
glob_const.html.addClass("swipemode");
$(this).addClass("flipping");
const start_angle = glob_const.paymentdialogbox.hasClass("flipped") ? 180 : 0;
glob_const.paymentdialogbox.css("transform", "rotateY(" + start_angle + "deg)");
const pre_angle = 180 * drag_distance / dialog_width;
glob_let.angle = glob_const.paymentdialogbox.hasClass("flipped") ? 180 + pre_angle : pre_angle;
glob_const.paymentdialogbox.css("transform", "rotateY(" + glob_let.angle + "deg)");
}
})
}
// Handles flip gesture completion and determines final rotation state
function flip_end() {
$(document).on("mouseup mouseleave touchend", "#payment", function() {
const current_unit = $(this);
$(document).off("mousemove touchmove", glob_const.paymentpopup);
if (current_unit.hasClass("flipping")) {
if (glob_const.paymentdialogbox.hasClass("flipped")) {
if (glob_let.angle > 250) {
flip_right2();
} else if (glob_let.angle < 110) {
flip_left2();
} else {
flip_reset1();
}
} else {
if (glob_let.angle > 70) {
flip_right1();
} else if (glob_let.angle < -70) {
flip_left1();
} else {
flip_reset2();
}
}
current_unit.removeClass("flipping");
glob_const.html.removeClass("swipemode");
}
setTimeout(function() {
glob_const.html.removeClass("flipmode");
}, 270);
})
}
// Executes clockwise flip from front to back face
function flip_right1() {
add_flip();
face_back();
}
// Completes full clockwise rotation back to front face
function flip_right2() {
glob_const.paymentdialogbox.css("transform", "rotateY(360deg)").removeClass("flipped");
face_front();
}
// Executes counterclockwise flip from front to back face
function flip_left1() {
glob_const.paymentdialogbox.css("transform", "rotateY(-180deg)").addClass("flipped");
face_back();
}
// Completes full counterclockwise rotation back to front face
function flip_left2() {
remove_flip();
face_front();
}
// Cancels flip animation when dialog is already flipped
function flip_reset1() {
glob_const.paymentdialogbox.css("transform", "");
}
// Cancels flip animation when dialog is not flipped
function flip_reset2() {
glob_const.paymentdialogbox.css("transform", "rotateY(0deg)");
}
// Applies 180-degree rotation and flipped state
function add_flip() {
glob_const.paymentdialogbox.css("transform", "rotateY(180deg)").addClass("flipped");
}
// Resets rotation and removes flipped state
function remove_flip() {
glob_const.paymentdialogbox.css("transform", "rotateY(0deg)").removeClass("flipped");
}
// Manages front-face view state and input focus
function face_front() {
if (request) {
if (request.isrequest === false) {
const share_button = $("#sharebutton"),
request_title = $("#requesttitle"),
request_name = $("#requestname"),
amount_input = $("#amountbreak input");
if (share_button.hasClass("sbactive")) {
if (amount_input.val().length > 0 && glob_const.supportsTouch) {
setTimeout(function() {
request_title.add(request_name).blur();
}, 300);
return
}
setTimeout(function() {
amount_input.focus();
}, 300);
return
}
setTimeout(function() {
request_title.attr("placeholder", tl("forexample") + ":" + request_title.attr("data-ph" + generate_random_number(1, 13)));
amount_input.focus();
}, 300);
return
}
if (request.iszero_request === true) {
setTimeout(function() {
$("#amountbreak input").focus();
}, 300);
}
}
}
// Manages back-face view state and input validation
function face_back() {
if (request) {
if (request.isrequest === false) {
const request_title = $("#requesttitle"),
request_name = $("#requestname");
if (request_name.val().length < 3) {
setTimeout(function() {
request_name.focus();
}, 300);
return
}
if (request_title.val().length < 2) {
setTimeout(function() {
request_title.focus();
}, 300);
return
}
const amount_input = $("#amountbreak input");
if (amount_input.val().length > 0 && glob_const.supportsTouch) {
setTimeout(function() {
amount_input.add(request_title).add(request_name).blur();
}, 300);
return
}
setTimeout(function() {
request_title.focus();
}, 300);
return
}
if (request.iszero_request === true) {
setTimeout(function() {
$("#paymentdialog #shareamount input:visible:first").focus();
}, 300);
}
}
}
// Triggers visual feedback animation for polling state
function poll_animate() {
glob_const.paymentdialogbox.removeClass("poll");
setTimeout(function() {
glob_const.paymentdialogbox.addClass("poll");
}, 100);
}
// ** Payment Flow: **
// Initializes the payment process and validates request parameters
function load_request(pass) {
reset_overflow();
glob_let.apikey_fails = false;
if (is_openrequest() === true) { // prevent double load
return
}
if (!empty_obj(glob_let.sockets)) {
play_audio(glob_const.funk);
notify(tl("closingsockets"));
return
}
glob_let.l2s = {};
loader();
glob_let.symbolcache = br_get_local("symbols", true);
if (glob_let.symbolcache) {
const url_params = get_urlparameters();
if (url_params.xss) { //xss detection
const content = "<h2 class='icon-warning'>" + glob_const.xss_alert + "</h2>";
popdialog(content, "canceldialog");
closeloader();
return
}
const contact_form = exists(url_params.contactform);
if (contact_form && pass !== true) { // show contactform
edit_contactform(true);
return
}
const payment_currency = url_params.payment,
coin_data = get_coin_config(payment_currency);
if (coin_data) {
const is_erc20 = coin_data.erc20 === true,
request_start_time = now(),
is_exact = exists(url_params.exact);
// Start building request object
request = {
"received": false,
"rq_init": request_start_time,
"rq_timer": request_start_time,
"payment": payment_currency,
"coindata": coin_data,
"erc20": is_erc20,
"eth_l2s": [],
"boltcard": false
}, // global request object
helper = {
"exact": is_exact,
"contactform": contact_form,
"lnd": false,
"lnd_status": false,
"l1_status": false,
"l2_status": false
}, // global helper object
glob_let.api_attempt["crypto_price_apis"] = {},
glob_let.api_attempt["fiat_price_apis"] = {},
glob_let.proxy_attempts = {},
glob_let.socket_attempt = {};
if (is_erc20 === true) {
const token_contract = coin_data.contract;
if (token_contract) {
request.token_contract = token_contract;
get_tokeninfo(payment_currency, token_contract);
return
}
const content = "<h2 class='icon-blocked'>" + tl("nofetchtokeninfo") + "</h2>";
popdialog(content, "canceldialog");
closeloader();
return
}
continue_request();
return
}
const content = "<h2 class='icon-blocked'>" + tl("currencynotsupported", {
"currency": payment_currency
}) + "</h2>";
popdialog(content, "canceldialog");
closeloader();
return
} // need to set fixer API key first
show_api_error("fixer", {
"errorcode": "300",
"errormessage": "Missing API key"
}, true);
}
// Fetches and caches ERC20 token decimal information
function get_tokeninfo(payment_currency, contract) {
const cached_decimals = br_get_local("decimals_" + payment_currency);
if (cached_decimals) { // check for cached values
request.decimals = cached_decimals;
continue_request();
return
}
set_loader_text(tl("gettokeninfo"));
api_proxy({
"api": "ethplorer",
"search": "getTokenInfo/" + contract,
"cachetime": 86400,
"cachefolder": "1d",
"params": {
"method": "GET"
}
}).done(function(response) {
const data = br_result(response).result,
token_error = data.error;
if (token_error) {
const fallback_decimals = get_tokeninfo_local();
if (fallback_decimals) {
request.decimals = fallback_decimals;
continue_request();
br_set_local("decimals_" + payment_currency, fallback_decimals); //cache token decimals
return
}
cancel_paymentdialog();
fail_dialogs("ethplorer", {
"error": token_error
});
return
}
const token_decimals = data.decimals;
request.decimals = token_decimals;
continue_request();
br_set_local("decimals_" + payment_currency, token_decimals); //cache token decimals
}).fail(function(xhr, status, error) {
const fallback_decimals = get_tokeninfo_local();
if (fallback_decimals) {
request.decimals = fallback_decimals;
continue_request();
br_set_local("decimals_" + payment_currency, fallback_decimals); //cache token decimals
return
}
if (get_next_proxy()) {
get_tokeninfo(payment_currency, contract);
return
}
cancel_paymentdialog();
const error_object = xhr || status || error;
fail_dialogs("ethplorer", {
"error": error_object
});
closeloader();
});
}
// Retrieves fallback token decimal information from local contract data
function get_tokeninfo_local() {
const coin_symbol = q_obj(request, "coindata.ccsymbol");
if (!coin_symbol) {
return false
}
const token_contracts = contracts(coin_symbol);
if (!token_contracts) {
return false
}
const token_decimals = token_contracts.dec;
return token_decimals ? token_decimals : (token_decimals === null) ? false : 18;
}
// Sets up payment dialog UI and initializes payment monitoring
function continue_request() {
//set globals
const url_params = get_urlparameters();
if (url_params.xss) { //xss detection
const content = "<h2 class='icon-warning'>" + glob_const.xss_alert + "</h2>";
popdialog(content, "canceldialog");
closeloader();
return
}
const payment_currency = request.payment,
is_erc20 = request.erc20,
recipient_address = url_params.address,
currency_check = is_erc20 ? "ethereum" : payment_currency,
encoded_data = url_params.d,
has_data = encoded_data && encoded_data.length > 5,
decoded_data = has_data ? JSON.parse(atob(encoded_data)) : null, // decode data param if exists;
is_lightning = decoded_data && decoded_data.imp, // check for lightning;
is_lnd_only = recipient_address === "lnurl",
is_valid_address = is_lnd_only ? true : check_address(recipient_address, currency_check); // validate address
if (is_valid_address === false) {
const error_message = !recipient_address ? tl("undefinedaddress") : tl("invalidaddress", {
"payment": payment_currency
}),
content = "<h2 class='icon-blocked'>" + error_message + "</h2>";
popdialog(content, "canceldialog");
closeloader();
return
}
const api_details = check_api(payment_currency, is_erc20),
is_request = br_get_local("editurl") !== glob_const.w_loc.search, // check if url is a request
coin_data = request.coindata,
coin_settings = active_coinsettings(payment_currency),
unit_of_account = url_params.uoa,
payment_amount = Number(url_params.amount),
request_type = url_params.type,
lightning_id = (decoded_data && decoded_data.lid) ? decoded_data.lid : false;
let view_key = false,
share_view_key = false,
payment_id = (decoded_data && decoded_data.pid) ? decoded_data.pid : false,
xmr_integrated_address = recipient_address;
if (payment_currency === "monero") { // check for monero viewkey
coin_data.monitored = false;
view_key = (decoded_data && decoded_data.vk) ? {
"account": recipient_address,
"vk": decoded_data.vk
} : get_vk(recipient_address),
share_view_key = share_vk(),
payment_id = payment_id ? payment_id : is_request ? false : get_xmrpid(),
xmr_integrated_address = xmr_integrated(recipient_address, payment_id);
}
const coin_symbol = coin_data.ccsymbol,
request_type_category = is_request ? (request_type) ? request_type :
glob_const.inframe ? "checkout" : "incoming" : "local",
type_code = request_type_category === "local" ? 1 :
request_type_category === "outgoing" || request_type_category === "incoming" ? 2 :
request_type_category === "checkout" ? 3 : 4,
is_crypto_account = unit_of_account === coin_symbol,
local_currency = $("#currencysettings").data("currencysymbol"), // can be changed in (settings)
fiat_currency = is_crypto_account ? local_currency : unit_of_account,
status_param = url_params.status,
status = status_param || "new",
is_paid = status ? status === "paid" : null,
coin_market_id = coin_data.cmcid,
coin_price_id = coin_symbol + "-" + payment_currency,
is_pending = ch_pending({
"address": xmr_integrated_address,
"cmcid": coin_market_id
}),
is_monitored = view_key ? true : coin_data.monitored,
pending_param = url_params.pending,
pending_status = pending_param || is_monitored ? "incoming" : "unknown",
socket_list = coin_settings.websockets || null,
selected_socket = socket_list ? socket_list.selected || null : null,
request_timestamp = decoded_data && decoded_data.ts ? decoded_data.ts : null,
request_name = decoded_data && decoded_data.n ? decoded_data.n : null,
request_title = decoded_data && decoded_data.t ? decoded_data.t : null,
eth_layer2_networks = decoded_data && decoded_data.l2 ? decoded_data.l2 : [],
current_confirmations = q_obj(coin_settings, "confirmations.selected"),
data_obj_confirmations = q_obj(decoded_data, "c"),
set_confirmations = parseFloat(data_obj_confirmations) || current_confirmations,
is_instant = !set_confirmations,
page_name_currency_param = is_crypto_account ? "" : payment_currency + " ",
page_name = request_name ? tl("sharetitlename", {
"requestname": request_name,
"pagenameccparam": page_name_currency_param,
"amount": payment_amount,
"uoa": unit_of_account,
"requesttitle": request_title
}) : tl("sharetitle", {
"pagenameccparam": page_name_currency_param,
"amount": payment_amount,
"uoa": unit_of_account
}),
request_class = is_request ? "request" : "norequest", //set classnames for request
is_zero_amount = payment_amount === 0 || isNaN(payment_amount),
is_zero_request = is_request && is_zero_amount,
zero_class = is_zero_request ? " iszero" : "",
display_class = is_crypto_account ? (unit_of_account === "btc" ? " showsat showlc showcc" : " showlc showcc") : (unit_of_account === fiat_currency ? "" : " showlc"),
status_attribute = status || "unknown",
status_class = status ? " " + status : " unknown",
show_satoshis = payment_currency === "bitcoin" && cs_node("bitcoin", "showsatoshis", true).selected,
type_class = " " + request_type_category,
offline_class = glob_const.offline ? " br_offline" : "",
pending_class = is_pending && is_monitored && request_type_category === "local" ? "ispending" : "",
has_integrated_address = xmr_integrated_address === recipient_address ? false : xmr_integrated_address,
show_qr = "showqr" in url_params,
saved_name = $("#accountsettings").data("selected"),
extended_request_data = {
"uoa": unit_of_account,
"amount": payment_amount,
"address": recipient_address,
"currencysymbol": coin_symbol,
"cmcid": coin_market_id,
"cpid": coin_price_id,
status,
saved_name,
"pending": pending_status,
"paid": is_paid,
"isrequest": is_request,
"requesttype": request_type_category,
"typecode": type_code,
"iscrypto": is_crypto_account,
"localcurrency": local_currency,
"fiatcurrency": fiat_currency,
"requestname": request_name,
"requesttitle": request_title,
"eth_l2s": eth_layer2_networks,
set_confirmations,
"no_conf": !is_monitored,
"instant": is_instant,
"shared": is_request && request_timestamp !== null, // check if request is from a shared source,
"iszero": is_zero_amount,
"iszero_request": is_zero_request,
"viewkey": view_key,
"share_vk": share_view_key,
payment_id,
lightning_id,
"xmr_ia": has_integrated_address,
"monitored": is_monitored,
"coinsettings": coin_settings,
"dataobject": decoded_data,
"showqr": show_qr
},
extended_helper_data = {
socket_list,
selected_socket,
"requestclass": request_class,
"iszeroclass": zero_class,
"currencylistitem": $("#currencylist > li[data-currency='" + payment_currency + "'] .rq_icon"),
"api_info": api_details,
"lnd_only": is_lnd_only
},
payment_attributes = {
"data-cmcid": coin_market_id,
"data-currencysymbol": coin_symbol,
"data-status": status_attribute,
"data-showsat": show_satoshis,
"data-pending": pending_class,
"class": request_class + status_class + display_class + type_class + offline_class + zero_class
},
lightning_switch = payment_currency === "bitcoin" ? (is_request && !is_lightning ? "" : "<div id='lightning_switch' title='lightning' class='lnswitch'><span class='icon-power'></span></div>") : "",
ndef_switch = payment_currency === "bitcoin" && glob_const.ndef ? "<div id='ndef_switch' title='Tap to pay' class='lnswitch'><span class='icon-connection'></span></div>" : "";
update_page_title(page_name);
glob_const.paymentdialogbox.append("<div id='request_back' class='share_request dialogstyle'></div><div id='request_front' class='dialogstyle'><div id='xratestats'><span id='rq_errlog'></span></div>" + ndef_switch + lightning_switch + "<div class='networks'></div></div>").attr(payment_attributes);
// Extend global request object
$.extend(request, extended_request_data);
// Extend global helper object
$.extend(helper, extended_helper_data);
if (payment_currency == "bitcoin") {
lightning_setup();
} else {
proceed_pf();
}
}
// Configures Lightning Network payment settings and validates node credentials for incoming or local requests
function lightning_setup() {
if (request.isrequest === true) {
const decoded_data = request.dataobject;
if (decoded_data) {
const implementation = decoded_data.imp;
if (implementation) { //lightning request
set_loader_text(tl("checklightningstatus", {
"imp": implementation
}));
const proxy_host_data = decoded_data.proxy,
node_host = decoded_data.host,
node_key = decoded_data.key,
direct_proxy = proxy_host_data ? proxy_host_data : (node_host && node_key) ? d_proxy() : null;
if (direct_proxy) {
const proxy_details = lnurl_deform(direct_proxy),
proxy_key = proxy_details ? proxy_details.k : false,
lightning_id = decoded_data.lid,
lnd_payment_id = lightning_id ? lightning_id : (decoded_data.pid) ? sha_sub(decoded_data.pid, 10) : null;
if (lnd_payment_id) {
const encoded_proxy_host = lnurl_encode_save(direct_proxy),
proxy_host = encoded_proxy_host ? lnurl_deform(encoded_proxy_host).url : null,
node_id_source = node_host ? (implementation === "lnbits" ? node_key : node_host) : null,
derived_node_id = node_id_source ? sha_sub(node_id_source, 10) : false,
node_id = decoded_data.nid ? decoded_data.nid : (derived_node_id ? derived_node_id : null),
node_password = decoded_data.pw ? sha_sub(decoded_data.pw, 10) : (proxy_key ? proxy_key : null),
use_lnurl = !(node_host && node_key),
is_lnurl_only = !node_id,
is_shared = !!lightning_id,
lnd_config = {
"request": true,
"shared": is_shared,
"imp": implementation,
"proxy_host": encoded_proxy_host,
"nid": node_id,
"lnurl": is_lnurl_only,
"selected": true,
"pid": lnd_payment_id,
"host": node_host,
"key": node_key,
"pw": node_password
};
helper.lnd = lnd_config;
glob_let.lnd_ph = proxy_host;
const has_credentials = !!(node_host && node_key && node_id),
lnd_payload = {
"status": lnd_payment_id,
"cred": has_credentials ? btoa(JSON.stringify({
"file": node_id,
"host": node_host,
"key": node_key
})) : false
}
lnd_put(proxy_host, node_password, lnd_payload, use_lnurl);
return
}
console.error("error", "missing payment id");
} else {
console.error("error", "missing proxy");
}
} else {
console.error("error", "missing implementation");
}
}
proceed_pf();
return
}
const lightning_list = get_lightning_settings(),
lightning_data = lightning_list.data(),
selected_service = lightning_data.selected_service;
if (selected_service) {
const implementation = selected_service.imp;
set_loader_text(tl("checklightningstatus", {
"imp": implementation
}));
const node_id = selected_service.node_id,
available_proxies = lightning_data.proxies,
proxy_id = selected_service.proxy_id,
fetched_proxy = fetch_proxy(available_proxies, proxy_id),
proxy_url = fetched_proxy ? fetched_proxy.proxy : lnurl_encode_save(lnd_pick_proxy()),
proxy_details = lnurl_deform(proxy_url),
proxy_host = proxy_details.url,
proxy_key = proxy_details.k,
is_local_proxy = is_local_node(proxy_url),
proxy_enabled = selected_service.proxy,
node_host = selected_service.host,
is_local_node_host = is_local_node(node_host),
node_key = selected_service.key,
is_lnurl = !!selected_service.lnurl,
is_lnurl_only = is_lnurl && !node_host,
is_proxy_enabled = (proxy_enabled == true) ? true : false,
saved_payment_id = br_get_session("lndpid"),
payment_id = saved_payment_id ? saved_payment_id : sha_sub(now(), 10),
use_lnurl = is_lnurl_only || is_proxy_enabled,
lnd_config = {
"request": false,
"shared": false,
"imp": implementation,
"proxy_host": proxy_url,
"pid": payment_id,
"proxy_id": proxy_id,
"nid": node_id,
"host": node_host,
"key": node_key,
"lnurl": is_lnurl,
"name": selected_service.name,
"proxy": is_proxy_enabled,
"local_node": is_local_node_host,
"local_proxy": is_local_proxy,
"selected": lightning_data.selected
};
helper.lnd = lnd_config;
glob_let.lnd_ph = proxy_host;
const has_credentials = !!(node_host && node_key && node_id && !is_lnurl),
lnd_payload = {
"status": payment_id,
"cred": has_credentials ? btoa(JSON.stringify({
"file": node_id,
"host": node_host,
"key": node_key
})) : false
}
lnd_put(proxy_host, proxy_key, lnd_payload, use_lnurl);
return
}
proceed_pf();
}
// Initializes communication with Lightning Network proxy server and handles authentication
function lnd_put(proxy_url, proxy_key, payload, is_lnurl) {
const request_type = request.requesttype === "local" ? undefined : request.requesttype;
glob_let.proxy_attempts[proxy_url] = true;
$.ajax({
"method": "POST",
"cache": false,
"timeout": 5000,
"url": proxy_url + "/proxy/v1/ln/api/",
"data": {
"fn": "put",
"pl": payload,
"rqtype": request_type,
"x-api": proxy_key
}
}).done(function(response) {
const is_successful = response.stat;
if (is_successful === true) {
test_lnd(is_lnurl);
return
}
if (is_successful === "no write acces") {
notify(tl("nowriteaccess"));
}
const data = br_result(response).result,
response_error = data.error,
default_error_message = tl("unabletoconnect");
if (response_error) {
const error_message = response_error.message || (typeof response_error === "string" ? response_error : default_error_message);
if (request.isrequest) {
if (helper.lnd_only) {
topnotify(error_message);
notify(tl("notmonitored"));
}
} else {
notify(error_message);
$("#rq_errlog").append("<span class='rq_err'>" + error_message + "</span>");
}
}
proceed_pf();
}).fail(function(xhr, status, error) {
const is_proxy = q_obj(helper, "lnd.lnurl");
if (is_proxy === false) {
const saved_proxy = s_lnd_proxy();
if (saved_proxy === false) {
if (get_next_proxy()) {
lightning_setup();
return
}
}
}
proceed_pf();
});
}
// Validates Lightning Network connection and implementation-specific status checks with caching
function test_lnd(is_lnurl) {
const lnd_config = helper.lnd;
if (!lnd_config.proxy_host) {
proceed_pf();
return
}
const status_cache_key = "lnd_timer_" + lnd_config.nid,
cached_status_time = sessionStorage.getItem(status_cache_key);
if (cached_status_time && (now() - cached_status_time) < 20000) { // get cached status
// lightning status is cached for 10 minutes
helper.lnd_status = true;
proceed_pf();
return
}
sessionStorage.removeItem(status_cache_key);
// functions in assets_js_bitrequest_lightning.js
const node_host = lnd_config.host,
is_onion_host = node_host && node_host.indexOf(".onion") > 0;
if (is_lnurl || is_onion_host) {
validate_lnurl_connection(lnd_config);
return
}
if (lnd_config.imp === "lnd") {
check_lnd_status(lnd_config);
return
}
if (lnd_config.imp === "lnbits") {
check_lnbits_status(lnd_config);