-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
97 lines (92 loc) · 3.14 KB
/
index.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
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
import CelikApi, { CelikApiResponseStatus } from "./models/api/api";
import { CardType, CertificateType, SignatureID } from "./models/api/types";
export default class MUPCelikApi {
private api: CelikApi;
device: string = "";
constructor(
device = "",
cardType: CardType = CardType.DEFAULT,
private isInit = false
) {
this.api = new CelikApi();
this.start();
if (device) this.startReadSync(device, cardType);
}
private start(apiVersion = 3) {
const status = this.api.startup(apiVersion).status;
if (status !== CelikApiResponseStatus.OK) throw new Error(status);
this.isInit = true;
}
private startReadSync(device: string, cardType: CardType = CardType.DEFAULT) {
if (!device) return;
const status = this.api.beginRead(device, cardType).status;
if (status !== CelikApiResponseStatus.OK) throw new Error(status);
this.device = device;
}
async startRead(device: string, cardType: CardType = CardType.DEFAULT) {
return this.startReadSync(device, cardType);
}
async readAllData() {
const res = await Promise.all([
this.readDocumentData(),
this.readFixedPersonalData(),
this.readVariablePersonalData(),
this.readPortrait(),
]);
return {
...res[0],
...res[1],
...res[2],
portrait: res[3],
};
}
async readDocumentData() {
const res = this.api.readDocumentData();
if (res.status !== CelikApiResponseStatus.OK) throw new Error(res.status);
return res.data!;
}
async readFixedPersonalData() {
const res = this.api.readFixedPersonalData();
if (res.status !== CelikApiResponseStatus.OK) throw new Error(res.status);
return res.data!;
}
async readVariablePersonalData() {
const res = this.api.readVariablePersonalData();
if (res.status !== CelikApiResponseStatus.OK) throw new Error(res.status);
return res.data!;
}
async readPortrait(): Promise<Buffer> {
const res = this.api.readPortrait();
if (res.status !== CelikApiResponseStatus.OK) throw new Error(res.status);
return res.data!.portrait;
}
async readCertificate(certType: CertificateType): Promise<Buffer> {
const res = this.api.readCertificate(certType);
if (res.status !== CelikApiResponseStatus.OK) throw new Error(res.status);
return res.data!.certificate;
}
async verifyAllSignatures() {
await this.verifySignature(SignatureID.CARD);
await this.verifySignature(SignatureID.FIXED);
await this.verifySignature(SignatureID.VARIABLE);
console.log("ALL VERIFIED!");
}
async verifySignature(sigId: SignatureID) {
const status = this.api.verifySignature(sigId).status;
if (status !== CelikApiResponseStatus.OK) throw new Error(status);
}
async endRead() {
if (!this.device) return;
const status = this.api.endRead().status;
if (status !== CelikApiResponseStatus.OK) throw new Error(status);
this.device = "";
}
async end() {
await this.endRead();
if (!this.isInit) return;
const status = this.api.cleanup().status;
if (status !== CelikApiResponseStatus.OK) throw new Error(status);
this.isInit = false;
}
}
export { MUPCelikApi, CardType, SignatureID, CertificateType };