@calimero-network/mero-react 9.10.0 → 9.11.1

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`: `packageName` decides. When the app passes one, the id is derived from the registry: `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 })`) and an account already used in other apps is never handed one of THEIR ids. The account's own contexts on the relay are consulted only when the app passes no `packageName` (or the registry did not answer, after one `console.warn`): one distinct application across them is the answer; several leaves it `null` on purpose (nothing says which the tab is for). 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) {
@@ -1559,12 +1599,26 @@ function MeroProvider({
1559
1599
  setIsLoading(false);
1560
1600
  if (!nodeKey) return;
1561
1601
  }
1602
+ if (packageName) {
1603
+ try {
1604
+ const { applicationId: derived } = await resolveApplicationIdFromRegistry(
1605
+ registryUrl ?? DEFAULT_REGISTRY_URL,
1606
+ packageName
1607
+ );
1608
+ if (!active) return;
1609
+ setApplicationIdState(derived);
1610
+ return;
1611
+ } catch (e) {
1612
+ console.warn(`[mero-react] could not learn the application id of ${packageName} from the registry`, e);
1613
+ if (!active) return;
1614
+ }
1615
+ }
1562
1616
  try {
1563
1617
  const contexts = await listDelegatedContexts(delegated);
1618
+ if (!active) return;
1564
1619
  const apps = [
1565
1620
  ...new Set((contexts ?? []).map((c) => c.applicationId).filter(Boolean))
1566
1621
  ];
1567
- if (!active) return;
1568
1622
  if (apps.length === 1) {
1569
1623
  setApplicationIdState(apps[0]);
1570
1624
  }
@@ -1575,7 +1629,7 @@ function MeroProvider({
1575
1629
  active = false;
1576
1630
  stop.abort();
1577
1631
  };
1578
- }, [delegated, contextId]);
1632
+ }, [delegated, contextId, packageName, registryUrl]);
1579
1633
  react.useEffect(() => {
1580
1634
  if (delegated) return;
1581
1635
  let active = true;
@@ -3924,7 +3978,7 @@ function useNamespacesForApplication(applicationId) {
3924
3978
  );
3925
3979
  return { namespaces: data, loading, error, refetch };
3926
3980
  }
3927
- var DEFAULT_REGISTRY_URL = "https://apps.calimero.network";
3981
+ var DEFAULT_REGISTRY_URL2 = "https://apps.calimero.network";
3928
3982
  function useCreateNamespace() {
3929
3983
  const { mero, isDelegated, app, cloudBaseUrl } = useMero();
3930
3984
  const { loading, error, run } = useAsyncMutation();
@@ -3938,7 +3992,7 @@ function useCreateNamespace() {
3938
3992
  if (!pkg) {
3939
3993
  throw new Error("this app names no registry package, so a founded namespace could not be given its application");
3940
3994
  }
3941
- const version = app.packageVersion ?? await latestPublishedVersion(app.registryUrl ?? DEFAULT_REGISTRY_URL, pkg);
3995
+ const version = app.packageVersion ?? await latestPublishedVersion(app.registryUrl ?? DEFAULT_REGISTRY_URL2, pkg);
3942
3996
  return foundDelegatedNamespace(
3943
3997
  session,
3944
3998
  {
@@ -5119,6 +5173,7 @@ exports.MigrationAdminPanel = MigrationAdminPanel;
5119
5173
  exports.MigrationPendingBanner = MigrationPendingBanner;
5120
5174
  exports.NoRelayError = NoRelayError;
5121
5175
  exports.NotForAccountError = NotForAccountError;
5176
+ exports.applicationIdForBundle = applicationIdForBundle;
5122
5177
  exports.bootstrapFromInvitation = bootstrapFromInvitation;
5123
5178
  exports.clearAllStorage = clearAllStorage;
5124
5179
  exports.clearApplicationId = clearApplicationId;
@@ -5149,9 +5204,11 @@ exports.readPinnedRelayNodeKey = readPinnedRelayNodeKey;
5149
5204
  exports.readRelayMap = readRelayMap;
5150
5205
  exports.relayForContext = relayForContext;
5151
5206
  exports.rememberRelay = rememberRelay;
5207
+ exports.resolveApplicationIdFromRegistry = resolveApplicationIdFromRegistry;
5152
5208
  exports.resolveMeroTheme = resolveMeroTheme;
5153
5209
  exports.resolveRelayFromInvitation = resolveRelayFromInvitation;
5154
5210
  exports.saveDelegatedCredential = saveDelegatedCredential;
5211
+ exports.selectLatestBundle = selectLatestBundle;
5155
5212
  exports.setApplicationId = setApplicationId;
5156
5213
  exports.setContextId = setContextId;
5157
5214
  exports.setContextIdentity = setContextIdentity;