@calimero-network/mero-react 9.10.0 → 9.11.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/README.md CHANGED
@@ -117,7 +117,7 @@ const {
117
117
  isOnline, // boolean — SSE connection state
118
118
  isLoading, // boolean — initial session restore
119
119
  nodeUrl, // string | null
120
- applicationId, // string | null — resolved from auth callback
120
+ applicationId, // string | null — node: from the auth callback; account: see below
121
121
  contextId, // string | null — from auth callback
122
122
  contextIdentity, // string | null — executor public key from auth callback
123
123
  connectToNode, // (url: string) => void — starts auth redirect
@@ -125,6 +125,8 @@ const {
125
125
  } = useMero();
126
126
  ```
127
127
 
128
+ How an account (delegated session) learns its `applicationId`: first from its own contexts on the relay — one distinct application across them is the answer; several leaves it `null` on purpose (nothing says which the tab is for). With no contexts yet, it is derived from the registry by `packageName`: the id is `sha256(borsh((package, signerId)))` of the newest non-yanked bundle, exactly as merod computes it at install, so a brand-new account can found its first namespace (`admin.createNamespace({ applicationId })`). Without a `packageName`, or when the registry lists the package under more than one publisher, it stays `null`. `applicationIdForBundle` and `resolveApplicationIdFromRegistry` are exported for apps that need the same derivation.
129
+
128
130
  Through `mero` you access the full MeroJs API:
129
131
 
130
132
  ```tsx
package/dist/index.cjs CHANGED
@@ -437,6 +437,56 @@ async function listDelegatedNamespaces(s) {
437
437
  return out;
438
438
  }
439
439
 
440
+ // src/delegated/application-id.ts
441
+ function borshString(s) {
442
+ const bytes = new TextEncoder().encode(s);
443
+ const out = new Uint8Array(4 + bytes.length);
444
+ new DataView(out.buffer).setUint32(0, bytes.length, true);
445
+ out.set(bytes, 4);
446
+ return out;
447
+ }
448
+ async function applicationIdForBundle(pkg, signerId) {
449
+ const a = borshString(pkg);
450
+ const b = borshString(signerId);
451
+ const tuple = new Uint8Array(a.length + b.length);
452
+ tuple.set(a, 0);
453
+ tuple.set(b, a.length);
454
+ const digest = await crypto.subtle.digest("SHA-256", tuple);
455
+ return Array.from(new Uint8Array(digest), (x) => x.toString(16).padStart(2, "0")).join("");
456
+ }
457
+ function newer(a, b) {
458
+ const [x, y] = [a, b].map((v) => v.split(/[.-]/).map((n) => Number.parseInt(n, 10) || 0));
459
+ for (let i = 0; i < Math.max(x.length, y.length); i++) {
460
+ if ((x[i] ?? 0) !== (y[i] ?? 0)) return (x[i] ?? 0) > (y[i] ?? 0);
461
+ }
462
+ return false;
463
+ }
464
+ function selectLatestBundle(bundles) {
465
+ return bundles.filter((b) => !b.yanked && typeof b.appVersion === "string" && b.appVersion.length > 0).reduce((best, b) => !best || newer(b.appVersion, best.appVersion) ? b : best, void 0);
466
+ }
467
+ async function fetchRegistryBundles(registryUrl, pkg, fetchFn = globalThis.fetch) {
468
+ const url = `${registryUrl.replace(/\/+$/, "")}/api/v2/bundles?package=${encodeURIComponent(pkg)}`;
469
+ const response = await fetchFn(url, { headers: { Accept: "application/json" } });
470
+ if (!response.ok) throw new Error(`the registry has no ${pkg} (HTTP ${response.status})`);
471
+ const body = await response.json();
472
+ return Array.isArray(body) ? body : [];
473
+ }
474
+ async function resolveApplicationIdFromRegistry(registryUrl, pkg, deps = {}) {
475
+ const bundles = await fetchRegistryBundles(registryUrl, pkg, deps.fetch);
476
+ const live = bundles.filter((b) => !b.yanked);
477
+ const signers = [...new Set(live.map((b) => b.signerId).filter((s) => typeof s === "string" && s.length > 0))];
478
+ if (signers.length > 1) {
479
+ throw new Error(
480
+ `the registry lists ${pkg} under more than one publisher (${signers.join(", ")}): refusing to guess which application it is`
481
+ );
482
+ }
483
+ const latest = selectLatestBundle(live);
484
+ if (!latest) throw new Error(`the registry lists no version of ${pkg}`);
485
+ const signerId = latest.signerId;
486
+ if (!signerId) throw new Error(`the registry names no publisher for ${pkg} ${latest.appVersion}`);
487
+ return { applicationId: await applicationIdForBundle(pkg, signerId), signerId, version: latest.appVersion };
488
+ }
489
+
440
490
  // src/delegated/create-context.ts
441
491
  function governanceNonce(relay, group) {
442
492
  return { next: async () => nextGovernanceNonce(relay, group) };
@@ -585,20 +635,9 @@ async function enableHaBestEffort(s, namespaceId, salt, relayUrl, deps) {
585
635
  }
586
636
  }
587
637
  async function latestPublishedVersion(registryUrl, pkg, fetchFn = globalThis.fetch) {
588
- const url = `${registryUrl.replace(/\/+$/, "")}/api/v2/bundles?package=${encodeURIComponent(pkg)}`;
589
- const response = await fetchFn(url, { headers: { Accept: "application/json" } });
590
- if (!response.ok) throw new Error(`the registry has no ${pkg} (HTTP ${response.status})`);
591
- const bundles = await response.json();
592
- const newer = (a, b) => {
593
- const [x, y] = [a, b].map((v) => v.split(/[.-]/).map((n) => Number.parseInt(n, 10) || 0));
594
- for (let i = 0; i < Math.max(x.length, y.length); i++) {
595
- if ((x[i] ?? 0) !== (y[i] ?? 0)) return (x[i] ?? 0) > (y[i] ?? 0);
596
- }
597
- return false;
598
- };
599
- const latest = bundles.map((b) => b.appVersion).filter((v) => typeof v === "string" && v.length > 0).reduce((best, v) => !best || newer(v, best) ? v : best, void 0);
638
+ const latest = selectLatestBundle(await fetchRegistryBundles(registryUrl, pkg, fetchFn));
600
639
  if (!latest) throw new Error(`the registry lists no version of ${pkg}`);
601
- return latest;
640
+ return latest.appVersion;
602
641
  }
603
642
 
604
643
  // src/admin/node-admin.ts
@@ -1395,6 +1434,7 @@ function clearAllStorage() {
1395
1434
  });
1396
1435
  }
1397
1436
  var MeroContext = react.createContext(null);
1437
+ var DEFAULT_REGISTRY_URL = "https://apps.calimero.network";
1398
1438
  var isBrowser = typeof window !== "undefined";
1399
1439
  function getPermissionsForMode(mode) {
1400
1440
  switch (mode) {
@@ -1568,6 +1608,17 @@ function MeroProvider({
1568
1608
  if (apps.length === 1) {
1569
1609
  setApplicationIdState(apps[0]);
1570
1610
  }
1611
+ if (apps.length === 0 && packageName) {
1612
+ try {
1613
+ const { applicationId: derived } = await resolveApplicationIdFromRegistry(
1614
+ registryUrl ?? DEFAULT_REGISTRY_URL,
1615
+ packageName
1616
+ );
1617
+ if (active) setApplicationIdState(derived);
1618
+ } catch (e) {
1619
+ console.warn(`[mero-react] could not learn the application id of ${packageName} from the registry`, e);
1620
+ }
1621
+ }
1571
1622
  } catch {
1572
1623
  }
1573
1624
  })();
@@ -1575,7 +1626,7 @@ function MeroProvider({
1575
1626
  active = false;
1576
1627
  stop.abort();
1577
1628
  };
1578
- }, [delegated, contextId]);
1629
+ }, [delegated, contextId, packageName, registryUrl]);
1579
1630
  react.useEffect(() => {
1580
1631
  if (delegated) return;
1581
1632
  let active = true;
@@ -3924,7 +3975,7 @@ function useNamespacesForApplication(applicationId) {
3924
3975
  );
3925
3976
  return { namespaces: data, loading, error, refetch };
3926
3977
  }
3927
- var DEFAULT_REGISTRY_URL = "https://apps.calimero.network";
3978
+ var DEFAULT_REGISTRY_URL2 = "https://apps.calimero.network";
3928
3979
  function useCreateNamespace() {
3929
3980
  const { mero, isDelegated, app, cloudBaseUrl } = useMero();
3930
3981
  const { loading, error, run } = useAsyncMutation();
@@ -3938,7 +3989,7 @@ function useCreateNamespace() {
3938
3989
  if (!pkg) {
3939
3990
  throw new Error("this app names no registry package, so a founded namespace could not be given its application");
3940
3991
  }
3941
- const version = app.packageVersion ?? await latestPublishedVersion(app.registryUrl ?? DEFAULT_REGISTRY_URL, pkg);
3992
+ const version = app.packageVersion ?? await latestPublishedVersion(app.registryUrl ?? DEFAULT_REGISTRY_URL2, pkg);
3942
3993
  return foundDelegatedNamespace(
3943
3994
  session,
3944
3995
  {
@@ -5119,6 +5170,7 @@ exports.MigrationAdminPanel = MigrationAdminPanel;
5119
5170
  exports.MigrationPendingBanner = MigrationPendingBanner;
5120
5171
  exports.NoRelayError = NoRelayError;
5121
5172
  exports.NotForAccountError = NotForAccountError;
5173
+ exports.applicationIdForBundle = applicationIdForBundle;
5122
5174
  exports.bootstrapFromInvitation = bootstrapFromInvitation;
5123
5175
  exports.clearAllStorage = clearAllStorage;
5124
5176
  exports.clearApplicationId = clearApplicationId;
@@ -5149,9 +5201,11 @@ exports.readPinnedRelayNodeKey = readPinnedRelayNodeKey;
5149
5201
  exports.readRelayMap = readRelayMap;
5150
5202
  exports.relayForContext = relayForContext;
5151
5203
  exports.rememberRelay = rememberRelay;
5204
+ exports.resolveApplicationIdFromRegistry = resolveApplicationIdFromRegistry;
5152
5205
  exports.resolveMeroTheme = resolveMeroTheme;
5153
5206
  exports.resolveRelayFromInvitation = resolveRelayFromInvitation;
5154
5207
  exports.saveDelegatedCredential = saveDelegatedCredential;
5208
+ exports.selectLatestBundle = selectLatestBundle;
5155
5209
  exports.setApplicationId = setApplicationId;
5156
5210
  exports.setContextId = setContextId;
5157
5211
  exports.setContextIdentity = setContextIdentity;