@calimero-network/mero-react 9.8.0 → 9.9.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
@@ -304,6 +304,11 @@ function rememberRelay(account, relayUrl, at) {
304
304
  function relayForContext(account, contextId) {
305
305
  return readRelayMap(account).contexts[contextId] ?? null;
306
306
  }
307
+ function carryExecutorAccount(prev, next) {
308
+ if (next.executorAccount || !prev?.executorAccount || !prev.relayUrl || !next.relayUrl) return next;
309
+ if (relayOrigin(prev.relayUrl) !== relayOrigin(next.relayUrl)) return next;
310
+ return { ...next, executorAccount: prev.executorAccount };
311
+ }
307
312
  function knownRelays(s) {
308
313
  const map = readRelayMap(s.account);
309
314
  const all = [s.relayUrl, ...Object.values(map.namespaces), ...Object.values(map.contexts)].filter((u) => typeof u === "string" && u.length > 0).map(relayOrigin);
@@ -461,6 +466,22 @@ async function foundDelegatedNamespace(s, req = {}, deps = {}) {
461
466
  return { namespaceId: founded.namespaceId, teeEnabled: founded.teeEnabled, ...ha };
462
467
  }
463
468
  var HA_ACCOUNT_NOT_LINKED_MESSAGE = "link this account to your cloud user in the wallet so invitees can find this namespace";
469
+ var HA_REFUSAL_MESSAGES = {
470
+ account_not_linked: HA_ACCOUNT_NOT_LINKED_MESSAGE,
471
+ account_linked_to_several_users: "this account is linked to more than one cloud user, so the cloud cannot tell whose plan hosts the namespace: unlink it from all but one in the wallet",
472
+ ha_request_pending: "another namespace of this account is still waiting to be hosted; the cloud hosts one new namespace at a time without a cloud sign-in. Sign in to the cloud and turn off hosting for the waiting namespace, or wait until it is hosted",
473
+ unknown_relay: "the cloud does not run the relay this namespace was founded on, so it cannot host it",
474
+ relay_not_dialable: "the relay this namespace was founded on has not reported its address to the cloud yet; try enabling hosting again shortly"
475
+ };
476
+ function blockingNamespace(e) {
477
+ try {
478
+ const body = JSON.parse(e.bodyText ?? "");
479
+ const id = body?.detail?.namespace_id ?? body?.namespace_id;
480
+ return typeof id === "string" && /^[0-9a-f]{64}$/i.test(id) ? id.toLowerCase() : null;
481
+ } catch {
482
+ return null;
483
+ }
484
+ }
464
485
  async function enableHaBestEffort(s, namespaceId, salt, relayUrl, deps) {
465
486
  try {
466
487
  await new meroJs.CloudClient({ cloudBaseUrl: deps.cloudBaseUrl, fetch: deps.fetch }).enableHaAsAccount({
@@ -476,6 +497,11 @@ async function enableHaBestEffort(s, namespaceId, salt, relayUrl, deps) {
476
497
  return { haEnabled: true };
477
498
  } catch (e) {
478
499
  if (e instanceof meroJs.AccountNotLinkedError) return { haEnabled: false, haError: HA_ACCOUNT_NOT_LINKED_MESSAGE };
500
+ if (e instanceof meroJs.AccountHaRefusedError && HA_REFUSAL_MESSAGES[e.code]) {
501
+ const blocking = e.code === "ha_request_pending" ? blockingNamespace(e) : null;
502
+ const message = HA_REFUSAL_MESSAGES[e.code];
503
+ return { haEnabled: false, haError: blocking ? `${message} (waiting: ${blocking})` : message };
504
+ }
479
505
  return { haEnabled: false, haError: e instanceof Error ? e.message : String(e) };
480
506
  }
481
507
  }
@@ -1050,7 +1076,7 @@ async function joinAsAccount(session, namespaceId, invitation, opts) {
1050
1076
  });
1051
1077
  }
1052
1078
  if (outcome.session.relayUrl) rememberRelay(session.account, outcome.session.relayUrl, { namespaceId });
1053
- opts.onJoined(outcome.session);
1079
+ opts.onJoined(carryExecutorAccount(session, outcome.session));
1054
1080
  return { namespaceId };
1055
1081
  }
1056
1082
 
@@ -2408,26 +2434,33 @@ var DEVICE_KEY = "calimero.device";
2408
2434
  var STATE_KEY = "calimero.enrol.state";
2409
2435
  var HOSTED_WALLET = "https://wallet.cloud.calimero.network/account-enroll";
2410
2436
  var hex = (b) => [...new Uint8Array(b)].map((x) => x.toString(16).padStart(2, "0")).join("");
2437
+ function executorOf(relay) {
2438
+ const value = relay.executorAccount;
2439
+ return typeof value === "string" && /^[0-9a-f]{64}$/.test(value) ? value : null;
2440
+ }
2411
2441
  function chooseRelay(relays) {
2412
2442
  const reachable = relays.filter(
2413
2443
  (r) => typeof r.relayUrl === "string" && r.relayUrl.length > 0
2414
2444
  );
2415
2445
  const fresh = reachable.find((r) => r.fresh);
2416
- if (fresh) return { relayUrl: fresh.relayUrl, note: null };
2446
+ if (fresh) return { relayUrl: fresh.relayUrl, executorAccount: executorOf(fresh), note: null };
2417
2447
  if (reachable.length > 0) {
2418
2448
  return {
2419
2449
  relayUrl: reachable[0].relayUrl,
2450
+ executorAccount: executorOf(reachable[0]),
2420
2451
  note: "Connected through a relay whose last heartbeat has lapsed \u2014 it may not answer. It was the only one with an address."
2421
2452
  };
2422
2453
  }
2423
2454
  if (relays.length > 0) {
2424
2455
  return {
2425
2456
  relayUrl: null,
2457
+ executorAccount: null,
2426
2458
  note: `Signed in. Your account has ${relays.length} relay${relays.length === 1 ? "" : "s"} assigned, but the cloud knows no address for any of them yet, so there is nowhere to write through for the moment. Reads and writes resume as soon as one reports in.`
2427
2459
  };
2428
2460
  }
2429
2461
  return {
2430
2462
  relayUrl: null,
2463
+ executorAccount: null,
2431
2464
  note: "Signed in, with nowhere to write yet: a new account is a member of nothing, so no node serves it. Redeeming an invitation admits this account to a namespace and gives it a relay \u2014 that is the normal first step, not an error."
2432
2465
  };
2433
2466
  }
@@ -2539,7 +2572,8 @@ function useAccountEnrolment({
2539
2572
  relayUrl: chosen.relayUrl,
2540
2573
  account: enrolled.account,
2541
2574
  credential: enrolled.credential,
2542
- deviceSecret: keys.signSk
2575
+ deviceSecret: keys.signSk,
2576
+ ...chosen.executorAccount ? { executorAccount: chosen.executorAccount } : {}
2543
2577
  });
2544
2578
  } catch (e) {
2545
2579
  if (!cancelled) setNote(e instanceof Error ? e.message : String(e));
@@ -4803,7 +4837,7 @@ function useDelegatedBootstrap(options = {}) {
4803
4837
  contextId: input.contextId
4804
4838
  });
4805
4839
  }
4806
- connectWithAccount(outcome.session);
4840
+ connectWithAccount(carryExecutorAccount(readDelegatedSession(), outcome.session));
4807
4841
  }
4808
4842
  return outcome;
4809
4843
  } finally {
@@ -4974,6 +5008,7 @@ exports.ConnectButtonAccount = ConnectButtonAccount;
4974
5008
  exports.ConnectionType = ConnectionType;
4975
5009
  exports.DEFAULT_MIGRATION_POLL_INTERVAL_MS = DEFAULT_MIGRATION_POLL_INTERVAL_MS;
4976
5010
  exports.HA_ACCOUNT_NOT_LINKED_MESSAGE = HA_ACCOUNT_NOT_LINKED_MESSAGE;
5011
+ exports.HA_REFUSAL_MESSAGES = HA_REFUSAL_MESSAGES;
4977
5012
  exports.InvitationNotClaimableError = InvitationNotClaimableError;
4978
5013
  exports.LoginModal = LoginModal;
4979
5014
  exports.MERO_CSS_VARS = MERO_CSS_VARS;