@calimero-network/mero-js 29.7.2 → 29.7.4

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
@@ -1379,6 +1379,13 @@ var ConfirmCancelled = class extends Error {
1379
1379
  this.name = "ConfirmCancelled";
1380
1380
  }
1381
1381
  };
1382
+ var ConfirmPromptBlocked = class extends Error {
1383
+ constructor(cause) {
1384
+ super("the passkey prompt was blocked by the browser or a password manager");
1385
+ this.cause = cause;
1386
+ this.name = "ConfirmPromptBlocked";
1387
+ }
1388
+ };
1382
1389
  var ArgsTooLarge = class extends CalimeroError {
1383
1390
  constructor(message, details = {}) {
1384
1391
  super(413, "ARGS_TOO_LARGE", message, details);
@@ -1585,17 +1592,36 @@ function cancelled(message) {
1585
1592
  error.name = "NotAllowedError";
1586
1593
  return error;
1587
1594
  }
1595
+ async function ask(method, options) {
1596
+ const container = navigator.credentials;
1597
+ const page = container[method];
1598
+ try {
1599
+ return await page.call(container, options);
1600
+ } catch (error) {
1601
+ const own = globalThis.CredentialsContainer?.prototype?.[method];
1602
+ if (!own || own === page || !stuck(error)) throw error;
1603
+ return own.call(container, options);
1604
+ }
1605
+ }
1606
+ function stuck(error) {
1607
+ return /request is already pending/i.test(
1608
+ error?.message?.toString() ?? ""
1609
+ );
1610
+ }
1588
1611
  function browserPasskey() {
1589
1612
  return {
1590
1613
  async create(options) {
1591
1614
  const o = options;
1592
- const cred = await navigator.credentials.create({
1615
+ const cred = await ask("create", {
1593
1616
  publicKey: {
1594
1617
  challenge: buf(fromB64url(o.challenge)),
1595
1618
  rp: o.rp,
1596
1619
  user: { ...o.user, id: buf(fromB64url(o.user.id)) },
1597
1620
  pubKeyCredParams: o.pubKeyCredParams,
1598
- excludeCredentials: (o.excludeCredentials ?? []).map((c) => ({ type: c.type, id: buf(fromB64url(c.id)) })),
1621
+ excludeCredentials: (o.excludeCredentials ?? []).map((c) => ({
1622
+ type: c.type,
1623
+ id: buf(fromB64url(c.id))
1624
+ })),
1599
1625
  authenticatorSelection: o.authenticatorSelection,
1600
1626
  attestation: "none",
1601
1627
  timeout: o.timeout
@@ -1608,13 +1634,16 @@ function browserPasskey() {
1608
1634
  clientDataJson: b64url(new Uint8Array(r.clientDataJSON))
1609
1635
  };
1610
1636
  },
1611
- async get(challenge, rpId, allowCredentials) {
1612
- const cred = await navigator.credentials.get({
1637
+ async get(challenge, rpId, allowCredentials, options) {
1638
+ const cred = await ask("get", {
1613
1639
  publicKey: {
1614
1640
  challenge: buf(challenge),
1615
1641
  rpId,
1616
- userVerification: "preferred",
1617
- allowCredentials: (allowCredentials ?? []).map((id) => ({ type: "public-key", id: buf(fromB64url(id)) }))
1642
+ userVerification: options?.userVerification ?? "preferred",
1643
+ allowCredentials: (allowCredentials ?? []).map((id) => ({
1644
+ type: "public-key",
1645
+ id: buf(fromB64url(id))
1646
+ }))
1618
1647
  }
1619
1648
  });
1620
1649
  if (!cred) throw cancelled("passkey sign-in was cancelled");
@@ -1834,6 +1863,19 @@ function pageDeployment(href) {
1834
1863
  return null;
1835
1864
  }
1836
1865
  }
1866
+ function userVerificationOf(named) {
1867
+ return named === "required" || named === "preferred" || named === "discouraged" ? named : "required";
1868
+ }
1869
+ function promptFailure(error) {
1870
+ const e = error;
1871
+ const name = e?.name;
1872
+ const message = typeof e?.message === "string" ? e.message : "";
1873
+ if (name === "InvalidStateError" || /not focused|already pending/i.test(message)) {
1874
+ return new ConfirmPromptBlocked(error);
1875
+ }
1876
+ if (name === "NotAllowedError" || name === "AbortError") return new ConfirmCancelled(error);
1877
+ return error;
1878
+ }
1837
1879
  function seg(value) {
1838
1880
  return encodeURIComponent(value);
1839
1881
  }
@@ -2611,11 +2653,11 @@ var CalimeroClient = class _CalimeroClient {
2611
2653
  const d = error.details;
2612
2654
  let assertion;
2613
2655
  try {
2614
- assertion = await passkey.get(fromB64url(d.challenge), d.rpId, d.allowCredentials);
2656
+ assertion = await passkey.get(fromB64url(d.challenge), d.rpId, d.allowCredentials, {
2657
+ userVerification: userVerificationOf(d.userVerification)
2658
+ });
2615
2659
  } catch (declined) {
2616
- const name = declined?.name;
2617
- if (name === "NotAllowedError" || name === "AbortError") throw new ConfirmCancelled(declined);
2618
- throw declined;
2660
+ throw promptFailure(declined);
2619
2661
  }
2620
2662
  return send({ ...body, confirm: { challengeId: d.challengeId, assertion } });
2621
2663
  }
@@ -3607,6 +3649,7 @@ export {
3607
3649
  CalimeroError,
3608
3650
  ClusterNotUpgraded,
3609
3651
  ConfirmCancelled,
3652
+ ConfirmPromptBlocked,
3610
3653
  ConfirmRequired,
3611
3654
  ConsentStale,
3612
3655
  DEFAULT_COLLATERAL_MIRROR,