forked from spesmilo/electrum-http
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
325 lines (311 loc) · 9.46 KB
/
index.html
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
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Payment request</title>
<script type="text/javascript" charset="utf-8" src="vendor/jquery-3.4.1.min.js"></script>
<script type="text/javascript" src="vendor/jquery-ui.min.js"></script>
<script type="text/javascript" src="vendor/qrcode.js"></script>
<link rel="stylesheet" type="text/css"
href="vendor/jquery-ui-themes-1.12.1/themes/ui-lightness/jquery-ui.css">
<style type="text/css">
body {
font-family: Arial;
}
.tab {
overflow: hidden;
border: 1px solid #ccc;
background-color: #f1f1f1;
}
.tab button {
background-color: inherit;
float: left;
border: none;
outline: none;
cursor: pointer;
padding: 14px 16px;
transition: 0.3s;
}
.tab button:hover {
background-color: #ddd;
}
.tab button.active {
background-color: #ccc;
}
.tabcontent {
display: none;
padding: 6px 12px;
border: 1px solid #ccc;
border-top: none;
}
.column {
float: left;
width: 50%;
}
.row{
padding: 26px 26px;
border: 1px solid #ccc;
}
.row:after {
content: "";
display: table;
clear: both;
}
.paybutton{
width:100%;
background-color:#ccc;
border-radius: 5px;
padding:10px;
}
#progressbar {
position: relative;
background: #555;
}
.progress-label {
position: absolute;
top: 8px;
color: #fff;
font-weight: bold;
text-shadow: 1px 1px 0 #000;
}
</style>
</head>
<body>
<div style="width:20em; margin:auto; font-family:arial, serif;">
<div class="error" id="error"></div>
<div id="container">
<div id="progressbar">
<div class="progress-label" style="left: 5%;" id="progress-text"></div>
<div class="progress-label" style="right: 5%;" id="progress-timer"></div>
</div>
<div class="row">
<div id="reason" style="float:left; width:50%"></div>
<div id="amount" style="float:right; width:50%"></div>
</div>
<!-- Tab links -->
<div class="tab">
<button id="onchain_button" class="tablinks" onclick="openTab('onchain')">On-chain</button>
<button id="lightning_button" class="tablinks" onclick="openTab('lightning')">Lightning</button>
</div>
<!-- Tab content -->
<div id="lightning" class="tabcontent">
<div id="qr1" align="center"></div>
<div class="error" id="lnerror" style="height:15em; text-align:justify;"></div>
<div>
<button class="paybutton">
<a style="color:#000; text-decoration:none;" id="paylink1" target="_blank">Open in Wallet</a>
</button>
</div>
<div>
<button id="copy1" class="paybutton" onclick="doCopy(this.value)" value="">
Copy Lightning Request
</button>
</div>
</div>
<div id="onchain" class="tabcontent">
<div id="qr2" align="center"></div>
<div class="error" id="onchainerror" style="height:15em; text-align:justify;"></div>
<div>
<button class="paybutton">
<a style="color:#000; text-decoration:none;" id="paylink2" target="_blank">Open in Wallet</a>
</button>
</div>
<div>
<button id="copy2" class="paybutton" onclick="doCopy(this.value)" value="">
Copy Bitcoin URI
</button>
</div>
</div>
<p id="powered" style="font-size:80%;"></p>
</div>
</div>
<script type="text/javascript">
function doCopy(text){
navigator.clipboard.writeText(text);
alert("Copied to Clipboard: " + text);
}
function openTab(tabName){
$(".tabcontent").hide();
$(".tablinks").removeClass("active");
$("#"+tabName).show();
$("#"+tabName+"_button").addClass("active");
}
function paymentExpired(){
$(".tabcontent").hide();
$(".tablinks").removeClass("active");
$("#progress-text").html("This invoice has expired");
$(".ui-widget-header").css("background", "#f33");
}
function paymentReceived(){
$(".tabcontent").hide();
$(".tablinks").removeClass("active");
$("#progress-text").html("Payment received.");
$(".ui-widget-header").css("background", "#0a0");
payment_received = true;
$("#progress-timer").text("");
$("#progressbar").progressbar({
value: 100,
max: 100
});
}
function paymentUnknown(){
$("#progress-text").html('Error: Unknown invoice');
$("#progressbar").progressbar({
value: 100,
max:100
});
$(".ui-widget-header").css("background", "#f33");
}
/* AI generated code: This function was written by ChatGPT. Copyright is dubious!!! */
function formatTime(timeInSeconds) {
var hours = Math.floor(timeInSeconds / 3600);
var minutes = Math.floor((timeInSeconds - (hours * 3600)) / 60);
var seconds = timeInSeconds - (hours * 3600) - (minutes * 60);
if (hours < 10) {
hours = "0" + hours;
}
if (minutes < 10) {
minutes = "0" + minutes;
}
if (seconds < 10) {
seconds = "0" + seconds;
}
return hours + ":" + minutes + ":" + seconds;
}
/* end of AI generated code */
function getUrlParameter(sParam)
{
var sPageURL = window.location.search.substring(1);
var sURLVariables = sPageURL.split('&');
for (var i = 0; i < sURLVariables.length; i++)
{
var sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] == sParam)
{
return sParameterName[1];
}
}
}
var id = getUrlParameter('id');
var payment_received = false;
if (id) {
var jqxhr = $.getJSON("/api/get_invoice?" + id, function() {
console.log("getJSON:success");
})
.done(function(data) {
var typeNumber = 0;
var errorCorrectionLevel = 'L';
var URI;
var request;
$("<p />").text("Powered by Electrum").appendTo($("p#powered"));
if(!data) {
paymentUnknown();
return;
}
if (data.is_lightning){
if (data.can_receive){
request = data.lightning_invoice;
URI = 'lightning:' + request;
var qr1 = qrcode(typeNumber, errorCorrectionLevel);
qr1.addData(request.toUpperCase());
qr1.make();
$("#payreq1").text(request);
$("#qr1").html(qr1.createImgTag(3));
$("#copy1").val(request);
$("#paylink1").attr("href", URI);
$("#lnerror").hide();
} else {
$("#lnerror").text("Error: The server does not have enough inbound capacity to receive this amount with lightning. Try reloading this page later, or pay onchain.");
$(".tabcontent").hide();
}
}
if (data.address){
URI = data.URI;
if (data.bip70_url) URI += '&r=' + data.bip70_url;
request = URI;
var qr2 = qrcode(typeNumber, errorCorrectionLevel);
qr2.addData(URI);
qr2.make();
$("#payreq2").text(request);
$("#qr2").html(qr2.createImgTag(6));
$("#copy2").val(URI);
$("#paylink2").attr("href", URI);
$("#onchainerror").hide();
} else {
$("#onchainerror").text("This request cannot be paid on-chain.");
$(".tabcontent").hide();
}
$("#reason").text(data.message || "no description");
$("#amount").text(data.amount_BTC + " BTC");
$(function () {
var current;
var max = 100;
var initial = data.timestamp;
var duration = data.expiration - data.timestamp;
$("#progressbar").progressbar({
value: 100,
max:max
});
$("#progress-text").text("Awaiting payment...");
if(data.expiration){
function update() {
var seconds = (Math.floor(Date.now()/1000) - initial);
var current = 100 * (seconds)/duration;
var remaining = duration - seconds
if(!payment_received){
$("#progressbar").progressbar({
value: current,
max:max
});
if (current < max) {
$("#progress-timer").text(formatTime(remaining));
}
}
};
update();
var interval = setInterval(update, 1000);
}
});
if(data.is_lightning) {
openTab('lightning');
} else {
openTab('onchain');
}
var wss_address = (document.URL.startsWith("https") ? "wss" : "ws") + "://" + window.location.host +"/api/get_status?" + id;
console.log("Opening WSS: " + wss_address)
var ws = new WebSocket(wss_address);
var received_msg;
ws.onmessage = function (evt) {
received_msg = evt.data;
if(received_msg == 'paid') {
paymentReceived();
}
if(received_msg == 'expired') {
paymentExpired();
}
}
ws.onerror = function () {
console.log("error ws.onerror");
$("#error").text("error with websocket. try reloading this page");
};
ws.onclose = function () {
console.log("error ws.onclose");
if(received_msg == 'waiting')
$("#error").text("error with websocket: socket was closed. try reloading this page");
};
})
.fail(function() {
console.log("error fail while fetching invoice json");
$("#error").text("error while fetching invoice json. try reloading this page");
});
};
// See http://stackoverflow.com/questions/29186154/chrome-clicking-mailto-links-closes-websocket-connection
$(document).on('click', 'a[href^="bitcoin:"]', function (e) {
e.preventDefault();
var btcWindow = window.open($(e.currentTarget).attr('href'));
btcWindow.close();
return false;
});
</script>
</body>
</html>