@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.
@@ -2011,7 +2011,7 @@ var L1PaymentsModule = class {
2011
2011
  }
2012
2012
  const info = await this._transport.resolve(nametag);
2013
2013
  if (!info) {
2014
- throw new SphereError(`Nametag not found: ${nametag}`, "INVALID_RECIPIENT");
2014
+ throw new SphereError(`Unicity ID not found: ${nametag}`, "INVALID_RECIPIENT");
2015
2015
  }
2016
2016
  if (!info.l1Address) {
2017
2017
  throw new SphereError(
@@ -4063,7 +4063,7 @@ var InstantSplitProcessor = class {
4063
4063
  const { ProxyAddress } = await import("@unicitylabs/state-transition-sdk/lib/address/ProxyAddress");
4064
4064
  const proxy = await ProxyAddress.fromTokenId(nametagToken.id);
4065
4065
  if (proxy.address !== recipientAddressStr) {
4066
- logger.warn("InstantSplit", "Nametag PROXY address mismatch, ignoring bundle token");
4066
+ logger.warn("InstantSplit", "Unicity ID PROXY address mismatch, ignoring bundle token");
4067
4067
  } else {
4068
4068
  nametagTokens = [nametagToken];
4069
4069
  logger.debug("InstantSplit", "Using nametag token from bundle (address validated)");
@@ -5580,7 +5580,7 @@ var PaymentsModule = class _PaymentsModule {
5580
5580
  if (proxy.address === proxyAddress) {
5581
5581
  return nametagToken;
5582
5582
  }
5583
- logger.debug("Payments", `Nametag PROXY address mismatch: ${proxy.address} !== ${proxyAddress}`);
5583
+ logger.debug("Payments", `Unicity ID PROXY address mismatch: ${proxy.address} !== ${proxyAddress}`);
5584
5584
  return null;
5585
5585
  } catch (err) {
5586
5586
  logger.debug("Payments", "Failed to parse nametag token:", err);
@@ -7133,7 +7133,7 @@ var PaymentsModule = class _PaymentsModule {
7133
7133
  this.nametags.push(nametag);
7134
7134
  }
7135
7135
  await this.save();
7136
- logger.debug("Payments", `Nametag set: ${nametag.name}`);
7136
+ logger.debug("Payments", `Unicity ID set: ${nametag.name}`);
7137
7137
  }
7138
7138
  /**
7139
7139
  * Get the current (first) nametag data.
@@ -7182,7 +7182,7 @@ var PaymentsModule = class _PaymentsModule {
7182
7182
  const parsed = parseTxfStorageData(result.data);
7183
7183
  if (parsed.nametags.length > 0) {
7184
7184
  this.nametags = parsed.nametags;
7185
- logger.debug("Payments", `Reloaded ${parsed.nametags.length} nametag(s) from storage`);
7185
+ logger.debug("Payments", `Reloaded ${parsed.nametags.length} Unicity ID(s) from storage`);
7186
7186
  return;
7187
7187
  }
7188
7188
  }
@@ -7235,7 +7235,7 @@ var PaymentsModule = class _PaymentsModule {
7235
7235
  const result = await minter.mintNametag(nametag, ownerAddress);
7236
7236
  if (result.success && result.nametagData) {
7237
7237
  await this.setNametag(result.nametagData);
7238
- logger.debug("Payments", `Nametag minted and saved: ${result.nametagData.name}`);
7238
+ logger.debug("Payments", `Unicity ID minted and saved: ${result.nametagData.name}`);
7239
7239
  this.deps.emitEvent("nametag:registered", {
7240
7240
  nametag: result.nametagData.name,
7241
7241
  addressIndex: 0
@@ -7718,12 +7718,12 @@ var PaymentsModule = class _PaymentsModule {
7718
7718
  const { ProxyAddress } = await import("@unicitylabs/state-transition-sdk/lib/address/ProxyAddress");
7719
7719
  let proxyNametag = this.getNametag();
7720
7720
  if (!proxyNametag?.token) {
7721
- logger.debug("Payments", "Nametag missing in memory, attempting reload from storage...");
7721
+ logger.debug("Payments", "Unicity ID missing in memory, attempting reload from storage...");
7722
7722
  await this.reloadNametagsFromStorage();
7723
7723
  proxyNametag = this.getNametag();
7724
7724
  }
7725
7725
  if (!proxyNametag?.token) {
7726
- throw new SphereError("Cannot finalize PROXY transfer - no nametag token", "VALIDATION_ERROR");
7726
+ throw new SphereError("Cannot finalize PROXY transfer - no Unicity ID token", "VALIDATION_ERROR");
7727
7727
  }
7728
7728
  const nametagToken = await import_Token6.Token.fromJSON(proxyNametag.token);
7729
7729
  const proxy = await ProxyAddress.fromTokenId(nametagToken.id);
@@ -8724,7 +8724,7 @@ var CommunicationsModule = class {
8724
8724
  const nametag = recipient.slice(1);
8725
8725
  const pubkey = await this.deps.transport.resolveNametag?.(nametag);
8726
8726
  if (!pubkey) {
8727
- throw new SphereError(`Nametag not found: ${recipient}`, "INVALID_RECIPIENT");
8727
+ throw new SphereError(`Unicity ID not found: ${recipient}`, "INVALID_RECIPIENT");
8728
8728
  }
8729
8729
  return { pubkey, nametag };
8730
8730
  }
@@ -8983,13 +8983,16 @@ var GroupChatModule = class {
8983
8983
  logger.error("GroupChat", "Max reconnection attempts reached");
8984
8984
  return;
8985
8985
  }
8986
+ const maxDelay = this.config.reconnectDelayMs * 16;
8987
+ const delay = Math.min(this.config.reconnectDelayMs * Math.pow(2, this.reconnectAttempts), maxDelay);
8986
8988
  this.reconnectAttempts++;
8989
+ logger.debug("GroupChat", `Reconnecting in ${delay}ms (attempt ${this.reconnectAttempts}/${this.config.maxReconnectAttempts})`);
8987
8990
  this.reconnectTimer = setTimeout(() => {
8988
8991
  this.reconnectTimer = null;
8989
8992
  if (this.deps) {
8990
8993
  this.connect().catch((err) => logger.error("GroupChat", "Reconnect failed:", err));
8991
8994
  }
8992
- }, this.config.reconnectDelayMs);
8995
+ }, delay);
8993
8996
  }
8994
8997
  // ===========================================================================
8995
8998
  // Subscription Management
@@ -12326,6 +12329,7 @@ var secp256k1 = /* @__PURE__ */ ecdsa(Pointk1, sha2564);
12326
12329
 
12327
12330
  // modules/market/MarketModule.ts
12328
12331
  init_errors();
12332
+ init_logger();
12329
12333
  var DEFAULT_MARKET_API_URL = "https://market-api.unicity.network";
12330
12334
  function hexToBytes3(hex) {
12331
12335
  const len = hex.length >> 1;
@@ -12498,16 +12502,54 @@ var MarketModule = class {
12498
12502
  */
12499
12503
  subscribeFeed(listener) {
12500
12504
  const wsUrl = this.apiUrl.replace(/^http/, "ws") + "/ws/feed";
12501
- const ws2 = new WebSocket(wsUrl);
12502
- ws2.onmessage = (event) => {
12503
- try {
12504
- const raw = JSON.parse(typeof event.data === "string" ? event.data : event.data.toString());
12505
- listener(mapFeedMessage(raw));
12506
- } catch {
12505
+ const BASE_DELAY2 = 2e3;
12506
+ const MAX_DELAY2 = 3e4;
12507
+ const MAX_ATTEMPTS = 10;
12508
+ let ws2 = null;
12509
+ let reconnectAttempts2 = 0;
12510
+ let reconnectTimer = null;
12511
+ let destroyed = false;
12512
+ function connect2() {
12513
+ if (destroyed) return;
12514
+ ws2 = new WebSocket(wsUrl);
12515
+ ws2.onopen = () => {
12516
+ reconnectAttempts2 = 0;
12517
+ logger.debug("Market", "Feed WebSocket connected");
12518
+ };
12519
+ ws2.onmessage = (event) => {
12520
+ try {
12521
+ const raw = JSON.parse(typeof event.data === "string" ? event.data : event.data.toString());
12522
+ listener(mapFeedMessage(raw));
12523
+ } catch {
12524
+ }
12525
+ };
12526
+ ws2.onclose = () => {
12527
+ if (destroyed) return;
12528
+ scheduleReconnect();
12529
+ };
12530
+ ws2.onerror = () => {
12531
+ };
12532
+ }
12533
+ function scheduleReconnect() {
12534
+ if (destroyed) return;
12535
+ if (reconnectAttempts2 >= MAX_ATTEMPTS) {
12536
+ logger.warn("Market", `Feed WebSocket: gave up after ${MAX_ATTEMPTS} reconnect attempts`);
12537
+ return;
12507
12538
  }
12508
- };
12539
+ const delay = Math.min(BASE_DELAY2 * Math.pow(2, reconnectAttempts2), MAX_DELAY2);
12540
+ reconnectAttempts2++;
12541
+ logger.debug("Market", `Feed WebSocket reconnecting in ${delay}ms (attempt ${reconnectAttempts2}/${MAX_ATTEMPTS})`);
12542
+ reconnectTimer = setTimeout(connect2, delay);
12543
+ }
12544
+ connect2();
12509
12545
  return () => {
12510
- ws2.close();
12546
+ destroyed = true;
12547
+ if (reconnectTimer) {
12548
+ clearTimeout(reconnectTimer);
12549
+ reconnectTimer = null;
12550
+ }
12551
+ ws2?.close();
12552
+ ws2 = null;
12511
12553
  };
12512
12554
  }
12513
12555
  // ---------------------------------------------------------------------------
@@ -12762,7 +12804,7 @@ async function scanAddressesImpl(deriveAddress, options = {}) {
12762
12804
  nametagsFoundCount++;
12763
12805
  }
12764
12806
  } catch (err) {
12765
- logger.debug("Sphere", "Nametag resolution failed during scan", err);
12807
+ logger.debug("Sphere", "Unicity ID resolution failed during scan", err);
12766
12808
  }
12767
12809
  }
12768
12810
  foundAddresses.push({
@@ -13874,7 +13916,7 @@ var Sphere = class _Sphere {
13874
13916
  _Sphere.instance = sphere;
13875
13917
  if (sphere._identity?.nametag && !sphere._payments.hasNametag()) {
13876
13918
  progress?.({ step: "registering_nametag", message: "Restoring nametag token..." });
13877
- logger.debug("Sphere", `Nametag @${sphere._identity.nametag} has no token, attempting to mint...`);
13919
+ logger.debug("Sphere", `Unicity ID @${sphere._identity.nametag} has no token, attempting to mint...`);
13878
13920
  try {
13879
13921
  const result = await sphere.mintNametag(sphere._identity.nametag);
13880
13922
  if (result.success) {
@@ -13971,9 +14013,9 @@ var Sphere = class _Sphere {
13971
14013
  logger.debug("Sphere", "Modules initialized");
13972
14014
  if (!options.nametag) {
13973
14015
  progress?.({ step: "recovering_nametag", message: "Recovering nametag..." });
13974
- logger.debug("Sphere", "Recovering nametag from transport...");
14016
+ logger.debug("Sphere", "Recovering Unicity ID from transport...");
13975
14017
  await sphere.recoverNametagFromTransport();
13976
- logger.debug("Sphere", "Nametag recovery done");
14018
+ logger.debug("Sphere", "Unicity ID recovery done");
13977
14019
  progress?.({ step: "syncing_identity", message: "Publishing identity..." });
13978
14020
  await sphere.syncIdentityWithTransport();
13979
14021
  }
@@ -13986,7 +14028,7 @@ var Sphere = class _Sphere {
13986
14028
  await sphere.ensureAddressTracked(0);
13987
14029
  if (options.nametag) {
13988
14030
  progress?.({ step: "registering_nametag", message: "Registering nametag..." });
13989
- logger.debug("Sphere", "Registering nametag...");
14031
+ logger.debug("Sphere", "Registering Unicity ID...");
13990
14032
  await sphere.registerNametag(options.nametag);
13991
14033
  }
13992
14034
  if (sphere._tokenStorageProviders.size > 0) {
@@ -14837,7 +14879,7 @@ var Sphere = class _Sphere {
14837
14879
  }
14838
14880
  const newNametag = options?.nametag ? this.cleanNametag(options.nametag) : void 0;
14839
14881
  if (newNametag && !isValidNametag(newNametag)) {
14840
- throw new SphereError("Invalid nametag format. Use lowercase alphanumeric, underscore, or hyphen (3-20 chars), or a valid phone number.", "VALIDATION_ERROR");
14882
+ throw new SphereError("Invalid Unicity ID format. Use lowercase alphanumeric, underscore, or hyphen (3-20 chars), or a valid phone number.", "VALIDATION_ERROR");
14841
14883
  }
14842
14884
  const addressInfo = this.deriveAddress(index, false);
14843
14885
  const ipnsHash = sha256(addressInfo.publicKey, "hex").slice(0, 40);
@@ -14847,7 +14889,7 @@ var Sphere = class _Sphere {
14847
14889
  if (newNametag) {
14848
14890
  const existing = await this._transport.resolveNametag?.(newNametag);
14849
14891
  if (existing) {
14850
- throw new SphereError(`Nametag @${newNametag} is already taken`, "VALIDATION_ERROR");
14892
+ throw new SphereError(`Unicity ID @${newNametag} is already taken`, "VALIDATION_ERROR");
14851
14893
  }
14852
14894
  let nametags = this._addressNametags.get(addressId);
14853
14895
  if (!nametags) {
@@ -14919,7 +14961,7 @@ var Sphere = class _Sphere {
14919
14961
  addressIndex: index
14920
14962
  });
14921
14963
  } else if (this._identity?.nametag && !this._payments.hasNametag()) {
14922
- logger.debug("Sphere", `Nametag @${this._identity.nametag} has no token after switch, minting...`);
14964
+ logger.debug("Sphere", `Unicity ID @${this._identity.nametag} has no token after switch, minting...`);
14923
14965
  try {
14924
14966
  const result = await this.mintNametag(this._identity.nametag);
14925
14967
  if (result.success) {
@@ -15105,7 +15147,7 @@ var Sphere = class _Sphere {
15105
15147
  const info = await this._transport.resolveAddressInfo(l1Address);
15106
15148
  return info?.nametag ?? null;
15107
15149
  } catch (err) {
15108
- logger.debug("Sphere", "Nametag resolution failed during scan", err);
15150
+ logger.debug("Sphere", "Unicity ID resolution failed during scan", err);
15109
15151
  return null;
15110
15152
  }
15111
15153
  } : void 0);
@@ -15516,10 +15558,10 @@ var Sphere = class _Sphere {
15516
15558
  this.ensureReady();
15517
15559
  const cleanNametag = this.cleanNametag(nametag);
15518
15560
  if (!isValidNametag(cleanNametag)) {
15519
- throw new SphereError("Invalid nametag format. Use lowercase alphanumeric, underscore, or hyphen (3-20 chars), or a valid phone number.", "VALIDATION_ERROR");
15561
+ throw new SphereError("Invalid Unicity ID format. Use lowercase alphanumeric, underscore, or hyphen (3-20 chars), or a valid phone number.", "VALIDATION_ERROR");
15520
15562
  }
15521
15563
  if (this._identity?.nametag) {
15522
- throw new SphereError(`Nametag already registered for address ${this._currentAddressIndex}: @${this._identity.nametag}`, "ALREADY_INITIALIZED");
15564
+ throw new SphereError(`Unicity ID already registered for address ${this._currentAddressIndex}: @${this._identity.nametag}`, "ALREADY_INITIALIZED");
15523
15565
  }
15524
15566
  if (this._transport.publishIdentityBinding) {
15525
15567
  const success = await this._transport.publishIdentityBinding(
@@ -15529,7 +15571,7 @@ var Sphere = class _Sphere {
15529
15571
  cleanNametag
15530
15572
  );
15531
15573
  if (!success) {
15532
- throw new SphereError("Failed to register nametag. It may already be taken.", "VALIDATION_ERROR");
15574
+ throw new SphereError("Failed to register Unicity ID. It may already be taken.", "VALIDATION_ERROR");
15533
15575
  }
15534
15576
  }
15535
15577
  this._identity.nametag = cleanNametag;
@@ -15557,7 +15599,7 @@ var Sphere = class _Sphere {
15557
15599
  nametag: cleanNametag,
15558
15600
  addressIndex: this._currentAddressIndex
15559
15601
  });
15560
- logger.debug("Sphere", `Nametag registered for address ${this._currentAddressIndex}:`, cleanNametag);
15602
+ logger.debug("Sphere", `Unicity ID registered for address ${this._currentAddressIndex}:`, cleanNametag);
15561
15603
  }
15562
15604
  /**
15563
15605
  * Persist tracked addresses to storage (only minimal fields via StorageProvider)
@@ -15797,7 +15839,7 @@ var Sphere = class _Sphere {
15797
15839
  this._identity.directAddress || "",
15798
15840
  recoveredNametag
15799
15841
  );
15800
- logger.debug("Sphere", `Migrated legacy binding with nametag @${recoveredNametag}`);
15842
+ logger.debug("Sphere", `Migrated legacy binding with Unicity ID @${recoveredNametag}`);
15801
15843
  return;
15802
15844
  }
15803
15845
  }
@@ -15828,9 +15870,9 @@ var Sphere = class _Sphere {
15828
15870
  nametag || void 0
15829
15871
  );
15830
15872
  if (success) {
15831
- logger.debug("Sphere", `Identity binding published${nametag ? ` with nametag @${nametag}` : ""}`);
15873
+ logger.debug("Sphere", `Identity binding published${nametag ? ` with Unicity ID @${nametag}` : ""}`);
15832
15874
  } else if (nametag) {
15833
- logger.warn("Sphere", `Nametag @${nametag} is taken by another pubkey`);
15875
+ logger.warn("Sphere", `Unicity ID @${nametag} is taken by another pubkey`);
15834
15876
  }
15835
15877
  } catch (error) {
15836
15878
  logger.warn("Sphere", `Identity binding sync failed:`, error);