-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.ts
66 lines (54 loc) · 1.93 KB
/
common.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
55
56
57
58
59
60
61
62
63
64
65
66
import { Field, Message, Type } from "protobufjs"
import zlib from "zlib"
export enum VaccinationStatus {
UNVACCINATED = 0,
PARTIAlLY_VACCINATED,
FULLY_VACCINATED
}
export interface IVaccineCertificate {
name: string,
imageData: Buffer,
vaccinationStatus: VaccinationStatus,
}
export interface ISignedVaccineCertificate extends IVaccineCertificate {
signature: Buffer
}
@Type.d("VaccineCertificate")
export class VaccineCertificate extends Message<VaccineCertificate> implements IVaccineCertificate {
@Field.d(1, "string", "required")
public name: string
@Field.d(2, "bytes", "required")
public imageData: Buffer
@Field.d(3, VaccinationStatus, "required")
public vaccinationStatus: VaccinationStatus
}
@Type.d("SignedVaccineCertificate")
export class SignedVaccineCertificate extends Message<SignedVaccineCertificate> implements ISignedVaccineCertificate {
@Field.d(1, "string", "required")
public name: string
@Field.d(2, "bytes", "required")
public imageData: Buffer
@Field.d(3, VaccinationStatus, "required")
public vaccinationStatus: VaccinationStatus
@Field.d(4, "bytes", "required")
public signature: Buffer
}
const _baseEncodeCertificate = (cert: VaccineCertificate | SignedVaccineCertificate): Uint8Array => {
if (cert instanceof SignedVaccineCertificate) {
return SignedVaccineCertificate.encode(cert).finish()
}
else {
return VaccineCertificate.encode(cert).finish()
}
}
export const encodeCertificate = (cert: VaccineCertificate | SignedVaccineCertificate): Buffer => {
return zlib.brotliCompressSync(
Buffer.from(_baseEncodeCertificate(cert)),
{
params: [zlib.constants.BROTLI_PARAM_MODE, zlib.constants.BROTLI_MODE_GENERIC]
}
)
}
export const decodeSignedCertificate = (data: Buffer): SignedVaccineCertificate => {
return SignedVaccineCertificate.decode(zlib.brotliDecompressSync(data))
}