forked from bitrequest/bitrequest.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassets_js_bitrequest_scanqr.js
More file actions
121 lines (110 loc) · 3.51 KB
/
assets_js_bitrequest_scanqr.js
File metadata and controls
121 lines (110 loc) · 3.51 KB
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
import QrScanner from "./assets_js_lib_qr-scanner.js";
QrScanner.WORKER_PATH = "./assets_js_lib_qr-scanner-worker.min.js";
var video = $("#qr-video")[0],
scanner = new QrScanner(video, result => setResult(result), error => {
console.log(error);
}),
currencyscan = null,
scantype = null;
$(document).ready(function() {
init_scan();
cam_trigger();
close_cam_trigger();
});
function init_scan() {
if (inframe === true) {
hascam = false;
return
}
QrScanner.hasCamera().then(hasCamera => detect_cam(hasCamera));
}
function detect_cam(result) {
hascam = result;
}
function start_scan(currency, type) {
scanner.start().then(() => {
currencyscan = currency,
scantype = type;
var currentpage = geturlparameters().p,
currentpage_correct = (currentpage) ? "?p=" + currentpage + "&scan=" : "?scan=",
url = currentpage_correct + currency,
title = "scanning " + currency + " " + type;
openpage(url, title, "scan");
show_cam();
closeloader();
}).catch((reason) => abort_cam(reason));
}
function abort_cam(reason) {
console.log(reason);
closeloader();
}
function cam_trigger() {
$(document).on("click", ".qrscanner", function() {
loader(true);
loadertext("Loading camera");
var thisqr = $(this),
currency = thisqr.attr("data-currency"),
type = thisqr.attr("data-id");
start_scan(currency, type);
});
}
function close_cam_trigger() {
$(document).on("click", "#closecam", function(e) {
if (e.originalEvent) {
window.history.back();
return;
}
close_cam();
});
}
function show_cam() {
body.addClass("showcam");
}
function close_cam() {
body.removeClass("showcam");
scanner.stop();
currencyscan = null;
}
function setResult(result) {
scanner.stop();
var payment = currencyscan,
thistype = scantype;
if (thistype == "address") {
var prefix = payment + ":",
mid_result = (result.indexOf(prefix) >= 0) ? result.split(prefix).pop() : result,
end_result = (result.indexOf("?") >= 0) ? mid_result.split("?")[0] : mid_result,
isxpub = (end_result.length > 103),
er_val = (payment == "nimiq") ? end_result.replace(/\s/g, "") : end_result;
validate = (isxpub) ? check_xpub(end_result, xpub_prefix(payment), payment) :
check_address(er_val, payment);
clear_xpub_inputs();
if (validate === true) {
$("#popup .formbox input.address").val(er_val);
if (supportsTouch === true) {} else {
$("#popup .formbox input.addresslabel").focus();
}
if (isxpub) {
clear_xpub_checkboxes();
validate_xpub($(".formbox"));
}
} else {
if (isxpub) {
xpub_fail(payment);
} else {
popnotify("error", "invalid " + payment + " address");
}
}
} else if (thistype == "viewkey") {
var validate = (result.length === 64) ? check_vk(result) : false;
if (validate === true) {
$("#popup .formbox input.vk_input").val(result);
if (supportsTouch === true) {} else {
$("#popup .formbox input.addresslabel").focus();
}
} else {
popnotify("error", "invalid " + payment + " viewkey");
}
}
window.history.back();
return false;
}