@calimero-network/mero-react 9.7.1 → 9.8.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.cjs CHANGED
@@ -454,7 +454,30 @@ async function foundDelegatedNamespace(s, req = {}, deps = {}) {
454
454
  `founded ${founded.namespaceId} but could not give it its application: ${founded.applicationError ?? "unknown reason"}`
455
455
  );
456
456
  }
457
- return { namespaceId: founded.namespaceId, teeEnabled: founded.teeEnabled };
457
+ const ha = founded.teeEnabled ? await enableHaBestEffort(s, founded.namespaceId, founded.salt, relay, deps) : {
458
+ haEnabled: false,
459
+ haError: `the relay did not attest the founding, so no fleet node could be admitted for HA${founded.teeError ? `: ${founded.teeError}` : ""}`
460
+ };
461
+ return { namespaceId: founded.namespaceId, teeEnabled: founded.teeEnabled, ...ha };
462
+ }
463
+ var HA_ACCOUNT_NOT_LINKED_MESSAGE = "link this account to your cloud user in the wallet so invitees can find this namespace";
464
+ async function enableHaBestEffort(s, namespaceId, salt, relayUrl, deps) {
465
+ try {
466
+ await new meroJs.CloudClient({ cloudBaseUrl: deps.cloudBaseUrl, fetch: deps.fetch }).enableHaAsAccount({
467
+ namespaceId,
468
+ salt,
469
+ accountId: s.account,
470
+ credential: s.credential,
471
+ deviceSecret: s.deviceSecret,
472
+ // The relay that founded it: the cloud hands it to the fleet node as its
473
+ // admitter, so the node does not have to find it by discovery.
474
+ relayUrl
475
+ });
476
+ return { haEnabled: true };
477
+ } catch (e) {
478
+ if (e instanceof meroJs.AccountNotLinkedError) return { haEnabled: false, haError: HA_ACCOUNT_NOT_LINKED_MESSAGE };
479
+ return { haEnabled: false, haError: e instanceof Error ? e.message : String(e) };
480
+ }
458
481
  }
459
482
  async function latestPublishedVersion(registryUrl, pkg, fetchFn = globalThis.fetch) {
460
483
  const url = `${registryUrl.replace(/\/+$/, "")}/api/v2/bundles?package=${encodeURIComponent(pkg)}`;
@@ -559,6 +582,16 @@ var NoRelayError = class extends Error {
559
582
  this.name = "NoRelayError";
560
583
  }
561
584
  };
585
+ var InvitationNotClaimableError = class extends Error {
586
+ constructor(namespaceId, reason) {
587
+ super(
588
+ reason === "not-hosted" ? `nobody could claim an invitation to ${namespaceId} yet: the namespace is not hosted in the cloud, so an invitee with no node has no relay to be admitted through. Link this account to your cloud user in the wallet and enable HA for the namespace, then invite.` : `nobody could claim an invitation to ${namespaceId}: the cloud routes to none of the relays or admins it would name as admitters`
589
+ );
590
+ this.namespaceId = namespaceId;
591
+ this.reason = reason;
592
+ this.name = "InvitationNotClaimableError";
593
+ }
594
+ };
562
595
  var EMPTY_READS = {
563
596
  listNamespaces: async () => [],
564
597
  listNamespacesForApplication: async () => [],
@@ -612,6 +645,7 @@ function createAccountAdmin(input, deps = {}) {
612
645
  const latestVersion = deps.latestVersion ?? latestPublishedVersion;
613
646
  const signInvitation = deps.signInvitation ?? meroJs.signGroupInvitation;
614
647
  const join = deps.join;
648
+ const routing = deps.routing;
615
649
  async function joinNamespace(namespaceId, req) {
616
650
  if (!join) throw new NotForAccountError("joinNamespace without a join handler");
617
651
  return join(namespaceId, req.invitation);
@@ -659,12 +693,12 @@ function createAccountAdmin(input, deps = {}) {
659
693
  async createNamespace(req) {
660
694
  if (!app.packageName) throw new NotForAccountError("createNamespace without a packageName");
661
695
  const version = app.packageVersion ?? await latestVersion(app.registryUrl ?? "https://apps.calimero.network", app.packageName);
662
- const { namespaceId } = await found(s, {
696
+ const { namespaceId, haEnabled, haError } = await found(s, {
663
697
  defaultCapabilities: 231,
664
698
  application: { applicationId: req.applicationId, package: app.packageName, version }
665
699
  });
666
700
  if (req.name) await group(s, namespaceId, meroJs.groupMetadataSetOp({ name: req.name }));
667
- return { namespaceId };
701
+ return { namespaceId, haEnabled, ...haError !== void 0 ? { haError } : {} };
668
702
  },
669
703
  async createGroupInNamespace(namespaceId, req = {}) {
670
704
  const { op } = await meroJs.subgroupCreation({ parentId: namespaceId, restricted: req.visibility !== "open", admin: s.account });
@@ -737,6 +771,22 @@ function createAccountAdmin(input, deps = {}) {
737
771
  async createNamespaceInvitation(namespaceId) {
738
772
  const [{ members }, info] = await Promise.all([relay().listGroupMembers(namespaceId), relay().getGroupInfo(namespaceId)]);
739
773
  const relays = members.filter((m) => m.role === "RelayTee").map((m) => m.identity.toLowerCase());
774
+ if (routing) {
775
+ const named = new Set(
776
+ relays.length > 0 ? relays : members.filter((m) => m.role === "Admin").map((m) => m.identity.toLowerCase())
777
+ );
778
+ let nodes = null;
779
+ try {
780
+ nodes = (await routing(namespaceId)).nodes;
781
+ } catch {
782
+ }
783
+ if (nodes !== null) {
784
+ if (nodes.length === 0) throw new InvitationNotClaimableError(namespaceId, "not-hosted");
785
+ if (!nodes.some((n) => n.account !== null && named.has(n.account.toLowerCase()))) {
786
+ throw new InvitationNotClaimableError(namespaceId, "no-named-node");
787
+ }
788
+ }
789
+ }
740
790
  const invitation = await signInvitation({
741
791
  groupId: namespaceId,
742
792
  inviterAccount: s.account,
@@ -1558,7 +1608,14 @@ function MeroProvider({
1558
1608
  const app = { packageName, packageVersion, registryUrl };
1559
1609
  if (delegated !== null) {
1560
1610
  const deps = {
1561
- join: (namespaceId, invitation) => joinAsAccount(delegated, namespaceId, invitation, { cloudBaseUrl, onJoined: connectWithAccount })
1611
+ join: (namespaceId, invitation) => joinAsAccount(delegated, namespaceId, invitation, { cloudBaseUrl, onJoined: connectWithAccount }),
1612
+ // Founding enables HA on the same cloud the joins resolve relays from.
1613
+ found: (session, req) => foundDelegatedNamespace(session, req, { cloudBaseUrl }),
1614
+ // Invitations are checked against the routing their claimants will use.
1615
+ routing: (namespaceId) => new meroJs.CloudClient({
1616
+ cloudBaseUrl,
1617
+ routingCredential: { credential: delegated.credential, deviceSecret: delegated.deviceSecret }
1618
+ }).getNamespaceRouting(namespaceId)
1562
1619
  };
1563
1620
  if (delegated.relayUrl === null) return createAccountAdmin({ session: delegated, read: null, app }, deps);
1564
1621
  if (!mero) return null;
@@ -3734,7 +3791,7 @@ function useNamespacesForApplication(applicationId) {
3734
3791
  }
3735
3792
  var DEFAULT_REGISTRY_URL = "https://apps.calimero.network";
3736
3793
  function useCreateNamespace() {
3737
- const { mero, isDelegated, app } = useMero();
3794
+ const { mero, isDelegated, app, cloudBaseUrl } = useMero();
3738
3795
  const { loading, error, run } = useAsyncMutation();
3739
3796
  const createNamespace = react.useCallback(
3740
3797
  async (request) => {
@@ -3747,16 +3804,20 @@ function useCreateNamespace() {
3747
3804
  throw new Error("this app names no registry package, so a founded namespace could not be given its application");
3748
3805
  }
3749
3806
  const version = app.packageVersion ?? await latestPublishedVersion(app.registryUrl ?? DEFAULT_REGISTRY_URL, pkg);
3750
- return foundDelegatedNamespace(session, {
3751
- defaultCapabilities: 231,
3752
- application: { applicationId: request.applicationId, package: pkg, version }
3753
- });
3807
+ return foundDelegatedNamespace(
3808
+ session,
3809
+ {
3810
+ defaultCapabilities: 231,
3811
+ application: { applicationId: request.applicationId, package: pkg, version }
3812
+ },
3813
+ { cloudBaseUrl }
3814
+ );
3754
3815
  });
3755
3816
  }
3756
3817
  if (!mero) return null;
3757
3818
  return run(() => mero.admin.createNamespace(request));
3758
3819
  },
3759
- [mero, run, isDelegated, app]
3820
+ [mero, run, isDelegated, app, cloudBaseUrl]
3760
3821
  );
3761
3822
  return { createNamespace, loading, error };
3762
3823
  }
@@ -4912,6 +4973,8 @@ exports.ConnectButton = ConnectButton;
4912
4973
  exports.ConnectButtonAccount = ConnectButtonAccount;
4913
4974
  exports.ConnectionType = ConnectionType;
4914
4975
  exports.DEFAULT_MIGRATION_POLL_INTERVAL_MS = DEFAULT_MIGRATION_POLL_INTERVAL_MS;
4976
+ exports.HA_ACCOUNT_NOT_LINKED_MESSAGE = HA_ACCOUNT_NOT_LINKED_MESSAGE;
4977
+ exports.InvitationNotClaimableError = InvitationNotClaimableError;
4915
4978
  exports.LoginModal = LoginModal;
4916
4979
  exports.MERO_CSS_VARS = MERO_CSS_VARS;
4917
4980
  exports.MeroContext = MeroContext;