-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cardmanager.ts
184 lines (138 loc) · 5.92 KB
/
cardmanager.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
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
import { TsCard } from "tscard";
import { PINStatus, Sle, MemoryCardTypes } from "tscard/cards/memorycard";
import SmartCard, { CardEvent } from "tscard/cards/smartcard";
import Reader, { Apdu, ApduResponse } from "tscard/reader";
import Utilities from "tscard/utilities";
export enum CardEvents {
CardInserted = CardEvent.Inserted,
CardRemoved = CardEvent.Removed,
}
export interface ICardInfo {
atr: string;
cardType: string;
memoryCardDetails: string;
}
export interface ICardManagerStatus {
isActive: boolean;
readerName: string;
isCardInserted: boolean;
cardInfo?: ICardInfo;
}
export class CardManager {
public static async openCardReader(): Promise<ICardManagerStatus> {
try {
const result: ICardManagerStatus = {
cardInfo: null,
isActive: this.cardManagerActive,
isCardInserted: false,
readerName: "No Reader Detected",
};
this.actualReader = await TsCard.instance.detectReader(5000);
if (this.actualReader == null || this.actualReader.name === "") {
return result;
}
this.cardManagerActive = true;
const [cardInserted, card] = await TsCard.instance.insertCard(3000);
result.readerName = this.actualReader.name;
result.isCardInserted = cardInserted;
if (cardInserted) {
let sleDetails: string = "";
this.actualCard = card;
if (this.actualCard instanceof Sle) {
const sleCard = this.actualCard as Sle;
sleDetails = sleCard.type == MemoryCardTypes.SLE5528 ? "SLEXX28" : "SLEXX42";
}
result.cardInfo = {
atr: Utilities.bytesToHexString(card.atr),
cardType: card.isMemoryCard ? "Memory Card" : "Chip Card",
memoryCardDetails: sleDetails,
};
}
result.isActive = this.cardManagerActive;
return result;
} catch (e) {
return {
cardInfo: null,
isActive: false,
isCardInserted: false,
readerName: "No Reader Detected",
};
}
}
public static listenCardEvents(f: (cardEvent: CardEvents, cardInfo: ICardInfo) => void) {
TsCard.instance.onCardEvent((e: CardEvent, c: SmartCard, err: Error) => {
const atrDetected: string = c !== null ? Utilities.bytesToHexString(c.atr) : "";
const isMemoryCardDetected: boolean = c !== null ? c.isMemoryCard : false;
this.actualCard = c;
let sleDetails: string = "";
if (this.actualCard instanceof Sle) {
const sleCard = this.actualCard as Sle;
sleDetails = sleCard.type == MemoryCardTypes.SLE5528 ? "SLEXX28" : "SLEXX42";
}
// tslint:disable-next-line: max-line-length
f(e === CardEvent.Inserted ? CardEvents.CardInserted : CardEvents.CardRemoved, { atr: atrDetected, cardType: isMemoryCardDetected ? "Memory Card" : "Chip Card", memoryCardDetails: sleDetails });
});
}
public static closeCardReader() {
TsCard.instance.close();
}
public static async sendApdu(apdu: Apdu, dataIn: number[]): Promise<[ApduResponse, string]> {
try {
return [await this.actualReader.sendApdu(this.actualCard, apdu, dataIn), ""];
} catch (e) {
return [null, e];
}
}
//#region Memory Cards
public static isSupportedMemoryCard(): boolean {
if (!this.actualCard)
return false;
if (!this.actualCard.isMemoryCard)
return false;
return this.actualCard instanceof Sle;
}
public static getMemoryCardType(): MemoryCardTypes {
if (!this.actualCard)
return null;
if (!this.actualCard.isMemoryCard)
return null;
const currentSle: Sle = this.actualCard as Sle;
return currentSle.type;
}
public static async readMemoryCard(startPosition: number, bytesToRead: number): Promise<[boolean, number[]]> {
if (!this.actualCard || !this.actualCard.isMemoryCard)
throw new Error("No MemoryCard Detected");
if (!(this.actualCard instanceof Sle))
throw new Error("MemoryCard Not Supported");
const sleCard = this.actualCard as Sle;
return sleCard.readBytes(startPosition, bytesToRead);
}
public static async writeMemoryCard(startPosition: number, bufferToWrite: number[]): Promise<boolean> {
if (!this.actualCard || !this.actualCard.isMemoryCard)
throw new Error("No MemoryCard Detected");
if (!(this.actualCard instanceof Sle))
throw new Error("MemoryCard Not Supported");
const sleCard = this.actualCard as Sle;
return sleCard.writeBytes(startPosition, bufferToWrite);
}
public static async verifyPSC(pinBuffer: number[]): Promise<[PINStatus, number]> {
if (!this.actualCard || !this.actualCard.isMemoryCard)
throw new Error("No MemoryCard Detected");
if (!(this.actualCard instanceof Sle))
throw new Error("MemoryCard Not Supported");
const currentSle: Sle = this.actualCard;
return await currentSle.verifyPIN(pinBuffer);
}
public static async updatePSC(pinBufferToUpdate: number[]): Promise<boolean> {
if (!this.actualCard || !this.actualCard.isMemoryCard)
throw new Error("No MemoryCard Detected");
if (!(this.actualCard instanceof Sle))
throw new Error("MemoryCard Not Supported");
const sleCard = this.actualCard as Sle;
return sleCard.changePIN(pinBufferToUpdate);
}
//#endregion
private static cardManagerActive: boolean = false;
private static actualReader: Reader = null;
private static actualCard: SmartCard = null;
}