@gerege-systems/eid-mongolia-sdk 0.30.1 → 0.31.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/dist/pades.js CHANGED
@@ -7,8 +7,10 @@ import {
7
7
  PRef,
8
8
  PdfError,
9
9
  Reader,
10
+ appearanceObjectCount,
10
11
  appearanceObjects,
11
12
  appendVerifyPage,
13
+ checkPdfImage,
12
14
  checkStructure,
13
15
  defaultStampPage,
14
16
  escapeName,
@@ -23,7 +25,7 @@ import {
23
25
  visibleRect,
24
26
  visibleWidget,
25
27
  zlibInflateStrict
26
- } from "./chunk-MA4RMWBO.js";
28
+ } from "./chunk-M4NS4UFP.js";
27
29
  import {
28
30
  CLAIMED_ROLE_MAX_RUNES,
29
31
  CertificateRevokedError,
@@ -59,7 +61,7 @@ import {
59
61
  } from "./chunk-ISTWNY6N.js";
60
62
  import {
61
63
  EidError
62
- } from "./chunk-H7JOACAZ.js";
64
+ } from "./chunk-R73IR6OH.js";
63
65
 
64
66
  // src/pades/limits.ts
65
67
  var MAX_INPUT_BYTES = (10 << 20) - (256 << 10);
@@ -1108,7 +1110,7 @@ async function prepareSignature(base, signingTime, signerName, reason, vis) {
1108
1110
  const sigID = inc.reserve();
1109
1111
  const fieldID = inc.reserve();
1110
1112
  const catID = inc.reserve();
1111
- const apIDs = vis ? Array.from({ length: 11 }, () => inc.reserve()) : [];
1113
+ const apIDs = vis ? Array.from({ length: appearanceObjectCount(vis) }, () => inc.reserve()) : [];
1112
1114
  let sig = `<<
1113
1115
  /Type /Sig
1114
1116
  /Filter /Adobe.PPKLite
@@ -1150,7 +1152,10 @@ async function prepareSignature(base, signingTime, signerName, reason, vis) {
1150
1152
  placement = placeOnPage(page, box, rot, vis.x, vis.y, vis.w, vis.h, vis.exact === true);
1151
1153
  if (placement.x !== vis.x || placement.y !== vis.y) rect = visibleRect(box, rot, placement.x, placement.y, vis.w, vis.h);
1152
1154
  }
1153
- const bodies = appearanceObjects({ w: vis.w, h: vis.h, rotate: rot, name: vis.name, role: vis.role, lang: vis.lang, date: vis.date, url: vis.url }, apIDs);
1155
+ const bodies = appearanceObjects(
1156
+ { w: vis.w, h: vis.h, rotate: rot, name: vis.name, role: vis.role, lang: vis.lang, date: vis.date, url: vis.url, sig: vis.sig, stamp: vis.stamp },
1157
+ apIDs
1158
+ );
1154
1159
  inc.writeObject(fieldID, visibleWidget(fieldName, rect, page.ptr.id, page.ptr.gen, sigID, apIDs[0]));
1155
1160
  bodies.forEach((b2, i) => inc.writeObject(apIDs[i], b2));
1156
1161
  }
@@ -1495,6 +1500,7 @@ function encodeState(s) {
1495
1500
  };
1496
1501
  if (s.pageSha256) head.pageSha256 = s.pageSha256;
1497
1502
  if (s.claimedRole) head.claimedRole = s.claimedRole;
1503
+ if (s.images?.length) head.images = s.images.map((x) => ({ kind: x.kind, sha256: x.sha256 }));
1498
1504
  head.preparedSha256 = s.preparedSha256;
1499
1505
  const h = enc.encode(JSON.stringify(head));
1500
1506
  const len = new Uint8Array(4);
@@ -1520,6 +1526,7 @@ async function decodeState(b) {
1520
1526
  hasPage: j.hasPage === true,
1521
1527
  pageSha256: str("pageSha256"),
1522
1528
  claimedRole: str("claimedRole"),
1529
+ images: stateImages(j.images),
1523
1530
  preparedSha256: str("preparedSha256"),
1524
1531
  pdf
1525
1532
  };
@@ -1529,6 +1536,16 @@ async function decodeState(b) {
1529
1536
  }
1530
1537
  return s;
1531
1538
  }
1539
+ function stateImages(v) {
1540
+ if (v === void 0 || v === null) return void 0;
1541
+ if (!Array.isArray(v)) throw new Error("state images \u0431\u0443\u0440\u0443\u0443");
1542
+ const out = v.map((x) => {
1543
+ const o = x;
1544
+ if (!o || o.kind !== "signature" && o.kind !== "stamp" || typeof o.sha256 !== "string") throw new Error("state images \u0431\u0443\u0440\u0443\u0443");
1545
+ return { kind: o.kind, sha256: o.sha256 };
1546
+ });
1547
+ return out.length ? out : void 0;
1548
+ }
1532
1549
 
1533
1550
  // src/pades.ts
1534
1551
  var PROFILE_VERSION = "pades-client/1";
@@ -1563,6 +1580,21 @@ var PadesError = class extends EidError {
1563
1580
  return this.code === "TSA_UNAVAILABLE" || this.code === "LTV_UNAVAILABLE";
1564
1581
  }
1565
1582
  };
1583
+ var DEFAULT_STAMP_SIZE = { width: 220, height: 64 };
1584
+ var DEFAULT_SIGNATURE_STAMP_SIZE = { width: 220, height: 110 };
1585
+ var DEFAULT_ORG_STAMP_SIZE = { width: 240, height: 147.5 };
1586
+ function appearanceSize(a) {
1587
+ if (a.width !== 0 || a.height !== 0) return { width: a.width, height: a.height };
1588
+ if (a.stampImage) return DEFAULT_ORG_STAMP_SIZE;
1589
+ if (a.signatureImage) return DEFAULT_SIGNATURE_STAMP_SIZE;
1590
+ return DEFAULT_STAMP_SIZE;
1591
+ }
1592
+ var SHA256_HEX_RE = /^[0-9a-f]{64}$/;
1593
+ var BASE64_STD_RE = /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/;
1594
+ function imageData(im) {
1595
+ if (im === void 0) return void 0;
1596
+ return { w: im.width, h: im.height, color: im.color, alpha: fromB64(im.alpha) };
1597
+ }
1566
1598
  var MAX_OUTPUT_BYTES = 10 << 20;
1567
1599
  var MAX_FILE_NAME = 120;
1568
1600
  var PAGE_BRAND = "eID Mongolia";
@@ -1662,6 +1694,9 @@ async function prepare(pdf, opts) {
1662
1694
  }
1663
1695
  const first = pos === "first";
1664
1696
  checkAppearance(opts);
1697
+ if (opts.appearance?.stampImage && !opts.claimedRole) {
1698
+ throw new PadesError("REQUEST_INVALID", "appearance: stampImage \u0437\u04E9\u0432\u0445\u04E9\u043D claimedRole (onBehalfOf)-\u0442\u043E\u0439");
1699
+ }
1665
1700
  const now = opts.signingTime ?? /* @__PURE__ */ new Date();
1666
1701
  if (Number.isNaN(now.getTime())) throw new PadesError("REQUEST_INVALID", "signingTime \u0431\u0443\u0440\u0443\u0443 Date");
1667
1702
  const inHex = toHex(await sha256(doc));
@@ -1735,6 +1770,7 @@ async function prepare(pdf, opts) {
1735
1770
  hasPage,
1736
1771
  pageSha256: pageHex,
1737
1772
  claimedRole: role,
1773
+ images: prepImages(opts.appearance),
1738
1774
  preparedSha256: toHex(await sha256(prep.pdf)),
1739
1775
  pdf: prep.pdf
1740
1776
  });
@@ -1758,6 +1794,24 @@ function checkAppearance(o) {
1758
1794
  if (a.exact !== void 0 && typeof a.exact !== "boolean") throw bad("exact: boolean");
1759
1795
  const role = o.claimedRole ?? "";
1760
1796
  if (role !== "" && /[\p{Cc}\p{Cf}\p{Cs}]/u.test(role)) throw bad("claimedRole-\u0434 \u0443\u0434\u0438\u0440\u0434\u0430\u0445 \u0442\u044D\u043C\u0434\u044D\u0433\u0442");
1797
+ for (const [name, im] of [["signatureImage", a.signatureImage], ["stampImage", a.stampImage]]) {
1798
+ if (im === void 0) continue;
1799
+ if (im === null || typeof im !== "object") throw bad(`${name}: \u043E\u0431\u044A\u0435\u043A\u0442`);
1800
+ if (typeof im.sha256 !== "string" || !SHA256_HEX_RE.test(im.sha256)) throw bad(`${name}.sha256: \u0436\u0438\u0436\u0438\u0433 hex SHA-256`);
1801
+ if (typeof im.alpha !== "string" || !BASE64_STD_RE.test(im.alpha)) throw bad(`${name}.alpha: \u0441\u0442\u0430\u043D\u0434\u0430\u0440\u0442 base64`);
1802
+ try {
1803
+ checkPdfImage(imageData(im));
1804
+ } catch (e) {
1805
+ if (e instanceof AppearanceError) throw bad(`${name}: ${e.message.replace(/^зураг: /, "")}`);
1806
+ throw e;
1807
+ }
1808
+ }
1809
+ }
1810
+ function prepImages(a) {
1811
+ const out = [];
1812
+ if (a?.signatureImage) out.push({ kind: "signature", sha256: a.signatureImage.sha256 });
1813
+ if (a?.stampImage) out.push({ kind: "stamp", sha256: a.stampImage.sha256 });
1814
+ return out.length ? out : void 0;
1761
1815
  }
1762
1816
  function visibleSpec(doc, o, name, now, docId, revIndex) {
1763
1817
  const a = o.appearance;
@@ -1772,12 +1826,15 @@ function visibleSpec(doc, o, name, now, docId, revIndex) {
1772
1826
  if (page > n) throw new PadesError("REQUEST_INVALID", `appearance: page ${page} > \u0445\u0443\u0443\u0434\u0430\u0441\u043D\u044B \u0442\u043E\u043E ${n}`);
1773
1827
  const z = new Date(now.getTime() + 8 * 36e5);
1774
1828
  const p2 = (v) => String(v).padStart(2, "0");
1829
+ const size = appearanceSize(a);
1775
1830
  return {
1776
1831
  page,
1777
1832
  x: round100(a.x),
1778
1833
  y: round100(a.y),
1779
- w: round100(a.width),
1780
- h: round100(a.height),
1834
+ w: round100(size.width),
1835
+ h: round100(size.height),
1836
+ sig: imageData(a.signatureImage),
1837
+ stamp: imageData(a.stampImage),
1781
1838
  name: goTrim(name),
1782
1839
  role: o.claimedRole ?? "",
1783
1840
  lang: a.lang || o.lang || "mn",
@@ -1810,8 +1867,9 @@ async function suggestPlacement(pdf, appearance) {
1810
1867
  const id = docIdFromPage(doc);
1811
1868
  if (id !== "") verifyUri = VERIFY_URL_PREFIX + id;
1812
1869
  }
1813
- const w = round100(appearance.width);
1814
- const h = round100(appearance.height);
1870
+ const size = appearanceSize(appearance);
1871
+ const w = round100(size.width);
1872
+ const h = round100(size.height);
1815
1873
  let s;
1816
1874
  try {
1817
1875
  s = suggestVisible(doc, verifyUri, appearance.page ?? 0, round100(appearance.x), round100(appearance.y), w, h, appearance.exact === true);
@@ -1972,11 +2030,15 @@ async function complete(state, signature, opts = {}) {
1972
2030
  priorCmsSha256: prior,
1973
2031
  priorRevisionSha256: priorRev,
1974
2032
  signerCert: cert.raw,
1975
- claimedRole: st.claimedRole
2033
+ claimedRole: st.claimedRole,
2034
+ ...st.images?.length ? { images: st.images.map((x) => ({ ...x })) } : {}
1976
2035
  };
1977
2036
  }
1978
2037
  export {
1979
2038
  CertificateRevokedError,
2039
+ DEFAULT_ORG_STAMP_SIZE,
2040
+ DEFAULT_SIGNATURE_STAMP_SIZE,
2041
+ DEFAULT_STAMP_SIZE,
1980
2042
  PADES_ERROR_CODES,
1981
2043
  PROFILE_VERSION,
1982
2044
  PadesError,
package/dist/validate.cjs CHANGED
@@ -1749,6 +1749,27 @@ function zlibInflate(src, limit = 64 << 20, initial) {
1749
1749
  function inflateRaw(src, limit = 64 << 20, info, initial) {
1750
1750
  return inflate(src, limit, info, initial, false);
1751
1751
  }
1752
+ function zlibInflateStrict(src, limit, exact = false) {
1753
+ if (src.length < 2) throw new InflateError("zlib: header \u0430\u043B\u0433\u0430");
1754
+ const cmf = src[0];
1755
+ const flg = src[1];
1756
+ if ((cmf & 15) !== 8 || cmf >> 4 > 7 || (cmf << 8 | flg) % 31 !== 0) throw new InflateError("zlib: header \u0431\u0443\u0440\u0443\u0443");
1757
+ let start = 2;
1758
+ if (flg & 32) {
1759
+ if (src.length < 6) throw new InflateError("zlib: DICTID \u0434\u0443\u0442\u0443\u0443");
1760
+ if (src[2] !== 0 || src[3] !== 0 || src[4] !== 0 || src[5] !== 1) throw new InflateError("zlib: preset dictionary");
1761
+ start = 6;
1762
+ }
1763
+ const info = { end: 0, truncated: false };
1764
+ const out = inflate(src.subarray(start), limit, info, void 0, true);
1765
+ if (info.truncated) throw new InflateError("inflate: \u04E9\u0433\u04E9\u0433\u0434\u04E9\u043B \u0434\u0443\u0442\u0443\u0443");
1766
+ const at = start + info.end;
1767
+ if (at + 4 > src.length) throw new InflateError("zlib: adler32 \u0430\u043B\u0433\u0430");
1768
+ const want = (src[at] << 24 | src[at + 1] << 16 | src[at + 2] << 8 | src[at + 3]) >>> 0;
1769
+ if (adler32(out) !== want) throw new InflateError("zlib: adler32 \u0442\u0430\u0430\u0440\u0430\u0445\u0433\u04AF\u0439");
1770
+ if (exact && at + 4 !== src.length) throw new InflateError("zlib: stream-\u0438\u0439\u043D \u0430\u0440\u0434 \u0438\u043B\u04AF\u04AF \u0431\u0430\u0439\u0442");
1771
+ return out;
1772
+ }
1752
1773
  function adler32(b) {
1753
1774
  let a = 1;
1754
1775
  let s = 0;
@@ -3287,6 +3308,61 @@ function toUnicode(f) {
3287
3308
  // src/pades/page/appearance.ts
3288
3309
  var AppearanceError = class extends PdfError {
3289
3310
  };
3311
+ var MAX_IMAGE_SIDE = 1600;
3312
+ var MAX_IMAGE_ALPHA_BYTES = 300 << 10;
3313
+ var isIntIn = (v, lo, hi) => Number.isInteger(v) && v >= lo && v <= hi;
3314
+ function checkPdfImage(im) {
3315
+ const bad = (msg) => new AppearanceError(`\u0437\u0443\u0440\u0430\u0433: ${msg}`);
3316
+ if (!isIntIn(im.w, 1, MAX_IMAGE_SIDE) || !isIntIn(im.h, 1, MAX_IMAGE_SIDE)) throw bad("\u0445\u044D\u043C\u0436\u044D\u044D 1\u20261600 px");
3317
+ if (!Array.isArray(im.color) || im.color.length !== 3) throw bad("\u04E9\u043D\u0433\u04E9 0\u2026255");
3318
+ for (const c of im.color) if (!isIntIn(c, 0, 255)) throw bad("\u04E9\u043D\u0433\u04E9 0\u2026255");
3319
+ if (!(im.alpha instanceof Uint8Array) || im.alpha.length === 0 || im.alpha.length > MAX_IMAGE_ALPHA_BYTES) throw bad("alpha 1 B\u2026300 KiB");
3320
+ if (im.alpha.length >= 2 && im.alpha[1] & 32) throw bad("alpha preset dictionary (FDICT)");
3321
+ let n;
3322
+ try {
3323
+ n = zlibInflateStrict(im.alpha, im.w * im.h, true).length;
3324
+ } catch (e) {
3325
+ if (!(e instanceof InflateError)) throw e;
3326
+ n = -1;
3327
+ }
3328
+ if (n !== im.w * im.h) throw bad("alpha \u043D\u044C \u044F\u0433 \u04E9\u0440\u0433\u04E9\u043D\xD7\u04E9\u043D\u0434\u04E9\u0440 \u0431\u0430\u0439\u0442 \u0431\u0438\u0448 (\u044D\u0441\u0432\u044D\u043B \u044D\u0432\u0434\u044D\u0440\u0441\u044D\u043D)");
3329
+ }
3330
+ var idiv = (a, b) => Math.trunc(a / b);
3331
+ function apFit(iw, ih, bw, bh) {
3332
+ if (iw * bh >= ih * bw) return [bw, Math.max(1, idiv(bw * ih, iw))];
3333
+ return [Math.max(1, idiv(bh * iw, ih)), bh];
3334
+ }
3335
+ function apLayoutImages(p) {
3336
+ const l = { boxH: p.h, rowH: p.h, sig: [0, 0, 0, 0], stamp: [0, 0, 0, 0] };
3337
+ if (!p.sig && !p.stamp) return l;
3338
+ const small = () => new AppearanceError("\u0445\u044D\u0442 \u0436\u0438\u0436\u0438\u0433 (\u0437\u0443\u0440\u0430\u0433\u0442\u0430\u0439 \u0434\u04AF\u0440\u0441\u043B\u044D\u043B)");
3339
+ if (p.stamp) l.boxH = idiv(p.h * 4, 5);
3340
+ const band = idiv(l.boxH * 9, 22);
3341
+ l.rowH = l.boxH - band;
3342
+ const sh = idiv(band * 14, 15);
3343
+ const sy = l.rowH - idiv(band * 2, 15);
3344
+ let sw = idiv(p.w * 15, 22);
3345
+ let sx = idiv(p.w - sw, 2);
3346
+ if (p.stamp) {
3347
+ const d = idiv(l.boxH * 2, 3);
3348
+ const qx = Math.min(idiv(p.w * 41, 60) - idiv(d, 2), p.w - d);
3349
+ if (qx < 0 || d <= 0) throw small();
3350
+ const [dw, dh] = apFit(p.stamp.w, p.stamp.h, d, d);
3351
+ l.stamp = [qx + idiv(d - dw, 2), p.h - d + idiv(d - dh, 2), dw, dh];
3352
+ sx = idiv(p.w, 30);
3353
+ sw = qx + idiv(d, 2) - sx;
3354
+ }
3355
+ if (p.sig) {
3356
+ if (sw <= 0 || sh <= 0) throw small();
3357
+ const [dw, dh] = apFit(p.sig.w, p.sig.h, sw, sh);
3358
+ const dx = p.stamp ? sx : sx + idiv(sw - dw, 2);
3359
+ l.sig = [dx, sy + idiv(sh - dh, 2), dw, dh];
3360
+ }
3361
+ return l;
3362
+ }
3363
+ function apDrawImage(w, name, r) {
3364
+ w.op(`q ${num100(r[2])} 0 0 ${num100(r[3])} ${num100(r[0])} ${num100(r[1])} cm /${name} Do Q`);
3365
+ }
3290
3366
  var AP_PAD = 400;
3291
3367
  var AP_GAP = 400;
3292
3368
  var AP_MIN_MODULE = 100;
@@ -3331,6 +3407,10 @@ function appearanceContent(p) {
3331
3407
  if (label === void 0) throw new AppearanceError(`\u0445\u044D\u043B ${p.lang}`);
3332
3408
  if (!asciiURL(p.url)) throw new AppearanceError("URL ASCII \u0431\u0438\u0448");
3333
3409
  if (p.w <= 0 || p.h <= 0 || p.w > MAX_APPEARANCE_SIDE || p.h > MAX_APPEARANCE_SIDE) throw new AppearanceError("\u0445\u044D\u043C\u0436\u044D\u044D");
3410
+ if (p.stamp && p.role === "") throw new AppearanceError("\u0442\u0430\u043C\u0433\u044B\u043D \u0437\u0443\u0440\u0430\u0433 \u0437\u04E9\u0432\u0445\u04E9\u043D \u0431\u0430\u0439\u0433\u0443\u0443\u043B\u043B\u0430\u0433\u044B\u043D \u043D\u044D\u0440\u0438\u0439\u043D \u04E9\u043C\u043D\u04E9\u04E9\u0441 (claimedRole)");
3411
+ for (const im of [p.sig, p.stamp]) if (im) checkPdfImage(im);
3412
+ const il = apLayoutImages(p);
3413
+ const rowH = il.rowH;
3334
3414
  let m;
3335
3415
  try {
3336
3416
  m = qrEncode(latin1(p.url));
@@ -3338,14 +3418,14 @@ function appearanceContent(p) {
3338
3418
  throw new AppearanceError(String(e));
3339
3419
  }
3340
3420
  const n = m.length;
3341
- const mod = Math.trunc((p.h - 2 * AP_PAD) / n);
3421
+ const mod = Math.trunc((rowH - 2 * AP_PAD) / n);
3342
3422
  if (mod < AP_MIN_MODULE) throw new AppearanceError(`\u0445\u044D\u0442 \u0436\u0438\u0436\u0438\u0433 (\u04E9\u043D\u0434\u04E9\u0440 \u2265 ${num100(2 * AP_PAD + n * AP_MIN_MODULE)} pt)`);
3343
3423
  const tx = AP_PAD + mod * n + AP_GAP;
3344
3424
  const tw = p.w - tx - AP_PAD;
3345
3425
  const [reg, bld] = loadFonts();
3346
3426
  const w = new PageWriter(reg, bld);
3347
3427
  let lines;
3348
- for (let f = AP_MAX_SIZE; f >= AP_MIN_SIZE && lines === void 0 && tw > 0; f--) lines = apLayout(w, p, label, f, tw);
3428
+ for (let f = AP_MAX_SIZE; f >= AP_MIN_SIZE && lines === void 0 && tw > 0; f--) lines = apLayout(w, p, label, f, tw, rowH);
3349
3429
  if (lines === void 0) throw new AppearanceError("\u0445\u044D\u0442 \u0436\u0438\u0436\u0438\u0433 (\u0431\u0438\u0447\u0432\u044D\u0440 \u0431\u0430\u0433\u0442\u0430\u0445\u0433\u04AF\u0439)");
3350
3430
  let total = 0;
3351
3431
  for (const l of lines) total += l.size * AP_LEADING;
@@ -3357,8 +3437,13 @@ function appearanceContent(p) {
3357
3437
  }
3358
3438
  w.op(`${C_OK} RG`);
3359
3439
  w.op("1 w");
3360
- w.op(`0.5 0.5 ${num100(p.w - 100)} ${num100(p.h - 100)} re S`);
3361
- w.op(`q ${num100(mod)} 0 0 ${num100(mod)} ${num100(AP_PAD)} ${num100(Math.trunc((p.h - mod * n) / 2))} cm`);
3440
+ w.op(`0.5 0.5 ${num100(p.w - 100)} ${num100(il.boxH - 100)} re S`);
3441
+ if (p.sig || p.stamp) {
3442
+ w.op("0.5 w");
3443
+ w.op(`0 ${num100(rowH)} m ${num100(p.w)} ${num100(rowH)} l S`);
3444
+ }
3445
+ if (p.sig) apDrawImage(w, "Sig", il.sig);
3446
+ w.op(`q ${num100(mod)} 0 0 ${num100(mod)} ${num100(AP_PAD)} ${num100(Math.trunc((rowH - mod * n) / 2))} cm`);
3362
3447
  w.op("0 0 0 rg");
3363
3448
  for (let r = 0; r < n; r++) {
3364
3449
  for (let c = 0; c < n; ) {
@@ -3373,17 +3458,18 @@ function appearanceContent(p) {
3373
3458
  }
3374
3459
  w.op("f");
3375
3460
  w.op("Q");
3376
- let cursor = p.h - AP_PAD - Math.trunc((p.h - 2 * AP_PAD - total) / 2);
3461
+ let cursor = rowH - AP_PAD - Math.trunc((rowH - 2 * AP_PAD - total) / 2);
3377
3462
  for (const l of lines) {
3378
3463
  cursor -= l.size * AP_LEADING;
3379
3464
  w.op(`${l.color} rg`);
3380
3465
  w.op(`BT /F${l.font} ${l.size} Tf ${num100(tx)} ${num100(cursor + l.size * AP_DESCENT)} Td ${w.glyphHex(l.font, l.text)} Tj ET`);
3381
3466
  }
3467
+ if (p.stamp) apDrawImage(w, "Stamp", il.stamp);
3382
3468
  if (cm !== "") w.op("Q");
3383
3469
  w.op("EMC");
3384
3470
  return w.ops;
3385
3471
  }
3386
- function apLayout(w, p, label, f, tw) {
3472
+ function apLayout(w, p, label, f, tw, rowH) {
3387
3473
  const b = f - AP_BODY_DELTA;
3388
3474
  const limit = tw * 10;
3389
3475
  const lines = [];
@@ -3397,20 +3483,40 @@ function apLayout(w, p, label, f, tw) {
3397
3483
  if (l.text === "" || w.width1000(l.font, l.size, l.text) > limit) return void 0;
3398
3484
  total += l.size * AP_LEADING;
3399
3485
  }
3400
- if (total > p.h - 2 * AP_PAD) return void 0;
3486
+ if (total > rowH - 2 * AP_PAD) return void 0;
3401
3487
  return lines;
3402
3488
  }
3489
+ function appearanceObjectCount(p) {
3490
+ return 11 + (p.sig ? 2 : 0) + (p.stamp ? 2 : 0);
3491
+ }
3403
3492
  function appearanceObjects(p, ids) {
3493
+ if (ids.length !== appearanceObjectCount(p)) throw new AppearanceError("\u043E\u0431\u044A\u0435\u043A\u0442\u044B\u043D \u0442\u043E\u043E");
3404
3494
  const [reg, bld] = loadFonts();
3405
3495
  const { bw, bh } = apRotation(p.rotate, p.w, p.h);
3406
3496
  const content = appearanceContent(p);
3497
+ let res = `/Font << /F1 ${ids[1]} 0 R /F2 ${ids[6]} 0 R >>`;
3498
+ const imgs = [];
3499
+ let next = 11;
3500
+ if (p.sig || p.stamp) {
3501
+ res += " /XObject <<";
3502
+ for (const [name, im] of [["Sig", p.sig], ["Stamp", p.stamp]]) {
3503
+ if (!im) continue;
3504
+ const base = ids[next];
3505
+ const mask = ids[next + 1];
3506
+ next += 2;
3507
+ res += ` /${name} ${base} 0 R`;
3508
+ imgs.push(
3509
+ streamObj(` /Type /XObject /Subtype /Image /Width 1 /Height 1 /ColorSpace /DeviceRGB /BitsPerComponent 8 /SMask ${mask} 0 R`, Uint8Array.from(im.color)),
3510
+ streamObj(` /Type /XObject /Subtype /Image /Width ${im.w} /Height ${im.h} /ColorSpace /DeviceGray /BitsPerComponent 8 /Filter /FlateDecode`, im.alpha)
3511
+ );
3512
+ }
3513
+ res += " >>";
3514
+ }
3407
3515
  return [
3408
- streamObj(
3409
- ` /Type /XObject /Subtype /Form /BBox [0 0 ${num100(bw)} ${num100(bh)}] /Resources << /Font << /F1 ${ids[1]} 0 R /F2 ${ids[6]} 0 R >> >>`,
3410
- latin1(content)
3411
- ),
3516
+ streamObj(` /Type /XObject /Subtype /Form /BBox [0 0 ${num100(bw)} ${num100(bh)}] /Resources << ${res} >>`, latin1(content)),
3412
3517
  ...fontBodies(ids.slice(1, 6), reg),
3413
- ...fontBodies(ids.slice(6, 11), bld)
3518
+ ...fontBodies(ids.slice(6, 11), bld),
3519
+ ...imgs
3414
3520
  ];
3415
3521
  }
3416
3522
  function visibleWidget(fieldName, rect, pageID, pageGen, sigID, formID) {
@@ -4233,7 +4339,7 @@ function ownVisible(c, d, widget) {
4233
4339
  const m = ms.v;
4234
4340
  if (!ownDate(m) || m.slice(16) !== "+08'00'") throw fail();
4235
4341
  const date = `${m.slice(2, 6)}-${m.slice(6, 8)}-${m.slice(8, 10)} ${m.slice(10, 12)}:${m.slice(12, 14)} (+08)`;
4236
- const ids = apIDs(c, nr);
4342
+ const { ids, sig, stamp } = apIDs(c, nr);
4237
4343
  const form = c.fin.get(ids[0]);
4238
4344
  if (!(form instanceof PStream2)) throw fail();
4239
4345
  let head = "";
@@ -4245,7 +4351,7 @@ function ownVisible(c, d, widget) {
4245
4351
  for (const lang of ["mn", "en"]) {
4246
4352
  let bodies;
4247
4353
  try {
4248
- bodies = appearanceObjects({ w, h, rotate: rot, name, role: meta.role, lang, date, url }, ids);
4354
+ bodies = appearanceObjects({ w, h, rotate: rot, name, role: meta.role, lang, date, url, sig, stamp }, ids);
4249
4355
  } catch {
4250
4356
  throw fail();
4251
4357
  }
@@ -4293,6 +4399,21 @@ function apIDs(c, n) {
4293
4399
  if (!(ff instanceof PRef2)) throw fail();
4294
4400
  refs.push(t0, cid, fd, ff, tu);
4295
4401
  }
4402
+ let sig;
4403
+ let stamp;
4404
+ if (isDict(res) && res.has("XObject")) {
4405
+ const xo = dget(res, "XObject");
4406
+ if (!isDict(xo)) throw fail();
4407
+ for (const key of ["Sig", "Stamp"]) {
4408
+ if (!xo.has(key)) continue;
4409
+ const br = dget(xo, key);
4410
+ if (!(br instanceof PRef2)) throw fail();
4411
+ const { im, mr } = apImage(c, br);
4412
+ refs.push(br, mr);
4413
+ if (key === "Sig") sig = im;
4414
+ else stamp = im;
4415
+ }
4416
+ }
4296
4417
  const seen = /* @__PURE__ */ new Set();
4297
4418
  const ids = [];
4298
4419
  for (const r of refs) {
@@ -4300,7 +4421,21 @@ function apIDs(c, n) {
4300
4421
  seen.add(r.num);
4301
4422
  ids.push(r.num);
4302
4423
  }
4303
- return ids;
4424
+ return { ids, sig, stamp };
4425
+ }
4426
+ function apImage(c, br) {
4427
+ const fail = errNotSignatureOnly;
4428
+ const base = c.fin.deref(br);
4429
+ if (!(base instanceof PStream2) || base.data.length !== 3) throw fail();
4430
+ const mr = dget(base.dict, "SMask");
4431
+ if (!(mr instanceof PRef2)) throw fail();
4432
+ const mask = c.fin.deref(mr);
4433
+ if (!(mask instanceof PStream2)) throw fail();
4434
+ const w = dget(mask.dict, "Width");
4435
+ const h = dget(mask.dict, "Height");
4436
+ const side = BigInt(MAX_IMAGE_SIDE);
4437
+ if (!isInt2(w) || !isInt2(h) || w < 1n || h < 1n || w > side || h > side) throw fail();
4438
+ return { im: { w: Number(w), h: Number(h), color: [base.data[0], base.data[1], base.data[2]], alpha: mask.data }, mr };
4304
4439
  }
4305
4440
  var isSpaceByte = (x) => x === 9 || x === 10 || x === 11 || x === 12 || x === 13 || x === 32;
4306
4441
  function sameObject(c, num, body) {
@@ -1,4 +1,4 @@
1
- import { E as EidError } from './errors-CxAr-WIA.cjs';
1
+ import { E as EidError } from './errors-3JEfE-FW.cjs';
2
2
  import { F as FetchFn } from './fetch-BjfH46q9.cjs';
3
3
 
4
4
  type Indication = "valid" | "invalid" | "indeterminate";
@@ -1,4 +1,4 @@
1
- import { E as EidError } from './errors-CxAr-WIA.js';
1
+ import { E as EidError } from './errors-3JEfE-FW.js';
2
2
  import { F as FetchFn } from './fetch-BjfH46q9.js';
3
3
 
4
4
  type Indication = "valid" | "invalid" | "indeterminate";
package/dist/validate.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import {
2
2
  Fetcher,
3
3
  InflateError,
4
+ MAX_IMAGE_SIDE,
4
5
  PRef,
5
6
  Reader,
6
7
  StructureError,
@@ -11,7 +12,7 @@ import {
11
12
  isBrowser,
12
13
  round100,
13
14
  visibleWidget
14
- } from "./chunk-MA4RMWBO.js";
15
+ } from "./chunk-M4NS4UFP.js";
15
16
  import {
16
17
  EID_ISSUERS,
17
18
  INTERMEDIATES,
@@ -65,7 +66,7 @@ import {
65
66
  } from "./chunk-ISTWNY6N.js";
66
67
  import {
67
68
  EidError
68
- } from "./chunk-H7JOACAZ.js";
69
+ } from "./chunk-R73IR6OH.js";
69
70
 
70
71
  // src/pades/validate/evaluate.ts
71
72
  var PROBLEMS = {
@@ -1408,7 +1409,7 @@ function ownVisible(c, d, widget) {
1408
1409
  const m = ms.v;
1409
1410
  if (!ownDate(m) || m.slice(16) !== "+08'00'") throw fail();
1410
1411
  const date = `${m.slice(2, 6)}-${m.slice(6, 8)}-${m.slice(8, 10)} ${m.slice(10, 12)}:${m.slice(12, 14)} (+08)`;
1411
- const ids = apIDs(c, nr);
1412
+ const { ids, sig, stamp } = apIDs(c, nr);
1412
1413
  const form = c.fin.get(ids[0]);
1413
1414
  if (!(form instanceof PStream)) throw fail();
1414
1415
  let head = "";
@@ -1420,7 +1421,7 @@ function ownVisible(c, d, widget) {
1420
1421
  for (const lang of ["mn", "en"]) {
1421
1422
  let bodies;
1422
1423
  try {
1423
- bodies = appearanceObjects({ w, h, rotate: rot, name, role: meta.role, lang, date, url }, ids);
1424
+ bodies = appearanceObjects({ w, h, rotate: rot, name, role: meta.role, lang, date, url, sig, stamp }, ids);
1424
1425
  } catch {
1425
1426
  throw fail();
1426
1427
  }
@@ -1468,6 +1469,21 @@ function apIDs(c, n) {
1468
1469
  if (!(ff instanceof PRef2)) throw fail();
1469
1470
  refs.push(t0, cid, fd, ff, tu);
1470
1471
  }
1472
+ let sig;
1473
+ let stamp;
1474
+ if (isDict(res) && res.has("XObject")) {
1475
+ const xo = dget(res, "XObject");
1476
+ if (!isDict(xo)) throw fail();
1477
+ for (const key of ["Sig", "Stamp"]) {
1478
+ if (!xo.has(key)) continue;
1479
+ const br = dget(xo, key);
1480
+ if (!(br instanceof PRef2)) throw fail();
1481
+ const { im, mr } = apImage(c, br);
1482
+ refs.push(br, mr);
1483
+ if (key === "Sig") sig = im;
1484
+ else stamp = im;
1485
+ }
1486
+ }
1471
1487
  const seen = /* @__PURE__ */ new Set();
1472
1488
  const ids = [];
1473
1489
  for (const r of refs) {
@@ -1475,7 +1491,21 @@ function apIDs(c, n) {
1475
1491
  seen.add(r.num);
1476
1492
  ids.push(r.num);
1477
1493
  }
1478
- return ids;
1494
+ return { ids, sig, stamp };
1495
+ }
1496
+ function apImage(c, br) {
1497
+ const fail = errNotSignatureOnly;
1498
+ const base = c.fin.deref(br);
1499
+ if (!(base instanceof PStream) || base.data.length !== 3) throw fail();
1500
+ const mr = dget(base.dict, "SMask");
1501
+ if (!(mr instanceof PRef2)) throw fail();
1502
+ const mask = c.fin.deref(mr);
1503
+ if (!(mask instanceof PStream)) throw fail();
1504
+ const w = dget(mask.dict, "Width");
1505
+ const h = dget(mask.dict, "Height");
1506
+ const side = BigInt(MAX_IMAGE_SIDE);
1507
+ if (!isInt(w) || !isInt(h) || w < 1n || h < 1n || w > side || h > side) throw fail();
1508
+ return { im: { w: Number(w), h: Number(h), color: [base.data[0], base.data[1], base.data[2]], alpha: mask.data }, mr };
1479
1509
  }
1480
1510
  var isSpaceByte = (x) => x === 9 || x === 10 || x === 11 || x === 12 || x === 13 || x === 32;
1481
1511
  function sameObject(c, num, body) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gerege-systems/eid-mongolia-sdk",
3
- "version": "0.30.1",
3
+ "version": "0.31.0",
4
4
  "description": "e-ID Mongolia Relying Party SDK — иргэнээр утсаар нь нэвтрэлт ба хууль ёсны цахим гарын үсэг (RP-API v3, Smart-ID нийцтэй).",
5
5
  "license": "MIT",
6
6
  "type": "module",