@calimero-network/mero-js 27.2.0 → 27.3.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
@@ -1463,6 +1463,17 @@ ${generation}`;
1463
1463
  function publisherBindingBytes(cluster, org, signerId, generation) {
1464
1464
  return new TextEncoder().encode(publisherBindingMessage(cluster, org, signerId, generation));
1465
1465
  }
1466
+ var CROCKFORD = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
1467
+ async function confirmationCode(requestId) {
1468
+ const h = new Uint8Array(await crypto.subtle.digest("SHA-256", new TextEncoder().encode(requestId)));
1469
+ const word = (h[0] << 24 | h[1] << 16 | h[2] << 8 | h[3]) >>> 0;
1470
+ let code = "";
1471
+ for (let i = 0; i < 6; i++) {
1472
+ if (i === 3) code += "-";
1473
+ code += CROCKFORD[word >>> 27 - 5 * i & 31];
1474
+ }
1475
+ return code;
1476
+ }
1466
1477
  function consentReturn(href) {
1467
1478
  const url2 = typeof href === "string" ? new URL(href) : href;
1468
1479
  const requestId = url2.searchParams.get("request_id");
@@ -2236,16 +2247,34 @@ var CalimeroClient = class {
2236
2247
  }
2237
2248
  /**
2238
2249
  * {@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}.
2250
+ * (`navigate`, by default `window.location.assign`). Pass `navigate` itself
2251
+ * or {@link OpenConsentOptions}, whose `confirm` runs first with the node's
2252
+ * answer so the app can show the person its `code`; when it answers `false`
2253
+ * the page is not opened. When the node sends a `code` that is not
2254
+ * {@link confirmationCode} of `requestId`, it throws `CalimeroError`
2255
+ * (`CONSENT_CODE_MISMATCH`) and does not navigate. Answers the request
2256
+ * once `navigate` returns (or `confirm` declines): with a popup
2257
+ * (`url => window.open(url)`), poll it with {@link waitForConsent}.
2242
2258
  */
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
- }) {
2259
+ async openConsentPage(request, navigateOrOptions = {}) {
2260
+ const options = typeof navigateOrOptions === "function" ? { navigate: navigateOrOptions } : navigateOrOptions;
2261
+ const navigate = options.navigate ?? ((url2) => {
2262
+ const w = globalThis.window;
2263
+ if (!w) throw new Error("no window to open the consent page in; pass `navigate`");
2264
+ w.location.assign(url2);
2265
+ });
2248
2266
  const started = await this.requestConsent(request);
2267
+ if (started.code !== void 0) {
2268
+ const expected = await confirmationCode(started.requestId);
2269
+ if (started.code !== expected) {
2270
+ throw new CalimeroError(200, "CONSENT_CODE_MISMATCH", "the consent page code does not match the request", {
2271
+ requestId: started.requestId,
2272
+ code: started.code,
2273
+ expected
2274
+ });
2275
+ }
2276
+ }
2277
+ if (options.confirm && !await options.confirm(started)) return started;
2249
2278
  navigate(started.url);
2250
2279
  return started;
2251
2280
  }
@@ -2871,6 +2900,7 @@ export {
2871
2900
  claimedRelease,
2872
2901
  cloudNodeReleaseUrl,
2873
2902
  compareVersions,
2903
+ confirmationCode,
2874
2904
  consentReturn,
2875
2905
  createQuoteVerifier,
2876
2906
  createSealedFetch,