@nexart/ai-execution 0.4.2 → 0.5.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/index.mjs CHANGED
@@ -8,7 +8,11 @@ var CerVerifyCode = {
8
8
  INVALID_SHA256_FORMAT: "INVALID_SHA256_FORMAT",
9
9
  CANONICALIZATION_ERROR: "CANONICALIZATION_ERROR",
10
10
  SCHEMA_ERROR: "SCHEMA_ERROR",
11
- UNKNOWN_ERROR: "UNKNOWN_ERROR"
11
+ UNKNOWN_ERROR: "UNKNOWN_ERROR",
12
+ ATTESTATION_MISSING: "ATTESTATION_MISSING",
13
+ ATTESTATION_KEY_NOT_FOUND: "ATTESTATION_KEY_NOT_FOUND",
14
+ ATTESTATION_INVALID_SIGNATURE: "ATTESTATION_INVALID_SIGNATURE",
15
+ ATTESTATION_KEY_FORMAT_UNSUPPORTED: "ATTESTATION_KEY_FORMAT_UNSUPPORTED"
12
16
  };
13
17
 
14
18
  // src/errors.ts
@@ -104,7 +108,7 @@ function computeOutputHash(output) {
104
108
  }
105
109
 
106
110
  // src/snapshot.ts
107
- var PACKAGE_VERSION = "0.4.2";
111
+ var PACKAGE_VERSION = "0.5.0";
108
112
  function validateParameters(params) {
109
113
  const errors = [];
110
114
  if (typeof params.temperature !== "number" || !Number.isFinite(params.temperature)) {
@@ -290,8 +294,8 @@ function verifyCer(bundle) {
290
294
  let snapshotResult = null;
291
295
  try {
292
296
  snapshotResult = verifySnapshot(bundle.snapshot);
293
- } catch (err) {
294
- canonicalizationError = err instanceof Error ? err.message : String(err);
297
+ } catch (err2) {
298
+ canonicalizationError = err2 instanceof Error ? err2.message : String(err2);
295
299
  }
296
300
  if (canonicalizationError !== null) {
297
301
  const errors2 = [...schemaErrors, ...formatErrors, canonicalizationError];
@@ -310,8 +314,8 @@ function verifyCer(bundle) {
310
314
  if (bundle.certificateHash !== expectedHash) {
311
315
  certHashErrors.push(`certificateHash mismatch: expected ${expectedHash}, got ${bundle.certificateHash}`);
312
316
  }
313
- } catch (err) {
314
- const msg = err instanceof Error ? err.message : String(err);
317
+ } catch (err2) {
318
+ const msg = err2 instanceof Error ? err2.message : String(err2);
315
319
  const errors2 = [...schemaErrors, ...formatErrors, ...snapshotErrors, msg];
316
320
  return { ok: false, errors: errors2, code: CerVerifyCode.CANONICALIZATION_ERROR, details: [msg] };
317
321
  }
@@ -498,9 +502,9 @@ async function attest(bundle, options) {
498
502
  body: JSON.stringify(sanitized),
499
503
  signal: controller.signal
500
504
  });
501
- } catch (err) {
505
+ } catch (err2) {
502
506
  clearTimeout(timer);
503
- const error = err;
507
+ const error = err2;
504
508
  if (error.name === "AbortError") {
505
509
  throw new CerAttestationError(
506
510
  `Attestation request timed out after ${timeoutMs}ms`
@@ -570,8 +574,8 @@ function importCer(json) {
570
574
  let parsed;
571
575
  try {
572
576
  parsed = JSON.parse(json);
573
- } catch (err) {
574
- throw new CerVerificationError([`Invalid JSON: ${err.message}`]);
577
+ } catch (err2) {
578
+ throw new CerVerificationError([`Invalid JSON: ${err2.message}`]);
575
579
  }
576
580
  const bundle = parsed;
577
581
  if (!bundle || typeof bundle !== "object") {
@@ -635,6 +639,10 @@ function buildReceipt(obj) {
635
639
  if (nodeId) receipt.nodeId = nodeId;
636
640
  const attestedAt = extractStr(obj, "attestedAt");
637
641
  if (attestedAt) receipt.attestedAt = attestedAt;
642
+ const attestorKeyId = extractStr(obj, "attestorKeyId");
643
+ if (attestorKeyId) receipt.attestorKeyId = attestorKeyId;
644
+ const signatureB64Url = extractStr(obj, "signature") ?? extractStr(obj, "signatureB64Url");
645
+ if (signatureB64Url) receipt.signatureB64Url = signatureB64Url;
638
646
  return receipt;
639
647
  }
640
648
  function getAttestationReceipt(bundle) {
@@ -683,6 +691,1097 @@ async function attestIfNeeded(bundle, options) {
683
691
  const receipt = receiptFromProof(proof, bundle);
684
692
  return { bundle, receipt };
685
693
  }
694
+
695
+ // node_modules/@noble/ed25519/index.js
696
+ var ed25519_CURVE = {
697
+ p: 0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffedn,
698
+ n: 0x1000000000000000000000000000000014def9dea2f79cd65812631a5cf5d3edn,
699
+ h: 8n,
700
+ a: 0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffecn,
701
+ d: 0x52036cee2b6ffe738cc740797779e89800700a4d4141d8ab75eb4dca135978a3n,
702
+ Gx: 0x216936d3cd6e53fec0a4e231fdd6dc5c692cc7609525a7b2c9562d608f25d51an,
703
+ Gy: 0x6666666666666666666666666666666666666666666666666666666666666658n
704
+ };
705
+ var { p: P, n: N, Gx, Gy, a: _a, d: _d, h } = ed25519_CURVE;
706
+ var L = 32;
707
+ var L2 = 64;
708
+ var captureTrace = (...args) => {
709
+ if ("captureStackTrace" in Error && typeof Error.captureStackTrace === "function") {
710
+ Error.captureStackTrace(...args);
711
+ }
712
+ };
713
+ var err = (message = "") => {
714
+ const e = new Error(message);
715
+ captureTrace(e, err);
716
+ throw e;
717
+ };
718
+ var isBig = (n) => typeof n === "bigint";
719
+ var isStr = (s) => typeof s === "string";
720
+ var isBytes = (a) => a instanceof Uint8Array || ArrayBuffer.isView(a) && a.constructor.name === "Uint8Array";
721
+ var abytes = (value, length, title = "") => {
722
+ const bytes = isBytes(value);
723
+ const len = value?.length;
724
+ const needsLen = length !== void 0;
725
+ if (!bytes || needsLen && len !== length) {
726
+ const prefix = title && `"${title}" `;
727
+ const ofLen = needsLen ? ` of length ${length}` : "";
728
+ const got = bytes ? `length=${len}` : `type=${typeof value}`;
729
+ err(prefix + "expected Uint8Array" + ofLen + ", got " + got);
730
+ }
731
+ return value;
732
+ };
733
+ var u8n = (len) => new Uint8Array(len);
734
+ var u8fr = (buf) => Uint8Array.from(buf);
735
+ var padh = (n, pad) => n.toString(16).padStart(pad, "0");
736
+ var bytesToHex = (b) => Array.from(abytes(b)).map((e) => padh(e, 2)).join("");
737
+ var C = { _0: 48, _9: 57, A: 65, F: 70, a: 97, f: 102 };
738
+ var _ch = (ch) => {
739
+ if (ch >= C._0 && ch <= C._9)
740
+ return ch - C._0;
741
+ if (ch >= C.A && ch <= C.F)
742
+ return ch - (C.A - 10);
743
+ if (ch >= C.a && ch <= C.f)
744
+ return ch - (C.a - 10);
745
+ return;
746
+ };
747
+ var hexToBytes = (hex) => {
748
+ const e = "hex invalid";
749
+ if (!isStr(hex))
750
+ return err(e);
751
+ const hl = hex.length;
752
+ const al = hl / 2;
753
+ if (hl % 2)
754
+ return err(e);
755
+ const array = u8n(al);
756
+ for (let ai = 0, hi = 0; ai < al; ai++, hi += 2) {
757
+ const n1 = _ch(hex.charCodeAt(hi));
758
+ const n2 = _ch(hex.charCodeAt(hi + 1));
759
+ if (n1 === void 0 || n2 === void 0)
760
+ return err(e);
761
+ array[ai] = n1 * 16 + n2;
762
+ }
763
+ return array;
764
+ };
765
+ var cr = () => globalThis?.crypto;
766
+ var subtle = () => cr()?.subtle ?? err("crypto.subtle must be defined, consider polyfill");
767
+ var concatBytes = (...arrs) => {
768
+ const r = u8n(arrs.reduce((sum, a) => sum + abytes(a).length, 0));
769
+ let pad = 0;
770
+ arrs.forEach((a) => {
771
+ r.set(a, pad);
772
+ pad += a.length;
773
+ });
774
+ return r;
775
+ };
776
+ var big = BigInt;
777
+ var assertRange = (n, min, max, msg = "bad number: out of range") => isBig(n) && min <= n && n < max ? n : err(msg);
778
+ var M = (a, b = P) => {
779
+ const r = a % b;
780
+ return r >= 0n ? r : b + r;
781
+ };
782
+ var modN = (a) => M(a, N);
783
+ var invert = (num, md) => {
784
+ if (num === 0n || md <= 0n)
785
+ err("no inverse n=" + num + " mod=" + md);
786
+ let a = M(num, md), b = md, x = 0n, y = 1n, u = 1n, v = 0n;
787
+ while (a !== 0n) {
788
+ const q = b / a, r = b % a;
789
+ const m = x - u * q, n = y - v * q;
790
+ b = a, a = r, x = u, y = v, u = m, v = n;
791
+ }
792
+ return b === 1n ? M(x, md) : err("no inverse");
793
+ };
794
+ var callHash = (name) => {
795
+ const fn = hashes[name];
796
+ if (typeof fn !== "function")
797
+ err("hashes." + name + " not set");
798
+ return fn;
799
+ };
800
+ var apoint = (p) => p instanceof Point ? p : err("Point expected");
801
+ var B256 = 2n ** 256n;
802
+ var Point = class _Point {
803
+ static BASE;
804
+ static ZERO;
805
+ X;
806
+ Y;
807
+ Z;
808
+ T;
809
+ constructor(X, Y, Z, T) {
810
+ const max = B256;
811
+ this.X = assertRange(X, 0n, max);
812
+ this.Y = assertRange(Y, 0n, max);
813
+ this.Z = assertRange(Z, 1n, max);
814
+ this.T = assertRange(T, 0n, max);
815
+ Object.freeze(this);
816
+ }
817
+ static CURVE() {
818
+ return ed25519_CURVE;
819
+ }
820
+ static fromAffine(p) {
821
+ return new _Point(p.x, p.y, 1n, M(p.x * p.y));
822
+ }
823
+ /** RFC8032 5.1.3: Uint8Array to Point. */
824
+ static fromBytes(hex, zip215 = false) {
825
+ const d = _d;
826
+ const normed = u8fr(abytes(hex, L));
827
+ const lastByte = hex[31];
828
+ normed[31] = lastByte & ~128;
829
+ const y = bytesToNumLE(normed);
830
+ const max = zip215 ? B256 : P;
831
+ assertRange(y, 0n, max);
832
+ const y2 = M(y * y);
833
+ const u = M(y2 - 1n);
834
+ const v = M(d * y2 + 1n);
835
+ let { isValid, value: x } = uvRatio(u, v);
836
+ if (!isValid)
837
+ err("bad point: y not sqrt");
838
+ const isXOdd = (x & 1n) === 1n;
839
+ const isLastByteOdd = (lastByte & 128) !== 0;
840
+ if (!zip215 && x === 0n && isLastByteOdd)
841
+ err("bad point: x==0, isLastByteOdd");
842
+ if (isLastByteOdd !== isXOdd)
843
+ x = M(-x);
844
+ return new _Point(x, y, 1n, M(x * y));
845
+ }
846
+ static fromHex(hex, zip215) {
847
+ return _Point.fromBytes(hexToBytes(hex), zip215);
848
+ }
849
+ get x() {
850
+ return this.toAffine().x;
851
+ }
852
+ get y() {
853
+ return this.toAffine().y;
854
+ }
855
+ /** Checks if the point is valid and on-curve. */
856
+ assertValidity() {
857
+ const a = _a;
858
+ const d = _d;
859
+ const p = this;
860
+ if (p.is0())
861
+ return err("bad point: ZERO");
862
+ const { X, Y, Z, T } = p;
863
+ const X2 = M(X * X);
864
+ const Y2 = M(Y * Y);
865
+ const Z2 = M(Z * Z);
866
+ const Z4 = M(Z2 * Z2);
867
+ const aX2 = M(X2 * a);
868
+ const left = M(Z2 * M(aX2 + Y2));
869
+ const right = M(Z4 + M(d * M(X2 * Y2)));
870
+ if (left !== right)
871
+ return err("bad point: equation left != right (1)");
872
+ const XY = M(X * Y);
873
+ const ZT = M(Z * T);
874
+ if (XY !== ZT)
875
+ return err("bad point: equation left != right (2)");
876
+ return this;
877
+ }
878
+ /** Equality check: compare points P&Q. */
879
+ equals(other) {
880
+ const { X: X1, Y: Y1, Z: Z1 } = this;
881
+ const { X: X2, Y: Y2, Z: Z2 } = apoint(other);
882
+ const X1Z2 = M(X1 * Z2);
883
+ const X2Z1 = M(X2 * Z1);
884
+ const Y1Z2 = M(Y1 * Z2);
885
+ const Y2Z1 = M(Y2 * Z1);
886
+ return X1Z2 === X2Z1 && Y1Z2 === Y2Z1;
887
+ }
888
+ is0() {
889
+ return this.equals(I);
890
+ }
891
+ /** Flip point over y coordinate. */
892
+ negate() {
893
+ return new _Point(M(-this.X), this.Y, this.Z, M(-this.T));
894
+ }
895
+ /** Point doubling. Complete formula. Cost: `4M + 4S + 1*a + 6add + 1*2`. */
896
+ double() {
897
+ const { X: X1, Y: Y1, Z: Z1 } = this;
898
+ const a = _a;
899
+ const A = M(X1 * X1);
900
+ const B = M(Y1 * Y1);
901
+ const C2 = M(2n * M(Z1 * Z1));
902
+ const D = M(a * A);
903
+ const x1y1 = X1 + Y1;
904
+ const E = M(M(x1y1 * x1y1) - A - B);
905
+ const G2 = D + B;
906
+ const F = G2 - C2;
907
+ const H = D - B;
908
+ const X3 = M(E * F);
909
+ const Y3 = M(G2 * H);
910
+ const T3 = M(E * H);
911
+ const Z3 = M(F * G2);
912
+ return new _Point(X3, Y3, Z3, T3);
913
+ }
914
+ /** Point addition. Complete formula. Cost: `8M + 1*k + 8add + 1*2`. */
915
+ add(other) {
916
+ const { X: X1, Y: Y1, Z: Z1, T: T1 } = this;
917
+ const { X: X2, Y: Y2, Z: Z2, T: T2 } = apoint(other);
918
+ const a = _a;
919
+ const d = _d;
920
+ const A = M(X1 * X2);
921
+ const B = M(Y1 * Y2);
922
+ const C2 = M(T1 * d * T2);
923
+ const D = M(Z1 * Z2);
924
+ const E = M((X1 + Y1) * (X2 + Y2) - A - B);
925
+ const F = M(D - C2);
926
+ const G2 = M(D + C2);
927
+ const H = M(B - a * A);
928
+ const X3 = M(E * F);
929
+ const Y3 = M(G2 * H);
930
+ const T3 = M(E * H);
931
+ const Z3 = M(F * G2);
932
+ return new _Point(X3, Y3, Z3, T3);
933
+ }
934
+ subtract(other) {
935
+ return this.add(apoint(other).negate());
936
+ }
937
+ /**
938
+ * Point-by-scalar multiplication. Scalar must be in range 1 <= n < CURVE.n.
939
+ * Uses {@link wNAF} for base point.
940
+ * Uses fake point to mitigate side-channel leakage.
941
+ * @param n scalar by which point is multiplied
942
+ * @param safe safe mode guards against timing attacks; unsafe mode is faster
943
+ */
944
+ multiply(n, safe = true) {
945
+ if (!safe && (n === 0n || this.is0()))
946
+ return I;
947
+ assertRange(n, 1n, N);
948
+ if (n === 1n)
949
+ return this;
950
+ if (this.equals(G))
951
+ return wNAF(n).p;
952
+ let p = I;
953
+ let f = G;
954
+ for (let d = this; n > 0n; d = d.double(), n >>= 1n) {
955
+ if (n & 1n)
956
+ p = p.add(d);
957
+ else if (safe)
958
+ f = f.add(d);
959
+ }
960
+ return p;
961
+ }
962
+ multiplyUnsafe(scalar) {
963
+ return this.multiply(scalar, false);
964
+ }
965
+ /** Convert point to 2d xy affine point. (X, Y, Z) ∋ (x=X/Z, y=Y/Z) */
966
+ toAffine() {
967
+ const { X, Y, Z } = this;
968
+ if (this.equals(I))
969
+ return { x: 0n, y: 1n };
970
+ const iz = invert(Z, P);
971
+ if (M(Z * iz) !== 1n)
972
+ err("invalid inverse");
973
+ const x = M(X * iz);
974
+ const y = M(Y * iz);
975
+ return { x, y };
976
+ }
977
+ toBytes() {
978
+ const { x, y } = this.assertValidity().toAffine();
979
+ const b = numTo32bLE(y);
980
+ b[31] |= x & 1n ? 128 : 0;
981
+ return b;
982
+ }
983
+ toHex() {
984
+ return bytesToHex(this.toBytes());
985
+ }
986
+ clearCofactor() {
987
+ return this.multiply(big(h), false);
988
+ }
989
+ isSmallOrder() {
990
+ return this.clearCofactor().is0();
991
+ }
992
+ isTorsionFree() {
993
+ let p = this.multiply(N / 2n, false).double();
994
+ if (N % 2n)
995
+ p = p.add(this);
996
+ return p.is0();
997
+ }
998
+ };
999
+ var G = new Point(Gx, Gy, 1n, M(Gx * Gy));
1000
+ var I = new Point(0n, 1n, 1n, 0n);
1001
+ Point.BASE = G;
1002
+ Point.ZERO = I;
1003
+ var numTo32bLE = (num) => hexToBytes(padh(assertRange(num, 0n, B256), L2)).reverse();
1004
+ var bytesToNumLE = (b) => big("0x" + bytesToHex(u8fr(abytes(b)).reverse()));
1005
+ var pow2 = (x, power) => {
1006
+ let r = x;
1007
+ while (power-- > 0n) {
1008
+ r *= r;
1009
+ r %= P;
1010
+ }
1011
+ return r;
1012
+ };
1013
+ var pow_2_252_3 = (x) => {
1014
+ const x2 = x * x % P;
1015
+ const b2 = x2 * x % P;
1016
+ const b4 = pow2(b2, 2n) * b2 % P;
1017
+ const b5 = pow2(b4, 1n) * x % P;
1018
+ const b10 = pow2(b5, 5n) * b5 % P;
1019
+ const b20 = pow2(b10, 10n) * b10 % P;
1020
+ const b40 = pow2(b20, 20n) * b20 % P;
1021
+ const b80 = pow2(b40, 40n) * b40 % P;
1022
+ const b160 = pow2(b80, 80n) * b80 % P;
1023
+ const b240 = pow2(b160, 80n) * b80 % P;
1024
+ const b250 = pow2(b240, 10n) * b10 % P;
1025
+ const pow_p_5_8 = pow2(b250, 2n) * x % P;
1026
+ return { pow_p_5_8, b2 };
1027
+ };
1028
+ var RM1 = 0x2b8324804fc1df0b2b4d00993dfbd7a72f431806ad2fe478c4ee1b274a0ea0b0n;
1029
+ var uvRatio = (u, v) => {
1030
+ const v3 = M(v * v * v);
1031
+ const v7 = M(v3 * v3 * v);
1032
+ const pow = pow_2_252_3(u * v7).pow_p_5_8;
1033
+ let x = M(u * v3 * pow);
1034
+ const vx2 = M(v * x * x);
1035
+ const root1 = x;
1036
+ const root2 = M(x * RM1);
1037
+ const useRoot1 = vx2 === u;
1038
+ const useRoot2 = vx2 === M(-u);
1039
+ const noRoot = vx2 === M(-u * RM1);
1040
+ if (useRoot1)
1041
+ x = root1;
1042
+ if (useRoot2 || noRoot)
1043
+ x = root2;
1044
+ if ((M(x) & 1n) === 1n)
1045
+ x = M(-x);
1046
+ return { isValid: useRoot1 || useRoot2, value: x };
1047
+ };
1048
+ var modL_LE = (hash) => modN(bytesToNumLE(hash));
1049
+ var sha512s = (...m) => callHash("sha512")(concatBytes(...m));
1050
+ var hashFinishS = (res) => res.finish(sha512s(res.hashable));
1051
+ var defaultVerifyOpts = { zip215: true };
1052
+ var _verify = (sig, msg, pub, opts = defaultVerifyOpts) => {
1053
+ sig = abytes(sig, L2);
1054
+ msg = abytes(msg);
1055
+ pub = abytes(pub, L);
1056
+ const { zip215 } = opts;
1057
+ let A;
1058
+ let R;
1059
+ let s;
1060
+ let SB;
1061
+ let hashable = Uint8Array.of();
1062
+ try {
1063
+ A = Point.fromBytes(pub, zip215);
1064
+ R = Point.fromBytes(sig.slice(0, L), zip215);
1065
+ s = bytesToNumLE(sig.slice(L, L2));
1066
+ SB = G.multiply(s, false);
1067
+ hashable = concatBytes(R.toBytes(), A.toBytes(), msg);
1068
+ } catch (error) {
1069
+ }
1070
+ const finish = (hashed) => {
1071
+ if (SB == null)
1072
+ return false;
1073
+ if (!zip215 && A.isSmallOrder())
1074
+ return false;
1075
+ const k = modL_LE(hashed);
1076
+ const RkA = R.add(A.multiply(k, false));
1077
+ return RkA.add(SB.negate()).clearCofactor().is0();
1078
+ };
1079
+ return { hashable, finish };
1080
+ };
1081
+ var verify = (signature, message, publicKey, opts = defaultVerifyOpts) => hashFinishS(_verify(signature, message, publicKey, opts));
1082
+ var hashes = {
1083
+ sha512Async: async (message) => {
1084
+ const s = subtle();
1085
+ const m = concatBytes(message);
1086
+ return u8n(await s.digest("SHA-512", m.buffer));
1087
+ },
1088
+ sha512: void 0
1089
+ };
1090
+ var W = 8;
1091
+ var scalarBits = 256;
1092
+ var pwindows = Math.ceil(scalarBits / W) + 1;
1093
+ var pwindowSize = 2 ** (W - 1);
1094
+ var precompute = () => {
1095
+ const points = [];
1096
+ let p = G;
1097
+ let b = p;
1098
+ for (let w = 0; w < pwindows; w++) {
1099
+ b = p;
1100
+ points.push(b);
1101
+ for (let i = 1; i < pwindowSize; i++) {
1102
+ b = b.add(p);
1103
+ points.push(b);
1104
+ }
1105
+ p = b.double();
1106
+ }
1107
+ return points;
1108
+ };
1109
+ var Gpows = void 0;
1110
+ var ctneg = (cnd, p) => {
1111
+ const n = p.negate();
1112
+ return cnd ? n : p;
1113
+ };
1114
+ var wNAF = (n) => {
1115
+ const comp = Gpows || (Gpows = precompute());
1116
+ let p = I;
1117
+ let f = G;
1118
+ const pow_2_w = 2 ** W;
1119
+ const maxNum = pow_2_w;
1120
+ const mask = big(pow_2_w - 1);
1121
+ const shiftBy = big(W);
1122
+ for (let w = 0; w < pwindows; w++) {
1123
+ let wbits = Number(n & mask);
1124
+ n >>= shiftBy;
1125
+ if (wbits > pwindowSize) {
1126
+ wbits -= maxNum;
1127
+ n += 1n;
1128
+ }
1129
+ const off = w * pwindowSize;
1130
+ const offF = off;
1131
+ const offP = off + Math.abs(wbits) - 1;
1132
+ const isEven = w % 2 !== 0;
1133
+ const isNeg = wbits < 0;
1134
+ if (wbits === 0) {
1135
+ f = f.add(ctneg(isEven, comp[offF]));
1136
+ } else {
1137
+ p = p.add(ctneg(isNeg, comp[offP]));
1138
+ }
1139
+ }
1140
+ if (n !== 0n)
1141
+ err("invalid wnaf");
1142
+ return { p, f };
1143
+ };
1144
+
1145
+ // node_modules/@noble/hashes/esm/utils.js
1146
+ function isBytes2(a) {
1147
+ return a instanceof Uint8Array || ArrayBuffer.isView(a) && a.constructor.name === "Uint8Array";
1148
+ }
1149
+ function abytes2(b, ...lengths) {
1150
+ if (!isBytes2(b))
1151
+ throw new Error("Uint8Array expected");
1152
+ if (lengths.length > 0 && !lengths.includes(b.length))
1153
+ throw new Error("Uint8Array expected of length " + lengths + ", got length=" + b.length);
1154
+ }
1155
+ function aexists(instance, checkFinished = true) {
1156
+ if (instance.destroyed)
1157
+ throw new Error("Hash instance has been destroyed");
1158
+ if (checkFinished && instance.finished)
1159
+ throw new Error("Hash#digest() has already been called");
1160
+ }
1161
+ function aoutput(out, instance) {
1162
+ abytes2(out);
1163
+ const min = instance.outputLen;
1164
+ if (out.length < min) {
1165
+ throw new Error("digestInto() expects output buffer of length at least " + min);
1166
+ }
1167
+ }
1168
+ function clean(...arrays) {
1169
+ for (let i = 0; i < arrays.length; i++) {
1170
+ arrays[i].fill(0);
1171
+ }
1172
+ }
1173
+ function createView(arr) {
1174
+ return new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
1175
+ }
1176
+ function utf8ToBytes(str) {
1177
+ if (typeof str !== "string")
1178
+ throw new Error("string expected");
1179
+ return new Uint8Array(new TextEncoder().encode(str));
1180
+ }
1181
+ function toBytes(data) {
1182
+ if (typeof data === "string")
1183
+ data = utf8ToBytes(data);
1184
+ abytes2(data);
1185
+ return data;
1186
+ }
1187
+ var Hash = class {
1188
+ };
1189
+ function createHasher(hashCons) {
1190
+ const hashC = (msg) => hashCons().update(toBytes(msg)).digest();
1191
+ const tmp = hashCons();
1192
+ hashC.outputLen = tmp.outputLen;
1193
+ hashC.blockLen = tmp.blockLen;
1194
+ hashC.create = () => hashCons();
1195
+ return hashC;
1196
+ }
1197
+
1198
+ // node_modules/@noble/hashes/esm/_md.js
1199
+ function setBigUint64(view, byteOffset, value, isLE) {
1200
+ if (typeof view.setBigUint64 === "function")
1201
+ return view.setBigUint64(byteOffset, value, isLE);
1202
+ const _32n2 = BigInt(32);
1203
+ const _u32_max = BigInt(4294967295);
1204
+ const wh = Number(value >> _32n2 & _u32_max);
1205
+ const wl = Number(value & _u32_max);
1206
+ const h2 = isLE ? 4 : 0;
1207
+ const l = isLE ? 0 : 4;
1208
+ view.setUint32(byteOffset + h2, wh, isLE);
1209
+ view.setUint32(byteOffset + l, wl, isLE);
1210
+ }
1211
+ var HashMD = class extends Hash {
1212
+ constructor(blockLen, outputLen, padOffset, isLE) {
1213
+ super();
1214
+ this.finished = false;
1215
+ this.length = 0;
1216
+ this.pos = 0;
1217
+ this.destroyed = false;
1218
+ this.blockLen = blockLen;
1219
+ this.outputLen = outputLen;
1220
+ this.padOffset = padOffset;
1221
+ this.isLE = isLE;
1222
+ this.buffer = new Uint8Array(blockLen);
1223
+ this.view = createView(this.buffer);
1224
+ }
1225
+ update(data) {
1226
+ aexists(this);
1227
+ data = toBytes(data);
1228
+ abytes2(data);
1229
+ const { view, buffer, blockLen } = this;
1230
+ const len = data.length;
1231
+ for (let pos = 0; pos < len; ) {
1232
+ const take = Math.min(blockLen - this.pos, len - pos);
1233
+ if (take === blockLen) {
1234
+ const dataView = createView(data);
1235
+ for (; blockLen <= len - pos; pos += blockLen)
1236
+ this.process(dataView, pos);
1237
+ continue;
1238
+ }
1239
+ buffer.set(data.subarray(pos, pos + take), this.pos);
1240
+ this.pos += take;
1241
+ pos += take;
1242
+ if (this.pos === blockLen) {
1243
+ this.process(view, 0);
1244
+ this.pos = 0;
1245
+ }
1246
+ }
1247
+ this.length += data.length;
1248
+ this.roundClean();
1249
+ return this;
1250
+ }
1251
+ digestInto(out) {
1252
+ aexists(this);
1253
+ aoutput(out, this);
1254
+ this.finished = true;
1255
+ const { buffer, view, blockLen, isLE } = this;
1256
+ let { pos } = this;
1257
+ buffer[pos++] = 128;
1258
+ clean(this.buffer.subarray(pos));
1259
+ if (this.padOffset > blockLen - pos) {
1260
+ this.process(view, 0);
1261
+ pos = 0;
1262
+ }
1263
+ for (let i = pos; i < blockLen; i++)
1264
+ buffer[i] = 0;
1265
+ setBigUint64(view, blockLen - 8, BigInt(this.length * 8), isLE);
1266
+ this.process(view, 0);
1267
+ const oview = createView(out);
1268
+ const len = this.outputLen;
1269
+ if (len % 4)
1270
+ throw new Error("_sha2: outputLen should be aligned to 32bit");
1271
+ const outLen = len / 4;
1272
+ const state = this.get();
1273
+ if (outLen > state.length)
1274
+ throw new Error("_sha2: outputLen bigger than state");
1275
+ for (let i = 0; i < outLen; i++)
1276
+ oview.setUint32(4 * i, state[i], isLE);
1277
+ }
1278
+ digest() {
1279
+ const { buffer, outputLen } = this;
1280
+ this.digestInto(buffer);
1281
+ const res = buffer.slice(0, outputLen);
1282
+ this.destroy();
1283
+ return res;
1284
+ }
1285
+ _cloneInto(to) {
1286
+ to || (to = new this.constructor());
1287
+ to.set(...this.get());
1288
+ const { blockLen, buffer, length, finished, destroyed, pos } = this;
1289
+ to.destroyed = destroyed;
1290
+ to.finished = finished;
1291
+ to.length = length;
1292
+ to.pos = pos;
1293
+ if (length % blockLen)
1294
+ to.buffer.set(buffer);
1295
+ return to;
1296
+ }
1297
+ clone() {
1298
+ return this._cloneInto();
1299
+ }
1300
+ };
1301
+ var SHA512_IV = /* @__PURE__ */ Uint32Array.from([
1302
+ 1779033703,
1303
+ 4089235720,
1304
+ 3144134277,
1305
+ 2227873595,
1306
+ 1013904242,
1307
+ 4271175723,
1308
+ 2773480762,
1309
+ 1595750129,
1310
+ 1359893119,
1311
+ 2917565137,
1312
+ 2600822924,
1313
+ 725511199,
1314
+ 528734635,
1315
+ 4215389547,
1316
+ 1541459225,
1317
+ 327033209
1318
+ ]);
1319
+
1320
+ // node_modules/@noble/hashes/esm/_u64.js
1321
+ var U32_MASK64 = /* @__PURE__ */ BigInt(2 ** 32 - 1);
1322
+ var _32n = /* @__PURE__ */ BigInt(32);
1323
+ function fromBig(n, le = false) {
1324
+ if (le)
1325
+ return { h: Number(n & U32_MASK64), l: Number(n >> _32n & U32_MASK64) };
1326
+ return { h: Number(n >> _32n & U32_MASK64) | 0, l: Number(n & U32_MASK64) | 0 };
1327
+ }
1328
+ function split(lst, le = false) {
1329
+ const len = lst.length;
1330
+ let Ah = new Uint32Array(len);
1331
+ let Al = new Uint32Array(len);
1332
+ for (let i = 0; i < len; i++) {
1333
+ const { h: h2, l } = fromBig(lst[i], le);
1334
+ [Ah[i], Al[i]] = [h2, l];
1335
+ }
1336
+ return [Ah, Al];
1337
+ }
1338
+ var shrSH = (h2, _l, s) => h2 >>> s;
1339
+ var shrSL = (h2, l, s) => h2 << 32 - s | l >>> s;
1340
+ var rotrSH = (h2, l, s) => h2 >>> s | l << 32 - s;
1341
+ var rotrSL = (h2, l, s) => h2 << 32 - s | l >>> s;
1342
+ var rotrBH = (h2, l, s) => h2 << 64 - s | l >>> s - 32;
1343
+ var rotrBL = (h2, l, s) => h2 >>> s - 32 | l << 64 - s;
1344
+ function add(Ah, Al, Bh, Bl) {
1345
+ const l = (Al >>> 0) + (Bl >>> 0);
1346
+ return { h: Ah + Bh + (l / 2 ** 32 | 0) | 0, l: l | 0 };
1347
+ }
1348
+ var add3L = (Al, Bl, Cl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0);
1349
+ var add3H = (low, Ah, Bh, Ch) => Ah + Bh + Ch + (low / 2 ** 32 | 0) | 0;
1350
+ var add4L = (Al, Bl, Cl, Dl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0);
1351
+ var add4H = (low, Ah, Bh, Ch, Dh) => Ah + Bh + Ch + Dh + (low / 2 ** 32 | 0) | 0;
1352
+ var add5L = (Al, Bl, Cl, Dl, El) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0) + (El >>> 0);
1353
+ var add5H = (low, Ah, Bh, Ch, Dh, Eh) => Ah + Bh + Ch + Dh + Eh + (low / 2 ** 32 | 0) | 0;
1354
+
1355
+ // node_modules/@noble/hashes/esm/sha2.js
1356
+ var K512 = /* @__PURE__ */ (() => split([
1357
+ "0x428a2f98d728ae22",
1358
+ "0x7137449123ef65cd",
1359
+ "0xb5c0fbcfec4d3b2f",
1360
+ "0xe9b5dba58189dbbc",
1361
+ "0x3956c25bf348b538",
1362
+ "0x59f111f1b605d019",
1363
+ "0x923f82a4af194f9b",
1364
+ "0xab1c5ed5da6d8118",
1365
+ "0xd807aa98a3030242",
1366
+ "0x12835b0145706fbe",
1367
+ "0x243185be4ee4b28c",
1368
+ "0x550c7dc3d5ffb4e2",
1369
+ "0x72be5d74f27b896f",
1370
+ "0x80deb1fe3b1696b1",
1371
+ "0x9bdc06a725c71235",
1372
+ "0xc19bf174cf692694",
1373
+ "0xe49b69c19ef14ad2",
1374
+ "0xefbe4786384f25e3",
1375
+ "0x0fc19dc68b8cd5b5",
1376
+ "0x240ca1cc77ac9c65",
1377
+ "0x2de92c6f592b0275",
1378
+ "0x4a7484aa6ea6e483",
1379
+ "0x5cb0a9dcbd41fbd4",
1380
+ "0x76f988da831153b5",
1381
+ "0x983e5152ee66dfab",
1382
+ "0xa831c66d2db43210",
1383
+ "0xb00327c898fb213f",
1384
+ "0xbf597fc7beef0ee4",
1385
+ "0xc6e00bf33da88fc2",
1386
+ "0xd5a79147930aa725",
1387
+ "0x06ca6351e003826f",
1388
+ "0x142929670a0e6e70",
1389
+ "0x27b70a8546d22ffc",
1390
+ "0x2e1b21385c26c926",
1391
+ "0x4d2c6dfc5ac42aed",
1392
+ "0x53380d139d95b3df",
1393
+ "0x650a73548baf63de",
1394
+ "0x766a0abb3c77b2a8",
1395
+ "0x81c2c92e47edaee6",
1396
+ "0x92722c851482353b",
1397
+ "0xa2bfe8a14cf10364",
1398
+ "0xa81a664bbc423001",
1399
+ "0xc24b8b70d0f89791",
1400
+ "0xc76c51a30654be30",
1401
+ "0xd192e819d6ef5218",
1402
+ "0xd69906245565a910",
1403
+ "0xf40e35855771202a",
1404
+ "0x106aa07032bbd1b8",
1405
+ "0x19a4c116b8d2d0c8",
1406
+ "0x1e376c085141ab53",
1407
+ "0x2748774cdf8eeb99",
1408
+ "0x34b0bcb5e19b48a8",
1409
+ "0x391c0cb3c5c95a63",
1410
+ "0x4ed8aa4ae3418acb",
1411
+ "0x5b9cca4f7763e373",
1412
+ "0x682e6ff3d6b2b8a3",
1413
+ "0x748f82ee5defb2fc",
1414
+ "0x78a5636f43172f60",
1415
+ "0x84c87814a1f0ab72",
1416
+ "0x8cc702081a6439ec",
1417
+ "0x90befffa23631e28",
1418
+ "0xa4506cebde82bde9",
1419
+ "0xbef9a3f7b2c67915",
1420
+ "0xc67178f2e372532b",
1421
+ "0xca273eceea26619c",
1422
+ "0xd186b8c721c0c207",
1423
+ "0xeada7dd6cde0eb1e",
1424
+ "0xf57d4f7fee6ed178",
1425
+ "0x06f067aa72176fba",
1426
+ "0x0a637dc5a2c898a6",
1427
+ "0x113f9804bef90dae",
1428
+ "0x1b710b35131c471b",
1429
+ "0x28db77f523047d84",
1430
+ "0x32caab7b40c72493",
1431
+ "0x3c9ebe0a15c9bebc",
1432
+ "0x431d67c49c100d4c",
1433
+ "0x4cc5d4becb3e42b6",
1434
+ "0x597f299cfc657e2a",
1435
+ "0x5fcb6fab3ad6faec",
1436
+ "0x6c44198c4a475817"
1437
+ ].map((n) => BigInt(n))))();
1438
+ var SHA512_Kh = /* @__PURE__ */ (() => K512[0])();
1439
+ var SHA512_Kl = /* @__PURE__ */ (() => K512[1])();
1440
+ var SHA512_W_H = /* @__PURE__ */ new Uint32Array(80);
1441
+ var SHA512_W_L = /* @__PURE__ */ new Uint32Array(80);
1442
+ var SHA512 = class extends HashMD {
1443
+ constructor(outputLen = 64) {
1444
+ super(128, outputLen, 16, false);
1445
+ this.Ah = SHA512_IV[0] | 0;
1446
+ this.Al = SHA512_IV[1] | 0;
1447
+ this.Bh = SHA512_IV[2] | 0;
1448
+ this.Bl = SHA512_IV[3] | 0;
1449
+ this.Ch = SHA512_IV[4] | 0;
1450
+ this.Cl = SHA512_IV[5] | 0;
1451
+ this.Dh = SHA512_IV[6] | 0;
1452
+ this.Dl = SHA512_IV[7] | 0;
1453
+ this.Eh = SHA512_IV[8] | 0;
1454
+ this.El = SHA512_IV[9] | 0;
1455
+ this.Fh = SHA512_IV[10] | 0;
1456
+ this.Fl = SHA512_IV[11] | 0;
1457
+ this.Gh = SHA512_IV[12] | 0;
1458
+ this.Gl = SHA512_IV[13] | 0;
1459
+ this.Hh = SHA512_IV[14] | 0;
1460
+ this.Hl = SHA512_IV[15] | 0;
1461
+ }
1462
+ // prettier-ignore
1463
+ get() {
1464
+ const { Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl } = this;
1465
+ return [Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl];
1466
+ }
1467
+ // prettier-ignore
1468
+ set(Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl) {
1469
+ this.Ah = Ah | 0;
1470
+ this.Al = Al | 0;
1471
+ this.Bh = Bh | 0;
1472
+ this.Bl = Bl | 0;
1473
+ this.Ch = Ch | 0;
1474
+ this.Cl = Cl | 0;
1475
+ this.Dh = Dh | 0;
1476
+ this.Dl = Dl | 0;
1477
+ this.Eh = Eh | 0;
1478
+ this.El = El | 0;
1479
+ this.Fh = Fh | 0;
1480
+ this.Fl = Fl | 0;
1481
+ this.Gh = Gh | 0;
1482
+ this.Gl = Gl | 0;
1483
+ this.Hh = Hh | 0;
1484
+ this.Hl = Hl | 0;
1485
+ }
1486
+ process(view, offset) {
1487
+ for (let i = 0; i < 16; i++, offset += 4) {
1488
+ SHA512_W_H[i] = view.getUint32(offset);
1489
+ SHA512_W_L[i] = view.getUint32(offset += 4);
1490
+ }
1491
+ for (let i = 16; i < 80; i++) {
1492
+ const W15h = SHA512_W_H[i - 15] | 0;
1493
+ const W15l = SHA512_W_L[i - 15] | 0;
1494
+ const s0h = rotrSH(W15h, W15l, 1) ^ rotrSH(W15h, W15l, 8) ^ shrSH(W15h, W15l, 7);
1495
+ const s0l = rotrSL(W15h, W15l, 1) ^ rotrSL(W15h, W15l, 8) ^ shrSL(W15h, W15l, 7);
1496
+ const W2h = SHA512_W_H[i - 2] | 0;
1497
+ const W2l = SHA512_W_L[i - 2] | 0;
1498
+ const s1h = rotrSH(W2h, W2l, 19) ^ rotrBH(W2h, W2l, 61) ^ shrSH(W2h, W2l, 6);
1499
+ const s1l = rotrSL(W2h, W2l, 19) ^ rotrBL(W2h, W2l, 61) ^ shrSL(W2h, W2l, 6);
1500
+ const SUMl = add4L(s0l, s1l, SHA512_W_L[i - 7], SHA512_W_L[i - 16]);
1501
+ const SUMh = add4H(SUMl, s0h, s1h, SHA512_W_H[i - 7], SHA512_W_H[i - 16]);
1502
+ SHA512_W_H[i] = SUMh | 0;
1503
+ SHA512_W_L[i] = SUMl | 0;
1504
+ }
1505
+ let { Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl } = this;
1506
+ for (let i = 0; i < 80; i++) {
1507
+ const sigma1h = rotrSH(Eh, El, 14) ^ rotrSH(Eh, El, 18) ^ rotrBH(Eh, El, 41);
1508
+ const sigma1l = rotrSL(Eh, El, 14) ^ rotrSL(Eh, El, 18) ^ rotrBL(Eh, El, 41);
1509
+ const CHIh = Eh & Fh ^ ~Eh & Gh;
1510
+ const CHIl = El & Fl ^ ~El & Gl;
1511
+ const T1ll = add5L(Hl, sigma1l, CHIl, SHA512_Kl[i], SHA512_W_L[i]);
1512
+ const T1h = add5H(T1ll, Hh, sigma1h, CHIh, SHA512_Kh[i], SHA512_W_H[i]);
1513
+ const T1l = T1ll | 0;
1514
+ const sigma0h = rotrSH(Ah, Al, 28) ^ rotrBH(Ah, Al, 34) ^ rotrBH(Ah, Al, 39);
1515
+ const sigma0l = rotrSL(Ah, Al, 28) ^ rotrBL(Ah, Al, 34) ^ rotrBL(Ah, Al, 39);
1516
+ const MAJh = Ah & Bh ^ Ah & Ch ^ Bh & Ch;
1517
+ const MAJl = Al & Bl ^ Al & Cl ^ Bl & Cl;
1518
+ Hh = Gh | 0;
1519
+ Hl = Gl | 0;
1520
+ Gh = Fh | 0;
1521
+ Gl = Fl | 0;
1522
+ Fh = Eh | 0;
1523
+ Fl = El | 0;
1524
+ ({ h: Eh, l: El } = add(Dh | 0, Dl | 0, T1h | 0, T1l | 0));
1525
+ Dh = Ch | 0;
1526
+ Dl = Cl | 0;
1527
+ Ch = Bh | 0;
1528
+ Cl = Bl | 0;
1529
+ Bh = Ah | 0;
1530
+ Bl = Al | 0;
1531
+ const All = add3L(T1l, sigma0l, MAJl);
1532
+ Ah = add3H(All, T1h, sigma0h, MAJh);
1533
+ Al = All | 0;
1534
+ }
1535
+ ({ h: Ah, l: Al } = add(this.Ah | 0, this.Al | 0, Ah | 0, Al | 0));
1536
+ ({ h: Bh, l: Bl } = add(this.Bh | 0, this.Bl | 0, Bh | 0, Bl | 0));
1537
+ ({ h: Ch, l: Cl } = add(this.Ch | 0, this.Cl | 0, Ch | 0, Cl | 0));
1538
+ ({ h: Dh, l: Dl } = add(this.Dh | 0, this.Dl | 0, Dh | 0, Dl | 0));
1539
+ ({ h: Eh, l: El } = add(this.Eh | 0, this.El | 0, Eh | 0, El | 0));
1540
+ ({ h: Fh, l: Fl } = add(this.Fh | 0, this.Fl | 0, Fh | 0, Fl | 0));
1541
+ ({ h: Gh, l: Gl } = add(this.Gh | 0, this.Gl | 0, Gh | 0, Gl | 0));
1542
+ ({ h: Hh, l: Hl } = add(this.Hh | 0, this.Hl | 0, Hh | 0, Hl | 0));
1543
+ this.set(Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl);
1544
+ }
1545
+ roundClean() {
1546
+ clean(SHA512_W_H, SHA512_W_L);
1547
+ }
1548
+ destroy() {
1549
+ clean(this.buffer);
1550
+ this.set(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
1551
+ }
1552
+ };
1553
+ var sha512 = /* @__PURE__ */ createHasher(() => new SHA512());
1554
+
1555
+ // src/nodeReceipt.ts
1556
+ hashes.sha512 = sha512;
1557
+ function base64urlToBytes(s) {
1558
+ const pad = s.length % 4;
1559
+ const base64 = s.replace(/-/g, "+").replace(/_/g, "/") + (pad ? "=".repeat(4 - pad) : "");
1560
+ if (typeof Buffer !== "undefined") {
1561
+ return new Uint8Array(Buffer.from(base64, "base64"));
1562
+ }
1563
+ const binary = atob(base64);
1564
+ return Uint8Array.from(binary, (c) => c.charCodeAt(0));
1565
+ }
1566
+ async function verifyNodeReceiptSignature(params) {
1567
+ try {
1568
+ const { receipt, signatureB64Url, key } = params;
1569
+ let pubKeyBytes;
1570
+ if (key.jwk) {
1571
+ if (key.jwk.kty !== "OKP" || key.jwk.crv !== "Ed25519") {
1572
+ return {
1573
+ ok: false,
1574
+ code: CerVerifyCode.ATTESTATION_KEY_FORMAT_UNSUPPORTED,
1575
+ details: [
1576
+ `JWK must have kty=OKP and crv=Ed25519, got kty=${key.jwk.kty} crv=${key.jwk.crv}`
1577
+ ]
1578
+ };
1579
+ }
1580
+ pubKeyBytes = base64urlToBytes(key.jwk.x);
1581
+ } else if (key.rawB64Url) {
1582
+ pubKeyBytes = base64urlToBytes(key.rawB64Url);
1583
+ } else if (key.spkiB64) {
1584
+ const spkiBytes = base64urlToBytes(key.spkiB64);
1585
+ if (spkiBytes.length < 32) {
1586
+ return {
1587
+ ok: false,
1588
+ code: CerVerifyCode.ATTESTATION_KEY_FORMAT_UNSUPPORTED,
1589
+ details: ["SPKI key too short to extract Ed25519 public key"]
1590
+ };
1591
+ }
1592
+ pubKeyBytes = spkiBytes.slice(spkiBytes.length - 32);
1593
+ } else {
1594
+ return {
1595
+ ok: false,
1596
+ code: CerVerifyCode.ATTESTATION_KEY_FORMAT_UNSUPPORTED,
1597
+ details: ["No usable key provided: supply jwk, rawB64Url, or spkiB64"]
1598
+ };
1599
+ }
1600
+ if (pubKeyBytes.length !== 32) {
1601
+ return {
1602
+ ok: false,
1603
+ code: CerVerifyCode.ATTESTATION_KEY_FORMAT_UNSUPPORTED,
1604
+ details: [`Ed25519 public key must be 32 bytes, got ${pubKeyBytes.length}`]
1605
+ };
1606
+ }
1607
+ const sigBytes = base64urlToBytes(signatureB64Url);
1608
+ if (sigBytes.length !== 64) {
1609
+ return {
1610
+ ok: false,
1611
+ code: CerVerifyCode.ATTESTATION_INVALID_SIGNATURE,
1612
+ details: [`Ed25519 signature must be 64 bytes, got ${sigBytes.length}`]
1613
+ };
1614
+ }
1615
+ const msgBytes = new TextEncoder().encode(toCanonicalJson(receipt));
1616
+ const isValid = await verify(sigBytes, msgBytes, pubKeyBytes);
1617
+ if (!isValid) {
1618
+ return {
1619
+ ok: false,
1620
+ code: CerVerifyCode.ATTESTATION_INVALID_SIGNATURE,
1621
+ details: ["Ed25519 signature verification failed"]
1622
+ };
1623
+ }
1624
+ return { ok: true, code: CerVerifyCode.OK };
1625
+ } catch (err2) {
1626
+ return {
1627
+ ok: false,
1628
+ code: CerVerifyCode.ATTESTATION_INVALID_SIGNATURE,
1629
+ details: [err2 instanceof Error ? err2.message : String(err2)]
1630
+ };
1631
+ }
1632
+ }
1633
+ async function fetchNodeKeys(nodeUrl) {
1634
+ const url = `${nodeUrl.replace(/\/+$/, "")}/.well-known/nexart-node.json`;
1635
+ const response = await fetch(url);
1636
+ if (!response.ok) {
1637
+ throw new Error(`Failed to fetch node keys from ${url}: HTTP ${response.status}`);
1638
+ }
1639
+ const data = await response.json();
1640
+ if (typeof data !== "object" || data === null) {
1641
+ throw new Error("Node keys response is not an object");
1642
+ }
1643
+ const doc = data;
1644
+ if (typeof doc.nodeId !== "string" || !Array.isArray(doc.keys)) {
1645
+ throw new Error("Node keys document missing required fields (nodeId, keys)");
1646
+ }
1647
+ return data;
1648
+ }
1649
+ function selectNodeKey(doc, kid) {
1650
+ if (kid) {
1651
+ const found = doc.keys.find((k) => k.kid === kid);
1652
+ if (!found) {
1653
+ return {
1654
+ error: {
1655
+ ok: false,
1656
+ code: CerVerifyCode.ATTESTATION_KEY_NOT_FOUND,
1657
+ details: [`Key with kid="${kid}" not found in node keys document`]
1658
+ }
1659
+ };
1660
+ }
1661
+ return { key: found };
1662
+ }
1663
+ if (doc.activeKid) {
1664
+ const found = doc.keys.find((k) => k.kid === doc.activeKid);
1665
+ if (!found) {
1666
+ return {
1667
+ error: {
1668
+ ok: false,
1669
+ code: CerVerifyCode.ATTESTATION_KEY_NOT_FOUND,
1670
+ details: [`activeKid="${doc.activeKid}" not found in keys array`]
1671
+ }
1672
+ };
1673
+ }
1674
+ return { key: found };
1675
+ }
1676
+ if (doc.keys.length === 0) {
1677
+ return {
1678
+ error: {
1679
+ ok: false,
1680
+ code: CerVerifyCode.ATTESTATION_KEY_NOT_FOUND,
1681
+ details: ["No keys available in node keys document"]
1682
+ }
1683
+ };
1684
+ }
1685
+ return { key: doc.keys[0] };
1686
+ }
1687
+ function extractReceiptAndSignature(bundle) {
1688
+ if (typeof bundle !== "object" || bundle === null) return null;
1689
+ const b = bundle;
1690
+ if (typeof b.receipt === "object" && b.receipt !== null && typeof b.signature === "string") {
1691
+ return {
1692
+ receipt: b.receipt,
1693
+ signatureB64Url: b.signature,
1694
+ attestorKeyId: typeof b.attestorKeyId === "string" ? b.attestorKeyId : void 0
1695
+ };
1696
+ }
1697
+ if (typeof b.meta === "object" && b.meta !== null) {
1698
+ const meta = b.meta;
1699
+ if (typeof meta.attestation === "object" && meta.attestation !== null) {
1700
+ const att = meta.attestation;
1701
+ if (typeof att.receipt === "object" && att.receipt !== null && typeof att.signature === "string") {
1702
+ return {
1703
+ receipt: att.receipt,
1704
+ signatureB64Url: att.signature,
1705
+ attestorKeyId: typeof att.attestorKeyId === "string" ? att.attestorKeyId : void 0
1706
+ };
1707
+ }
1708
+ }
1709
+ }
1710
+ return null;
1711
+ }
1712
+ async function verifyBundleAttestation(bundle, options) {
1713
+ const extracted = extractReceiptAndSignature(bundle);
1714
+ if (!extracted) {
1715
+ return {
1716
+ ok: false,
1717
+ code: CerVerifyCode.ATTESTATION_MISSING,
1718
+ details: ["No signed receipt found in bundle (expected bundle.receipt + bundle.signature)"]
1719
+ };
1720
+ }
1721
+ const nodeId = extracted.receipt.nodeId;
1722
+ const resolvedKid = options.kid ?? extracted.attestorKeyId ?? extracted.receipt.attestorKeyId;
1723
+ function ctx() {
1724
+ const lines = [];
1725
+ if (nodeId) lines.push(`nodeId: ${nodeId}`);
1726
+ if (resolvedKid) lines.push(`kid: ${resolvedKid}`);
1727
+ return lines;
1728
+ }
1729
+ let keysDoc;
1730
+ try {
1731
+ keysDoc = await fetchNodeKeys(options.nodeUrl);
1732
+ } catch (err2) {
1733
+ return {
1734
+ ok: false,
1735
+ code: CerVerifyCode.ATTESTATION_KEY_NOT_FOUND,
1736
+ details: [...ctx(), err2 instanceof Error ? err2.message : String(err2)]
1737
+ };
1738
+ }
1739
+ if (typeof bundle.certificateHash === "string") {
1740
+ const bundleCertHash = bundle.certificateHash;
1741
+ if (extracted.receipt.certificateHash !== bundleCertHash) {
1742
+ return {
1743
+ ok: false,
1744
+ code: CerVerifyCode.CERTIFICATE_HASH_MISMATCH,
1745
+ details: [
1746
+ `Receipt certificateHash does not match bundle certificateHash`,
1747
+ `receipt.certificateHash: ${extracted.receipt.certificateHash}`,
1748
+ `bundle.certificateHash: ${bundleCertHash}`,
1749
+ ...ctx()
1750
+ ]
1751
+ };
1752
+ }
1753
+ }
1754
+ const selected = selectNodeKey(keysDoc, resolvedKid);
1755
+ if (selected.error) {
1756
+ return {
1757
+ ...selected.error,
1758
+ details: [...ctx(), ...selected.error.details ?? []]
1759
+ };
1760
+ }
1761
+ const keyEntry = selected.key;
1762
+ const keyParam = {};
1763
+ if (keyEntry.publicKeyJwk) keyParam.jwk = keyEntry.publicKeyJwk;
1764
+ else if (keyEntry.publicKey) keyParam.rawB64Url = keyEntry.publicKey;
1765
+ else if (keyEntry.publicKeySpkiB64) keyParam.spkiB64 = keyEntry.publicKeySpkiB64;
1766
+ else {
1767
+ return {
1768
+ ok: false,
1769
+ code: CerVerifyCode.ATTESTATION_KEY_FORMAT_UNSUPPORTED,
1770
+ details: [
1771
+ `Key kid="${keyEntry.kid}" has no usable public key field (publicKeyJwk, publicKey, or publicKeySpkiB64)`,
1772
+ ...ctx()
1773
+ ]
1774
+ };
1775
+ }
1776
+ const sigResult = await verifyNodeReceiptSignature({
1777
+ receipt: extracted.receipt,
1778
+ signatureB64Url: extracted.signatureB64Url,
1779
+ key: keyParam
1780
+ });
1781
+ const contextLines = ctx();
1782
+ if (contextLines.length === 0) return sigResult;
1783
+ return sigResult.ok ? { ok: true, code: CerVerifyCode.OK, details: contextLines } : { ...sigResult, details: [...contextLines, ...sigResult.details ?? []] };
1784
+ }
686
1785
  export {
687
1786
  CerAttestationError,
688
1787
  CerVerificationError,
@@ -696,6 +1795,7 @@ export {
696
1795
  computeOutputHash,
697
1796
  createSnapshot,
698
1797
  exportCer,
1798
+ fetchNodeKeys,
699
1799
  getAttestationReceipt,
700
1800
  hasAttestation,
701
1801
  hashCanonicalJson,
@@ -703,11 +1803,22 @@ export {
703
1803
  importCer,
704
1804
  sanitizeForAttestation,
705
1805
  sealCer,
1806
+ selectNodeKey,
706
1807
  sha256Hex,
707
1808
  toCanonicalJson,
708
1809
  verifyCer as verify,
1810
+ verifyBundleAttestation,
709
1811
  verifyCer,
1812
+ verifyNodeReceiptSignature,
710
1813
  verifySnapshot,
711
1814
  wrapProvider
712
1815
  };
1816
+ /*! Bundled license information:
1817
+
1818
+ @noble/ed25519/index.js:
1819
+ (*! noble-ed25519 - MIT License (c) 2019 Paul Miller (paulmillr.com) *)
1820
+
1821
+ @noble/hashes/esm/utils.js:
1822
+ (*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
1823
+ */
713
1824
  //# sourceMappingURL=index.mjs.map