memdeklaro/memdeklaro-mirrorpublic⑂ Fork 0
⑂ main
Commits
⬇ Clone ▾
git clone https://git.rickub.com/memdeklaro/memdeklaro-mirror.git
git clone ssh://git@rickub.com/memdeklaro/memdeklaro-mirror.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

Upload files e4de6e7 · on main · user · 2h ago
generator.js · 442 lines · 15.3 KBJavaScript Blame HistoryRaw
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
var loaded_libs = [];

document.addEventListener("DOMContentLoaded", function(event) {
  document.querySelector("#id_number").value = generate_id_number();
  document.querySelector("#issued").value = generate_issuance_date();
  document.querySelector("#expiry").value = generate_expiry_date();
});

function load_image(src) {
  return new Promise((resolve, reject) => {
    let img = new Image();
    img.setAttribute("crossorigin", "anonymous");
    img.onload = () => resolve(img);
    img.onerror = reject;
    img.src = src;
  });
}

function load_lib(name) {
  return new Promise((resolve, reject) => {
    if(loaded_libs.includes(name)) {
      resolve(true);
    }
    if(!loaded_libs.includes(name)) {
      loaded_libs.push(name);
      let script = document.createElement("script");
      script.src = name+".js";
      script.async = true;
      script.onload = () => resolve(true);
      script.onerror = reject;
      document.body.append(script);
    }
  });
}

function generate_id_number() {
  return ""+Math.floor(1000000000 + (Math.random() * 9000000000));
}

function generate_issuance_date() {
  return new Date().getFullYear() + "-" + ("0" + (new Date().getMonth()+1)).slice(-2) + "-" + ("0" + new Date().getDate()).slice(-2);
}

function generate_expiry_date() {
  return (parseInt(new Date().getFullYear()) + 5) + "-" + ("0" + (new Date().getMonth()+1)).slice(-2) + "-" + ("0" + new Date().getDate()).slice(-2);
}

function generate_checksum(str) {
  var counter = 0;
  var total = 0;
  for(var i = 0; i < str.length; i++) {
    if(counter == 3) {
      var counter = 0;
    }
    if(counter == 0) {
      var total = total + (parseInt(str[i]) * 7);
    }
    if(counter == 1) {
      var total = total + (parseInt(str[i]) * 3);
    }
    if(counter == 2) {
      var total = total + (parseInt(str[i]) * 1);
    }
    counter++;
  }
  var check_digit = total % 10;
  return check_digit;
}

function generate_mrz() {

  var first_name = document.querySelector("#first_name").value;
  var first_name = transliteration.transliterate(first_name);
  var first_name = first_name.replace(/[\-\ ]/g, "<").normalize("NFD").replace(/[^A-Z\<]/gi, "").toUpperCase();
  var surname = document.querySelector("#surname").value;
  var surname = transliteration.transliterate(surname);
  var surname = surname.replace(/[\-\ ]/g, "<").normalize("NFD").replace(/[^A-Z\<]/gi, "").toUpperCase();
  var issuer = document.querySelector("#issuer").value;
  var issuer = transliteration.transliterate(issuer);
  var issuer = issuer.replace(/[^A-Z]/gi, "").toUpperCase().slice(0,3);
  var birth_date = document.querySelector("#birth_date").value;
  var birth_date = birth_date.split("-").join("").substring(2);
  var expiry = document.querySelector("#expiry").value;
  var expiry = expiry.split("-").join("").substring(2);
  var id_number = document.querySelector("#id_number").value;

  var final = generate_checksum(id_number + generate_checksum(id_number) + birth_date + generate_checksum(birth_date) + expiry + generate_checksum(expiry));

  var mrz1 = "I<" + issuer + id_number + generate_checksum(id_number) + "<<<<<<<<<<<<<<";
  var mrz2 = birth_date + generate_checksum(birth_date) + "<" + expiry + generate_checksum(expiry) + "<<<<<<<<<<<<<<" + final;
  var mrz3 = surname + "<<" + first_name;
  if(mrz3.length >= 30) {
    var mrz3 = mrz3.slice(0, 29);
  }
  while(mrz3.length < 30) {
    var mrz3 = mrz3 + "<";
  }
  var mrz = mrz1 + "\r\n" + mrz2 + "\r\n" + mrz3;

  return mrz;

}

function generate_aamva() {

  var first_name = document.querySelector("#first_name").value;
  var first_name = transliteration.transliterate(first_name);
  var first_name = first_name.normalize("NFD").replace(/[^A-Z\ \-]/gi, "").toUpperCase();
  var surname = document.querySelector("#surname").value;
  var surname = transliteration.transliterate(surname);
  var surname = surname.normalize("NFD").replace(/[^A-Z\ \-]/gi, "").toUpperCase();
  var issuer = document.querySelector("#issuer").value;
  var issuer = transliteration.transliterate(issuer);
  var issuer = issuer.replace(/[^A-Z]/gi, "").toUpperCase().slice(0,3);
  var birth_date = document.querySelector("#birth_date").value;
  var birth_date = birth_date.split("-").join("");
  var issued = document.querySelector("#issued").value;
  var issued = issued.split("-").join("");
  var expiry = document.querySelector("#expiry").value;
  var expiry = expiry.split("-").join("");
  var id_number = document.querySelector("#id_number").value;

  var header = "@\n\x1E\rANSI 000000000001ID00000000";
  var data = "IDDAQ"+id_number+"\nDCS"+surname+"\nDAC"+first_name+"\nDBD"+issued+"\nDBB"+birth_date+"\nDBA"+expiry+"\nDCG"+issuer+"\r";
  var header_length = (new TextEncoder().encode(header)).length.toString().padStart(4, "0");
  var data_length = (new TextEncoder().encode(data)).length.toString().padStart(4, "0");
  var header = "@\n\x1E\rANSI 000000000001ID"+header_length+data_length;
  var aamva = header+data;

  return aamva;

}

function generate_did() {

  var first_name = document.querySelector("#first_name").value;
  var surname = document.querySelector("#surname").value;
  var birth_date = document.querySelector("#birth_date").value;
  var issued = document.querySelector("#issued").value;
  var expiry = document.querySelector("#expiry").value;
  var id_number = document.querySelector("#id_number").value;
  var note = document.querySelector("#note").value;
  var did_identifier = document.querySelector("#did_identifier").value;

  var did = {
    "@context": [
      "https://www.w3.org/ns/credentials/v2"
    ],
    "type": ["VerifiableCredential", "IDCard"],
    "issuer": {
      "id": did_identifier
    },
    "identifier": id_number,
    "name": "Memdeklaro Self-Declaration of Identity",
    "description": "Generated at https://memdeklaro.org",
    "validFrom": issued+"T00:00:00Z",
    "validUntil": expiry+"T00:00:00Z",
    "credentialSubject": {
      "id": did_identifier,
      "type": ["Person"],
      "givenName": first_name,
      "familyName": surname,
      "birthDate": birth_date,
      "note": note
    }
  };

  return did;

}

function generate_json_ld() {

  var first_name = document.querySelector("#first_name").value;
  var surname = document.querySelector("#surname").value;
  var birth_date = document.querySelector("#birth_date").value;
  var issued = document.querySelector("#issued").value;
  var expiry = document.querySelector("#expiry").value;
  var id_number = document.querySelector("#id_number").value;
  var note = document.querySelector("#note").value;

  var json_ld = {
    "type": ["IDCard"],
    "identifier": id_number,
    "name": "Memdeklaro Self-Declaration of Identity",
    "description": "Generated at https://memdeklaro.org",
    "validFrom": issued+"T00:00:00Z",
    "validUntil": expiry+"T00:00:00Z",
    "credentialSubject": {
      "type": ["Person"],
      "givenName": first_name,
      "familyName": surname,
      "birthDate": birth_date,
      "note": note
    }
  };

  return json_ld;

}

function add_text(ctx, text, size, x, y, emboss=true) {
  ctx.font = "bold "+size+"px OCR-B, monospace";
  ctx.fillStyle = "rgb(0, 0, 0)";
  ctx.textAlign = "left";
  ctx.textBaseline = "top";
  if(emboss) {
    ctx.shadowOffsetX = -3;
    ctx.shadowOffsetY = -3;
    ctx.shadowBlur = 2;
    ctx.shadowColor = "rgba(255,255,255,0.7)";
    ctx.fillText(text, x, y);
    ctx.shadowOffsetX = 3;
    ctx.shadowOffsetY = 3;
    ctx.shadowBlur = 2;
    ctx.shadowColor = "rgba(0,0,0,0.3)";
    ctx.fillText(text, x, y);
    ctx.shadowOffsetX = 0;
    ctx.shadowOffsetY = 0;
    ctx.shadowBlur = 0;
    ctx.shadowColor = "rgba(0,0,0,0)";
  } else {
    ctx.fillText(text, x, y);
  }
}

async function generate_declaration() {

  document.querySelector("#submit").innerText = "Generating (10%)";

  await load_lib("transliterate");

  if(document.querySelector("#generate_pdf417").checked) {
    await load_lib("pdf417");
  }

  document.querySelector("#submit").innerText = "Generating (20%)";

  var surname = document.querySelector("#surname").value.normalize("NFD").toUpperCase();
  var first_name = document.querySelector("#first_name").value.normalize("NFD").toUpperCase();
  var birth_date = document.querySelector("#birth_date").value;
  var issued = document.querySelector("#issued").value;
  var expiry = document.querySelector("#expiry").value;
  var id_number = document.querySelector("#id_number").value;
  var note = document.querySelector("#note").value.normalize("NFD").toUpperCase();
  var issuer = document.querySelector("#issuer").value.normalize("NFD").toUpperCase();
  var mrz = document.querySelector("#mrz").value = generate_mrz();
  var photo = document.querySelector("#photo").files[0];
  var signature = document.querySelector("#signature").files[0];

  document.querySelector("#submit").innerText = "Generating (30%)";

  if(document.querySelector("#generate_did").checked) {
    document.querySelector("#did").value = JSON.stringify(generate_did());
  }

  document.querySelector("#submit").innerText = "Generating (40%)";

  var template_front = await load_image("template_front.jpg");
  var template_back = await load_image("template_back.jpg");
  var add_photo = await load_image(URL.createObjectURL(photo));
  var add_signature = await load_image(URL.createObjectURL(signature));

  document.querySelector("#submit").innerText = "Generating (50%)";

  var font = new FontFace("OCR-B", 'url("OCR-B.ttf")');
  await font.load();
  document.fonts.add(font);
  await document.fonts.load("bold 80px OCR-B");

  document.querySelector("#submit").innerText = "Generating (60%)";

  var canvas_front = document.querySelector("#canvas_front");
  var ctx_front = canvas_front.getContext("2d");
  canvas_front.width = template_front.width;
  canvas_front.height = template_front.height;

  document.querySelector("#submit").innerText = "Generating (70%)";

  ctx_front.drawImage(template_front, 0, 0);

  add_text(ctx_front, surname, 75, 855, 325);
  add_text(ctx_front, first_name, 75, 855, 505);
  add_text(ctx_front, birth_date, 65, 855, 690);
  add_text(ctx_front, issued, 65, 855, 870);
  add_text(ctx_front, expiry, 65, 1415, 870);
  add_text(ctx_front, id_number, 65, 1415, 1050);
  
  var radius = 20;
  var x1 = 58;
  var x2 = 58 + 745;
  var y1 = 260;
  var y2 = 260 + 956;
  ctx_front.beginPath();
  ctx_front.moveTo((x1 + radius), y1);
  ctx_front.arcTo(x2, y1, x2, y2, radius);
  ctx_front.arcTo(x2, y2, x1, y2, radius);
  ctx_front.arcTo(x1, y2, x1, y1, radius);
  ctx_front.arcTo(x1, y1, x2, y1, radius);
  ctx_front.closePath();
  ctx_front.save();
  ctx_front.clip();
  ctx_front.drawImage(add_photo, 58, 260, 745, 956);
  ctx_front.restore();
  
  ctx_front.globalCompositeOperation = "multiply";
  ctx_front.drawImage(add_signature, 870, 1055, Math.round(add_signature.width * (150 / add_signature.height)), 150);
  ctx_front.globalCompositeOperation = "source-over";

  document.querySelector("#submit").innerText = "Generating (80%)";

  var canvas_back = document.querySelector("#canvas_back");
  var ctx_back = canvas_back.getContext("2d");
  canvas_back.width = template_back.width;
  canvas_back.height = template_back.height;
  
  ctx_back.drawImage(template_back, 0, 0);
  
  var note_lines = note.split("\n");
  for(var i = 0; i < note_lines.length; i++) {
    add_text(ctx_back, note_lines[i], 65, 55, 150 + (75 * i));
  }
  add_text(ctx_back, issuer, 75, 55, 410);
  
  document.querySelector("#submit").innerText = "Generating (90%)";

  if(!document.querySelector("#generate_pdf417").checked) {
    var mrz_lines = mrz.split("\n");
    for(var i = 0; i < mrz_lines.length; i++) {
      add_text(ctx_back, mrz_lines[i], 75, 185, 875 + (100 * i), false);
    }
  }

  if(document.querySelector("#generate_pdf417").checked) {
    var aamva = generate_aamva();
    var canvas_barcode = document.createElement("canvas");
    var barcode = PDF417.draw(aamva, canvas_barcode, 1, -1, 10, "rgb(0,0,0)");
    var barcode_image = canvas_barcode.toDataURL("image/png");
    var add_barcode = await load_image(barcode_image);
    ctx_back.drawImage(add_barcode, 155, 865, 1700, 300);
  }

  document.querySelector("#submit").innerText = "Generating (100%)";

  document.querySelector("#generator_front").style.display = "block";
  document.querySelector("#generator_back").style.display = "block"; 
  document.querySelector("#generator_pdf").style.display = "block"; 

  if(document.querySelector("#generate_did").checked) {
    document.querySelector("#generator_did").style.display = "block";
  }

  document.querySelector("#submit").innerText = "Generate";
  document.querySelector("#download_front").innerText = "Download Front";
  document.querySelector("#download_back").innerText = "Download Back";
  document.querySelector("#download_pdf").innerText = "Download PDF";

  return true;

}

function download_front() {
  document.querySelector("#download_front").innerText = "Downloading...";
  var download_link = document.createElement("a");
  download_link.setAttribute("download", "memdeklaro_front.png");
  var canvas = document.querySelector("#canvas_front");
  canvas.toBlob(function(blob) {
    var url = URL.createObjectURL(blob);
    download_link.setAttribute("href", url);
    download_link.click();
    document.querySelector("#download_front").innerText = "Downloaded";
  });
}

function download_back() {
  document.querySelector("#download_back").innerText = "Downloading...";
  var download_link = document.createElement("a");
  download_link.setAttribute("download", "memdeklaro_back.png");
  var canvas = document.querySelector("#canvas_back");
  canvas.toBlob(function(blob) {
    var url = URL.createObjectURL(blob);
    download_link.setAttribute("href", url);
    download_link.click();
    document.querySelector("#download_back").innerText = "Downloaded";
  });
}

async function download_pdf() {

  document.querySelector("#download_pdf").innerText = "Generating (15%)";

  await load_lib("qrcode");
  await load_lib("jspdf");

  document.querySelector("#download_pdf").innerText = "Generating (30%)";

  var copy_front_data = document.querySelector("#canvas_front").toDataURL("image/jpeg");
  var copy_back_data = document.querySelector("#canvas_back").toDataURL("image/jpeg");

  var template_pdf = await load_image("template_pdf.jpg");
  var add_front = await load_image(copy_front_data);
  var add_back = await load_image(copy_back_data);

  document.querySelector("#download_pdf").innerText = "Generating (45%)";

  var canvas_pdf = document.createElement("canvas");
  var ctx_pdf = canvas_pdf.getContext("2d");
  canvas_pdf.width = template_pdf.width;
  canvas_pdf.height = template_pdf.height;
  
  ctx_pdf.drawImage(template_pdf, 0, 0);
 
  ctx_pdf.drawImage(add_front, 734, 620, 1012, 638);
  ctx_pdf.drawImage(add_back, 734, 1466, 1012, 638);

  document.querySelector("#download_pdf").innerText = "Generating (60%)";

  if(document.querySelector("#generate_did").checked) {
    var qr_data = JSON.stringify(generate_did());
  } else {
    var qr_data = JSON.stringify(generate_json_ld());
  }

  var qr_code = await QRCode.toDataURL(qr_data, { width: 800, height: 800 });
  var add_qr_code = await load_image(qr_code);
  ctx_pdf.drawImage(add_qr_code, 840, 2303, 800, 800);

  var copy_canvas = canvas_pdf.toDataURL("image/jpeg");

  document.querySelector("#download_pdf").innerText = "Generating (75%)";

  var doc = new jspdf.jsPDF();
  doc.addImage(copy_canvas, "JPEG", 0, 0, 210, 297);
  doc.save("memdeklaro.pdf");

  document.querySelector("#download_pdf").innerText = "Generating (100%)";

  document.querySelector("#download_pdf").innerText = "Downloaded";

  return true;

}