@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,408 @@
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 TD1-sized machine-readable travel documents
14
+ * (MRTDs) with machine-readable zones. The US Passport Card is an example
15
+ * of this type of document.
16
+ *
17
+ * `TD1Document` may be used on its own or may be used to compose different
18
+ * kinds of MRTDs.
19
+ */
20
+ export class TD1Document {
21
+ /**
22
+ * Create a `TD1Document`.
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.number] - A string no longer than 9 characters
32
+ * consisting of the characters A-Z, 0-9, ' ', or <.
33
+ * @param { string | Date | IcaoDate } [opt.birthDate] - A calendar date string in
34
+ * YYYY-MM-DD format, a `Date` object, or an `IcaoDate` object.
35
+ * @param { 'F' | 'M' | 'X' } [opt.genderMarker] - The character 'F', 'M', or 'X'.
36
+ * @param { string | Date | IcaoDate } [opt.expirationDate] - A calendar date string in
37
+ * YYYY-MM-DD format, a `Date` object, or an `IcaoDate` object.
38
+ * @param { string } [opt.nationalityCode] - A 3-character string consisting
39
+ * of the characters A-Z, 0-9, ' ', or <. A code from ISO-3166-1,
40
+ * ICAO 9303-3, or these user-assigned ranges are recommended: AAA-AAZ,
41
+ * QMA-QZZ, XAA-XZZ, or ZZA-ZZZ.
42
+ * @param { string } [opt.primaryIdentifier] - The document holder's primary
43
+ * identifier.
44
+ * @param { string | null } [opt.primaryIdentifierNative] - The document
45
+ * holder's primary identifier in their native language.
46
+ * @param { string | null } [opt.secondaryIdentifer] - The document holder's
47
+ * secondary identifier.
48
+ * @param { string | null } [opt.secondaryIdentifierNative] - The document
49
+ * holder's secondary identifier in their native language.
50
+ * @param { string } [opt.optionalData] - Up to 26 characters. Valid
51
+ * characters are from the ranges A-Z, 0-9, ' ', or <.
52
+ * @param { string } [opt.mrzLine1] - A MRZ line string of a 30-character
53
+ * length.
54
+ * @param { string } [opt.mrzLine2] - A MRZ line string of a 30-character
55
+ * length.
56
+ * @param { string } [opt.mrzLine3] - A MRZ line string of a 30-character
57
+ * length.
58
+ * @param { string } [opt.machineReadableZone] - A MRZ string of a
59
+ * 90-character length.
60
+ * @param { string | HTMLImageElement | SVGImageElement | HTMLVideoElement |
61
+ * HTMLCanvasElement | ImageBitmap | OffscreenCanvas |
62
+ * VideoFrame } [opt.photo] - A path/URL to an image, or an image object,
63
+ * representing a photo of the document holder.
64
+ * @param { string | HTMLImageElement | SVGImageElement | HTMLVideoElement |
65
+ * HTMLCanvasElement | ImageBitmap | OffscreenCanvas |
66
+ * VideoFrame } [opt.signatureImage] - A path/URL to an image, or an image
67
+ * object, representing the signature or usual mark of the document
68
+ * holder.
69
+ */
70
+ constructor(opt) {
71
+ this.#document = new TravelDocument();
72
+
73
+ this.typeCode = opt?.typeCode ?? "I";
74
+ this.authorityCode = opt?.authorityCode ?? "UTO";
75
+ this.number = opt?.number ?? "D23145890";
76
+ this.birthDate = opt?.birthDate ?? "1974-08-12";
77
+ this.genderMarker = opt?.genderMarker ?? "F";
78
+ this.expirationDate = opt?.expirationDate ?? "2012-04-15";
79
+ this.nationalityCode = opt?.nationalityCode ?? "UTO";
80
+ this.primaryIdentifier = opt?.primaryIdentifier ?? "Eriksson";
81
+ this.primaryIdentifierNative = opt?.primaryIdentifierNative ?? null;
82
+ this.secondaryIdentifier = opt?.secondaryIdentifier ?? null;
83
+ this.secondaryIdentifierNative = opt?.secondaryIdentifierNative ?? null;
84
+ this.optionalData = opt?.optionalData ?? "";
85
+
86
+ if (opt?.photo) { this.photo = opt.photo; }
87
+ if (opt?.signatureImage) { this.signatureImage = opt.signatureImage; }
88
+ if (opt?.mrzLine1) { this.mrzLine1 = opt.mrzLine1; }
89
+ if (opt?.mrzLine2) { this.mrzLine2 = opt.mrzLine2; }
90
+ if (opt?.mrzLine3) { this.mrzLine3 = opt.mrzLine3; }
91
+ if (opt?.machineReadableZone) {
92
+ this.machineReadableZone = opt.machineReadableZone;
93
+ }
94
+ }
95
+
96
+ // The object `TD1Document` uses to compose itself.
97
+ #document;
98
+
99
+ /**
100
+ * A code identifying the document type.
101
+ * @type { string }
102
+ */
103
+ get typeCode() { return this.#document.typeCode; }
104
+ /**
105
+ * @param { string } value - A 1-2 character string consisting of the
106
+ * characters A-Z, 0-9, ' ', or <. 'A', 'I', 'P', or 'V' are recommended
107
+ * for the first character.
108
+ */
109
+ set typeCode(value) { this.#document.typeCode = value; }
110
+
111
+ /**
112
+ * A code identifying the authority who issued this document.
113
+ * @type { string }
114
+ */
115
+ get authorityCode() { return this.#document.authorityCode; }
116
+ /**
117
+ * @param { string } value - A 3-character string consisting of the characters
118
+ * A-Z, 0-9, ' ', or <. A code from ISO-3166-1, ICAO 9303-3, or these
119
+ * user-assigned ranges are recommended: AAA-AAZ, QMA-QZZ, XAA-XZZ, or
120
+ * ZZA-ZZZ.
121
+ */
122
+ set authorityCode(value) { this.#document.authorityCode = value; }
123
+
124
+ /**
125
+ * An identity document number unique for this document.
126
+ * @type { string }
127
+ */
128
+ get number() { return this.#document.number; }
129
+ /**
130
+ * @param { string } value - A string no longer than 9 characters consisting
131
+ * of the characters A-Z, 0-9, ' ', or <.
132
+ */
133
+ set number(value) { this.#document.number = value; }
134
+
135
+ /**
136
+ * The document holder's date of birth.
137
+ * @type { IcaoDate }
138
+ */
139
+ get birthDate() { return this.#document.birthDate; }
140
+ /**
141
+ * @param { string | Date | IcaoDate } value - A calendar date string in YYYY-MM-DD
142
+ * format, a `Date` object, or an `IcaoDate` object.
143
+ */
144
+ set birthDate(value) { this.#document.birthDate = value; }
145
+
146
+ /**
147
+ * A marker representing the document holder's gender.
148
+ * @type { 'F' | 'M' | 'X' }
149
+ */
150
+ get genderMarker() { return this.#document.genderMarker; }
151
+ /**
152
+ * @param { 'F' | 'M' | 'X' } value - The character 'F', 'M', or 'X'.
153
+ */
154
+ set genderMarker(value) { this.#document.genderMarker = value; }
155
+
156
+ /**
157
+ * The last date on which this document is valid.
158
+ * @type { IcaoDate }
159
+ */
160
+ get expirationDate() { return this.#document.expirationDate; }
161
+ /**
162
+ * @param { string | Date | IcaoDate } value - A calendar date string in YYYY-MM-DD
163
+ * format, a `Date` object, or an `IcaoDate` object.
164
+ */
165
+ set expirationDate(value) { this.#document.expirationDate = value; }
166
+
167
+ /**
168
+ * A code identifying the document holder's nationality (or lack thereof).
169
+ * @type { string }
170
+ */
171
+ get nationalityCode() { return this.#document.nationalityCode; }
172
+ /**
173
+ * @param { string } value - A 3-character string consisting of the characters
174
+ * A-Z, 0-9, ' ', or <. A code from ISO-3166-1, ICAO 9303-3, or these
175
+ * user-assigned ranges are recommended: AAA-AAZ, QMA-QZZ, XAA-XZZ, or
176
+ * ZZA-ZZZ.
177
+ */
178
+ set nationalityCode(value) { this.#document.nationalityCode = value; }
179
+
180
+ /**
181
+ * The document holder's primary identifier
182
+ * @type { string }
183
+ */
184
+ get primaryIdentifier() { return this.#document.primaryIdentifier; }
185
+ /**
186
+ * @param { string }
187
+ */
188
+ set primaryIdentifier(value) { this.#document.primaryIdentifier = value; }
189
+
190
+ /**
191
+ * The document holder's primary identifier in their native language
192
+ * @type { string | null }
193
+ */
194
+ get primaryIdentifierNative() { return this.#document.primaryIdentifierNative; }
195
+ /**
196
+ * @param { string | null }
197
+ */
198
+ set primaryIdentifierNative(value) { this.#document.primaryIdentifierNative = value; }
199
+
200
+ /**
201
+ * The document holder's secondary identifier
202
+ * @type { string | null }
203
+ */
204
+ get secondaryIdentifier() { return this.#document.secondaryIdentifier; }
205
+ /**
206
+ * @param { string | null }
207
+ */
208
+ set secondaryIdentifier(value) { this.#document.secondaryIdentifier = value; }
209
+
210
+ /**
211
+ * The document holder's secondary identifier in their native language
212
+ * @type { string | null }
213
+ */
214
+ get secondaryIdentifierNative() { return this.#document.secondaryIdentifierNative; }
215
+ /**
216
+ * @param { string | null }
217
+ */
218
+ set secondaryIdentifierNative(value) { this.#document.secondaryIdentifierNative = value; }
219
+
220
+ /**
221
+ * Optional data to include in the Machine-Readable Zone (MRZ).
222
+ * @type { string }
223
+ */
224
+ get optionalData() { return this.#document.optionalData; }
225
+ /**
226
+ * @param { string } value - Up to 26 characters. Valid characters are from
227
+ * the ranges A-Z, 0-9, ' ', or <.
228
+ */
229
+ set optionalData(value) { this.#document.optionalData = value; }
230
+
231
+ /**
232
+ * A path/URL to an image, or an image object, representing a photo of the
233
+ * document holder.
234
+ * @type { string | HTMLImageElement | SVGImageElement | HTMLVideoElement |
235
+ * HTMLCanvasElement | ImageBitmap | OffscreenCanvas | VideoFrame }
236
+ */
237
+ get photo() { return this.#document.photo; }
238
+ /**
239
+ * @param { string | HTMLImageElement | SVGImageElement | HTMLVideoElement |
240
+ * HTMLCanvasElement | ImageBitmap | OffscreenCanvas | VideoFrame } value
241
+ */
242
+ set photo(value) { this.#document.photo = value; }
243
+
244
+ /**
245
+ * A path/URL to an image, or an image object, representing the signature or
246
+ * usual mark of the document holder.
247
+ * @type { string | HTMLImageElement | SVGImageElement | HTMLVideoElement |
248
+ * HTMLCanvasElement | ImageBitmap | OffscreenCanvas | VideoFrame }
249
+ */
250
+ get signatureImage() { return this.#document.signatureImage; }
251
+ /**
252
+ * @param { string | HTMLImageElement | SVGImageElement | HTMLVideoElement |
253
+ * HTMLCanvasElement | ImageBitmap | OffscreenCanvas | VideoFrame } value
254
+ */
255
+ set signatureImage(value) { this.#document.signatureImage = value; }
256
+
257
+ /**
258
+ * The first line of the Machine-Readable Zone (MRZ).
259
+ * @type { string }
260
+ */
261
+ get mrzLine1() {
262
+ return padMRZString(this.typeCode.replace(/\s/gi, "<"), 2) +
263
+ padMRZString(this.authorityCode.replace(/\s/gi, "<"), 3) +
264
+ padMRZString(this.number.replace(/\s/gi, "<"), 9) +
265
+ generateMRZCheckDigit(
266
+ padMRZString(this.number.replace(/\s/gi, "<"), 9)
267
+ ) +
268
+ optionalDataMRZ(this.optionalData, 26).slice(0,15);
269
+ }
270
+ /**
271
+ * @param { string } value - A MRZ line string of a 30-character length.
272
+ */
273
+ set mrzLine1(value) {
274
+ if (value.length !== 30) {
275
+ throw new RangeError(
276
+ `Length '${value.length}' does not match the length of a TD1-sized ` +
277
+ `Machine-Readable Zone (MRZ) line.`
278
+ );
279
+ }
280
+ if (value[14] !== generateMRZCheckDigit(
281
+ value.slice(5, 14)
282
+ )) {
283
+ throw new EvalError(
284
+ `Check digit '${value[14]}' does not match for the check digit on the` +
285
+ ` document number.`
286
+ );
287
+ }
288
+ this.typeCode = value.slice(0, 2).replace(/</gi, "");
289
+ this.authorityCode = value.slice(2, 5).replace(/</gi, "");
290
+ this.number = value.slice(5, 14).replace(/</gi, "");
291
+ }
292
+
293
+ /**
294
+ * The second line of the Machine-Readable Zone (MRZ).
295
+ * @type { string }
296
+ */
297
+ get mrzLine2() {
298
+ const UNCHECKED_LINE = this.birthDate.toMRZString() +
299
+ generateMRZCheckDigit(this.birthDate.toMRZString()) +
300
+ genderMarkerToMRZ(this.genderMarker) +
301
+ this.expirationDate.toMRZString() +
302
+ generateMRZCheckDigit(this.expirationDate.toMRZString()) +
303
+ padMRZString(this.nationalityCode.replace(/\s/gi, "<"), 3) +
304
+ optionalDataMRZ(this.optionalData, 26).slice(15);
305
+ return UNCHECKED_LINE +
306
+ generateMRZCheckDigit(
307
+ this.mrzLine1.slice(5) +
308
+ UNCHECKED_LINE.slice(0,7) +
309
+ UNCHECKED_LINE.slice(8,15) +
310
+ UNCHECKED_LINE.slice(18)
311
+ );
312
+ }
313
+ /**
314
+ * @param { string } value - A MRZ line string of a 30-character length.
315
+ */
316
+ set mrzLine2(value) {
317
+ if (value.length !== 30) {
318
+ throw new RangeError(
319
+ `Length '${value.length}' does not match the length of a TD1-sized ` +
320
+ `Machine Readable-Zone (MRZ) line.`
321
+ );
322
+ }
323
+ if (value[6] !== generateMRZCheckDigit(
324
+ value.slice(0, 6)
325
+ )) {
326
+ throw new EvalError(
327
+ `Check digit '${value[6]} does not match for the check digit on the ` +
328
+ `date of birth.`
329
+ );
330
+ }
331
+ if (value[14] !== generateMRZCheckDigit(
332
+ value.slice(8, 14)
333
+ )) {
334
+ throw new EvalError(
335
+ `Check digit '${value[14]}' does not match for the check digit on the` +
336
+ ` date of expiration.`
337
+ );
338
+ }
339
+ this.birthDate = new IcaoDate(value.slice(0, 6), new Date().getFullYear());
340
+ this.genderMarker = value[7] === "<" ? "X" : value[7];
341
+ this.expirationDate = new IcaoDate(value.slice(8, 14));
342
+ this.nationalityCode = value.slice(15, 18).replace(/</gi, "");
343
+ }
344
+
345
+ /**
346
+ * The third line of the Machine-Readable Zone (MRZ).
347
+ * @type { string }
348
+ */
349
+ get mrzLine3() {
350
+ return padMRZString(
351
+ normalizeMRZString(this.primaryIdentifier) +
352
+ "<<" +
353
+ normalizeMRZString(this.secondaryIdentifier)
354
+ , 30);
355
+ }
356
+ /**
357
+ * @param { string } value - A MRZ line string of a 30-character length.
358
+ */
359
+ set mrzLine3(value) {
360
+ if (value.length !== 30) {
361
+ throw new RangeError(
362
+ `Length '${value.length}' does not match the length of a TD1-sized ` +
363
+ `Machine-Readable Zone (MRZ) line.`
364
+ );
365
+ }
366
+ const [primary, ...secondary] = value.split("<<");
367
+ this.primaryIdentifier = primary.replace(/</gi, " ").trimEnd();
368
+ this.secondaryIdentifier = secondary.join("").replace(/</gi, " ").trimEnd();
369
+ }
370
+
371
+ /**
372
+ * The full Machine-Readable Zone (MRZ).
373
+ * @type { string }
374
+ */
375
+ get machineReadableZone() {
376
+ return this.mrzLine1 + this.mrzLine2 + this.mrzLine3;
377
+ }
378
+ /**
379
+ * @param { string } value - A MRZ string of a 90-character length.
380
+ */
381
+ set machineReadableZone(value) {
382
+ if (value.length !== 90) {
383
+ throw new RangeError(
384
+ `Length '${value.length}' does not match the length of a TD1-sized ` +
385
+ `Machine-Readable Zone (MRZ).`
386
+ );
387
+ }
388
+ const LINE_CHECK_DIGIT = generateMRZCheckDigit(
389
+ value.slice(5, 30) +
390
+ value.slice(30, 37) +
391
+ value.slice(38, 45) +
392
+ value.slice(48, 59)
393
+ );
394
+ if (value[59] !== LINE_CHECK_DIGIT) {
395
+ throw new EvalError(
396
+ `Check digit '${value[59]}' does not match for the check digit on the` +
397
+ ` Machine-Readable Zone (MRZ) lines 1 and 2.`
398
+ );
399
+ }
400
+ this.mrzLine1 = value.slice(0, 30);
401
+ this.mrzLine2 = value.slice(30, 60);
402
+ this.mrzLine3 = value.slice(60);
403
+ this.optionalData = (value.slice(15, 30) + value.slice(
404
+ 48, 59
405
+ )).replace(/</gi, " ").trimEnd();
406
+ }
407
+ }
408
+