forked from arx-research/halo-bulk-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
551 lines (460 loc) · 18 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="./style.css?3" />
<title>HaLo Verify</title>
</head>
<body>
<button id="export"><img src="./download.svg" /></button>
<button id="clear"><img src="./x-white.svg" /></button>
<div class="wrapper">
<div class="width">
<div class="box">
<p class="empty">Tap Scan and hold HaLo to phone NFC antenna.</p>
</div>
<a class="kong-logo" href="https://kong.land">
<svg
width="54"
fill="none"
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 54 17"
>
<path
d="M12.338 16.713h-4.9L4.42 9.143l.12 7.57H0V.298h4.54l-.119 7.038L7.1.298h4.791l-3.626 7.92 4.073 8.495Zm13.154-8.155c0 2.502-.585 4.537-1.753 6.103C22.577 16.221 21.064 17 19.198 17c-1.655 0-3.075-.599-4.258-1.797-1.488-1.516-2.232-3.792-2.232-6.825 0-2.906.777-5.128 2.33-6.666C16.192.57 17.561 0 19.143 0c1.939 0 3.481.773 4.628 2.318 1.147 1.545 1.72 3.625 1.72 6.24Zm-4.715.032c0-2.97-.537-4.454-1.612-4.454-.537 0-.965.39-1.285 1.17-.312.722-.468 1.785-.468 3.189 0 1.382.147 2.454.441 3.216.294.762.702 1.143 1.225 1.143.537 0 .951-.376 1.241-1.127.305-.752.458-1.797.458-3.137Zm6.653 8.123V.298h4.334L34.04 6.06l.42 1.17c.155.439.328.946.516 1.52l.469 1.446a121.693 121.693 0 0 1-.382-3.658 30.671 30.671 0 0 1-.13-2.551V.297h4.334v16.416h-4.334l-2.287-6.039a72.53 72.53 0 0 1-.648-1.813c-.192-.563-.361-1.1-.506-1.61a99.78 99.78 0 0 1 .207 3.02c.043.857.065 1.608.065 2.253v4.189H27.43Zm20.232-9.494H54v.68c0 3.161-.483 5.437-1.448 6.826C51.499 16.242 49.942 17 47.88 17c-2.025 0-3.615-.744-4.77-2.233-1.146-1.502-1.72-3.565-1.72-6.187 0-2.658.577-4.753 1.732-6.284C44.276.766 45.855 0 47.858 0c2.657 0 4.585 1.354 5.783 4.061l-4.4 1.574c-.065-.518-.216-.927-.452-1.228-.236-.301-.531-.452-.887-.452-1.249 0-1.873 1.676-1.873 5.029 0 1.424.138 2.46.414 3.104.276.645.715.968 1.317.968.24 0 .458-.057.654-.17a1.47 1.47 0 0 0 .495-.479c.135-.205.24-.455.316-.75.076-.293.114-.621.114-.983h-1.677V7.22Z"
fill="#040202"
/>
</svg>
</a>
<div class="scan-form">
<input
type="text"
class="metadata-input"
placeholder="Optional Metadata"
/>
<button
id="scan"
class="scan-button"
onClick="readKeyU2F();"
>
Scan
</button>
<button
disabled
id="sign"
class="sign-button"
onClick="signU2F();"
>
Sign
</button>
</div>
<p id="pkey1raw" hidden>0x</p>
</div>
</div>
<script
src="https://cdn.ethers.io/lib/ethers-5.1.umd.min.js"
type="text/javascript"
></script>
<script>
let scannedHalos = {};
setTimeout(function () {
document.querySelector("body").classList.add("ready");
}, 400);
let currentURL = new URL(window.location.href);
const fromHexString = (hexString) =>
new Uint8Array(
hexString.match(/.{1,2}/g).map((byte) => parseInt(byte, 16))
);
function buf2hex(buffer) {
// buffer is an ArrayBuffer
return [...new Uint8Array(buffer)]
.map((x) => x.toString(16).padStart(2, "0"))
.join("");
}
function parseKeys(payload) {
primaryPublicKeyLength = parseInt("0x" + payload.slice(0, 2)) * 2;
primaryPublicKeyRaw = payload.slice(2, primaryPublicKeyLength + 2);
primaryPublicKeyHash = ethers.utils.sha256(
"0x" + primaryPublicKeyRaw.slice(2)
);
secondaryPublicKeyLength =
parseInt(
"0x" +
payload.slice(
primaryPublicKeyLength + 2,
primaryPublicKeyLength + 4
)
) * 2;
secondaryPublicKeyRaw = payload.slice(
primaryPublicKeyLength + 4,
primaryPublicKeyLength + secondaryPublicKeyLength + 4
);
secondaryPublicKeyHash = ethers.utils.sha256(
"0x" + secondaryPublicKeyRaw.slice(2)
);
tertiaryPublicKeyLength =
parseInt(
"0x" +
payload.slice(
primaryPublicKeyLength + secondaryPublicKeyLength + 4,
primaryPublicKeyLength + secondaryPublicKeyLength + 6
)
) * 2;
var tertiaryPublicKeyRaw = null;
var tertiaryPublicKeyHash = null;
if (tertiaryPublicKeyLength > 0) {
tertiaryPublicKeyRaw = payload.slice(
primaryPublicKeyLength + secondaryPublicKeyLength + 6,
primaryPublicKeyLength +
secondaryPublicKeyLength +
tertiaryPublicKeyLength +
6
);
tertiaryPublicKeyHash = ethers.utils.sha256(
"0x" + tertiaryPublicKeyRaw.slice(2)
);
}
const keys = {
primaryPublicKeyHash: primaryPublicKeyHash,
primaryPublicKeyRaw: primaryPublicKeyRaw,
secondaryPublicKeyHash: secondaryPublicKeyHash,
secondaryPublicKeyRaw: secondaryPublicKeyRaw,
tertiaryPublicKeyHash: tertiaryPublicKeyHash,
tertiaryPublicKeyRaw: tertiaryPublicKeyRaw,
};
return keys;
}
// This shit is nasty.
function unpackDERSignature(sig) {
let header0 = sig.slice(0, 2);
if (parseInt("0x" + header0) !== 0x30) {
throw Error("Invalid header.");
}
let header_r0 = sig.slice(4, 6);
let header_r1 = sig.slice(6, 8);
if (parseInt("0x" + header_r0) !== 0x02) {
throw Error("Invalid header (2).");
}
let length_r = parseInt("0x" + header_r1) * 2;
let r = sig.slice(8, length_r + 8);
let header_s0 = sig.slice(length_r + 8, length_r + 10);
let header_s1 = sig.slice(length_r + 10, length_r + 12);
// console.log(header_s1);
if (parseInt("0x" + header_s0) !== 0x02) {
throw Error("Invalid header (2).");
}
// console.log(length_r + 12);
// console.log(parseInt("0x" + header_s1) * 2);
let s = sig.slice(
length_r + 12,
length_r + 12 + parseInt("0x" + header_s1) * 2
);
// console.log(s.length);
if (r.length == 66) {
r = r.slice(2, 130);
}
if (s.length == 66) {
s = s.slice(2, 130);
}
return {
r: r,
s: s,
};
}
const staticHaloData = currentURL.searchParams.get("static");
// Parsing information and storing.
function prepareVerify(staticHaloData) {
keys = parseKeys(buf2hex(staticHaloData));
let primaryPublicKeyHash = keys["primaryPublicKeyHash"];
keys["metadata"] = document.querySelector(".metadata-input").value;
keys["address"] = ethers.utils.computeAddress("0x" + keys["primaryPublicKeyRaw"]);
// TODO: check scannedHalos as well...
var dupeCheck = document.querySelector(
`[data-primary="${primaryPublicKeyHash}"]`
);
if (!dupeCheck) {
scannedHalos[primaryPublicKeyHash] = keys;
const row = createRow(
keys["primaryPublicKeyHash"]
);
document.querySelector(".box").appendChild(row);
document.querySelector(".empty")?.remove();
localStorage.setItem("halos", JSON.stringify(scannedHalos));
document.querySelector("#clear").style.display = "flex";
}
}
// Generate a command formatted for the HaLo.
function generateCmd(cmd, keyslot, message = null, ethereumSigFormat = null) {
let messageBytes = null;
if (ethereumSigFormat == "eip191") {
messageBytes = ethers.utils.hashMessage(message);
} else {
// Always fall back to EIP191.
messageBytes = ethers.utils.hashMessage(message);
}
// Remove prepended 0x.
let messageBytesSlice = messageBytes.slice(2);
// Structure command bytes.
let cmdBytes = new Uint8Array(2);
cmdBytes[0] = cmd;
cmdBytes[1] = keyslot;
cmdBytes = buf2hex(cmdBytes);
// Prepend the message with the command.
inputBytes = cmdBytes + messageBytesSlice;
return [inputBytes, messageBytes, ethereumSigFormat];
}
// Verify the DER encoded signature against the input message and uncompressed public key.
function verifySignature(message, signature, publicKey) {
console.log(`digest: ${message}`);
console.log(`signature: ${signature}`);
console.log(`publicKey: ${publicKey}`);
// Compute the Ethereum address from the publicKey.
const computedAddress = ethers.utils.computeAddress("0x" + publicKey);
// Strip out DER formatting to get r and s.
const sigRaw = unpackDERSignature(signature);
// We generate DER signatures, not RLP. As such we do not have the v parameter and must ascertain it.
let vByte = new Uint8Array(1);
vByte[0] = 27;
const vByte0 = buf2hex(vByte);
vByte[0] = 28;
const vByte1 = buf2hex(vByte);
// Test which byte was used in the message.
switch (computedAddress) {
case ethers.utils.verifyMessage(
message,
"0x" + sigRaw.r + sigRaw.s + vByte0
):
return true;
case ethers.utils.verifyMessage(
message,
"0x" + sigRaw.r + sigRaw.s + vByte1
):
return true;
default:
return false;
}
}
if (staticHaloData) {
prepareVerify(staticHaloData);
}
async function authU2F(reqx) {
var req = {
publicKey: {
allowCredentials: [
{
id: reqx,
transports: ["nfc"],
type: "public-key",
},
],
challenge: new Uint8Array([
113, 241, 176, 49, 249, 113, 39, 237, 135, 170, 177, 61, 15, 14,
105, 236, 120, 140, 4, 41, 65, 225, 107, 63, 214, 129, 133, 223,
169, 200, 21, 88,
]),
rpId: "bulk.vrfy.ch",
timeout: 60000,
userVerification: "discouraged",
},
};
var xdd = await navigator.credentials.get(req);
return xdd.response.signature;
}
// This runs a command to read out the public keys.
async function readKeyU2F() {
let res = await authU2F(fromHexString("02"));
prepareVerify(res);
}
// Carry out a signature command against a HaLo.
async function signU2F() {
// Generate the challenge and command bytes as 'input'.
const challenge = document.querySelector(".metadata-input").value;
const [input, digest, ethereumSigFormat] = generateCmd(1, 1, challenge, "eip191");
// Send the input bytes.
var req = fromHexString(input);
var res = await authU2F(req);
// Grab the signature response.
const signature = buf2hex(res);
// Strip out DER formatting to get r and s.
const sigRaw = unpackDERSignature(signature);
// SECP256k1 order constant
let curveOrder = 115792089237316195423570985008687907852837564279074904382605163141518161494337n;
let rn = BigInt('0x' + sigRaw.r.toString('hex'));
let sn = BigInt('0x' + sigRaw.s.toString('hex'));
if (sn > curveOrder / 2n) {
// malleable signature, not compliant with Ethereum's EIP-2
// we need to flip s value in the signature
sn = -sn + curveOrder;
}
let foundPrimaryPublicKeyHash = null;
for(halo in scannedHalos) {
// Test against 27 and 28; this only works if we have already stored the device address.
let recover27;
let recover28;
try {
recover27 = ethers.utils.recoverAddress(digest, { r: '0x' + rn.toString(16), s: '0x' + sn.toString(16), v: 27 })
} catch(error) {
console.error(error)
}
try {
recover28 = ethers.utils.recoverAddress(digest, { r: '0x' + rn.toString(16), s: '0x' + sn.toString(16), v: 28 })
} catch(error) {
console.error(error)
}
// Recover address and compare to list.
if (recover27 && recover27 == scannedHalos[halo]["address"]) {
foundPrimaryPublicKeyHash = scannedHalos[halo]["primaryPublicKeyHash"];
break;
} else if (recover28 && recover28 == scannedHalos[halo]["address"]) {
foundPrimaryPublicKeyHash = scannedHalos[halo]["primaryPublicKeyHash"];
break;
}
}
// TODO: add to file, post to server.
if (foundPrimaryPublicKeyHash) {
scannedHalos[foundPrimaryPublicKeyHash]["signature"] = signature;
scannedHalos[foundPrimaryPublicKeyHash]["sigDigest"] = digest;
scannedHalos[foundPrimaryPublicKeyHash]["sigMessage"] = challenge;
scannedHalos[foundPrimaryPublicKeyHash]["sigFormat"] = ethereumSigFormat;
localStorage.setItem("halos", JSON.stringify(scannedHalos));
} else {
// TODO: alert has not been scanned.
alert("Please scan this chip first, then sign again.")
}
}
//show/hide Toggle
document.addEventListener(
"click",
function (event) {
if (event.target.matches(".scan-row-header, .scan-row-header *")) {
event.preventDefault();
event.target.closest(".scan-row").classList.toggle("active");
}
},
false
);
document.addEventListener(
"click",
function (event) {
if (
event.target.matches(
".scan-row-header-remove, .scan-row-header-remove *"
)
) {
event.preventDefault();
const el = event.target.closest(".scan-row");
const pk = el
.querySelector(".scan-row-header")
.getAttribute("data-primary");
// Remove el
el?.remove();
// Remove from saved
delete scannedHalos[pk]
if (Object.entries(scannedHalos).length === 0) {
document.querySelector("#clear").style.display = "none";
}
localStorage.setItem("halos", JSON.stringify(scannedHalos));
}
},
false
);
document.querySelector("#clear").addEventListener("click", function () {
if (confirm("Clear all scanned chips?")) {
scannedHalos = {};
localStorage.removeItem("halos");
document.querySelectorAll(".scan-row").forEach((el) => el.remove());
}
});
function createRow(primary) {
const el = document.createElement("div");
el.classList.add("scan-row");
let metadata = scannedHalos[primary]["metadata"] ? scannedHalos[primary]["metadata"] : "";
let signature = scannedHalos[primary]["signature"] ? scannedHalos[primary]["signature"] : "";
if (!metadata) {
rowTitle = primary;
metadata = "undefined"
} else {
rowTitle = metadata;
}
el.innerHTML = `
<header class="scan-row-header" data-primary="${primary}">
<button class="scan-row-header-remove">
<img src="./x.svg" />
</button>
<h2>
${rowTitle}
</h2>
<div class="scan-row-header-toggle">
<img src="./chevron-down.svg" />
</div>
</header>
<div class="scan-row-body">
<div class="scan-row-body-section">
<h3 class="scan-row-body-heading">Metadata</h3>
<p class="scan-row-body-data">
${metadata}
</p>
</div>
<div class="scan-row-body-section">
<h3 class="scan-row-body-heading">Primary Public Key Hash</h3>
<p class="scan-row-body-data">
${primary}
</p>
</div>
<div class="scan-row-body-section">
<h3 class="scan-row-body-heading">Signature</h3>
<p class="scan-row-body-data">
${signature}
</p>
</div>
</div>
`;
return el;
}
function download(content, fileName, contentType) {
var a = document.createElement("a");
var file = new Blob([content], { type: contentType });
a.href = URL.createObjectURL(file);
a.download = fileName;
a.click();
}
document.querySelector("#export").addEventListener("click", function () {
download(
JSON.stringify(scannedHalos),
`scanned-halos-${Date.now()}.json`,
"application/json"
);
});
// Populate from last time
try {
const halos = localStorage.getItem("halos");
if (halos) {
scannedHalos = JSON.parse(halos);
} else {
scannedHalos = {};
}
if (scannedHalos && Object.entries(scannedHalos).length > 0) {
for (halo in scannedHalos) {
var row = createRow(
scannedHalos[halo]["primaryPublicKeyHash"]
);
document.querySelector(".box").appendChild(row);
document.querySelector(".empty")?.remove();
document.querySelector("#clear").style.display = "flex";
}
}
} catch (err) {
console.log(err);
}
document
.querySelector(".metadata-input")
.addEventListener("input", function (e) {
document.querySelector("#sign").disabled = !(
e.target.value
);
});
</script>
</body>
</html>