-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwrite_key.ts
More file actions
38 lines (27 loc) · 867 Bytes
/
write_key.ts
File metadata and controls
38 lines (27 loc) · 867 Bytes
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
import { readFile } from "fs/promises";
import { NFC } from "nfc-pcsc";
const nfc = new NFC();
console.log("Waiting for NFC reader...");
nfc.on("reader", (reader) => {
console.log(`${reader.name} reader connected`);
const waitForCardTimeout = setTimeout(() => {
console.log("Waiting for card...");
}, 1000);
reader.on("card", async (card) => {
clearTimeout(waitForCardTimeout);
console.log(`${card.uid} card detected`);
console.log("Writing secret key to card...");
try {
const buffer = Buffer.alloc(64);
buffer.write(await readFile("secret.key", "binary"), "binary");
await reader.write(4, buffer);
console.log("Secret key written to card");
process.exit(0);
} catch (error) {
console.error(error);
}
});
});
nfc.on("error", (err) => {
console.log("an error occurred", err);
});