@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.
- package/LICENSES/CC0-1.0.txt +121 -0
- package/LICENSES/LGPL-3.0-or-later.txt +304 -0
- package/README.md +79 -0
- package/package.json +17 -0
- package/package.json.license +3 -0
- package/src/digitalseal.js +234 -0
- package/src/digitalsealv3.js +359 -0
- package/src/digitalsealv4.js +369 -0
- package/src/index.js +41 -0
- package/src/mrvadocument.js +465 -0
- package/src/mrvadocumentwithvds3.js +559 -0
- package/src/mrvadocumentwithvds4.js +559 -0
- package/src/mrvbdocument.js +466 -0
- package/src/mrvbdocumentwithvds3.js +560 -0
- package/src/mrvbdocumentwithvds4.js +560 -0
- package/src/td1document.js +408 -0
- package/src/td1documentwithvds3.js +479 -0
- package/src/td1documentwithvds4.js +479 -0
- package/src/td2document.js +379 -0
- package/src/td2documentwithvds3.js +466 -0
- package/src/td2documentwithvds4.js +466 -0
- package/src/td3document.js +391 -0
- package/src/td3documentwithvds3.js +466 -0
- package/src/td3documentwithvds4.js +466 -0
- package/src/traveldocument.js +306 -0
- package/src/utilities/bytes-to-date.js +23 -0
- package/src/utilities/c40-decode.js +225 -0
- package/src/utilities/c40-encode.js +254 -0
- package/src/utilities/c40-shift1.js +10 -0
- package/src/utilities/date-to-bytes.js +38 -0
- package/src/utilities/der-length-to-length.js +33 -0
- package/src/utilities/expand-year.js +26 -0
- package/src/utilities/gender-marker-to-mrz.js +14 -0
- package/src/utilities/generate-mrz-check-digit.js +139 -0
- package/src/utilities/icao-date.js +234 -0
- package/src/utilities/length-to-der-length.js +41 -0
- package/src/utilities/nationality-codes.js +303 -0
- package/src/utilities/normalize-mrz-string.js +19 -0
- package/src/utilities/optional-data-mrz.js +29 -0
- package/src/utilities/pad-mrz-string.js +16 -0
- package/src/utilities/set-signature-zone.js +34 -0
- package/src/utilities/stringify-validation-array.js +27 -0
- package/src/utilities/validate-hex-string.js +41 -0
- package/src/utilities/validate-identifier-code.js +38 -0
- package/src/utilities/validate-mrz-string.js +44 -0
- package/src/utilities/vds-magic.js +9 -0
- package/src/utilities/vds-signature-marker.js +9 -0
- package/src/utilities/vds-version-3.js +9 -0
- package/src/utilities/vds-version-4.js +9 -0
- package/src/visadocument.js +114 -0
- package/tests/models/digitalsealv4.test.js +67 -0
- package/tests/models/mrvadocument.test.js +61 -0
- package/tests/models/mrvbdocument.test.js +61 -0
- package/tests/models/td1document.test.js +50 -0
- package/tests/models/td2document.test.js +49 -0
- package/tests/models/td3document.test.js +88 -0
- package/tests/utilities/c40-encodation-scheme.test.js +34 -0
- package/tests/utilities/der-tlv-length-conversion.test.js +99 -0
- package/tests/utilities/document-utilities.test.js +199 -0
- package/tests/utilities/icao-constants.test.js +35 -0
- package/tests/utilities/icao-date.test.js +141 -0
- package/tests/utilities/vds-date-conversions.test.js +28 -0
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2026 Don Geronimo <https://sentamal.in>
|
|
2
|
+
// SPDX-License-Identifier: LGPL-3.0-or-later
|
|
3
|
+
|
|
4
|
+
import { expandYear } from './expand-year.js';
|
|
5
|
+
import { dateToBytes } from './date-to-bytes.js';
|
|
6
|
+
import { bytesToDate } from './bytes-to-date.js';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* A custom date object for ICAO 9303 machine-readable travel documents.
|
|
10
|
+
*
|
|
11
|
+
* Dates in the ICAO 9303 specification only deal with year, month, and day
|
|
12
|
+
* and additionally can have blank values for unknown fields. The dates
|
|
13
|
+
* are rarely stored or displayed in YYYY-MM-DD format, may be displayed
|
|
14
|
+
* in different ways based on the document feature (MRZ, VDS, VIZ), and
|
|
15
|
+
* may be displayed in different ways based on locale.
|
|
16
|
+
*
|
|
17
|
+
* An `IcaoDate` object mimicks some methods and properties of a Date object
|
|
18
|
+
* pertaining to dates for ICAO 9303 document creation and management.
|
|
19
|
+
*/
|
|
20
|
+
export class IcaoDate {
|
|
21
|
+
/**
|
|
22
|
+
* Create an `IcaoDate`.
|
|
23
|
+
* @param { string | Date | number[] | IcaoDate } [value = new Date()] - A date string in YYYY-MM-DD
|
|
24
|
+
* ISO 8601 format, a date string in MRZ format, a Date object, a VDS date array, or
|
|
25
|
+
* another IcaoDate object..
|
|
26
|
+
* @param { number } [maxYear = new Date().getFullYear() + 20] - Upper bound of the
|
|
27
|
+
* 100-year window (inclusive) used when expanding a two-digit year.
|
|
28
|
+
*/
|
|
29
|
+
constructor(value = new Date(), maxYear = new Date().getFullYear() + 20) {
|
|
30
|
+
if (typeof value === 'string') {
|
|
31
|
+
const mrz = /^[0-9<]{6}$/i;
|
|
32
|
+
const iso = /^[0-9x]{4}-[0-9x]{2}-[0-9x]{2}$/i;
|
|
33
|
+
if (mrz.test(value)) {
|
|
34
|
+
const year = value.slice(0, 2);
|
|
35
|
+
const month = value.slice(2, 4).toUpperCase() === '<<' ? value.slice(2, 4).toUpperCase() : Number(value.slice(2, 4)) - 1;
|
|
36
|
+
const date = value.slice(4);
|
|
37
|
+
this.setFullYear(year, month, date, maxYear);
|
|
38
|
+
} else if (iso.test(value)) {
|
|
39
|
+
const year = value.slice(0, 4);
|
|
40
|
+
const month = value.slice(5, 7).toUpperCase() === 'XX' ? value.slice(5, 7).toUpperCase() : Number(value.slice(5, 7)) - 1;
|
|
41
|
+
const date = value.slice(8);
|
|
42
|
+
this.setFullYear(year, month, date);
|
|
43
|
+
} else {
|
|
44
|
+
throw new TypeError('String passed to IcaoDate constructor is an invalid ISO 8601 or MRZ format string.');
|
|
45
|
+
}
|
|
46
|
+
} else if (Object.prototype.toString.call(value) === '[object Date]') {
|
|
47
|
+
if (isNaN(value.valueOf())) {
|
|
48
|
+
throw new TypeError('Date object passed to IcaoDate constructor is an invalid Date.');
|
|
49
|
+
}
|
|
50
|
+
this.setFullYear(value.getFullYear(), value.getMonth(), value.getDate());
|
|
51
|
+
} else if (Array.isArray(value)) {
|
|
52
|
+
const convert = bytesToDate(value);
|
|
53
|
+
if (isNaN(convert.valueOf())) {
|
|
54
|
+
throw new TypeError('Byte array passed to IcaoDate constructor is an invalid VDS byte array.');
|
|
55
|
+
}
|
|
56
|
+
this.setFullYear(convert.getFullYear(), convert.getMonth(), convert.getDate());
|
|
57
|
+
} else if (value instanceof IcaoDate) {
|
|
58
|
+
return value;
|
|
59
|
+
} else {
|
|
60
|
+
throw new TypeError('Parameter passed to IcaoDate constructor is not a supported date format.');
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
#date = null;
|
|
65
|
+
/**
|
|
66
|
+
* Returns the day of the month (1 - 31) for the specified date.
|
|
67
|
+
* @returns { number | null }
|
|
68
|
+
*/
|
|
69
|
+
getDate() { return this.#date; }
|
|
70
|
+
/**
|
|
71
|
+
* Sets the day of the month for a specified date.
|
|
72
|
+
* @param { string | number | null } dateValue
|
|
73
|
+
*/
|
|
74
|
+
setDate(dateValue) {
|
|
75
|
+
this.#setDate(dateValue);
|
|
76
|
+
this.#isValidDate();
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Sets the day of the month for a specified date.
|
|
80
|
+
* @param { string | number | null }
|
|
81
|
+
*/
|
|
82
|
+
#setDate(value) {
|
|
83
|
+
if (value === null || value === 'XX' || value === '<<') {
|
|
84
|
+
this.#date = null;
|
|
85
|
+
} else {
|
|
86
|
+
const date = Number(value);
|
|
87
|
+
if (isNaN(date) || date < 1 || date > 31) {
|
|
88
|
+
throw new RangeError('Invalid day of the month supplied.');
|
|
89
|
+
}
|
|
90
|
+
this.#date = date;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
#month = null;
|
|
95
|
+
/**
|
|
96
|
+
* Returns the month (0 - 11) in the specified date.
|
|
97
|
+
* @returns { number | null }
|
|
98
|
+
*/
|
|
99
|
+
getMonth() { return this.#month; }
|
|
100
|
+
/**
|
|
101
|
+
* Sets the month for a specified date.
|
|
102
|
+
* @param { string | number | null } monthValue
|
|
103
|
+
* @param { string | number | null } [dateValue]
|
|
104
|
+
*/
|
|
105
|
+
setMonth(monthValue, dateValue) {
|
|
106
|
+
this.#setMonth(monthValue);
|
|
107
|
+
if (dateValue !== undefined) { this.#setDate(dateValue); }
|
|
108
|
+
this.#isValidDate();
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Sets the month for a specified date.
|
|
112
|
+
* @param { string | number | null }
|
|
113
|
+
*/
|
|
114
|
+
#setMonth(value) {
|
|
115
|
+
if (value === null || value === 'XX' || value === '<<') {
|
|
116
|
+
this.#month = null;
|
|
117
|
+
} else {
|
|
118
|
+
const month = Number(value);
|
|
119
|
+
if (isNaN(month) || month < 0 || month > 11) {
|
|
120
|
+
throw new RangeError('Invalid month supplied.');
|
|
121
|
+
}
|
|
122
|
+
this.#month = month;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
#year = null;
|
|
127
|
+
/**
|
|
128
|
+
* Returns the year (4 digits for 4-digit years) of the specified date.
|
|
129
|
+
* @returns { number | null }
|
|
130
|
+
*/
|
|
131
|
+
getFullYear() { return this.#year; }
|
|
132
|
+
/**
|
|
133
|
+
* Sets the full year (e.g., 4 digits for 4-digit years) for a specified date.
|
|
134
|
+
* Note if the year is two digits (0-99), the year will be expanded to the
|
|
135
|
+
* current or previous century.
|
|
136
|
+
* @param { string | number | null } yearValue
|
|
137
|
+
* @param { string | number | null } [monthValue]
|
|
138
|
+
* @param { string | number | null } [dateValue]
|
|
139
|
+
* @param { number } [maxYear = new Date().getFullYear() + 20] - Upper bound of the
|
|
140
|
+
* 100-year window (inclusive) used when expanding a two-digit year.
|
|
141
|
+
*/
|
|
142
|
+
setFullYear(yearValue, monthValue, dateValue, maxYear = new Date().getFullYear() + 20) {
|
|
143
|
+
this.#setFullYear(yearValue, maxYear);
|
|
144
|
+
if (monthValue !== undefined) { this.#setMonth(monthValue); }
|
|
145
|
+
if (dateValue !== undefined) { this.#setDate(dateValue); }
|
|
146
|
+
this.#isValidDate();
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Sets the full year (e.g., 4 digits for 4-digit years) for a specified date.
|
|
150
|
+
* Note if the year is two digits (0-99), the year will be expanded to the
|
|
151
|
+
* current or previous century.
|
|
152
|
+
* @param { string | number | null } value
|
|
153
|
+
* @param { number } [maxYear = new Date().getFullYear() + 20] - Upper bound of the
|
|
154
|
+
* 100-year window (inclusive) used when expanding a two-digit year.
|
|
155
|
+
*/
|
|
156
|
+
#setFullYear(value, maxYear = new Date().getFullYear() + 20) {
|
|
157
|
+
if (value === null || value === '<<' || value === 'XXXX') {
|
|
158
|
+
this.#year = null;
|
|
159
|
+
} else {
|
|
160
|
+
const year = Number(value);
|
|
161
|
+
if (isNaN(year) || year < 0) {
|
|
162
|
+
throw new RangeError('Invalid year supplied.');
|
|
163
|
+
}
|
|
164
|
+
const expandedYear = year < 100 ? expandYear(year, maxYear) : year;
|
|
165
|
+
this.#year = expandedYear;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Checks the year, month, and date for correctness.
|
|
171
|
+
*/
|
|
172
|
+
#isValidDate() {
|
|
173
|
+
const year = this.getFullYear() !== null ? this.getFullYear() : 2024;
|
|
174
|
+
const month = this.getMonth() !== null ? this.getMonth() + 1 : 1;
|
|
175
|
+
const date = this.getDate() !== null ? this.getDate() : 1;
|
|
176
|
+
const test = new Date(
|
|
177
|
+
`${String(year).padStart(4, '0')}-` +
|
|
178
|
+
`${String(month).padStart(2, '0')}-` +
|
|
179
|
+
String(date).padStart(2, '0') +
|
|
180
|
+
'T00:00:00'
|
|
181
|
+
);
|
|
182
|
+
if (isNaN(test.valueOf()) || test.getFullYear() !== year || test.getMonth() + 1 !== month || test.getDate() !== date) {
|
|
183
|
+
this.#invalidateDate();
|
|
184
|
+
throw new RangeError('Attempted date change results in invalid date.');
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Invalidate the date object by setting all values to null.
|
|
190
|
+
*/
|
|
191
|
+
#invalidateDate() {
|
|
192
|
+
this.#date = null;
|
|
193
|
+
this.#month = null;
|
|
194
|
+
this.#year = null;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Returns a string representing the ICAODate object.
|
|
199
|
+
* @returns { string }
|
|
200
|
+
*/
|
|
201
|
+
toString() { return this.toISOString(); }
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Converts an IcaoDate to a string following the ISO 8601 Extended format, but with
|
|
205
|
+
* unknown (null) date variables displaying as X's.
|
|
206
|
+
* @returns { string }
|
|
207
|
+
*/
|
|
208
|
+
toISOString() {
|
|
209
|
+
const year = this.getFullYear() === null ? 'XXXX' : String(this.getFullYear()).padStart(4, '0');
|
|
210
|
+
const month = this.getMonth() === null ? 'XX' : String(this.getMonth() + 1).padStart(2, '0');
|
|
211
|
+
const date = this.getDate() === null ? 'XX' : String(this.getDate()).padStart(2, '0');
|
|
212
|
+
return `${year}-${month}-${date}`;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Converts an IcaoDate to a string following the Machine-Readable Zone format.
|
|
217
|
+
* @returns { string }
|
|
218
|
+
*/
|
|
219
|
+
toMRZString() {
|
|
220
|
+
return this.toISOString().slice(2).replace(/-/gi, '').replace(/x/gi, '<');
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Converts an IcaoDate to a byte sequence following the VDS date byte format.
|
|
225
|
+
* @returns { number[] }
|
|
226
|
+
*/
|
|
227
|
+
toVDSArray() {
|
|
228
|
+
if (this.getDate() === null || this.getMonth() === null || this.getFullYear() === null) {
|
|
229
|
+
throw new RangeError('All date variables (date, month, year) must be set, known, and valid to create a VDS date byte sequence.');
|
|
230
|
+
}
|
|
231
|
+
return dateToBytes(this.toISOString());
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2026 Don Geronimo <https://sentamal.in>
|
|
2
|
+
// SPDX-License-Identifier: LGPL-3.0-or-later
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Get a length in the shortest BER/DER definite form for a number.
|
|
6
|
+
* @param { number } length
|
|
7
|
+
* @example
|
|
8
|
+
* // Returns [130, 1, 179]
|
|
9
|
+
* DigitalSeal.lengthToDERLength(435);
|
|
10
|
+
*/
|
|
11
|
+
export function lengthToDERLength(length) {
|
|
12
|
+
if (length < 128) {
|
|
13
|
+
return [length];
|
|
14
|
+
} else {
|
|
15
|
+
const output = [];
|
|
16
|
+
let base2 = length.toString(2);
|
|
17
|
+
if (base2.length % 8 !== 0) {
|
|
18
|
+
base2 = base2.padStart((Math.floor(base2.length / 8) + 1) * 8, "0");
|
|
19
|
+
}
|
|
20
|
+
if (base2.length / 8 > 4) {
|
|
21
|
+
throw new RangeError(
|
|
22
|
+
"The definite long-form length value for this TLV is too big for " +
|
|
23
|
+
"the context of ICAO 9303 Digital Seals."
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
output.push(
|
|
27
|
+
parseInt(`1${(base2.length / 8).toString(2).padStart(7, "0")}`, 2)
|
|
28
|
+
);
|
|
29
|
+
const b = [0, 0, 0, 0, 0, 0, 0, 0];
|
|
30
|
+
[...base2].forEach((bit, i) => {
|
|
31
|
+
b[i % 8] = bit;
|
|
32
|
+
if ((i + 1) % 8 === 0) {
|
|
33
|
+
output.push(parseInt(
|
|
34
|
+
`${b[0]}${b[1]}${b[2]}${b[3]}${b[4]}${b[5]}${b[6]}${b[7]}`, 2
|
|
35
|
+
));
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
return output;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2026 Don Geronimo <https://sentamal.in>
|
|
2
|
+
// SPDX-License-Identifier: LGPL-3.0-or-later
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* A list of 3-digit nationality codes available in ISO 3166-1 and
|
|
6
|
+
* ICAO 9303-3.
|
|
7
|
+
* @readonly
|
|
8
|
+
* @enum { Symbol }
|
|
9
|
+
*/
|
|
10
|
+
export const NATIONALITY_CODES = Object.freeze({
|
|
11
|
+
// Official ISO 3166-1 alpha-3 nationality codes
|
|
12
|
+
ABW: Symbol.for("Aruba"),
|
|
13
|
+
AFG: Symbol.for("Afghanistan"),
|
|
14
|
+
AGO: Symbol.for("Angola"),
|
|
15
|
+
AIA: Symbol.for("Anguilla"),
|
|
16
|
+
ALA: Symbol.for("Åland Islands"),
|
|
17
|
+
ALB: Symbol.for("Albania"),
|
|
18
|
+
AND: Symbol.for("Andorra"),
|
|
19
|
+
ARE: Symbol.for("United Arab Emirates"),
|
|
20
|
+
ARG: Symbol.for("Argentina"),
|
|
21
|
+
ARM: Symbol.for("Armenia"),
|
|
22
|
+
ASM: Symbol.for("American Samoa"),
|
|
23
|
+
ATA: Symbol.for("Antarctica"),
|
|
24
|
+
ATF: Symbol.for("French Southern Territories"),
|
|
25
|
+
ATG: Symbol.for("Antigua and Barbuda"),
|
|
26
|
+
AUS: Symbol.for("Australia"),
|
|
27
|
+
AUT: Symbol.for("Austria"),
|
|
28
|
+
AZE: Symbol.for("Azerbaijan"),
|
|
29
|
+
BDI: Symbol.for("Burundi"),
|
|
30
|
+
BEL: Symbol.for("Belgium"),
|
|
31
|
+
BEN: Symbol.for("Benin"),
|
|
32
|
+
BES: Symbol.for("Bonaire, Sint Eustatius, and Saba"),
|
|
33
|
+
BFA: Symbol.for("Burkina Faso"),
|
|
34
|
+
BGD: Symbol.for("Bangladesh"),
|
|
35
|
+
BGR: Symbol.for("Bulgaria"),
|
|
36
|
+
BHR: Symbol.for("Bahrain"),
|
|
37
|
+
BHS: Symbol.for("Bahamas"),
|
|
38
|
+
BIH: Symbol.for("Bosnia and Herzegovina"),
|
|
39
|
+
BLM: Symbol.for("Saint Barthélemy"),
|
|
40
|
+
BLR: Symbol.for("Belarus"),
|
|
41
|
+
BLZ: Symbol.for("Belize"),
|
|
42
|
+
BMU: Symbol.for("Bermuda"),
|
|
43
|
+
BOL: Symbol.for("Bolivia (Plurinational State of)"),
|
|
44
|
+
BRA: Symbol.for("Brazil"),
|
|
45
|
+
BRB: Symbol.for("Barbados"),
|
|
46
|
+
BRN: Symbol.for("Brunei Darussalam"),
|
|
47
|
+
BTN: Symbol.for("Bhutan"),
|
|
48
|
+
BVT: Symbol.for("Bouvet Island"),
|
|
49
|
+
BWA: Symbol.for("Botswana"),
|
|
50
|
+
CAF: Symbol.for("Central African Republic"),
|
|
51
|
+
CAN: Symbol.for("Canada"),
|
|
52
|
+
CCK: Symbol.for("Cocos (Keeling) Islands"),
|
|
53
|
+
CHE: Symbol.for("Switzerland"),
|
|
54
|
+
CHL: Symbol.for("Chile"),
|
|
55
|
+
CHN: Symbol.for("China"),
|
|
56
|
+
CIV: Symbol.for("Côte d'Ivoire"),
|
|
57
|
+
CMR: Symbol.for("Cameroon"),
|
|
58
|
+
COD: Symbol.for("Congo, Democratic Republic of the"),
|
|
59
|
+
COG: Symbol.for("Congo"),
|
|
60
|
+
COK: Symbol.for("Cook Islands"),
|
|
61
|
+
COL: Symbol.for("Colombia"),
|
|
62
|
+
COM: Symbol.for("Comoros"),
|
|
63
|
+
CPV: Symbol.for("Cabo Verde"),
|
|
64
|
+
CRI: Symbol.for("Costa Rica"),
|
|
65
|
+
CUB: Symbol.for("Cuba"),
|
|
66
|
+
CUW: Symbol.for("Curaçao"),
|
|
67
|
+
CXR: Symbol.for("Christmas Island"),
|
|
68
|
+
CYM: Symbol.for("Cayman Islands"),
|
|
69
|
+
CYP: Symbol.for("Cyprus"),
|
|
70
|
+
CZE: Symbol.for("Czechia"),
|
|
71
|
+
DEU: Symbol.for("Germany"),
|
|
72
|
+
DJI: Symbol.for("Djibouti"),
|
|
73
|
+
DMA: Symbol.for("Dominica"),
|
|
74
|
+
DNK: Symbol.for("Denmark"),
|
|
75
|
+
DOM: Symbol.for("Dominican Republic"),
|
|
76
|
+
DZA: Symbol.for("Algeria"),
|
|
77
|
+
ECU: Symbol.for("Ecuador"),
|
|
78
|
+
EGY: Symbol.for("Egypt"),
|
|
79
|
+
ERI: Symbol.for("Eritrea"),
|
|
80
|
+
ESH: Symbol.for("Western Sahara"),
|
|
81
|
+
ESP: Symbol.for("Spain"),
|
|
82
|
+
EST: Symbol.for("Estonia"),
|
|
83
|
+
ETH: Symbol.for("Ethiopia"),
|
|
84
|
+
FIN: Symbol.for("Finland"),
|
|
85
|
+
FJI: Symbol.for("Fiji"),
|
|
86
|
+
FLK: Symbol.for("Falkland Islands (Malvinas)"),
|
|
87
|
+
FRA: Symbol.for("France"),
|
|
88
|
+
FRO: Symbol.for("Faroe Islands"),
|
|
89
|
+
FSM: Symbol.for("Micronesia (Federated States of)"),
|
|
90
|
+
GAB: Symbol.for("Gabon"),
|
|
91
|
+
GBR: Symbol.for("United Kingdom of Great Britain and Northern Ireland"),
|
|
92
|
+
GEO: Symbol.for("Georgia"),
|
|
93
|
+
GGY: Symbol.for("Guernsey"),
|
|
94
|
+
GHA: Symbol.for("Ghana"),
|
|
95
|
+
GIB: Symbol.for("Gibraltar"),
|
|
96
|
+
GIN: Symbol.for("Guinea"),
|
|
97
|
+
GLP: Symbol.for("Guadeloupe"),
|
|
98
|
+
GMB: Symbol.for("Gambia"),
|
|
99
|
+
GNB: Symbol.for("Guinea-Bissau"),
|
|
100
|
+
GNQ: Symbol.for("Equatorial Guinea"),
|
|
101
|
+
GRC: Symbol.for("Greece"),
|
|
102
|
+
GRD: Symbol.for("Grenada"),
|
|
103
|
+
GRL: Symbol.for("Greenland"),
|
|
104
|
+
GTM: Symbol.for("Guatemala"),
|
|
105
|
+
GUF: Symbol.for("French Guiana"),
|
|
106
|
+
GUM: Symbol.for("Guam"),
|
|
107
|
+
GUY: Symbol.for("Guyana"),
|
|
108
|
+
HKG: Symbol.for("Hong Kong"),
|
|
109
|
+
HMD: Symbol.for("Heard Island and McDonald Islands"),
|
|
110
|
+
HND: Symbol.for("Honduras"),
|
|
111
|
+
HRV: Symbol.for("Croatia"),
|
|
112
|
+
HTI: Symbol.for("Haiti"),
|
|
113
|
+
HUN: Symbol.for("Hungary"),
|
|
114
|
+
IDN: Symbol.for("Indonesia"),
|
|
115
|
+
IMN: Symbol.for("Isle of Man"),
|
|
116
|
+
IND: Symbol.for("India"),
|
|
117
|
+
IOT: Symbol.for("British Indian Ocean Territory"),
|
|
118
|
+
IRL: Symbol.for("Ireland"),
|
|
119
|
+
IRN: Symbol.for("Iran (Islamic Republic of)"),
|
|
120
|
+
IRQ: Symbol.for("Iraq"),
|
|
121
|
+
ISL: Symbol.for("Iceland"),
|
|
122
|
+
ISR: Symbol.for("Israel"),
|
|
123
|
+
ITA: Symbol.for("Italy"),
|
|
124
|
+
JAM: Symbol.for("Jamaica"),
|
|
125
|
+
JEY: Symbol.for("Jersey"),
|
|
126
|
+
JOR: Symbol.for("Jordan"),
|
|
127
|
+
JPN: Symbol.for("Japan"),
|
|
128
|
+
KAZ: Symbol.for("Kazakhstan"),
|
|
129
|
+
KEN: Symbol.for("Kenya"),
|
|
130
|
+
KGZ: Symbol.for("Kyrgyzstan"),
|
|
131
|
+
KHM: Symbol.for("Cambodia"),
|
|
132
|
+
KIR: Symbol.for("Kiribati"),
|
|
133
|
+
KNA: Symbol.for("Saint Kitts and Nevis"),
|
|
134
|
+
KOR: Symbol.for("Korea, Republic of"),
|
|
135
|
+
KWT: Symbol.for("Kuwait"),
|
|
136
|
+
LAO: Symbol.for("Lao People's Democratic Republic"),
|
|
137
|
+
LBN: Symbol.for("Lebanon"),
|
|
138
|
+
LBR: Symbol.for("Liberia"),
|
|
139
|
+
LBY: Symbol.for("Libya"),
|
|
140
|
+
LCA: Symbol.for("Saint Lucia"),
|
|
141
|
+
LIE: Symbol.for("Liechtenstein"),
|
|
142
|
+
LKA: Symbol.for("Sri Lanka"),
|
|
143
|
+
LSO: Symbol.for("Lesotho"),
|
|
144
|
+
LTU: Symbol.for("Lithuania"),
|
|
145
|
+
LUX: Symbol.for("Luxembourg"),
|
|
146
|
+
LVA: Symbol.for("Latvia"),
|
|
147
|
+
MAC: Symbol.for("Macao"),
|
|
148
|
+
MAF: Symbol.for("Saint Martin (French part)"),
|
|
149
|
+
MAR: Symbol.for("Morocco"),
|
|
150
|
+
MCO: Symbol.for("Monaco"),
|
|
151
|
+
MDA: Symbol.for("Moldova, Republic of"),
|
|
152
|
+
MDG: Symbol.for("Madagascar"),
|
|
153
|
+
MDV: Symbol.for("Maldives"),
|
|
154
|
+
MEX: Symbol.for("Mexico"),
|
|
155
|
+
MHL: Symbol.for("Marshall Islands"),
|
|
156
|
+
MKD: Symbol.for("North Macedonia"),
|
|
157
|
+
MLI: Symbol.for("Mali"),
|
|
158
|
+
MLT: Symbol.for("Malta"),
|
|
159
|
+
MMR: Symbol.for("Myanmar"),
|
|
160
|
+
MNE: Symbol.for("Montenegro"),
|
|
161
|
+
MNG: Symbol.for("Mongolia"),
|
|
162
|
+
MNP: Symbol.for("Northern Mariana Islands"),
|
|
163
|
+
MOZ: Symbol.for("Mozambique"),
|
|
164
|
+
MRT: Symbol.for("Mauritania"),
|
|
165
|
+
MSR: Symbol.for("Montserrat"),
|
|
166
|
+
MTQ: Symbol.for("Martinique"),
|
|
167
|
+
MUS: Symbol.for("Mauritius"),
|
|
168
|
+
MWI: Symbol.for("Malawi"),
|
|
169
|
+
MYS: Symbol.for("Malaysia"),
|
|
170
|
+
MYT: Symbol.for("Mayotte"),
|
|
171
|
+
NAM: Symbol.for("Namibia"),
|
|
172
|
+
NCL: Symbol.for("New Caledonia"),
|
|
173
|
+
NER: Symbol.for("Niger"),
|
|
174
|
+
NFK: Symbol.for("Norfolk Island"),
|
|
175
|
+
NGA: Symbol.for("Nigeria"),
|
|
176
|
+
NIC: Symbol.for("Nicaragua"),
|
|
177
|
+
NIU: Symbol.for("Niue"),
|
|
178
|
+
NLD: Symbol.for("Netherlands, Kingdom of the"),
|
|
179
|
+
NOR: Symbol.for("Norway"),
|
|
180
|
+
NPL: Symbol.for("Nepal"),
|
|
181
|
+
NRU: Symbol.for("Nauru"),
|
|
182
|
+
NZL: Symbol.for("New Zealand"),
|
|
183
|
+
OMN: Symbol.for("Oman"),
|
|
184
|
+
PAK: Symbol.for("Pakistan"),
|
|
185
|
+
PAN: Symbol.for("Panama"),
|
|
186
|
+
PCN: Symbol.for("Pitcairn"),
|
|
187
|
+
PER: Symbol.for("Peru"),
|
|
188
|
+
PHL: Symbol.for("Philippines"),
|
|
189
|
+
PLW: Symbol.for("Palau"),
|
|
190
|
+
PNG: Symbol.for("Papua New Guinea"),
|
|
191
|
+
POL: Symbol.for("Poland"),
|
|
192
|
+
PRI: Symbol.for("Puerto Rico"),
|
|
193
|
+
PRK: Symbol.for("Korea (Democratic People's Republic of)"),
|
|
194
|
+
PRT: Symbol.for("Portugal"),
|
|
195
|
+
PRY: Symbol.for("Paraguay"),
|
|
196
|
+
PSE: Symbol.for("Palestine, State of"),
|
|
197
|
+
PYF: Symbol.for("French Polynesia"),
|
|
198
|
+
QAT: Symbol.for("Qatar"),
|
|
199
|
+
REU: Symbol.for("Réunion"),
|
|
200
|
+
ROU: Symbol.for("Romania"),
|
|
201
|
+
RUS: Symbol.for("Russian Federation"),
|
|
202
|
+
RWA: Symbol.for("Rwanda"),
|
|
203
|
+
SAU: Symbol.for("Saudi Arabia"),
|
|
204
|
+
SDN: Symbol.for("Sudan"),
|
|
205
|
+
SEN: Symbol.for("Senegal"),
|
|
206
|
+
SGP: Symbol.for("Singapore"),
|
|
207
|
+
SGS: Symbol.for("South Georgia and the South Sandwich Islands"),
|
|
208
|
+
SHN: Symbol.for("Saint Helena, Ascension, and Tristan de Cunha"),
|
|
209
|
+
SJM: Symbol.for("Svalbard and Jan Mayen"),
|
|
210
|
+
SLB: Symbol.for("Solomon Islands"),
|
|
211
|
+
SLE: Symbol.for("Sierra Leone"),
|
|
212
|
+
SLV: Symbol.for("El Salvador"),
|
|
213
|
+
SMR: Symbol.for("San Marino"),
|
|
214
|
+
SOM: Symbol.for("Somalia"),
|
|
215
|
+
SPM: Symbol.for("Saint Pierre and Miquelon"),
|
|
216
|
+
SRB: Symbol.for("Serbia"),
|
|
217
|
+
SSD: Symbol.for("South Sudan"),
|
|
218
|
+
STP: Symbol.for("Sao Tome and Principe"),
|
|
219
|
+
SUR: Symbol.for("Suriname"),
|
|
220
|
+
SVK: Symbol.for("Slovakia"),
|
|
221
|
+
SVN: Symbol.for("Slovenia"),
|
|
222
|
+
SWE: Symbol.for("Sweden"),
|
|
223
|
+
SWZ: Symbol.for("Eswatini"),
|
|
224
|
+
SXM: Symbol.for("Sint Maarten (Dutch part)"),
|
|
225
|
+
SYC: Symbol.for("Seychelles"),
|
|
226
|
+
SYR: Symbol.for("Syrian Arab Republic"),
|
|
227
|
+
TCA: Symbol.for("Turks and Caicos Islands"),
|
|
228
|
+
TCD: Symbol.for("Chad"),
|
|
229
|
+
TGO: Symbol.for("Togo"),
|
|
230
|
+
THA: Symbol.for("Thailand"),
|
|
231
|
+
TJK: Symbol.for("Tajikistan"),
|
|
232
|
+
TKL: Symbol.for("Tokelau"),
|
|
233
|
+
TKM: Symbol.for("Turkmenistan"),
|
|
234
|
+
TLS: Symbol.for("Timor-Leste"),
|
|
235
|
+
TON: Symbol.for("Tonga"),
|
|
236
|
+
TTO: Symbol.for("Trinidad and Tobago"),
|
|
237
|
+
TUN: Symbol.for("Tunisia"),
|
|
238
|
+
TUR: Symbol.for("Türkiye"),
|
|
239
|
+
TUV: Symbol.for("Tuvalu"),
|
|
240
|
+
TWN: Symbol.for("Taiwan, Province of China"),
|
|
241
|
+
TZA: Symbol.for("Tanzania, United Republic of"),
|
|
242
|
+
UGA: Symbol.for("Uganda"),
|
|
243
|
+
UKR: Symbol.for("Ukraine"),
|
|
244
|
+
UMI: Symbol.for("United States Minor Outlying Islands"),
|
|
245
|
+
URY: Symbol.for("Uruguay"),
|
|
246
|
+
USA: Symbol.for("United States of America"),
|
|
247
|
+
UZB: Symbol.for("Uzbekistan"),
|
|
248
|
+
VAT: Symbol.for("Holy See"),
|
|
249
|
+
VCT: Symbol.for("Saint Vincent and the Grenadines"),
|
|
250
|
+
VEN: Symbol.for("Venezuela (Bolivarian Republic of)"),
|
|
251
|
+
VGB: Symbol.for("Virgin Islands (British)"),
|
|
252
|
+
VIR: Symbol.for("Virgin Islands (U.S.)"),
|
|
253
|
+
VNM: Symbol.for("Viet Nam"),
|
|
254
|
+
VUT: Symbol.for("Vanuatu"),
|
|
255
|
+
WLF: Symbol.for("Wallis and Futuna"),
|
|
256
|
+
WSM: Symbol.for("Samoa"),
|
|
257
|
+
YEM: Symbol.for("Yemen"),
|
|
258
|
+
ZAF: Symbol.for("South Africa"),
|
|
259
|
+
ZMB: Symbol.for("Zambia"),
|
|
260
|
+
ZWE: Symbol.for("Zimbabwe"),
|
|
261
|
+
|
|
262
|
+
// Codes not included in ISO 3166-1
|
|
263
|
+
GBD: Symbol.for("British Overseas Territories Citizen"),
|
|
264
|
+
GBN: Symbol.for("British National (Overseas)"),
|
|
265
|
+
GBO: Symbol.for("British Overseas Citizen"),
|
|
266
|
+
GBP: Symbol.for("British Protected Person"),
|
|
267
|
+
GBS: Symbol.for("British Subject"),
|
|
268
|
+
RKS: Symbol.for("Republic of Kosovo"),
|
|
269
|
+
|
|
270
|
+
// Codes reserved by ISO 3166/MA
|
|
271
|
+
EUE: Symbol.for("European Union (EU)"),
|
|
272
|
+
|
|
273
|
+
// Codes used in UN Travel Documents
|
|
274
|
+
UNO: Symbol.for("United Nations Organization"),
|
|
275
|
+
UNA: Symbol.for("United Nations Specialized Agency"),
|
|
276
|
+
UNK: Symbol.for("United Nations Interim Administration Mission in Kosovo"),
|
|
277
|
+
|
|
278
|
+
// Other issuing authorities
|
|
279
|
+
XBA: Symbol.for("African Development Bank (ADB)"),
|
|
280
|
+
XIM: Symbol.for("African Export-Import Bank (AFREXIM)"),
|
|
281
|
+
XCC: Symbol.for("Caribbean Community (CARICOM)"),
|
|
282
|
+
XCE: Symbol.for("Council of Europe"),
|
|
283
|
+
XCO: Symbol.for("Common Market for Eastern and Southern Africa (COMESA)"),
|
|
284
|
+
XEC: Symbol.for("Economic Community of West African States (ECOWAS"),
|
|
285
|
+
XPO: Symbol.for("International Criminal Police Organization (INTERPOL)"),
|
|
286
|
+
XES: Symbol.for("Organization of Eastern Caribbean States (OECS)"),
|
|
287
|
+
XMP: Symbol.for("Parliamentary Assembly of the Mediterranean (PAM)"),
|
|
288
|
+
XOM: Symbol.for("Sovereign Military Order of Malta"),
|
|
289
|
+
XDC: Symbol.for("Southern African Development Community"),
|
|
290
|
+
|
|
291
|
+
// Persons without a defined nationality
|
|
292
|
+
XXA: Symbol.for("Stateless Person"),
|
|
293
|
+
XXB: Symbol.for("Refugee (defined in Article 1 of the 1951 Convention)"),
|
|
294
|
+
XXC: Symbol.for("Refugee (not defined under code XXB)"),
|
|
295
|
+
XXX: Symbol.for("Person of Unspecified Nationality"),
|
|
296
|
+
|
|
297
|
+
// Specimen Documents
|
|
298
|
+
UTO: Symbol.for("Utopia"),
|
|
299
|
+
|
|
300
|
+
// Used by ICAO
|
|
301
|
+
IAO: Symbol.for("International Civil Aviation Organization (ICAO)"),
|
|
302
|
+
});
|
|
303
|
+
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2026 Don Geronimo <https://sentamal.in>
|
|
2
|
+
// SPDX-License-Identifier: LGPL-3.0-or-later
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Remove diacritics and punctuation from a Machine-Readable Zone (MRZ)
|
|
6
|
+
* string.
|
|
7
|
+
* @param { string } string
|
|
8
|
+
* @example
|
|
9
|
+
* // Returns "ADRIAN<CLAUDE<DEVELEAU"
|
|
10
|
+
* TravelDocument.normalizeMRZString("ADRIAN-CLAUDE D'EVELEAU");
|
|
11
|
+
*/
|
|
12
|
+
export function normalizeMRZString(string) {
|
|
13
|
+
const NORMALIZE = string.normalize("NFD").replace(/\p{Diacritic}/gu,"");
|
|
14
|
+
const REPLACE_COMMA_AND_DASH =
|
|
15
|
+
NORMALIZE.replace(/'/gi,"").replace(/-/gi,"<");
|
|
16
|
+
const REPLACE_SPACE_AND_COMMA =
|
|
17
|
+
REPLACE_COMMA_AND_DASH.replace(/ /gi,"<").replace(/,/gi,"");
|
|
18
|
+
return REPLACE_SPACE_AND_COMMA.toUpperCase();
|
|
19
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2026 Don Geronimo <https://sentamal.in>
|
|
2
|
+
// SPDX-License-Identifier: LGPL-3.0-or-later
|
|
3
|
+
|
|
4
|
+
import { normalizeMRZString } from "./normalize-mrz-string.js";
|
|
5
|
+
import { padMRZString } from "./pad-mrz-string.js";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Normalize and pad optional data for the optional data area of a
|
|
9
|
+
* Machine-Readable Zone (MRZ) of a given character length.
|
|
10
|
+
* @param { string } data - Optional data to include in the Machine-Readable
|
|
11
|
+
* Zone (MRZ). Valid characters are from the ranges 0-9 and A-Z.
|
|
12
|
+
* @param { number } length - The number of characters available for the
|
|
13
|
+
* optional data in a Machine-Readable Zone (MRZ).
|
|
14
|
+
* @example
|
|
15
|
+
* // Returns "EXAMPLE<<<<<<<"
|
|
16
|
+
* optionalDataMRZ("EXAMPLE", 14);
|
|
17
|
+
*/
|
|
18
|
+
export function optionalDataMRZ(data, length) {
|
|
19
|
+
const NORMALIZED_DATA = normalizeMRZString(data);
|
|
20
|
+
if (NORMALIZED_DATA.length > length) {
|
|
21
|
+
console.warn(
|
|
22
|
+
`Optional data (optionalData) is longer than ${length} and will be ` +
|
|
23
|
+
`truncated.`
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
return padMRZString(
|
|
27
|
+
NORMALIZED_DATA.substring(0,length), length
|
|
28
|
+
);
|
|
29
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2026 Don Geronimo <https://sentamal.in>
|
|
2
|
+
// SPDX-License-Identifier: LGPL-3.0-or-later
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Pad the end of a Machine-Readable Zone (MRZ) string to the desired length
|
|
6
|
+
* with a filler character.
|
|
7
|
+
* @param { string } string
|
|
8
|
+
* @param { number } length
|
|
9
|
+
* @example
|
|
10
|
+
* // Returns "ALFALFA<<"
|
|
11
|
+
* TravelDocument.padMRZString("ALFALFA", 9);
|
|
12
|
+
*/
|
|
13
|
+
export function padMRZString(string, length) {
|
|
14
|
+
return string.padEnd(length, "<").toUpperCase();
|
|
15
|
+
}
|
|
16
|
+
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2026 Don Geronimo <https://sentamal.in>
|
|
2
|
+
// SPDX-License-Identifier: LGPL-3.0-or-later
|
|
3
|
+
|
|
4
|
+
import { VDS_SIGNATURE_MARKER } from "./vds-signature-marker.js";
|
|
5
|
+
import { derLengthToLength } from "./der-length-to-length.js";
|
|
6
|
+
import { lengthToDERLength } from "./length-to-der-length.js";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Given a point 'start' in an array 'value', extract the raw signature data.
|
|
10
|
+
* @param { number } start
|
|
11
|
+
* @param { number[] } value
|
|
12
|
+
*/
|
|
13
|
+
export function setSignatureZone(start, value) {
|
|
14
|
+
if (value[start] !== VDS_SIGNATURE_MARKER) {
|
|
15
|
+
throw new TypeError(
|
|
16
|
+
`Value '${value[start].toString(16).padStart(2, "0").toUpperCase()}' ` +
|
|
17
|
+
`does not match signature marker (` +
|
|
18
|
+
`${VDS_SIGNATURE_MARKER.toString(16).padStart(
|
|
19
|
+
2, "0"
|
|
20
|
+
).toUpperCase()}).`
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
start += 1;
|
|
24
|
+
const LENGTH = derLengthToLength(value.slice(start));
|
|
25
|
+
start += lengthToDERLength(LENGTH).length;
|
|
26
|
+
if (value.slice(start).length !== LENGTH) {
|
|
27
|
+
throw new RangeError(
|
|
28
|
+
`Length '${LENGTH}' of signature does not match the actual length ` +
|
|
29
|
+
`(${value.slice(start).length}).`
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
return value.slice(start);
|
|
33
|
+
}
|
|
34
|
+
|