@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,306 @@
1
+ // SPDX-FileCopyrightText: 2026 Don Geronimo <https://sentamal.in>
2
+ // SPDX-License-Identifier: LGPL-3.0-or-later
3
+
4
+ import { validateMRZString } from "./utilities/validate-mrz-string.js";
5
+ import { normalizeMRZString } from "./utilities/normalize-mrz-string.js";
6
+ import { IcaoDate } from "./utilities/icao-date.js";
7
+
8
+ /**
9
+ * Stores common properties and methods for all ICAO 9303 machine-readable
10
+ * travel documents (MRTDs) with machine-readable zones.
11
+ *
12
+ * `TravelDocument` is intended to be used to compose different kinds of MRTDs.
13
+ * It is not intended to be instantiated directly.
14
+ * @abstract
15
+ */
16
+ export class TravelDocument {
17
+ /**
18
+ * Create a `TravelDocument`.
19
+ * @param { Object } [opt] - An options object.
20
+ * @param { string } [opt.typeCode] - A 1-2 character string consisting of the
21
+ * characters A-Z, 0-9, ' ', or <. 'A', 'I', 'P', or 'V' are recommended
22
+ * for the first character.
23
+ * @param { string } [opt.authorityCode] - A 3-character string consisting of
24
+ * the characters A-Z, 0-9, ' ', or <. A code from ISO-3166-1,
25
+ * ICAO 9303-3, or these user-assigned ranges are recommended: AAA-AAZ,
26
+ * QMA-QZZ, XAA-XZZ, or ZZA-ZZZ.
27
+ * @param { string } [opt.number] - A string no longer than 9 characters
28
+ * consisting of the characters A-Z, 0-9, ' ', or <.
29
+ * @param { string | Date | IcaoDate } [opt.birthDate] - A calendar date string in
30
+ * YYYY-MM-DD format, a `Date` object, or an `IcaoDate` object.
31
+ * @param { 'F' | 'M' | 'X' } [opt.genderMarker] - The character 'F', 'M', or 'X'.
32
+ * @param { string | Date | IcaoDate } [opt.expirationDate] - A calendar date string in
33
+ * YYYY-MM-DD format, a `Date` object, or an `IcaoDate` object.
34
+ * @param { string } [opt.nationalityCode] - A 3-character string consisting
35
+ * of the characters A-Z, 0-9, ' ', or <. A code from ISO-3166-1,
36
+ * ICAO 9303-3, or these user-assigned ranges are recommended: AAA-AAZ,
37
+ * QMA-QZZ, XAA-XZZ, or ZZA-ZZZ.
38
+ * @param { string } [opt.primaryIdentifier] - The document holder's primary
39
+ * identifier.
40
+ * @param { string | null } [opt.primaryIdentifierNative] - The document
41
+ * holder's primary identifier in their native language.
42
+ * @param { string | null } [opt.secondaryIdentifer] - The document holder's
43
+ * secondary identifier.
44
+ * @param { string | null } [opt.secondaryIdentifierNative] - The document
45
+ * holder's secondary identifier in their native language.
46
+ * @param { string } [opt.optionalData] - Valid characters are from the ranges
47
+ * A-Z, 0-9, ' ', or <.
48
+ * @param { string | HTMLImageElement | SVGImageElement | HTMLVideoElement |
49
+ * HTMLCanvasElement | ImageBitmap | OffscreenCanvas |
50
+ * VideoFrame } [opt.photo] - A path/URL to an image, or an image object,
51
+ * representing a photo of the document holder.
52
+ * @param { string | HTMLImageElement | SVGImageElement | HTMLVideoElement |
53
+ * HTMLCanvasElement | ImageBitmap | OffscreenCanvas |
54
+ * VideoFrame } [opt.signatureImage] - A path/URL to an image, or an image
55
+ * object, representing the signature or usual mark of the document
56
+ * holder.
57
+ */
58
+ constructor(opt) {
59
+ this.typecode = opt?.typeCode ?? "I";
60
+ this.authorityCode = opt?.authorityCode ?? "UTO";
61
+ this.number = opt?.number ?? "D23145890";
62
+ this.birthDate = opt?.birthDate ?? "1974-08-12";
63
+ this.genderMarker = opt?.genderMarker ?? "F";
64
+ this.expirationDate = opt?.expirationDate ?? "2012-04-15";
65
+ this.nationalityCode = opt?.nationalityCode ?? "UTO";
66
+ this.primaryIdentifier = opt?.primaryIdentifier ?? "";
67
+ this.primaryIdentifierNative = opt?.primaryIdentifierNative ?? null;
68
+ this.secondaryIdentifier = opt?.secondaryIdentifier ?? null;
69
+ this.secondaryIdentifierNative = opt?.secondaryIdentifierNative ?? null;
70
+ this.optionalData = opt?.optionalData ?? "";
71
+
72
+ if (opt?.photo) { this.photo = opt.photo; }
73
+ if (opt?.signatureImage) { this.signatureImage = opt.signatureImage; }
74
+ }
75
+
76
+ #typeCode;
77
+ /**
78
+ * A code identifying the document type.
79
+ * @type { string }
80
+ */
81
+ get typeCode() { return this.#typeCode; }
82
+ /**
83
+ * @param { string } value - A 1-2 character string consisting of the
84
+ * characters A-Z, 0-9, ' ', or <. 'A', 'I', 'P', or 'V' are recommended
85
+ * for the first character.
86
+ */
87
+ set typeCode(value) {
88
+ const isInvalid = validateMRZString(value, {
89
+ minimum: 1,
90
+ maximum: 2
91
+ });
92
+ if (isInvalid) {
93
+ throw new RangeError(`Value set on 'typeCode' has errors: ${isInvalid}`);
94
+ }
95
+ this.#typeCode = value.toUpperCase();
96
+ }
97
+
98
+ #authorityCode;
99
+ /**
100
+ * A code identifying the authority who issued this document.
101
+ * @type { string }
102
+ */
103
+ get authorityCode() { return this.#authorityCode; }
104
+ /**
105
+ * @param { string } value - A 3-character string consisting of the characters
106
+ * A-Z, 0-9, ' ', or <. A code from ISO-3166-1, ICAO 9303-3, or these
107
+ * user-assigned ranges are recommended: AAA-AAZ, QMA-QZZ, XAA-XZZ, or
108
+ * ZZA-ZZZ.
109
+ */
110
+ set authorityCode(value) {
111
+ const isInvalid = validateMRZString(value, {
112
+ minimum: 1,
113
+ maximum: 3
114
+ });
115
+ if (isInvalid) {
116
+ throw new RangeError(
117
+ `Value set on 'authorityCode' has errors: ${isInvalid}`
118
+ );
119
+ }
120
+ this.#authorityCode = value.toUpperCase();
121
+ }
122
+
123
+ #number;
124
+ /**
125
+ * An identity document number unique for this document.
126
+ * @type { string }
127
+ */
128
+ get number() { return this.#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) {
134
+ const isInvalid = validateMRZString(value, {
135
+ minimum: 1,
136
+ maximum: 9
137
+ });
138
+ if (isInvalid) {
139
+ throw new RangeError(`Value set on 'number' has errors: ${isInvalid}`);
140
+ }
141
+ this.#number = value.toUpperCase();
142
+ }
143
+
144
+ #birthDate;
145
+ /**
146
+ * The document holder's date of birth.
147
+ * @type { IcaoDate }
148
+ */
149
+ get birthDate() { return this.#birthDate; }
150
+ /**
151
+ * @param { string | Date | IcaoDate } value - A calendar date string in YYYY-MM-DD
152
+ * format, a `Date` object, or an `IcaoDate` object.
153
+ */
154
+ set birthDate(value) {
155
+ this.#birthDate = new IcaoDate(value);
156
+ }
157
+
158
+ #genderMarker;
159
+ /**
160
+ * A marker representing the document holder's gender.
161
+ * @type { 'F' | 'M' | 'X' }
162
+ */
163
+ get genderMarker() { return this.#genderMarker; }
164
+ /**
165
+ * @param { 'F' | 'M' | 'X' } value - The character 'F', 'M', or 'X'.
166
+ */
167
+ set genderMarker(value) {
168
+ if (!["F", "M", "X"].includes(value.toUpperCase())) {
169
+ throw new RangeError(
170
+ "Gender marker (genderMarker) must be [F]emale, [M]ale, " +
171
+ "or Other/Unspecified [X]."
172
+ );
173
+ }
174
+ this.#genderMarker = value.toUpperCase();
175
+ }
176
+
177
+ #expirationDate;
178
+ /**
179
+ * The last date on which this document is valid.
180
+ * @type { IcaoDate }
181
+ */
182
+ get expirationDate() { return this.#expirationDate; }
183
+ /**
184
+ * @param { string | Date | IcaoDate } value - A calendar date string in YYYY-MM-DD
185
+ * format, a `Date` object, or an `IcaoDate` object.
186
+ */
187
+ set expirationDate(value) {
188
+ this.#expirationDate = new IcaoDate(value);
189
+ }
190
+
191
+ #nationalityCode;
192
+ /**
193
+ * A code identifying the document holder's nationality (or lack thereof).
194
+ * @type { string }
195
+ */
196
+ get nationalityCode() { return this.#nationalityCode; }
197
+ /**
198
+ * @param { string } value - A 3-character string consisting of the characters
199
+ * A-Z, 0-9, ' ', or <. A code from ISO-3166-1, ICAO 9303-3, or these
200
+ * user-assigned ranges are recommended: AAA-AAZ, QMA-QZZ, XAA-XZZ, or
201
+ * ZZA-ZZZ.
202
+ */
203
+ set nationalityCode(value) {
204
+ const isInvalid = validateMRZString(value, {
205
+ minimum: 1,
206
+ maximum: 3
207
+ });
208
+ if (isInvalid) {
209
+ throw new TypeError(
210
+ `Value set on 'nationalityCode has errors: ${isInvalid}`
211
+ );
212
+ }
213
+ this.#nationalityCode = value.toUpperCase();
214
+ }
215
+
216
+ /**
217
+ * The document holder's primary identifier
218
+ * @type { string }
219
+ */
220
+ primaryIdentifier
221
+
222
+ #primaryIdentifierNative
223
+ /**
224
+ * The document holder's primary identifier in their native language
225
+ * @type { string | null }
226
+ */
227
+ get primaryIdentifierNative() { return this.#primaryIdentifierNative; }
228
+ /**
229
+ * @param { string | null }
230
+ */
231
+ set primaryIdentifierNative(value) { this.#primaryIdentifierNative = typeof value === 'string' ? value.trim() : null; }
232
+
233
+ #secondaryIdentifier
234
+ /**
235
+ * The document holder's secondary identifier
236
+ * @type { string | null }
237
+ */
238
+ get secondaryIdentifier() { return this.#secondaryIdentifier; }
239
+ /**
240
+ * @param { string | null }
241
+ */
242
+ set secondaryIdentifier(value) { this.#secondaryIdentifier = typeof value === 'string' ? value.trim() : null; }
243
+
244
+ #secondaryIdentifierNative
245
+ /**
246
+ * The document holder's secondary identifier in their native language
247
+ * @type { string | null }
248
+ */
249
+ get secondaryIdentifierNative() { return this.#secondaryIdentifierNative; }
250
+ /**
251
+ * @param { string | null }
252
+ */
253
+ set secondaryIdentifierNative(value) { this.#secondaryIdentifierNative = typeof value === 'string' ? value.trim() : null; }
254
+
255
+ #optionalData;
256
+ /**
257
+ * Optional data to include in the Machine-Readable Zone (MRZ).
258
+ * @type { string }
259
+ */
260
+ get optionalData() { return this.#optionalData; }
261
+ /**
262
+ * @param { string } value - Valid characters are from the ranges A-Z, 0-9,
263
+ * ' ', or <.
264
+ */
265
+ set optionalData(value) {
266
+ const isInvalid = validateMRZString(value);
267
+ if (isInvalid) {
268
+ throw new RangeError(
269
+ `Value set on 'optionalData' has errors: ${isInvalid}`
270
+ );
271
+ }
272
+ this.#optionalData = value;
273
+ }
274
+
275
+ /**
276
+ * A path/URL to an image, or an image object, representing a photo of the
277
+ * document holder or image chosen by the issuer.
278
+ * @type { string | HTMLImageElement | SVGImageElement | HTMLVideoElement |
279
+ * HTMLCanvasElement | ImageBitmap | OffscreenCanvas | VideoFrame }
280
+ */
281
+ photo;
282
+
283
+ /**
284
+ * A path/URL to an image, or an image object, representing the signature or
285
+ * usual mark of the document holder or issuer.
286
+ * @type { string | HTMLImageElement | SVGImageElement | HTMLVideoElement |
287
+ * HTMLCanvasElement | ImageBitmap | OffscreenCanvas | VideoFrame }
288
+ */
289
+ signatureImage;
290
+
291
+ /**
292
+ * The full Machine-Readable Zone (MRZ)
293
+ * @abstract
294
+ * @type { string }
295
+ */
296
+ get machineReadableZone() {
297
+ throw new TypeError("An inherited or composed class must implement abstract property 'machineReadableZone'.");
298
+ }
299
+ /**
300
+ * @param { string } value - a MRZ string of appropriate length for the document.
301
+ */
302
+ set machineReadableZone(value) {
303
+ throw new TypeError("An inherited or composed class must implement abstract property 'machineReadableZone'.");
304
+ }
305
+ }
306
+
@@ -0,0 +1,23 @@
1
+ // SPDX-FileCopyrightText: 2026 Don Geronimo <https://sentamal.in>
2
+ // SPDX-License-Identifier: LGPL-3.0-or-later
3
+
4
+ /**
5
+ * Get a corresponding calendar date from an ICAO 9303 Part 13 VDS date byte sequence.
6
+ * @param { number[] } date - A `number` array of length 3.
7
+ * @example
8
+ * // Returns 1957-03-25
9
+ * bytesToDate([49, 158, 245]);
10
+ */
11
+ export function bytesToDate(date) {
12
+ let base2 = "";
13
+ date.forEach((byte) => {
14
+ base2 += byte.toString(2).padStart(8, "0");
15
+ });
16
+ const DATE_INTEGER_STRING =
17
+ parseInt(base2, 2).toString(10).padStart(8, "0");
18
+ const MONTH = DATE_INTEGER_STRING.slice(0, 2);
19
+ const DAY = DATE_INTEGER_STRING.slice(2, 4);
20
+ const YEAR = DATE_INTEGER_STRING.slice(4);
21
+ return new Date(`${YEAR}-${MONTH}-${DAY}T00:00:00`);
22
+ }
23
+
@@ -0,0 +1,225 @@
1
+ // SPDX-FileCopyrightText: 2026 Don Geronimo <https://sentamal.in>
2
+ // SPDX-License-Identifier: LGPL-3.0-or-later
3
+
4
+ import { C40SHIFT1 } from "./c40-shift1.js";
5
+
6
+ /**
7
+ * Decode a C40-encoded number array as defined in ICAO 9303 part 13 into a
8
+ * string.
9
+ * @param { number[] } c40
10
+ * @example
11
+ * // Returns "XKCD"
12
+ * c40Decode([235, 17, 254, 69]);
13
+ */
14
+ export function c40Decode(c40) {
15
+ let output = "";
16
+ let i1 = null;
17
+ let i2 = null;
18
+ c40.forEach((byte) => {
19
+ if (i1 === null) {
20
+ i1 = byte;
21
+ } else {
22
+ if (i1 === parseInt("0xFE")) {
23
+ output += dataMatrixASCIIToChar(byte);
24
+ } else {
25
+ i2 = byte;
26
+ const I16 = (i1 * 256) + i2;
27
+ const U1 = Math.floor((I16 - 1) / 1600);
28
+ const U2 = Math.floor((I16 - (U1 * 1600) - 1) / 40);
29
+ const U3 = I16 - (U1 * 1600) - (U2 * 40) - 1;
30
+ output += c40ToChar(U1);
31
+ output += c40ToChar(U2);
32
+ if (c40ToChar(U3) !== C40SHIFT1) {
33
+ output += c40ToChar(U3);
34
+ }
35
+ }
36
+ i1 = null;
37
+ i2 = null;
38
+ }
39
+ });
40
+ return output;
41
+ }
42
+
43
+ /**
44
+ * Get the corresponding character for a C40 value.
45
+ * @param { number } char - A number in the range of 0-39.
46
+ * @example
47
+ * // Returns "D"
48
+ * c40ToChar(17);
49
+ */
50
+ function c40ToChar(char) {
51
+ switch (char) {
52
+ case 0:
53
+ return C40SHIFT1;
54
+ case 3:
55
+ return " ";
56
+ case 4:
57
+ return "0";
58
+ case 5:
59
+ return "1";
60
+ case 6:
61
+ return "2";
62
+ case 7:
63
+ return "3";
64
+ case 8:
65
+ return "4";
66
+ case 9:
67
+ return "5";
68
+ case 10:
69
+ return "6";
70
+ case 11:
71
+ return "7";
72
+ case 12:
73
+ return "8";
74
+ case 13:
75
+ return "9";
76
+ case 14:
77
+ return "A";
78
+ case 15:
79
+ return "B";
80
+ case 16:
81
+ return "C";
82
+ case 17:
83
+ return "D";
84
+ case 18:
85
+ return "E";
86
+ case 19:
87
+ return "F";
88
+ case 20:
89
+ return "G";
90
+ case 21:
91
+ return "H";
92
+ case 22:
93
+ return "I";
94
+ case 23:
95
+ return "J";
96
+ case 24:
97
+ return "K";
98
+ case 25:
99
+ return "L";
100
+ case 26:
101
+ return "M";
102
+ case 27:
103
+ return "N";
104
+ case 28:
105
+ return "O";
106
+ case 29:
107
+ return "P";
108
+ case 30:
109
+ return "Q";
110
+ case 31:
111
+ return "R";
112
+ case 32:
113
+ return "S";
114
+ case 33:
115
+ return "T";
116
+ case 34:
117
+ return "U";
118
+ case 35:
119
+ return "V";
120
+ case 36:
121
+ return "W";
122
+ case 37:
123
+ return "X";
124
+ case 38:
125
+ return "Y";
126
+ case 39:
127
+ return "Z";
128
+ default:
129
+ throw new RangeError(
130
+ "A C40 value in Digital Seals must be 0 or in the range of 3-39."
131
+ );
132
+ }
133
+ }
134
+
135
+ /**
136
+ * Get the corresponding character for a DataMatrix ASCII value.
137
+ * @param { number } char - 33, 49-58, or 66-91.
138
+ * @example
139
+ * // Returns "D"
140
+ * dataMatrixASCIIToChar(69);
141
+ */
142
+ function dataMatrixASCIIToChar(char) {
143
+ switch (char) {
144
+ case 33:
145
+ return " ";
146
+ case 49:
147
+ return "0";
148
+ case 50:
149
+ return "1";
150
+ case 51:
151
+ return "2";
152
+ case 52:
153
+ return "3";
154
+ case 53:
155
+ return "4";
156
+ case 54:
157
+ return "5";
158
+ case 55:
159
+ return "6";
160
+ case 56:
161
+ return "7";
162
+ case 57:
163
+ return "8";
164
+ case 58:
165
+ return "9";
166
+ case 66:
167
+ return "A";
168
+ case 67:
169
+ return "B";
170
+ case 68:
171
+ return "C";
172
+ case 69:
173
+ return "D";
174
+ case 70:
175
+ return "E";
176
+ case 71:
177
+ return "F";
178
+ case 72:
179
+ return "G";
180
+ case 73:
181
+ return "H";
182
+ case 74:
183
+ return "I";
184
+ case 75:
185
+ return "J";
186
+ case 76:
187
+ return "K";
188
+ case 77:
189
+ return "L";
190
+ case 78:
191
+ return "M";
192
+ case 79:
193
+ return "N";
194
+ case 80:
195
+ return "O";
196
+ case 81:
197
+ return "P";
198
+ case 82:
199
+ return "Q";
200
+ case 83:
201
+ return "R";
202
+ case 84:
203
+ return "S";
204
+ case 85:
205
+ return "T";
206
+ case 86:
207
+ return "U";
208
+ case 87:
209
+ return "V";
210
+ case 88:
211
+ return "W";
212
+ case 89:
213
+ return "X";
214
+ case 90:
215
+ return "Y";
216
+ case 91:
217
+ return "Z";
218
+ default:
219
+ throw new RangeError(
220
+ "A DataMatrix ASCII value in Digital Seals must be 33, 49-58, or " +
221
+ "66-91."
222
+ );
223
+ }
224
+ }
225
+