-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexample.ts
165 lines (136 loc) · 6.29 KB
/
example.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
// example for typescript library TsCard
// https://github.com/lewixlabs/TsCard
// command to use:
// tsc --target ES5 example.ts
import { TsCard } from './index';
import Reader, { ApduResponse } from './reader';
import SmartCard from './cards/smartcard';
import { Sle } from './cards/memorycard';
import Utilities from './utilities';
class Example {
static async eventTest(){
console.log("Event example started...");
let tsPcsc = TsCard.instance;
console.log("Waiting for reader plugged...");
try {
let cardReader : Reader = await tsPcsc.detectReader(15000);
if (cardReader != null) {
console.log(`Reader detected:${cardReader.name}`);
tsPcsc.onCardEvent((ev,crd,error) => {
console.log(`CardEvent: ${ev}`);
});
}
}
catch (error) {
console.log(`Error!\n${error}`)
}
finally {
setTimeout(() => {
tsPcsc.close();
console.log("Event Example completed...");
},15000);
}
}
static async main() {
console.log("New example started...");
let tsPcsc = TsCard.instance;
console.log("Waiting for reader plugged...");
try {
let cardReader : Reader = await tsPcsc.detectReader(15000);
if (cardReader != null) {
console.log(`Reader detected:${cardReader.name}`);
let cardInfo : [boolean , SmartCard?] = await tsPcsc.insertCard(15000);
if (cardInfo[0]){
let atrHex : string = "";
cardInfo[1].atr.map(val => {
atrHex += val.toString(16);
});
console.log(`Smartcard inserted:${cardInfo["0"]}\nSmartCard ATR: ${Utilities.bytesToHexString(cardInfo[1].atr)}`);
console.log(`Is SmartCard Object? ${cardInfo[1] instanceof SmartCard}`);
console.log(`Is Sle Object? ${cardInfo[1] instanceof Sle}`);
console.log(`Is Memory Card:${cardInfo[1].isMemoryCard}`)
}
else
console.log(`Smartcard inserted:${cardInfo["0"]}`);
if (!cardInfo[1].isMemoryCard) {
console.log("Select DF Name...");
let buffer : Array<number> = [0xA0, 0x00, 0x00, 0x05, 0x73, 0x54, 0x52, 0x45, 0x53, 0x59 ];
//let buffer : Array<number> = [0xA0, 0x56, 0x96, 0x00, 0x00, 0x00, 0x01];
let apduResult : ApduResponse = await cardReader.sendApdu(
cardInfo[1],
{
Cla: 0x00,
Ins: 0xA4,
P1: 0x04,
P2: 0x00,
Le: 0x28,
Lc: buffer.length
},
buffer
);
console.log(`SW: ${Utilities.bytesToHexString(apduResult.SW)}\nData Received: ${Utilities.bytesToHexString(apduResult.Data)}\n`);
console.log("Read DF Info...")
//buffer = [0x6F ,0x26 ,0x84 ,0x0A ,0xA0 ,0x00 ,0x00 ,0x05 ,0x73 ,0x54 ,0x52 ,0x45 ,0x53 ,0x59 ,0xA5 ,0x18 ,0xA6 ,0x05 ,0x56 ,0x41 ,0x41 ,0x35 ,0x30 ,0xA7 ,0x05 ,0x54 ,0x52 ,0x45 ,0x53 ,0x54 ,0xA8 ,0x01 ,0x01 ,0xA9 ,0x01 ,0x54 ,0xAA ,0x02 ,0x03 ,0x01];
apduResult = await cardReader.sendApdu(
cardInfo[1],
{
Cla: 0x00,
Ins: 0xC0,
P1: 0x00,
P2: 0x00,
Le: 0x28,
Lc: 0x28
},
null
);
console.log(`SW: ${Utilities.bytesToHexString(apduResult.SW)}\nData Received: ${Utilities.bytesToHexString(apduResult.Data)}\n`);
console.log("Read Record?..")
apduResult = await cardReader.sendApdu(
cardInfo[1],
{
Cla: 0x00,
Ins: 0xB2,
P1: 0x01,
P2: 0x24,
Le: 0x16,
Lc: 0
},
null
);
console.log(`SW: ${Utilities.bytesToHexString(apduResult.SW)}\nData Received: ${Utilities.bytesToHexString(apduResult.Data)}\n`);
} else {
if (cardInfo[1] instanceof Sle){
const bytesToRead : number = 224;
console.log(`reading ${bytesToRead} bytes from offset #32...`);
let mySle = cardInfo[1] as Sle;
let retRead = await mySle.readBytes(32, bytesToRead);
console.log(`SLE Read Result: ${retRead[0]}\nBytes Read: ${Utilities.bytesToHexString(retRead[1]).toUpperCase()}`);
/*
console.log("verify PIN");
let myPIN : Array<number> = [ 0x12, 0x34 ];
let retVerify = await mySle.verifyPIN(myPIN);
console.log(`SLE Verify Result: ${retVerify[0]}\nError Counter: ${Utilities.bytesToHexString([retVerify[1]])}`);
console.log("writing 4 bytes from offset #32...");
let myBuffer : Array<number> = [ 0xAB, 0xCD, 0xDE, 0xF0 ];
let retWrite = await mySle.writeBytes(32,myBuffer);
console.log(`SLE Write Result: ${retWrite}`);
*/
}
}
}
}
catch (error) {
console.log(`Error!\n${error}`)
}
finally {
// setTimeout(() => console.log("Time elapsed!!"),15000);
// return;
tsPcsc.close();
console.log("Example completed...");
}
console.log("Reader closed");
}
}
//Example.eventTest();
Example.main();
//Example.eventTest();