-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
276 lines (257 loc) · 9.36 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
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="css/swap-style.css">
<script src="lib/jquery-3.5.1.min.js"></script>
<script src="lib/noirjs-lib.js"></script>
<script src="lib/qrcode.js"></script>
</head>
<body id="body">
<div class="center-title">
<img src="img/noir-slogan-icon.png" height="96px">
<p>chain swap</p>
</div>
<div class="div-spacer"></div>
<div class="center">
<p>To redeem your balance on the new chain, you simply need to put in your addresses private key, a list of address private keys or import your wallet dump file here, then fill in your address from the new wallet and click on 'Swap!', your balance should be sent to you in the next couple of minutes!</p>
<table>
<tr class="tr-spacer"></tr>
<tr>
<td class="td-title td-left">Old wallet details</td>
<td class="td-center"><input class="td-textinput" placeholder="Private Key" type="text" id="privKey" value=""></td>
<td class="td-right">
<input class="walletfile" id="walletfile" name="walletfile" type="file" />
<label for="walletfile">Choose a file</label>
</td>
</tr>
<tr class="tr-spacer"></tr>
<tr>
<td class="td-title td-left">New wallet details</td>
<td class="td-center"><input class="td-textinput" placeholder="Noir Address" type="text" id="address" value=""></td>
<td class="td-right"><button onclick="scanAddress()">Scan address</button></td>
</tr>
<tr class="tr-spacer"></tr>
</table>
<button class="swap-button" onclick="sendData()">Swap!</button>
</div>
<div class="div-spacer"></div>
<div class="progress" id="progress" style="visibility: hidden;">
</div>
<div id="myModal" class="modal">
<div class="modal-content">
<span class="close">×</span>
<video id="video" width="300" height="200" style="border: 1px solid gray"></video>
</div>
</div>
<div id="response"></div>
</body>
</html>
<script type="text/javascript">
var modal = document.getElementById("myModal");
var span = document.getElementsByClassName("close")[0];
var privKeys = "";
var p2pkhAdd = "";
var p2shAdd = "";
var p2wpkhAdd = "";
var progressCounter = 0;
function WIFtoP2PKH(wif) {
const keyPair = Noir.ECPair.fromWIF(wif,);
const { address } = Noir.payments.p2pkh({ pubkey: keyPair.publicKey }); // P2PKH
return address;
}
function WIFtoP2SH(wif) {
const keyPair = Noir.ECPair.fromWIF(wif,);
const { address } = Noir.payments.p2sh({redeem: Noir.payments.p2wpkh({ pubkey: keyPair.publicKey }),}); // P2SH
return address;
}
function WIFtoP2WPKH(wif) {
const keyPair = Noir.ECPair.fromWIF(wif,);
const { address } = Noir.payments.p2wpkh({ pubkey: keyPair.publicKey }); // P2WPKH
return address;
}
document.getElementById('walletfile').addEventListener('change', function() {
wait(15); // Otherwise it may skip displaying the process
document.getElementById('progress').style = "visibility: visible;"
document.getElementById('progress').innerHTML = '<div class="pr-center">Reading wallet dump, please wait...</div><div class="loader"></div>';
if (!window.FileReader) { // This is VERY unlikely, browser support is near-universal
alert("The file API isn't supported on this browser yet.");
document.getElementById('progress').innerHTML = "The file API isn't supported on this browser yet."
return;
}
var input = document.getElementById('walletfile');
if (!input.files) { // This is VERY unlikely, browser support is near-universal
alert("This browser doesn't seem to support the `files` property of file inputs.");
document.getElementById('progress').innerHTML = "This browser doesn't seem to support the `files` property of file inputs."
return;
}
var file = input.files[0];
var fr = new FileReader();
fr.readAsText(this.files[0]);
fr.onload = function(){
var contents = fr.result;
var lines = contents.split("\n");
lines.forEach(checkLine);
privKeys = privKeys.substring(1);
var privKeyArr = privKeys.split(',');
privKeyArr.forEach(checkWIF);
p2pkhAdd = p2pkhAdd.substring(1);
p2shAdd = p2shAdd.substring(1);
p2wpkhAdd = p2wpkhAdd.substring(1);
var zAdds = p2pkhAdd.split(',');
var nAdds = p2shAdd.split(',');
var norAdds = p2wpkhAdd.split(',');
document.getElementById('progress').innerHTML = "<pre>From file: <b>" + file.name + "</b><br>Found:<br> " + privKeyArr.length + " valid private keys"
+ "<br> " + zAdds.length + " valid P2PKH Addresses"
+ "<br> " + nAdds.length + " valid P2SH Addresses"
+ "<br> " + norAdds.length + " valid P2WPKH Addresses</pre>";
document.getElementById('privKey').value = privKeys;
}
})
function wait(ms) {
var d = new Date();
var d2 = null;
do { d2 = new Date(); }
while(d2-d < ms);
}
function checkLine(lineArr) {
var line = lineArr;
var lineSplit = line.split(" ");
var potPrivKey = lineSplit[0];
if (potPrivKey.startsWith("X")) {
privKeys = privKeys + "," + potPrivKey;
}
}
function checkWIF(item) {
if (privKeys === ""){
privKeys = item;
} else {
privKeys = privKeys + "," + item;
}
var p2pkh = WIFtoP2PKH(item);
if (p2pkh.startsWith("Z")) {
p2pkhAdd = p2pkhAdd + "," + p2pkh;
}
var p2sh = WIFtoP2SH(item);
if (p2sh.startsWith("N")) {
p2shAdd = p2shAdd + "," + p2sh;
}
var p2wpkh = WIFtoP2WPKH(item);
if (p2wpkh.startsWith("nor")) {
p2wpkhAdd = p2wpkhAdd + "," + p2wpkh;
}
}
function scanAddress() {
modal.style.display = "block";
const codeReader = new ZXing.BrowserQRCodeReader();
codeReader
.listVideoInputDevices()
.then(videoInputDevices => {videoInputDevices.forEach(device => console.log(`${device.label}, ${device.deviceId}`));})
.catch(err => {
var errStr = String(err);
if (errStr.includes("NotFoundError: Requested device not found")) {
alert("No camera found!");
modal.style.display = "none";
} else {
console.error(err);
}
});
codeReader
.decodeOnceFromVideoDevice(undefined, 'video')
.then(result => {
if(!validateScannedAddress(result.text)){
alert("Failed to decode Noir address, please check!")
}
codeReader.reset();
return;
})
.catch(err => {
var errStr = String(err);
if (errStr.includes("NotFoundError: Requested device not found")) {
alert("No camera found!");
modal.style.display = "none";
} else {
console.error(err);
}
});
}
function validateScannedAddress(addr) {
try {
if (addr.startsWith('noir:')) {
addr = addr.substring(5);
Noir.address.toOutputScript(addr);
document.getElementById('address').value = addr;
} else {
Noir.address.toOutputScript(addr);
document.getElementById('address').value = addr;
}
return true;
} catch (e) {
console.error(e);
return false;
}
}
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
span.onclick = function() {
modal.style.display = "none";
}
function getAllAddressTypes() {
var privKey = document.getElementById("privKey").value;
if (privKey != "") {
var privKeyArr = privKey.split(',');
console.log(privKeyArr);
privKeyArr.forEach(checkWIF);
p2pkhAdd = p2pkhAdd.substring(1);
p2shAdd = p2shAdd.substring(1);
p2wpkhAdd = p2wpkhAdd.substring(1);
}
}
function sendData() {
var newAddress = document.getElementById('address').value;
if (privKeys === ""){
getAllAddressTypes();
}
if (privKeys === ""){
alert("Please put in at least one private key!");
return;
}
if (newAddress === ""){
alert("Please put in your new address!");
return;
}
var privKeyArr = privKeys.split(',');
var zAdds = p2pkhAdd.split(',');
var nAdds = p2shAdd.split(',');
var norAdds = p2wpkhAdd.split(',');
var data = [privKeyArr, zAdds, nAdds, norAdds, newAddress];
console.log(data);
/*var url = "./index.html";
var form = '<form style="visibility: hidden;" id="swap-form"action="' + url + '" method="post">' +
'<input type="text" name="data" value="' + data + '" />' +
'</form>';*/
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState <= 3){
document.getElementById('progress').style = "visibility: visible;"
document.getElementById("progress").innerHTML = '<div class="pr-center">Swap server is validating the keys. This could take a while.</div><div class="loader"></div>';
}
else {
document.getElementById('progress').style = "visibility: visible;"
document.getElementById("progress").innerHTML = '<div style="color: red;" class="pr-center">Communication with swap server is has failed.<br>Please try again in a few minutes.<br>If this issue persists, please contact us at: [email protected] for support.</div>';
}
if (this.readyState == 4 && this.status == 200) {
document.getElementById('progress').style = "visibility: visible;"
document.getElementById("progress").innerHTML = this.responseText;
}
};
xhttp.open("POST", "http://192.168.0.129/validate.php", true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
var encdata=window.btoa(data);
xhttp.send("data=" + encdata);
/*document.getElementById('body').innerHTML = form;
document.getElementById('swap-form').submit();*/
}
</script>