@gwydion-id/icao-9303-models 0.1.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.
Files changed (62) hide show
  1. package/LICENSES/CC0-1.0.txt +121 -0
  2. package/LICENSES/LGPL-3.0-or-later.txt +304 -0
  3. package/README.md +79 -0
  4. package/package.json +17 -0
  5. package/package.json.license +3 -0
  6. package/src/digitalseal.js +234 -0
  7. package/src/digitalsealv3.js +359 -0
  8. package/src/digitalsealv4.js +369 -0
  9. package/src/index.js +41 -0
  10. package/src/mrvadocument.js +465 -0
  11. package/src/mrvadocumentwithvds3.js +559 -0
  12. package/src/mrvadocumentwithvds4.js +559 -0
  13. package/src/mrvbdocument.js +466 -0
  14. package/src/mrvbdocumentwithvds3.js +560 -0
  15. package/src/mrvbdocumentwithvds4.js +560 -0
  16. package/src/td1document.js +408 -0
  17. package/src/td1documentwithvds3.js +479 -0
  18. package/src/td1documentwithvds4.js +479 -0
  19. package/src/td2document.js +379 -0
  20. package/src/td2documentwithvds3.js +466 -0
  21. package/src/td2documentwithvds4.js +466 -0
  22. package/src/td3document.js +391 -0
  23. package/src/td3documentwithvds3.js +466 -0
  24. package/src/td3documentwithvds4.js +466 -0
  25. package/src/traveldocument.js +306 -0
  26. package/src/utilities/bytes-to-date.js +23 -0
  27. package/src/utilities/c40-decode.js +225 -0
  28. package/src/utilities/c40-encode.js +254 -0
  29. package/src/utilities/c40-shift1.js +10 -0
  30. package/src/utilities/date-to-bytes.js +38 -0
  31. package/src/utilities/der-length-to-length.js +33 -0
  32. package/src/utilities/expand-year.js +26 -0
  33. package/src/utilities/gender-marker-to-mrz.js +14 -0
  34. package/src/utilities/generate-mrz-check-digit.js +139 -0
  35. package/src/utilities/icao-date.js +234 -0
  36. package/src/utilities/length-to-der-length.js +41 -0
  37. package/src/utilities/nationality-codes.js +303 -0
  38. package/src/utilities/normalize-mrz-string.js +19 -0
  39. package/src/utilities/optional-data-mrz.js +29 -0
  40. package/src/utilities/pad-mrz-string.js +16 -0
  41. package/src/utilities/set-signature-zone.js +34 -0
  42. package/src/utilities/stringify-validation-array.js +27 -0
  43. package/src/utilities/validate-hex-string.js +41 -0
  44. package/src/utilities/validate-identifier-code.js +38 -0
  45. package/src/utilities/validate-mrz-string.js +44 -0
  46. package/src/utilities/vds-magic.js +9 -0
  47. package/src/utilities/vds-signature-marker.js +9 -0
  48. package/src/utilities/vds-version-3.js +9 -0
  49. package/src/utilities/vds-version-4.js +9 -0
  50. package/src/visadocument.js +114 -0
  51. package/tests/models/digitalsealv4.test.js +67 -0
  52. package/tests/models/mrvadocument.test.js +61 -0
  53. package/tests/models/mrvbdocument.test.js +61 -0
  54. package/tests/models/td1document.test.js +50 -0
  55. package/tests/models/td2document.test.js +49 -0
  56. package/tests/models/td3document.test.js +88 -0
  57. package/tests/utilities/c40-encodation-scheme.test.js +34 -0
  58. package/tests/utilities/der-tlv-length-conversion.test.js +99 -0
  59. package/tests/utilities/document-utilities.test.js +199 -0
  60. package/tests/utilities/icao-constants.test.js +35 -0
  61. package/tests/utilities/icao-date.test.js +141 -0
  62. package/tests/utilities/vds-date-conversions.test.js +28 -0
@@ -0,0 +1,379 @@
1
+ // SPDX-FileCopyrightText: 2026 Don Geronimo <https://sentamal.in>
2
+ // SPDX-License-Identifier: LGPL-3.0-or-later
3
+
4
+ import { TravelDocument } from "./traveldocument.js";
5
+ import { generateMRZCheckDigit } from "./utilities/generate-mrz-check-digit.js";
6
+ import { normalizeMRZString } from "./utilities/normalize-mrz-string.js";
7
+ import { optionalDataMRZ } from "./utilities/optional-data-mrz.js";
8
+ import { padMRZString } from "./utilities/pad-mrz-string.js";
9
+ import { genderMarkerToMRZ } from "./utilities/gender-marker-to-mrz.js";
10
+ import { IcaoDate } from "./utilities/icao-date.js";
11
+
12
+ /**
13
+ * Stores properties and methods for TD2-sized machine-readable travel documents
14
+ * (MRTDs) with machine-readable zones. The Icelandic identity card before
15
+ * December 2023 and some Emergency Travel Documents (ETDs) are examples.
16
+ *
17
+ * `TD2Document` may be used on its own or may be used to compose different
18
+ * kinds of MRTDs.
19
+ */
20
+ export class TD2Document {
21
+ /**
22
+ * Create a `TD2Document`.
23
+ * @param { Object } [opt] - An options object.
24
+ * @param { string } [opt.typeCode] - A 1-2 character string consisting of the
25
+ * characters A-Z, 0-9, ' ', or <. 'A', 'I', 'P', or 'V' are recommended
26
+ * for the first character.
27
+ * @param { string } [opt.authorityCode] - A 3-character string consisting of
28
+ * the characters A-Z, 0-9, ' ', or <. A code from ISO-3166-1,
29
+ * ICAO 9303-3, or these user-assigned ranges are recommended: AAA-AAZ,
30
+ * QMA-QZZ, XAA-XZZ, or ZZA-ZZZ.
31
+ * @param { string } [opt.primaryIdentifier] - The document holder's primary
32
+ * identifier.
33
+ * @param { string | null } [opt.primaryIdentifierNative] - The document
34
+ * holder's primary identifier in their native language.
35
+ * @param { string | null } [opt.secondaryIdentifer] - The document holder's
36
+ * secondary identifier.
37
+ * @param { string | null } [opt.secondaryIdentifierNative] - The document
38
+ * holder's secondary identifier in their native language.
39
+ * @param { string } [opt.number] - A string no longer than 9 characters
40
+ * consisting of the characters A-Z, 0-9, ' ', or <.
41
+ * @param { string } [opt.nationalityCode] - A 3-character string consisting
42
+ * of the characters A-Z, 0-9, ' ', or <. A code from ISO-3166-1,
43
+ * ICAO 9303-3, or these user-assigned ranges are recommended: AAA-AAZ,
44
+ * QMA-QZZ, XAA-XZZ, or ZZA-ZZZ.
45
+ * @param { string | Date | IcaoDate } [opt.birthDate] - A calendar date string in
46
+ * YYYY-MM-DD format, a `Date` object, or an `IcaoDate` object.
47
+ * @param { 'F' | 'M' | 'X' } [opt.genderMarker] - The character 'F', 'M', or 'X'.
48
+ * @param { string | Date | IcaoDate } [opt.expirationDate] - A calendar date string in
49
+ * YYYY-MM-DD format, a `Date` object, or an `IcaoDate` object.
50
+ * @param { string } [opt.optionalData] - Up to 7 characters. Valid characters
51
+ * are from the ranges A-Z, 0-9, ' ', or <.
52
+ * @param { string } [opt.mrzLine1] - A MRZ line string of a 36-character
53
+ * length.
54
+ * @param { string } [opt.mrzLine2] - A MRZ line string of a 36-character
55
+ * length.
56
+ * @param { string } [opt.machineReadableZone] - A MRZ string of a
57
+ * 72-character length.
58
+ * @param { string | HTMLImageElement | SVGImageElement | HTMLVideoElement |
59
+ * HTMLCanvasElement | ImageBitmap | OffscreenCanvas |
60
+ * VideoFrame } [opt.photo] - A path/URL to an image, or an image object,
61
+ * representing a photo of the document holder.
62
+ * @param { string | HTMLImageElement | SVGImageElement | HTMLVideoElement |
63
+ * HTMLCanvasElement | ImageBitmap | OffscreenCanvas |
64
+ * VideoFrame } [opt.signatureImage] - A path/URL to an image, or an image
65
+ * object, representing the signature or usual mark of the document
66
+ * holder.
67
+ */
68
+ constructor(opt) {
69
+ this.#document = new TravelDocument();
70
+
71
+ this.typeCode = opt?.typeCode ?? "I";
72
+ this.authorityCode = opt?.authorityCode ?? "UTO";
73
+ this.primaryIdentifier = opt?.primaryIdentifier ?? "Eriksson";
74
+ this.primaryIdentifierNative = opt?.primaryIdentifierNative ?? null;
75
+ this.secondaryIdentifier = opt?.secondaryIdentifier ?? null;
76
+ this.secondaryIdentifierNative = opt?.secondaryIdentifierNative ?? null;
77
+ this.number = opt?.number ?? "D23145890";
78
+ this.nationalityCode = opt?.nationalityCode ?? "UTO";
79
+ this.birthDate = opt?.birthDate ?? "1974-08-12";
80
+ this.genderMarker = opt?.genderMarker ?? "F";
81
+ this.expirationDate = opt?.expirationDate ?? "2012-04-15";
82
+ this.optionalData = opt?.optionalData ?? "";
83
+
84
+ if (opt?.photo) { this.photo = opt.photo; }
85
+ if (opt?.signatureImage) { this.signatureImage = opt.signatureImage; }
86
+ if (opt?.mrzLine1) { this.mrzLine1 = opt.mrzLine1; }
87
+ if (opt?.mrzLine2) { this.mrzLine2 = opt.mrzLine2; }
88
+ if (opt?.machineReadableZone) {
89
+ this.machineReadableZone = opt.machineReadableZone;
90
+ }
91
+ }
92
+
93
+ // The object `TD2Document` uses to compose itself.
94
+ #document;
95
+
96
+ /**
97
+ * A code identifying the document type.
98
+ * @type { string }
99
+ */
100
+ get typeCode() { return this.#document.typeCode; }
101
+ /**
102
+ * @param { string } value - A 1-2 character string consisting of the
103
+ * characters A-Z, 0-9, ' ', or <. 'A', 'I', 'P', or 'V' are recommended
104
+ * for the first character.
105
+ */
106
+ set typeCode(value) { this.#document.typeCode = value; }
107
+
108
+ /**
109
+ * A code identifying the authority who issued this document.
110
+ * @type { string }
111
+ */
112
+ get authorityCode() { return this.#document.authorityCode; }
113
+ /**
114
+ * @param { string } value - A 3-character string consisting of the characters
115
+ * A-Z, 0-9, ' ', or <. A code from ISO-3166-1, ICAO 9303-3, or these
116
+ * user-assigned ranges are recommended: AAA-AAZ, QMA-QZZ, XAA-XZZ, or
117
+ * ZZA-ZZZ.
118
+ */
119
+ set authorityCode(value) { this.#document.authorityCode = value; }
120
+
121
+ /**
122
+ * The document holder's primary identifier
123
+ * @type { string }
124
+ */
125
+ get primaryIdentifier() { return this.#document.primaryIdentifier; }
126
+ /**
127
+ * @param { string }
128
+ */
129
+ set primaryIdentifier(value) { this.#document.primaryIdentifier = value; }
130
+
131
+ /**
132
+ * The document holder's primary identifier in their native language
133
+ * @type { string | null }
134
+ */
135
+ get primaryIdentifierNative() { return this.#document.primaryIdentifierNative; }
136
+ /**
137
+ * @param { string | null }
138
+ */
139
+ set primaryIdentifierNative(value) { this.#document.primaryIdentifierNative = value; }
140
+
141
+ /**
142
+ * The document holder's secondary identifier
143
+ * @type { string | null }
144
+ */
145
+ get secondaryIdentifier() { return this.#document.secondaryIdentifier; }
146
+ /**
147
+ * @param { string | null }
148
+ */
149
+ set secondaryIdentifier(value) { this.#document.secondaryIdentifier = value; }
150
+
151
+ /**
152
+ * The document holder's secondary identifier in their native language
153
+ * @type { string | null }
154
+ */
155
+ get secondaryIdentifierNative() { return this.#document.secondaryIdentifierNative; }
156
+ /**
157
+ * @param { string | null }
158
+ */
159
+ set secondaryIdentifierNative(value) { this.#document.secondaryIdentifierNative = value; }
160
+
161
+ /**
162
+ * An identity document number unique for this document.
163
+ * @type { string }
164
+ */
165
+ get number() { return this.#document.number; }
166
+ /**
167
+ * @param { string } value - A string no longer than 9 characters consisting
168
+ * of the characters A-Z, 0-9, ' ', or <.
169
+ */
170
+ set number(value) { this.#document.number = value; }
171
+
172
+ /**
173
+ * A code identifying the document holder's nationality (or lack thereof).
174
+ * @type { string }
175
+ */
176
+ get nationalityCode() { return this.#document.nationalityCode; }
177
+ /**
178
+ * @param { string } value - A 3-character string consisting of the characters
179
+ * A-Z, 0-9, ' ', or <. A code from ISO-3166-1, ICAO 9303-3, or these
180
+ * user-assigned ranges are recommended: AAA-AAZ, QMA-QZZ, XAA-XZZ, or
181
+ * ZZA-ZZZ.
182
+ */
183
+ set nationalityCode(value) { this.#document.nationalityCode = value; }
184
+
185
+ /**
186
+ * The document holder's date of birth.
187
+ * @type { IcaoDate }
188
+ */
189
+ get birthDate() { return this.#document.birthDate; }
190
+ /**
191
+ * @param { string | Date | IcaoDate } value - A calendar date string in YYYY-MM-DD
192
+ * format, a `Date` object, or an `IcaoDate` object.
193
+ */
194
+ set birthDate(value) { this.#document.birthDate = value; }
195
+
196
+ /**
197
+ * A marker representing the document holder's gender.
198
+ * @type { 'F' | 'M' | 'X' }
199
+ */
200
+ get genderMarker() { return this.#document.genderMarker; }
201
+ /**
202
+ * @param { 'F' | 'M' | 'X' } value - The character 'F', 'M', or 'X'.
203
+ */
204
+ set genderMarker(value) { this.#document.genderMarker = value; }
205
+
206
+ /**
207
+ * The last date on which this document is valid.
208
+ * @type { IcaoDate }
209
+ */
210
+ get expirationDate() { return this.#document.expirationDate; }
211
+ /**
212
+ * @param { string | Date | IcaoDate } value - A calendar date string in YYYY-MM-DD
213
+ * format, a `Date` object, or an `IcaoDate` object.
214
+ */
215
+ set expirationDate(value) { this.#document.expirationDate = value; }
216
+
217
+ /**
218
+ * Optional data to include in the Machine-Readable Zone (MRZ).
219
+ * @type { string }
220
+ */
221
+ get optionalData() { return this.#document.optionalData; }
222
+ /**
223
+ * @param { string } value - Up to 7 characters. Valid characters are from the
224
+ * ranges A-Z, 0-9, ' ', or <.
225
+ */
226
+ set optionalData(value) { this.#document.optionalData = value; }
227
+
228
+ /**
229
+ * A path/URL to an image, or an image object, representing a photo of the
230
+ * document holder.
231
+ * @type { string | HTMLImageElement | SVGImageElement | HTMLVideoElement |
232
+ * HTMLCanvasElement | ImageBitmap | OffscreenCanvas | VideoFrame }
233
+ */
234
+ get photo() { return this.#document.photo; }
235
+ /**
236
+ * @param { string | HTMLImageElement | SVGImageElement | HTMLVideoElement |
237
+ * HTMLCanvasElement | ImageBitmap | OffscreenCanvas | VideoFrame } value
238
+ */
239
+ set photo(value) { this.#document.photo = value; }
240
+
241
+ /**
242
+ * A path/URL to an image, or an image object, representing the signature or
243
+ * usual mark of the document holder.
244
+ * @type { string | HTMLImageElement | SVGImageElement | HTMLVideoElement |
245
+ * HTMLCanvasElement | ImageBitmap | OffscreenCanvas | VideoFrame }
246
+ */
247
+ get signatureImage() { return this.#document.signatureImage; }
248
+ /**
249
+ * @param { string | HTMLImageElement | SVGImageElement | HTMLVideoElement |
250
+ * HTMLCanvasElement | ImageBitmap | OffscreenCanvas | VideoFrame } value
251
+ */
252
+ set signatureImage(value) { this.#document.signatureImage = value; }
253
+
254
+ /**
255
+ * The first line of the Machine-Readable Zone (MRZ).
256
+ * @type { string }
257
+ */
258
+ get mrzLine1() {
259
+ return padMRZString(this.typeCode.replace(/\s/gi, "<"), 2) +
260
+ padMRZString(this.authorityCode.replace(/\s/gi, "<"), 3) +
261
+ padMRZString(
262
+ normalizeMRZString(this.primaryIdentifier) +
263
+ "<<" +
264
+ normalizeMRZString(this.secondaryIdentifier)
265
+ , 31);
266
+ }
267
+ /**
268
+ * @param { string } value - A MRZ line string of a 36-character length.
269
+ */
270
+ set mrzLine1(value) {
271
+ if (value.length !== 36) {
272
+ throw new RangeError(
273
+ `Length '${value.length}' does not match the length of a TD2-sized ` +
274
+ `Machine Readable Zone (MRZ) line.`
275
+ );
276
+ }
277
+ this.typeCode = value.slice(0, 2).replace(/</gi, "");
278
+ this.authorityCode = value.slice(2, 5).replace(/</gi, "");
279
+ const [primary, ...secondary] = value.slice(5).split("<<");
280
+ this.primaryIdentifier = primary.replace(/</gi, " ").trimEnd();
281
+ this.secondaryIdentifier = secondary.join("").replace(/</gi, " ").trimEnd();
282
+ }
283
+
284
+ /**
285
+ * The second line of the Machine-Readable Zone (MRZ).
286
+ * @type { string }
287
+ */
288
+ get mrzLine2() {
289
+ const UNCHECKED_LINE = padMRZString(this.number.replace(/\s/gi, "<"), 9) +
290
+ generateMRZCheckDigit(padMRZString(this.number.replace(/\s/gi, "<"), 9)) +
291
+ padMRZString(this.nationalityCode.replace(/\s/gi, "<"), 3) +
292
+ this.birthDate.toMRZString() +
293
+ generateMRZCheckDigit(this.birthDate.toMRZString()) +
294
+ genderMarkerToMRZ(this.genderMarker) +
295
+ this.expirationDate.toMRZString() +
296
+ generateMRZCheckDigit(this.expirationDate.toMRZString()) +
297
+ optionalDataMRZ(this.optionalData, 7);
298
+ return UNCHECKED_LINE +
299
+ generateMRZCheckDigit(
300
+ UNCHECKED_LINE.slice(0,10) +
301
+ UNCHECKED_LINE.slice(13,20) +
302
+ UNCHECKED_LINE.slice(21)
303
+ );
304
+ }
305
+ /**
306
+ * @param { string } value - A MRZ line string of a 36-character length.
307
+ */
308
+ set mrzLine2(value) {
309
+ if (value.length !== 36) {
310
+ throw new RangeError(
311
+ `Length '${value.length}' does not match the length of a TD2-sized ` +
312
+ `Machine Readable Zone (MRZ) line.`
313
+ );
314
+ }
315
+ const LINE_CHECK_DIGIT = generateMRZCheckDigit(
316
+ value.slice(0, 10) +
317
+ value.slice(13, 20) +
318
+ value.slice(21, 35)
319
+ );
320
+ if (value[35] !== LINE_CHECK_DIGIT) {
321
+ throw new EvalError(
322
+ `Check digit '${value[43]}' does not match for the check digit on the` +
323
+ ` entire Machine-Readable Zone (MRZ) line 2.`
324
+ );
325
+ }
326
+ if (value[9] !== generateMRZCheckDigit(
327
+ value.slice(0, 9)
328
+ )) {
329
+ throw new EvalError(
330
+ `Check digit '${value[9]}' does not match for the check digit on the ` +
331
+ `document number.`
332
+ );
333
+ }
334
+ if (value[19] !== generateMRZCheckDigit(
335
+ value.slice(13, 19)
336
+ )) {
337
+ throw new EvalError(
338
+ `Check digit '${value[19]}' does not match for the check digit on the` +
339
+ ` date of birth.`
340
+ );
341
+ }
342
+ if (value[27] !== generateMRZCheckDigit(
343
+ value.slice(21, 27)
344
+ )) {
345
+ throw new EvalError(
346
+ `Check digit '${value[27]}' does not match for the check digit on the` +
347
+ ` date of expiration.`
348
+ );
349
+ }
350
+ this.number = value.slice(0, 9).replace(/</gi, "");
351
+ this.nationalityCode = value.slice(10, 13).replace(/</gi, "");
352
+ this.birthDate = new IcaoDate(value.slice(13, 19), new Date().getFullYear());
353
+ this.genderMarker = value[20] === "<" ? "X" : value[20];
354
+ this.expirationDate = new IcaoDate(value.slice(21, 27));
355
+ this.optionalData = value.slice(28, 35).replace(/</gi, " ").trimEnd();
356
+ }
357
+
358
+ /**
359
+ * The full Machine-Readable Zone (MRZ).
360
+ * @type { string }
361
+ */
362
+ get machineReadableZone() {
363
+ return this.mrzLine1 + this.mrzLine2;
364
+ }
365
+ /**
366
+ * @param { string } value - A MRZ string of a 72-character length.
367
+ */
368
+ set machineReadableZone(value) {
369
+ if (value.length !== 72) {
370
+ throw new RangeError(
371
+ `Length '${value.length}' does not match the length of a TD2-sized ` +
372
+ `Machine-Readable Zone (MRZ).`
373
+ );
374
+ }
375
+ this.mrzLine1 = value.slice(0, 36);
376
+ this.mrzLine2 = value.slice(36);
377
+ }
378
+ }
379
+