@calimero-network/mero-js 27.2.0 → 27.4.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
@@ -1000,6 +1000,7 @@ var NODE_RELEASE_SIGNER = Object.freeze({
1000
1000
  });
1001
1001
  var MEASUREMENTS = ["mrtd", "rtmr0", "rtmr1", "rtmr2", "rtmr3"];
1002
1002
  var DEFAULT_RELEASE_MIRROR = "https://cloud.calimero.network";
1003
+ var DEFAULT_COLLATERAL_MIRROR = `${DEFAULT_RELEASE_MIRROR}/api/tee/pcs`;
1003
1004
  async function verifySignedNodeRelease(publishedMrtds, bundle, options = {}) {
1004
1005
  const body = typeof publishedMrtds === "string" ? new TextEncoder().encode(publishedMrtds) : publishedMrtds;
1005
1006
  await verifyCosignBundle(
@@ -1164,6 +1165,34 @@ async function releaseFor(version, policy, fetchImpl) {
1164
1165
  if (source === "github") return fetchGithubNodeRelease(version, { fetch: fetchImpl });
1165
1166
  return fetchNodeRelease(cloudNodeReleaseUrl(source ?? DEFAULT_RELEASE_MIRROR, version), { fetch: fetchImpl });
1166
1167
  }
1168
+ function defaultCollateral(policy) {
1169
+ const source = policy.collateralSource ?? DEFAULT_COLLATERAL_MIRROR;
1170
+ if (source === "intel") {
1171
+ return async (quote) => (await dcap("getCollateralFromPcs"))(quote);
1172
+ }
1173
+ return async (quote) => (await dcap("getCollateral"))(source, quote);
1174
+ }
1175
+ function freshCollateral(fetch2, policy) {
1176
+ const maxDays = policy.maxCollateralAgeDays;
1177
+ if (maxDays === void 0) return fetch2;
1178
+ return async (quote) => {
1179
+ const collateral = await fetch2(quote);
1180
+ let issued;
1181
+ try {
1182
+ issued = Date.parse(JSON.parse(collateral.tcb_info).issueDate ?? "");
1183
+ } catch {
1184
+ issued = Number.NaN;
1185
+ }
1186
+ if (!Number.isFinite(issued)) throw new Error("The quote's collateral names no TCB info issue date");
1187
+ const ageDays = ((policy.now ?? Date.now)() - issued) / 864e5;
1188
+ if (ageDays > maxDays) {
1189
+ throw new Error(
1190
+ `The quote's collateral was issued ${Math.floor(ageDays)} days ago, more than the ${maxDays} this client accepts`
1191
+ );
1192
+ }
1193
+ return collateral;
1194
+ };
1195
+ }
1167
1196
  async function verifyRelease(doc, args, policy, fetchImpl) {
1168
1197
  const version = claimedRelease(doc, policy.minReleaseVersion);
1169
1198
  const trusted = await trustSignedRelease({
@@ -1174,7 +1203,7 @@ async function verifyRelease(doc, args, policy, fetchImpl) {
1174
1203
  identity: policy.identity,
1175
1204
  issuer: policy.issuer
1176
1205
  });
1177
- const fetchCollateral = policy.fetchCollateral ?? (async (quote) => (await dcap("getCollateralFromPcs"))(quote));
1206
+ const fetchCollateral = freshCollateral(policy.fetchCollateral ?? defaultCollateral(policy), policy);
1178
1207
  const verifier = createQuoteVerifier({
1179
1208
  dcapVerify: policy.dcapVerify ?? await dcap("verify"),
1180
1209
  fetchCollateral,
@@ -1463,6 +1492,17 @@ ${generation}`;
1463
1492
  function publisherBindingBytes(cluster, org, signerId, generation) {
1464
1493
  return new TextEncoder().encode(publisherBindingMessage(cluster, org, signerId, generation));
1465
1494
  }
1495
+ var CROCKFORD = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
1496
+ async function confirmationCode(requestId) {
1497
+ const h = new Uint8Array(await crypto.subtle.digest("SHA-256", new TextEncoder().encode(requestId)));
1498
+ const word = (h[0] << 24 | h[1] << 16 | h[2] << 8 | h[3]) >>> 0;
1499
+ let code = "";
1500
+ for (let i = 0; i < 6; i++) {
1501
+ if (i === 3) code += "-";
1502
+ code += CROCKFORD[word >>> 27 - 5 * i & 31];
1503
+ }
1504
+ return code;
1505
+ }
1466
1506
  function consentReturn(href) {
1467
1507
  const url2 = typeof href === "string" ? new URL(href) : href;
1468
1508
  const requestId = url2.searchParams.get("request_id");
@@ -2236,16 +2276,34 @@ var CalimeroClient = class {
2236
2276
  }
2237
2277
  /**
2238
2278
  * {@link requestConsent}, then send the browser to the consent page
2239
- * (`navigate`, by default `window.location.assign`). Answers the request
2240
- * once `navigate` returns: with a popup (`url => window.open(url)`), poll it
2241
- * with {@link waitForConsent}.
2279
+ * (`navigate`, by default `window.location.assign`). Pass `navigate` itself
2280
+ * or {@link OpenConsentOptions}, whose `confirm` runs first with the node's
2281
+ * answer so the app can show the person its `code`; when it answers `false`
2282
+ * the page is not opened. When the node sends a `code` that is not
2283
+ * {@link confirmationCode} of `requestId`, it throws `CalimeroError`
2284
+ * (`CONSENT_CODE_MISMATCH`) and does not navigate. Answers the request
2285
+ * once `navigate` returns (or `confirm` declines): with a popup
2286
+ * (`url => window.open(url)`), poll it with {@link waitForConsent}.
2242
2287
  */
2243
- async openConsentPage(request, navigate = (url2) => {
2244
- const w = globalThis.window;
2245
- if (!w) throw new Error("no window to open the consent page in; pass `navigate`");
2246
- w.location.assign(url2);
2247
- }) {
2288
+ async openConsentPage(request, navigateOrOptions = {}) {
2289
+ const options = typeof navigateOrOptions === "function" ? { navigate: navigateOrOptions } : navigateOrOptions;
2290
+ const navigate = options.navigate ?? ((url2) => {
2291
+ const w = globalThis.window;
2292
+ if (!w) throw new Error("no window to open the consent page in; pass `navigate`");
2293
+ w.location.assign(url2);
2294
+ });
2248
2295
  const started = await this.requestConsent(request);
2296
+ if (started.code !== void 0) {
2297
+ const expected = await confirmationCode(started.requestId);
2298
+ if (started.code !== expected) {
2299
+ throw new CalimeroError(200, "CONSENT_CODE_MISMATCH", "the consent page code does not match the request", {
2300
+ requestId: started.requestId,
2301
+ code: started.code,
2302
+ expected
2303
+ });
2304
+ }
2305
+ }
2306
+ if (options.confirm && !await options.confirm(started)) return started;
2249
2307
  navigate(started.url);
2250
2308
  return started;
2251
2309
  }
@@ -2840,6 +2898,7 @@ export {
2840
2898
  ConfirmCancelled,
2841
2899
  ConfirmRequired,
2842
2900
  ConsentStale,
2901
+ DEFAULT_COLLATERAL_MIRROR,
2843
2902
  DEFAULT_ORG_CAPABILITIES,
2844
2903
  DEFAULT_RELEASE_MIRROR,
2845
2904
  GITHUB_RELEASES,
@@ -2871,6 +2930,7 @@ export {
2871
2930
  claimedRelease,
2872
2931
  cloudNodeReleaseUrl,
2873
2932
  compareVersions,
2933
+ confirmationCode,
2874
2934
  consentReturn,
2875
2935
  createQuoteVerifier,
2876
2936
  createSealedFetch,
@@ -2879,6 +2939,7 @@ export {
2879
2939
  fetchAttestedKey,
2880
2940
  fetchGithubNodeRelease,
2881
2941
  fetchNodeRelease,
2942
+ freshCollateral,
2882
2943
  fromB64url,
2883
2944
  generateOwnerKey,
2884
2945
  orgCapabilities,