-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsignpayreq.js
140 lines (131 loc) · 4.7 KB
/
signpayreq.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
var requestId;
var paymentRequest;
var messageToSign;
var signature;
function validate() {
var client = getClient(document.querySelector("#webclient-select").value);
var url = client.endpoint || "";
var login = client.login || "";
var pwd = client.password || "";
var xhr = new XMLHttpRequest();
xhr.open("POST", url + "/api/lnd/sendpayment", true);
xhr.setRequestHeader("Authorization", "Basic " + window.btoa(login + ":" + pwd));
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onload = function () {
if (xhr.status >= 200 && xhr.status < 300) {
var paymentResponse = JSON.parse(xhr.responseText);
console.log(paymentResponse);
if (paymentResponse.error) {
alert(paymentResponse.error);
} else if (paymentResponse.payment_preimage) {
var paymentPreImage = array2hex(paymentResponse.payment_preimage.data);
chrome.runtime.sendMessage({ state: "validated", reqid: requestId, proof: [ signature, paymentPreImage ] }, function (response) {
console.log("validated response", response);
window.close();
});
}
} else {
alert(xhr.responseText);
}
};
xhr.onerror = function () {
alert(xhr.responseText);
};
xhr.send(JSON.stringify({ payreq: paymentRequest }));
}
function cancel() {
console.log("payment canceled");
chrome.runtime.sendMessage({ state: "canceled", reqid: requestId }, function (response) {
console.log("response", response);
window.close();
});
}
function decodePaymentRequest() {
var client = getClient(document.querySelector("#webclient-select").value);
var url = client.endpoint || "";
var login = client.login || "";
var pwd = client.password || "";
var xhr = new XMLHttpRequest();
xhr.open("POST", url + "/api/lnd/decodepayreq", true);
xhr.setRequestHeader("Authorization", "Basic " + window.btoa(login + ":" + pwd));
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onload = function () {
if (xhr.status >= 200 && xhr.status < 300) {
var paymentResponse = JSON.parse(xhr.responseText);
console.log(paymentResponse);
document.querySelector("#payment-pubkey").innerHTML = paymentResponse.destination;
document.querySelector("#payment-amount").innerHTML = paymentResponse.num_satoshis;
document.querySelector("#payment-hash").innerHTML = paymentResponse.payment_hash;
} else {
alert(xhr.responseText);
}
};
xhr.onerror = function () {
alert(xhr.responseText);
};
xhr.send(JSON.stringify({ payreq: paymentRequest }));
}
function signMessage() {
var client = getClient(document.querySelector("#webclient-select").value);
var url = client.endpoint || "";
var login = client.login || "";
var pwd = client.password || "";
var xhr = new XMLHttpRequest();
xhr.open("POST", url + "/api/lnd/signmessage", true);
xhr.setRequestHeader("Authorization", "Basic " + window.btoa(login + ":" + pwd));
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onload = function () {
if (xhr.status >= 200 && xhr.status < 300) {
var signatureResponse = JSON.parse(xhr.responseText);
console.log(signatureResponse);
signature = signatureResponse.signature;
document.querySelector("#signature-value").innerHTML = signature;
} else {
alert(xhr.responseText);
}
};
xhr.onerror = function () {
alert(xhr.responseText);
};
xhr.send(JSON.stringify({ msg: messageToSign }));
}
function changeClient() {
var clientSelect = document.querySelector("#webclient-select");
localStorage["defaultClient"] = clientSelect.value;
refreshDialog();
}
function refreshDialog() {
decodePaymentRequest();
signMessage();
}
function initDialog () {
requestId = getParameterByName("request");
paymentRequest = getParameterByName("payreq");
document.querySelector("#payment-request").innerHTML = paymentRequest;
messageToSign = paymentRequest;
document.querySelector("#signature-message").innerHTML = messageToSign;
var clientSelect = document.querySelector("#webclient-select");
for (var endpoint in webClients) {
if (webClients.hasOwnProperty(endpoint)) {
var client = webClients[endpoint];
var opt = document.createElement("option");
opt.value = client.endpoint;
opt.innerHTML = client.endpoint;
clientSelect.appendChild(opt);
}
}
clientSelect.value = defaultClient;
if ((clientSelect.selectedIndex < 0) && (clientSelect.length > 0)) {
clientSelect[0].selected = true;
}
refreshDialog();
}
document.addEventListener("DOMContentLoaded", function () {
document.querySelector("#validate-button").addEventListener("click", validate);
document.querySelector("#cancel-button").addEventListener("click", cancel);
document.querySelector("#webclient-select").addEventListener("change", changeClient);
initDialog();
});
window.onunload = function (event) {
chrome.runtime.sendMessage({ state: "closed", reqid: requestId });
};