-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.ts
54 lines (45 loc) · 1.67 KB
/
demo.ts
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
import {promises as fs} from 'fs'
import { decodeSignedCertificate, encodeCertificate, SignedVaccineCertificate, VaccinationStatus } from './common'
import { buildCertificate, generateKeypair, signCertificate } from './generator'
import { generateQrCode } from './presenter'
import { verifyCertificate } from './verifier'
(async () => {
// Generate Keypair
const {privateKey, publicKey} = generateKeypair()
console.log(privateKey.export({
type: 'pkcs1',
format: 'pem'
}))
console.log(publicKey.export({
type: 'pkcs1',
format: 'pem'
}))
// Generate certificate
const unsignedCert = await buildCertificate("Person ABCDEF GHIJKLMNOP", "./avatar.jpg", VaccinationStatus.FULLY_VACCINATED)
const cert: SignedVaccineCertificate = signCertificate(unsignedCert, privateKey)
console.log(cert)
const data = encodeCertificate(cert)
console.log(`Encoded to: ${data.toString("base64")}`)
const decodedCert = decodeSignedCertificate(data)
if (verifyCertificate(decodedCert, publicKey)) {
console.log("Certificate Verified!")
}
else {
console.log("Certificate verification failed.")
}
if (decodedCert.name === cert.name && decodedCert.imageData.equals(cert.imageData) && decodedCert.vaccinationStatus === cert.vaccinationStatus) {
console.log("Certificates are identical.")
}
else {
console.log("Certificates differ.")
}
// Write SignedCertificate out as a QR code.
generateQrCode(cert, "cert.png")
// Write picture out
await fs.writeFile("qr_avatar.webp", cert.imageData)
})().then(
() => console.log("done."),
(ex) => {
throw ex
}
)