iso7816 2.0.0 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -3
- package/dist/command-apdu.d.ts +25 -8
- package/dist/command-apdu.d.ts.map +1 -1
- package/dist/command-apdu.js +53 -10
- package/dist/command-apdu.js.map +1 -1
- package/dist/iso7816-application.d.ts +2 -1
- package/dist/iso7816-application.d.ts.map +1 -1
- package/dist/iso7816-application.js +14 -7
- package/dist/iso7816-application.js.map +1 -1
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -134,7 +134,7 @@ Get additional response bytes.
|
|
|
134
134
|
|
|
135
135
|
### `application.issueCommand(commandApdu, maxRetries?)`
|
|
136
136
|
|
|
137
|
-
Issue a raw APDU command.
|
|
137
|
+
Issue a raw APDU command. When the card answers `61xx` (more data available), this sends GET RESPONSE until the card has sent everything, and returns all the data with the final status word.
|
|
138
138
|
|
|
139
139
|
- `commandApdu` - A `CommandApdu` instance
|
|
140
140
|
- `maxRetries` - Maximum retries for wrong length responses (default: `3`)
|
|
@@ -149,8 +149,8 @@ const apdu = createCommandApdu({
|
|
|
149
149
|
ins: 0xa4, // Instruction byte
|
|
150
150
|
p1: 0x04, // Parameter 1
|
|
151
151
|
p2: 0x00, // Parameter 2
|
|
152
|
-
data: [...], // Optional command data
|
|
153
|
-
le: 0x00, // Optional expected response length
|
|
152
|
+
data: [...], // Optional command data, up to 65535 bytes
|
|
153
|
+
le: 0x00, // Optional expected response length, 0-65536
|
|
154
154
|
});
|
|
155
155
|
|
|
156
156
|
apdu.toString(); // Hex string
|
|
@@ -159,6 +159,10 @@ apdu.toByteArray(); // number[]
|
|
|
159
159
|
apdu.setLe(0x10); // Update expected length
|
|
160
160
|
```
|
|
161
161
|
|
|
162
|
+
The command always includes Le. `le: 0`, the default, asks for the maximum: 256 bytes, or 65536 in the extended form. The command uses the short form when the data is at most 255 bytes and `le` is at most 256, and the ISO 7816-4 extended length form otherwise.
|
|
163
|
+
|
|
164
|
+
A value that can't be encoded, such as a `p1` of 256 or a negative `le`, throws a `RangeError` instead of being silently truncated.
|
|
165
|
+
|
|
162
166
|
### `createResponseApdu(buffer)`
|
|
163
167
|
|
|
164
168
|
Parse a response APDU.
|
package/dist/command-apdu.d.ts
CHANGED
|
@@ -2,23 +2,38 @@
|
|
|
2
2
|
* Options for creating a CommandApdu
|
|
3
3
|
*/
|
|
4
4
|
export interface CommandApduOptions {
|
|
5
|
-
/** Class byte */
|
|
5
|
+
/** Class byte (0-255) */
|
|
6
6
|
cla: number;
|
|
7
|
-
/** Instruction byte */
|
|
7
|
+
/** Instruction byte (0-255) */
|
|
8
8
|
ins: number;
|
|
9
|
-
/** Parameter 1 */
|
|
9
|
+
/** Parameter 1 (0-255) */
|
|
10
10
|
p1: number;
|
|
11
|
-
/** Parameter 2 */
|
|
11
|
+
/** Parameter 2 (0-255) */
|
|
12
12
|
p2: number;
|
|
13
|
-
/** Command data */
|
|
13
|
+
/** Command data, up to 65535 bytes. More than 255 bytes uses the extended length form. */
|
|
14
14
|
data?: number[];
|
|
15
|
-
/**
|
|
15
|
+
/**
|
|
16
|
+
* Expected response length (Ne), from 0 to 65536. The default, 0, asks for the maximum: 256,
|
|
17
|
+
* or 65536 in the extended form. More than 256 uses the extended length form.
|
|
18
|
+
*/
|
|
16
19
|
le?: number;
|
|
17
20
|
}
|
|
18
21
|
/**
|
|
19
|
-
* APDU Command class for building ISO 7816 command APDUs
|
|
22
|
+
* APDU Command class for building ISO 7816 command APDUs.
|
|
23
|
+
*
|
|
24
|
+
* The command always includes Le. It uses the short form when the data is at most 255 bytes and
|
|
25
|
+
* Le is at most 256, and the extended form otherwise (ISO/IEC 7816-4):
|
|
26
|
+
*
|
|
27
|
+
* short CLA INS P1 P2 [Lc data] Le
|
|
28
|
+
* extended CLA INS P1 P2 00 Le1 Le2 (no data)
|
|
29
|
+
* CLA INS P1 P2 00 Lc1 Lc2 data Le1 Le2 (with data)
|
|
30
|
+
*
|
|
31
|
+
* A value that can't be encoded throws a RangeError instead of being truncated. The full
|
|
32
|
+
* specification is in command-apdu.properties.test.ts.
|
|
20
33
|
*/
|
|
21
34
|
export declare class CommandApdu {
|
|
35
|
+
private readonly _header;
|
|
36
|
+
private readonly _data;
|
|
22
37
|
private _bytes;
|
|
23
38
|
constructor(options: CommandApduOptions);
|
|
24
39
|
/**
|
|
@@ -38,7 +53,9 @@ export declare class CommandApdu {
|
|
|
38
53
|
*/
|
|
39
54
|
toBuffer(): Buffer;
|
|
40
55
|
/**
|
|
41
|
-
* Update expected response length (Le)
|
|
56
|
+
* Update expected response length (Le). Re-encodes the command, so a Le over 256 switches
|
|
57
|
+
* it to the extended form. Throws a RangeError, leaving the command unchanged, if `le` is
|
|
58
|
+
* not an integer from 0 to 65536.
|
|
42
59
|
*/
|
|
43
60
|
setLe(le: number): void;
|
|
44
61
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"command-apdu.d.ts","sourceRoot":"","sources":["../src/command-apdu.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,WAAW,kBAAkB;IAC/B,
|
|
1
|
+
{"version":3,"file":"command-apdu.d.ts","sourceRoot":"","sources":["../src/command-apdu.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,WAAW,kBAAkB;IAC/B,yBAAyB;IACzB,GAAG,EAAE,MAAM,CAAC;IACZ,+BAA+B;IAC/B,GAAG,EAAE,MAAM,CAAC;IACZ,0BAA0B;IAC1B,EAAE,EAAE,MAAM,CAAC;IACX,0BAA0B;IAC1B,EAAE,EAAE,MAAM,CAAC;IACX,0FAA0F;IAC1F,IAAI,CAAC,EAAE,MAAM,EAAE,CAAC;IAChB;;;OAGG;IACH,EAAE,CAAC,EAAE,MAAM,CAAC;CACf;AAKD;;;;;;;;;;;;GAYG;AACH,qBAAa,WAAW;IACpB,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAW;IACnC,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAW;IACjC,OAAO,CAAC,MAAM,CAAW;gBAEb,OAAO,EAAE,kBAAkB;IAmBvC;;OAEG;IACH,IAAI,KAAK,IAAI,MAAM,EAAE,CAEpB;IAED;;OAEG;IACH,QAAQ,IAAI,MAAM;IAIlB;;OAEG;IACH,WAAW,IAAI,MAAM,EAAE;IAIvB;;OAEG;IACH,QAAQ,IAAI,MAAM;IAIlB;;;;OAIG;IACH,KAAK,CAAC,EAAE,EAAE,MAAM,GAAG,IAAI;CAG1B;AA6BD;;GAEG;AACH,MAAM,CAAC,OAAO,UAAU,iBAAiB,CAAC,OAAO,EAAE,kBAAkB,GAAG,WAAW,CAElF"}
|
package/dist/command-apdu.js
CHANGED
|
@@ -1,16 +1,36 @@
|
|
|
1
|
+
const MAX_DATA_LENGTH = 65535;
|
|
2
|
+
const MAX_LE = 65536;
|
|
1
3
|
/**
|
|
2
|
-
* APDU Command class for building ISO 7816 command APDUs
|
|
4
|
+
* APDU Command class for building ISO 7816 command APDUs.
|
|
5
|
+
*
|
|
6
|
+
* The command always includes Le. It uses the short form when the data is at most 255 bytes and
|
|
7
|
+
* Le is at most 256, and the extended form otherwise (ISO/IEC 7816-4):
|
|
8
|
+
*
|
|
9
|
+
* short CLA INS P1 P2 [Lc data] Le
|
|
10
|
+
* extended CLA INS P1 P2 00 Le1 Le2 (no data)
|
|
11
|
+
* CLA INS P1 P2 00 Lc1 Lc2 data Le1 Le2 (with data)
|
|
12
|
+
*
|
|
13
|
+
* A value that can't be encoded throws a RangeError instead of being truncated. The full
|
|
14
|
+
* specification is in command-apdu.properties.test.ts.
|
|
3
15
|
*/
|
|
4
16
|
export class CommandApdu {
|
|
17
|
+
_header;
|
|
18
|
+
_data;
|
|
5
19
|
_bytes;
|
|
6
20
|
constructor(options) {
|
|
7
|
-
const { cla, ins, p1, p2,
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
21
|
+
const { cla, ins, p1, p2, le = 0 } = options;
|
|
22
|
+
const data = options.data ?? [];
|
|
23
|
+
this._header = [
|
|
24
|
+
checkByte('cla', cla),
|
|
25
|
+
checkByte('ins', ins),
|
|
26
|
+
checkByte('p1', p1),
|
|
27
|
+
checkByte('p2', p2),
|
|
28
|
+
];
|
|
29
|
+
this._data = Array.from(data, (b, i) => checkByte(`data[${i}]`, b));
|
|
30
|
+
if (this._data.length > MAX_DATA_LENGTH) {
|
|
31
|
+
throw new RangeError(`data is ${this._data.length} bytes; the maximum is ${MAX_DATA_LENGTH}`);
|
|
12
32
|
}
|
|
13
|
-
this._bytes.
|
|
33
|
+
this._bytes = encode(this._header, this._data, checkLe(le));
|
|
14
34
|
}
|
|
15
35
|
/**
|
|
16
36
|
* Get the bytes array (read-only copy)
|
|
@@ -37,13 +57,36 @@ export class CommandApdu {
|
|
|
37
57
|
return Buffer.from(this._bytes);
|
|
38
58
|
}
|
|
39
59
|
/**
|
|
40
|
-
* Update expected response length (Le)
|
|
60
|
+
* Update expected response length (Le). Re-encodes the command, so a Le over 256 switches
|
|
61
|
+
* it to the extended form. Throws a RangeError, leaving the command unchanged, if `le` is
|
|
62
|
+
* not an integer from 0 to 65536.
|
|
41
63
|
*/
|
|
42
64
|
setLe(le) {
|
|
43
|
-
this._bytes.
|
|
44
|
-
this._bytes.push(le);
|
|
65
|
+
this._bytes = encode(this._header, this._data, checkLe(le));
|
|
45
66
|
}
|
|
46
67
|
}
|
|
68
|
+
function checkByte(name, value) {
|
|
69
|
+
if (!Number.isInteger(value) || value < 0 || value > 0xff) {
|
|
70
|
+
throw new RangeError(`${name} must be a byte (0-255), got ${value}`);
|
|
71
|
+
}
|
|
72
|
+
return value;
|
|
73
|
+
}
|
|
74
|
+
function checkLe(le) {
|
|
75
|
+
if (!Number.isInteger(le) || le < 0 || le > MAX_LE) {
|
|
76
|
+
throw new RangeError(`le must be an integer from 0 to ${MAX_LE}, got ${le}`);
|
|
77
|
+
}
|
|
78
|
+
return le;
|
|
79
|
+
}
|
|
80
|
+
/** Encodes a validated command. An Le of 256 (short) or 65536 (extended) encodes as zeros. */
|
|
81
|
+
function encode(header, data, le) {
|
|
82
|
+
const nc = data.length;
|
|
83
|
+
if (nc <= 255 && le <= 256) {
|
|
84
|
+
const lc = nc > 0 ? [nc, ...data] : [];
|
|
85
|
+
return [...header, ...lc, le & 0xff];
|
|
86
|
+
}
|
|
87
|
+
const lc = nc > 0 ? [0x00, nc >> 8, nc & 0xff, ...data] : [0x00];
|
|
88
|
+
return [...header, ...lc, (le >> 8) & 0xff, le & 0xff];
|
|
89
|
+
}
|
|
47
90
|
/**
|
|
48
91
|
* Factory function to create a CommandApdu
|
|
49
92
|
*/
|
package/dist/command-apdu.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"command-apdu.js","sourceRoot":"","sources":["../src/command-apdu.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"command-apdu.js","sourceRoot":"","sources":["../src/command-apdu.ts"],"names":[],"mappings":"AAqBA,MAAM,eAAe,GAAG,KAAK,CAAC;AAC9B,MAAM,MAAM,GAAG,KAAK,CAAC;AAErB;;;;;;;;;;;;GAYG;AACH,MAAM,OAAO,WAAW;IACH,OAAO,CAAW;IAClB,KAAK,CAAW;IACzB,MAAM,CAAW;IAEzB,YAAY,OAA2B;QACnC,MAAM,EAAE,GAAG,EAAE,GAAG,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,GAAG,CAAC,EAAE,GAAG,OAAO,CAAC;QAC7C,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI,IAAI,EAAE,CAAC;QAEhC,IAAI,CAAC,OAAO,GAAG;YACX,SAAS,CAAC,KAAK,EAAE,GAAG,CAAC;YACrB,SAAS,CAAC,KAAK,EAAE,GAAG,CAAC;YACrB,SAAS,CAAC,IAAI,EAAE,EAAE,CAAC;YACnB,SAAS,CAAC,IAAI,EAAE,EAAE,CAAC;SACtB,CAAC;QACF,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC,IAAI,CAAC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43
43
|
*/
|
|
44
44
|
get card(): Card;
|
|
45
45
|
/**
|
|
46
|
-
* Issue a raw APDU command
|
|
46
|
+
* Issue a raw APDU command. When the card answers 61xx, sends GET RESPONSE until it has
|
|
47
|
+
* everything, and returns all the data with the final status word.
|
|
47
48
|
* @param commandApdu The command to send
|
|
48
49
|
* @param maxRetries Maximum number of retries for wrong length responses (default: 3)
|
|
49
50
|
* @returns Promise resolving to the response
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"iso7816-application.d.ts","sourceRoot":"","sources":["../src/iso7816-application.ts"],"names":[],"mappings":"AAAA,OAA0B,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AACnE,OAA2B,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAEtE;;GAEG;AACH,eAAO,MAAM,YAAY;;;;;;;;;;;;;;;;;;;CAmBf,CAAC;AAEX;;GAEG;AACH,MAAM,WAAW,IAAI;IACjB,iCAAiC;IACjC,GAAG,EAAE,MAAM,CAAC;IACZ,oCAAoC;IACpC,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;CACtD;AAED;;GAEG;AACH,qBAAa,OAAO;IAChB,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAO;gBAEjB,IAAI,EAAE,IAAI;IAItB;;OAEG;IACH,IAAI,IAAI,IAAI,IAAI,CAEf;IAED
|
|
1
|
+
{"version":3,"file":"iso7816-application.d.ts","sourceRoot":"","sources":["../src/iso7816-application.ts"],"names":[],"mappings":"AAAA,OAA0B,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AACnE,OAA2B,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAEtE;;GAEG;AACH,eAAO,MAAM,YAAY;;;;;;;;;;;;;;;;;;;CAmBf,CAAC;AAEX;;GAEG;AACH,MAAM,WAAW,IAAI;IACjB,iCAAiC;IACjC,GAAG,EAAE,MAAM,CAAC;IACZ,oCAAoC;IACpC,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;CACtD;AAED;;GAEG;AACH,qBAAa,OAAO;IAChB,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAO;gBAEjB,IAAI,EAAE,IAAI;IAItB;;OAEG;IACH,IAAI,IAAI,IAAI,IAAI,CAEf;IAED;;;;;;;OAOG;IACG,YAAY,CAAC,WAAW,EAAE,WAAW,EAAE,UAAU,GAAE,MAAU,GAAG,OAAO,CAAC,YAAY,CAAC;IAgC3F;;;;;;OAMG;IACG,UAAU,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,EAAE,GAAE,MAAa,EAAE,EAAE,GAAE,MAAa,GAAG,OAAO,CAAC,YAAY,CAAC;IAY9F;;;;OAIG;IACG,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,YAAY,CAAC;IAYxD;;;;;OAKG;IACG,UAAU,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,YAAY,CAAC;IAYpE;;;;;OAKG;IACG,OAAO,CAAC,EAAE,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,YAAY,CAAC;CAW/D;AAED;;;;GAIG;AACH,MAAM,CAAC,OAAO,UAAU,aAAa,CAAC,IAAI,EAAE,IAAI,GAAG,OAAO,CAEzD;AAGD,OAAO,EAAE,WAAW,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AACpE,OAAO,EAAE,YAAY,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC"}
|
|
@@ -38,7 +38,8 @@ export class Iso7816 {
|
|
|
38
38
|
return this._card;
|
|
39
39
|
}
|
|
40
40
|
/**
|
|
41
|
-
* Issue a raw APDU command
|
|
41
|
+
* Issue a raw APDU command. When the card answers 61xx, sends GET RESPONSE until it has
|
|
42
|
+
* everything, and returns all the data with the final status word.
|
|
42
43
|
* @param commandApdu The command to send
|
|
43
44
|
* @param maxRetries Maximum number of retries for wrong length responses (default: 3)
|
|
44
45
|
* @returns Promise resolving to the response
|
|
@@ -50,14 +51,20 @@ export class Iso7816 {
|
|
|
50
51
|
const resp = await this._card.transmit(commandApdu.toBuffer());
|
|
51
52
|
const response = createResponseApdu(resp);
|
|
52
53
|
if (response.hasMoreBytesAvailable()) {
|
|
53
|
-
|
|
54
|
+
// Keep any data that came with this response, then add the rest. getResponse
|
|
55
|
+
// follows further 61xx responses the same way.
|
|
56
|
+
const rest = await this.getResponse(response.numberOfBytesAvailable());
|
|
57
|
+
const data = response.getBuffer().subarray(0, -2);
|
|
58
|
+
return createResponseApdu(Buffer.concat([data, rest.getBuffer()]));
|
|
54
59
|
}
|
|
55
60
|
else if (response.isWrongLength()) {
|
|
56
61
|
if (retries >= maxRetries) {
|
|
57
62
|
throw new Error(`Maximum retries (${maxRetries}) exceeded for wrong length response`);
|
|
58
63
|
}
|
|
59
64
|
retries++;
|
|
60
|
-
|
|
65
|
+
// SW2 of 00 means exactly 256. setLe(0) would ask for the maximum, which is
|
|
66
|
+
// 65536 for an extended command.
|
|
67
|
+
commandApdu.setLe(response.correctLength() || 256);
|
|
61
68
|
return execute();
|
|
62
69
|
}
|
|
63
70
|
return response;
|
|
@@ -71,7 +78,7 @@ export class Iso7816 {
|
|
|
71
78
|
* @param p2 P2 parameter (default: 0x00)
|
|
72
79
|
* @returns Promise resolving to the response
|
|
73
80
|
*/
|
|
74
|
-
selectFile(bytes, p1 = 0x04, p2 = 0x00) {
|
|
81
|
+
async selectFile(bytes, p1 = 0x04, p2 = 0x00) {
|
|
75
82
|
return this.issueCommand(createCommandApdu({
|
|
76
83
|
cla: 0x00,
|
|
77
84
|
ins: Instructions.SELECT_FILE,
|
|
@@ -85,7 +92,7 @@ export class Iso7816 {
|
|
|
85
92
|
* @param length Number of bytes to retrieve
|
|
86
93
|
* @returns Promise resolving to the response
|
|
87
94
|
*/
|
|
88
|
-
getResponse(length) {
|
|
95
|
+
async getResponse(length) {
|
|
89
96
|
return this.issueCommand(createCommandApdu({
|
|
90
97
|
cla: 0x00,
|
|
91
98
|
ins: Instructions.GET_RESPONSE,
|
|
@@ -100,7 +107,7 @@ export class Iso7816 {
|
|
|
100
107
|
* @param record Record number
|
|
101
108
|
* @returns Promise resolving to the response
|
|
102
109
|
*/
|
|
103
|
-
readRecord(sfi, record) {
|
|
110
|
+
async readRecord(sfi, record) {
|
|
104
111
|
return this.issueCommand(createCommandApdu({
|
|
105
112
|
cla: 0x00,
|
|
106
113
|
ins: Instructions.READ_RECORD,
|
|
@@ -115,7 +122,7 @@ export class Iso7816 {
|
|
|
115
122
|
* @param p2 P2 parameter
|
|
116
123
|
* @returns Promise resolving to the response
|
|
117
124
|
*/
|
|
118
|
-
getData(p1, p2) {
|
|
125
|
+
async getData(p1, p2) {
|
|
119
126
|
return this.issueCommand(createCommandApdu({
|
|
120
127
|
cla: 0x00,
|
|
121
128
|
ins: Instructions.GET_DATA,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"iso7816-application.js","sourceRoot":"","sources":["../src/iso7816-application.ts"],"names":[],"mappings":"AAAA,OAAO,iBAAkC,MAAM,mBAAmB,CAAC;AACnE,OAAO,kBAAoC,MAAM,oBAAoB,CAAC;AAEtE;;GAEG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG;IACxB,aAAa,EAAE,IAAI;IACnB,QAAQ,EAAE,IAAI;IACd,YAAY,EAAE,IAAI;IAClB,qBAAqB,EAAE,IAAI;IAC3B,aAAa,EAAE,IAAI;IACnB,QAAQ,EAAE,IAAI;IACd,YAAY,EAAE,IAAI;IAClB,qBAAqB,EAAE,IAAI;IAC3B,cAAc,EAAE,IAAI;IACpB,QAAQ,EAAE,IAAI;IACd,WAAW,EAAE,IAAI;IACjB,WAAW,EAAE,IAAI;IACjB,WAAW,EAAE,IAAI;IACjB,aAAa,EAAE,IAAI;IACnB,aAAa,EAAE,IAAI;IACnB,MAAM,EAAE,IAAI;IACZ,YAAY,EAAE,IAAI;IAClB,YAAY,EAAE,IAAI;CACZ,CAAC;AAYX;;GAEG;AACH,MAAM,OAAO,OAAO;IACC,KAAK,CAAO;IAE7B,YAAY,IAAU;QAClB,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC;IACtB,CAAC;IAED;;OAEG;IACH,IAAI,IAAI;QACJ,OAAO,IAAI,CAAC,KAAK,CAAC;IACtB,CAAC;IAED
|
|
1
|
+
{"version":3,"file":"iso7816-application.js","sourceRoot":"","sources":["../src/iso7816-application.ts"],"names":[],"mappings":"AAAA,OAAO,iBAAkC,MAAM,mBAAmB,CAAC;AACnE,OAAO,kBAAoC,MAAM,oBAAoB,CAAC;AAEtE;;GAEG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG;IACxB,aAAa,EAAE,IAAI;IACnB,QAAQ,EAAE,IAAI;IACd,YAAY,EAAE,IAAI;IAClB,qBAAqB,EAAE,IAAI;IAC3B,aAAa,EAAE,IAAI;IACnB,QAAQ,EAAE,IAAI;IACd,YAAY,EAAE,IAAI;IAClB,qBAAqB,EAAE,IAAI;IAC3B,cAAc,EAAE,IAAI;IACpB,QAAQ,EAAE,IAAI;IACd,WAAW,EAAE,IAAI;IACjB,WAAW,EAAE,IAAI;IACjB,WAAW,EAAE,IAAI;IACjB,aAAa,EAAE,IAAI;IACnB,aAAa,EAAE,IAAI;IACnB,MAAM,EAAE,IAAI;IACZ,YAAY,EAAE,IAAI;IAClB,YAAY,EAAE,IAAI;CACZ,CAAC;AAYX;;GAEG;AACH,MAAM,OAAO,OAAO;IACC,KAAK,CAAO;IAE7B,YAAY,IAAU;QAClB,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC;IACtB,CAAC;IAED;;OAEG;IACH,IAAI,IAAI;QACJ,OAAO,IAAI,CAAC,KAAK,CAAC;IACtB,CAAC;IAED;;;;;;;OAOG;IACH,KAAK,CAAC,YAAY,CAAC,WAAwB,EAAE,aAAqB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CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "iso7816",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"description": "ISO 7816 APDU command and response handling library",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -58,6 +58,7 @@
|
|
|
58
58
|
"@vitest/coverage-v8": "^4.0.16",
|
|
59
59
|
"eslint": "^9.39.2",
|
|
60
60
|
"eslint-config-prettier": "^10.1.8",
|
|
61
|
+
"fast-check": "^4.10.2",
|
|
61
62
|
"prettier": "^3.7.4",
|
|
62
63
|
"typescript": "^5.7.0",
|
|
63
64
|
"typescript-eslint": "^8.50.1",
|