@unicitylabs/sphere-sdk 0.5.5 → 0.5.7

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.
@@ -1913,7 +1913,7 @@ var L1PaymentsModule = class {
1913
1913
  }
1914
1914
  const info = await this._transport.resolve(nametag);
1915
1915
  if (!info) {
1916
- throw new SphereError(`Nametag not found: ${nametag}`, "INVALID_RECIPIENT");
1916
+ throw new SphereError(`Unicity ID not found: ${nametag}`, "INVALID_RECIPIENT");
1917
1917
  }
1918
1918
  if (!info.l1Address) {
1919
1919
  throw new SphereError(
@@ -3965,7 +3965,7 @@ var InstantSplitProcessor = class {
3965
3965
  const { ProxyAddress } = await import("@unicitylabs/state-transition-sdk/lib/address/ProxyAddress");
3966
3966
  const proxy = await ProxyAddress.fromTokenId(nametagToken.id);
3967
3967
  if (proxy.address !== recipientAddressStr) {
3968
- logger.warn("InstantSplit", "Nametag PROXY address mismatch, ignoring bundle token");
3968
+ logger.warn("InstantSplit", "Unicity ID PROXY address mismatch, ignoring bundle token");
3969
3969
  } else {
3970
3970
  nametagTokens = [nametagToken];
3971
3971
  logger.debug("InstantSplit", "Using nametag token from bundle (address validated)");
@@ -5482,7 +5482,7 @@ var PaymentsModule = class _PaymentsModule {
5482
5482
  if (proxy.address === proxyAddress) {
5483
5483
  return nametagToken;
5484
5484
  }
5485
- logger.debug("Payments", `Nametag PROXY address mismatch: ${proxy.address} !== ${proxyAddress}`);
5485
+ logger.debug("Payments", `Unicity ID PROXY address mismatch: ${proxy.address} !== ${proxyAddress}`);
5486
5486
  return null;
5487
5487
  } catch (err) {
5488
5488
  logger.debug("Payments", "Failed to parse nametag token:", err);
@@ -7035,7 +7035,7 @@ var PaymentsModule = class _PaymentsModule {
7035
7035
  this.nametags.push(nametag);
7036
7036
  }
7037
7037
  await this.save();
7038
- logger.debug("Payments", `Nametag set: ${nametag.name}`);
7038
+ logger.debug("Payments", `Unicity ID set: ${nametag.name}`);
7039
7039
  }
7040
7040
  /**
7041
7041
  * Get the current (first) nametag data.
@@ -7084,7 +7084,7 @@ var PaymentsModule = class _PaymentsModule {
7084
7084
  const parsed = parseTxfStorageData(result.data);
7085
7085
  if (parsed.nametags.length > 0) {
7086
7086
  this.nametags = parsed.nametags;
7087
- logger.debug("Payments", `Reloaded ${parsed.nametags.length} nametag(s) from storage`);
7087
+ logger.debug("Payments", `Reloaded ${parsed.nametags.length} Unicity ID(s) from storage`);
7088
7088
  return;
7089
7089
  }
7090
7090
  }
@@ -7137,7 +7137,7 @@ var PaymentsModule = class _PaymentsModule {
7137
7137
  const result = await minter.mintNametag(nametag, ownerAddress);
7138
7138
  if (result.success && result.nametagData) {
7139
7139
  await this.setNametag(result.nametagData);
7140
- logger.debug("Payments", `Nametag minted and saved: ${result.nametagData.name}`);
7140
+ logger.debug("Payments", `Unicity ID minted and saved: ${result.nametagData.name}`);
7141
7141
  this.deps.emitEvent("nametag:registered", {
7142
7142
  nametag: result.nametagData.name,
7143
7143
  addressIndex: 0
@@ -7620,12 +7620,12 @@ var PaymentsModule = class _PaymentsModule {
7620
7620
  const { ProxyAddress } = await import("@unicitylabs/state-transition-sdk/lib/address/ProxyAddress");
7621
7621
  let proxyNametag = this.getNametag();
7622
7622
  if (!proxyNametag?.token) {
7623
- logger.debug("Payments", "Nametag missing in memory, attempting reload from storage...");
7623
+ logger.debug("Payments", "Unicity ID missing in memory, attempting reload from storage...");
7624
7624
  await this.reloadNametagsFromStorage();
7625
7625
  proxyNametag = this.getNametag();
7626
7626
  }
7627
7627
  if (!proxyNametag?.token) {
7628
- throw new SphereError("Cannot finalize PROXY transfer - no nametag token", "VALIDATION_ERROR");
7628
+ throw new SphereError("Cannot finalize PROXY transfer - no Unicity ID token", "VALIDATION_ERROR");
7629
7629
  }
7630
7630
  const nametagToken = await SdkToken2.fromJSON(proxyNametag.token);
7631
7631
  const proxy = await ProxyAddress.fromTokenId(nametagToken.id);
@@ -8626,7 +8626,7 @@ var CommunicationsModule = class {
8626
8626
  const nametag = recipient.slice(1);
8627
8627
  const pubkey = await this.deps.transport.resolveNametag?.(nametag);
8628
8628
  if (!pubkey) {
8629
- throw new SphereError(`Nametag not found: ${recipient}`, "INVALID_RECIPIENT");
8629
+ throw new SphereError(`Unicity ID not found: ${recipient}`, "INVALID_RECIPIENT");
8630
8630
  }
8631
8631
  return { pubkey, nametag };
8632
8632
  }
@@ -8889,13 +8889,16 @@ var GroupChatModule = class {
8889
8889
  logger.error("GroupChat", "Max reconnection attempts reached");
8890
8890
  return;
8891
8891
  }
8892
+ const maxDelay = this.config.reconnectDelayMs * 16;
8893
+ const delay = Math.min(this.config.reconnectDelayMs * Math.pow(2, this.reconnectAttempts), maxDelay);
8892
8894
  this.reconnectAttempts++;
8895
+ logger.debug("GroupChat", `Reconnecting in ${delay}ms (attempt ${this.reconnectAttempts}/${this.config.maxReconnectAttempts})`);
8893
8896
  this.reconnectTimer = setTimeout(() => {
8894
8897
  this.reconnectTimer = null;
8895
8898
  if (this.deps) {
8896
8899
  this.connect().catch((err) => logger.error("GroupChat", "Reconnect failed:", err));
8897
8900
  }
8898
- }, this.config.reconnectDelayMs);
8901
+ }, delay);
8899
8902
  }
8900
8903
  // ===========================================================================
8901
8904
  // Subscription Management
@@ -12232,6 +12235,7 @@ var secp256k1 = /* @__PURE__ */ ecdsa(Pointk1, sha2564);
12232
12235
 
12233
12236
  // modules/market/MarketModule.ts
12234
12237
  init_errors();
12238
+ init_logger();
12235
12239
  var DEFAULT_MARKET_API_URL = "https://market-api.unicity.network";
12236
12240
  function hexToBytes3(hex) {
12237
12241
  const len = hex.length >> 1;
@@ -12404,16 +12408,54 @@ var MarketModule = class {
12404
12408
  */
12405
12409
  subscribeFeed(listener) {
12406
12410
  const wsUrl = this.apiUrl.replace(/^http/, "ws") + "/ws/feed";
12407
- const ws2 = new WebSocket(wsUrl);
12408
- ws2.onmessage = (event) => {
12409
- try {
12410
- const raw = JSON.parse(typeof event.data === "string" ? event.data : event.data.toString());
12411
- listener(mapFeedMessage(raw));
12412
- } catch {
12411
+ const BASE_DELAY2 = 2e3;
12412
+ const MAX_DELAY2 = 3e4;
12413
+ const MAX_ATTEMPTS = 10;
12414
+ let ws2 = null;
12415
+ let reconnectAttempts2 = 0;
12416
+ let reconnectTimer = null;
12417
+ let destroyed = false;
12418
+ function connect2() {
12419
+ if (destroyed) return;
12420
+ ws2 = new WebSocket(wsUrl);
12421
+ ws2.onopen = () => {
12422
+ reconnectAttempts2 = 0;
12423
+ logger.debug("Market", "Feed WebSocket connected");
12424
+ };
12425
+ ws2.onmessage = (event) => {
12426
+ try {
12427
+ const raw = JSON.parse(typeof event.data === "string" ? event.data : event.data.toString());
12428
+ listener(mapFeedMessage(raw));
12429
+ } catch {
12430
+ }
12431
+ };
12432
+ ws2.onclose = () => {
12433
+ if (destroyed) return;
12434
+ scheduleReconnect();
12435
+ };
12436
+ ws2.onerror = () => {
12437
+ };
12438
+ }
12439
+ function scheduleReconnect() {
12440
+ if (destroyed) return;
12441
+ if (reconnectAttempts2 >= MAX_ATTEMPTS) {
12442
+ logger.warn("Market", `Feed WebSocket: gave up after ${MAX_ATTEMPTS} reconnect attempts`);
12443
+ return;
12413
12444
  }
12414
- };
12445
+ const delay = Math.min(BASE_DELAY2 * Math.pow(2, reconnectAttempts2), MAX_DELAY2);
12446
+ reconnectAttempts2++;
12447
+ logger.debug("Market", `Feed WebSocket reconnecting in ${delay}ms (attempt ${reconnectAttempts2}/${MAX_ATTEMPTS})`);
12448
+ reconnectTimer = setTimeout(connect2, delay);
12449
+ }
12450
+ connect2();
12415
12451
  return () => {
12416
- ws2.close();
12452
+ destroyed = true;
12453
+ if (reconnectTimer) {
12454
+ clearTimeout(reconnectTimer);
12455
+ reconnectTimer = null;
12456
+ }
12457
+ ws2?.close();
12458
+ ws2 = null;
12417
12459
  };
12418
12460
  }
12419
12461
  // ---------------------------------------------------------------------------
@@ -12668,7 +12710,7 @@ async function scanAddressesImpl(deriveAddress, options = {}) {
12668
12710
  nametagsFoundCount++;
12669
12711
  }
12670
12712
  } catch (err) {
12671
- logger.debug("Sphere", "Nametag resolution failed during scan", err);
12713
+ logger.debug("Sphere", "Unicity ID resolution failed during scan", err);
12672
12714
  }
12673
12715
  }
12674
12716
  foundAddresses.push({
@@ -13780,7 +13822,7 @@ var Sphere = class _Sphere {
13780
13822
  _Sphere.instance = sphere;
13781
13823
  if (sphere._identity?.nametag && !sphere._payments.hasNametag()) {
13782
13824
  progress?.({ step: "registering_nametag", message: "Restoring nametag token..." });
13783
- logger.debug("Sphere", `Nametag @${sphere._identity.nametag} has no token, attempting to mint...`);
13825
+ logger.debug("Sphere", `Unicity ID @${sphere._identity.nametag} has no token, attempting to mint...`);
13784
13826
  try {
13785
13827
  const result = await sphere.mintNametag(sphere._identity.nametag);
13786
13828
  if (result.success) {
@@ -13877,9 +13919,9 @@ var Sphere = class _Sphere {
13877
13919
  logger.debug("Sphere", "Modules initialized");
13878
13920
  if (!options.nametag) {
13879
13921
  progress?.({ step: "recovering_nametag", message: "Recovering nametag..." });
13880
- logger.debug("Sphere", "Recovering nametag from transport...");
13922
+ logger.debug("Sphere", "Recovering Unicity ID from transport...");
13881
13923
  await sphere.recoverNametagFromTransport();
13882
- logger.debug("Sphere", "Nametag recovery done");
13924
+ logger.debug("Sphere", "Unicity ID recovery done");
13883
13925
  progress?.({ step: "syncing_identity", message: "Publishing identity..." });
13884
13926
  await sphere.syncIdentityWithTransport();
13885
13927
  }
@@ -13892,7 +13934,7 @@ var Sphere = class _Sphere {
13892
13934
  await sphere.ensureAddressTracked(0);
13893
13935
  if (options.nametag) {
13894
13936
  progress?.({ step: "registering_nametag", message: "Registering nametag..." });
13895
- logger.debug("Sphere", "Registering nametag...");
13937
+ logger.debug("Sphere", "Registering Unicity ID...");
13896
13938
  await sphere.registerNametag(options.nametag);
13897
13939
  }
13898
13940
  if (sphere._tokenStorageProviders.size > 0) {
@@ -14743,7 +14785,7 @@ var Sphere = class _Sphere {
14743
14785
  }
14744
14786
  const newNametag = options?.nametag ? this.cleanNametag(options.nametag) : void 0;
14745
14787
  if (newNametag && !isValidNametag(newNametag)) {
14746
- throw new SphereError("Invalid nametag format. Use lowercase alphanumeric, underscore, or hyphen (3-20 chars), or a valid phone number.", "VALIDATION_ERROR");
14788
+ throw new SphereError("Invalid Unicity ID format. Use lowercase alphanumeric, underscore, or hyphen (3-20 chars), or a valid phone number.", "VALIDATION_ERROR");
14747
14789
  }
14748
14790
  const addressInfo = this.deriveAddress(index, false);
14749
14791
  const ipnsHash = sha256(addressInfo.publicKey, "hex").slice(0, 40);
@@ -14753,7 +14795,7 @@ var Sphere = class _Sphere {
14753
14795
  if (newNametag) {
14754
14796
  const existing = await this._transport.resolveNametag?.(newNametag);
14755
14797
  if (existing) {
14756
- throw new SphereError(`Nametag @${newNametag} is already taken`, "VALIDATION_ERROR");
14798
+ throw new SphereError(`Unicity ID @${newNametag} is already taken`, "VALIDATION_ERROR");
14757
14799
  }
14758
14800
  let nametags = this._addressNametags.get(addressId);
14759
14801
  if (!nametags) {
@@ -14825,7 +14867,7 @@ var Sphere = class _Sphere {
14825
14867
  addressIndex: index
14826
14868
  });
14827
14869
  } else if (this._identity?.nametag && !this._payments.hasNametag()) {
14828
- logger.debug("Sphere", `Nametag @${this._identity.nametag} has no token after switch, minting...`);
14870
+ logger.debug("Sphere", `Unicity ID @${this._identity.nametag} has no token after switch, minting...`);
14829
14871
  try {
14830
14872
  const result = await this.mintNametag(this._identity.nametag);
14831
14873
  if (result.success) {
@@ -15011,7 +15053,7 @@ var Sphere = class _Sphere {
15011
15053
  const info = await this._transport.resolveAddressInfo(l1Address);
15012
15054
  return info?.nametag ?? null;
15013
15055
  } catch (err) {
15014
- logger.debug("Sphere", "Nametag resolution failed during scan", err);
15056
+ logger.debug("Sphere", "Unicity ID resolution failed during scan", err);
15015
15057
  return null;
15016
15058
  }
15017
15059
  } : void 0);
@@ -15422,10 +15464,10 @@ var Sphere = class _Sphere {
15422
15464
  this.ensureReady();
15423
15465
  const cleanNametag = this.cleanNametag(nametag);
15424
15466
  if (!isValidNametag(cleanNametag)) {
15425
- throw new SphereError("Invalid nametag format. Use lowercase alphanumeric, underscore, or hyphen (3-20 chars), or a valid phone number.", "VALIDATION_ERROR");
15467
+ throw new SphereError("Invalid Unicity ID format. Use lowercase alphanumeric, underscore, or hyphen (3-20 chars), or a valid phone number.", "VALIDATION_ERROR");
15426
15468
  }
15427
15469
  if (this._identity?.nametag) {
15428
- throw new SphereError(`Nametag already registered for address ${this._currentAddressIndex}: @${this._identity.nametag}`, "ALREADY_INITIALIZED");
15470
+ throw new SphereError(`Unicity ID already registered for address ${this._currentAddressIndex}: @${this._identity.nametag}`, "ALREADY_INITIALIZED");
15429
15471
  }
15430
15472
  if (this._transport.publishIdentityBinding) {
15431
15473
  const success = await this._transport.publishIdentityBinding(
@@ -15435,7 +15477,7 @@ var Sphere = class _Sphere {
15435
15477
  cleanNametag
15436
15478
  );
15437
15479
  if (!success) {
15438
- throw new SphereError("Failed to register nametag. It may already be taken.", "VALIDATION_ERROR");
15480
+ throw new SphereError("Failed to register Unicity ID. It may already be taken.", "VALIDATION_ERROR");
15439
15481
  }
15440
15482
  }
15441
15483
  this._identity.nametag = cleanNametag;
@@ -15463,7 +15505,7 @@ var Sphere = class _Sphere {
15463
15505
  nametag: cleanNametag,
15464
15506
  addressIndex: this._currentAddressIndex
15465
15507
  });
15466
- logger.debug("Sphere", `Nametag registered for address ${this._currentAddressIndex}:`, cleanNametag);
15508
+ logger.debug("Sphere", `Unicity ID registered for address ${this._currentAddressIndex}:`, cleanNametag);
15467
15509
  }
15468
15510
  /**
15469
15511
  * Persist tracked addresses to storage (only minimal fields via StorageProvider)
@@ -15703,7 +15745,7 @@ var Sphere = class _Sphere {
15703
15745
  this._identity.directAddress || "",
15704
15746
  recoveredNametag
15705
15747
  );
15706
- logger.debug("Sphere", `Migrated legacy binding with nametag @${recoveredNametag}`);
15748
+ logger.debug("Sphere", `Migrated legacy binding with Unicity ID @${recoveredNametag}`);
15707
15749
  return;
15708
15750
  }
15709
15751
  }
@@ -15734,9 +15776,9 @@ var Sphere = class _Sphere {
15734
15776
  nametag || void 0
15735
15777
  );
15736
15778
  if (success) {
15737
- logger.debug("Sphere", `Identity binding published${nametag ? ` with nametag @${nametag}` : ""}`);
15779
+ logger.debug("Sphere", `Identity binding published${nametag ? ` with Unicity ID @${nametag}` : ""}`);
15738
15780
  } else if (nametag) {
15739
- logger.warn("Sphere", `Nametag @${nametag} is taken by another pubkey`);
15781
+ logger.warn("Sphere", `Unicity ID @${nametag} is taken by another pubkey`);
15740
15782
  }
15741
15783
  } catch (error) {
15742
15784
  logger.warn("Sphere", `Identity binding sync failed:`, error);