@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.
package/dist/index.js CHANGED
@@ -2117,7 +2117,7 @@ var L1PaymentsModule = class {
2117
2117
  }
2118
2118
  const info = await this._transport.resolve(nametag);
2119
2119
  if (!info) {
2120
- throw new SphereError(`Nametag not found: ${nametag}`, "INVALID_RECIPIENT");
2120
+ throw new SphereError(`Unicity ID not found: ${nametag}`, "INVALID_RECIPIENT");
2121
2121
  }
2122
2122
  if (!info.l1Address) {
2123
2123
  throw new SphereError(
@@ -4260,7 +4260,7 @@ var InstantSplitProcessor = class {
4260
4260
  const { ProxyAddress } = await import("@unicitylabs/state-transition-sdk/lib/address/ProxyAddress");
4261
4261
  const proxy = await ProxyAddress.fromTokenId(nametagToken.id);
4262
4262
  if (proxy.address !== recipientAddressStr) {
4263
- logger.warn("InstantSplit", "Nametag PROXY address mismatch, ignoring bundle token");
4263
+ logger.warn("InstantSplit", "Unicity ID PROXY address mismatch, ignoring bundle token");
4264
4264
  } else {
4265
4265
  nametagTokens = [nametagToken];
4266
4266
  logger.debug("InstantSplit", "Using nametag token from bundle (address validated)");
@@ -5777,7 +5777,7 @@ var PaymentsModule = class _PaymentsModule {
5777
5777
  if (proxy.address === proxyAddress) {
5778
5778
  return nametagToken;
5779
5779
  }
5780
- logger.debug("Payments", `Nametag PROXY address mismatch: ${proxy.address} !== ${proxyAddress}`);
5780
+ logger.debug("Payments", `Unicity ID PROXY address mismatch: ${proxy.address} !== ${proxyAddress}`);
5781
5781
  return null;
5782
5782
  } catch (err) {
5783
5783
  logger.debug("Payments", "Failed to parse nametag token:", err);
@@ -7330,7 +7330,7 @@ var PaymentsModule = class _PaymentsModule {
7330
7330
  this.nametags.push(nametag);
7331
7331
  }
7332
7332
  await this.save();
7333
- logger.debug("Payments", `Nametag set: ${nametag.name}`);
7333
+ logger.debug("Payments", `Unicity ID set: ${nametag.name}`);
7334
7334
  }
7335
7335
  /**
7336
7336
  * Get the current (first) nametag data.
@@ -7379,7 +7379,7 @@ var PaymentsModule = class _PaymentsModule {
7379
7379
  const parsed = parseTxfStorageData(result.data);
7380
7380
  if (parsed.nametags.length > 0) {
7381
7381
  this.nametags = parsed.nametags;
7382
- logger.debug("Payments", `Reloaded ${parsed.nametags.length} nametag(s) from storage`);
7382
+ logger.debug("Payments", `Reloaded ${parsed.nametags.length} Unicity ID(s) from storage`);
7383
7383
  return;
7384
7384
  }
7385
7385
  }
@@ -7432,7 +7432,7 @@ var PaymentsModule = class _PaymentsModule {
7432
7432
  const result = await minter.mintNametag(nametag, ownerAddress);
7433
7433
  if (result.success && result.nametagData) {
7434
7434
  await this.setNametag(result.nametagData);
7435
- logger.debug("Payments", `Nametag minted and saved: ${result.nametagData.name}`);
7435
+ logger.debug("Payments", `Unicity ID minted and saved: ${result.nametagData.name}`);
7436
7436
  this.deps.emitEvent("nametag:registered", {
7437
7437
  nametag: result.nametagData.name,
7438
7438
  addressIndex: 0
@@ -7915,12 +7915,12 @@ var PaymentsModule = class _PaymentsModule {
7915
7915
  const { ProxyAddress } = await import("@unicitylabs/state-transition-sdk/lib/address/ProxyAddress");
7916
7916
  let proxyNametag = this.getNametag();
7917
7917
  if (!proxyNametag?.token) {
7918
- logger.debug("Payments", "Nametag missing in memory, attempting reload from storage...");
7918
+ logger.debug("Payments", "Unicity ID missing in memory, attempting reload from storage...");
7919
7919
  await this.reloadNametagsFromStorage();
7920
7920
  proxyNametag = this.getNametag();
7921
7921
  }
7922
7922
  if (!proxyNametag?.token) {
7923
- throw new SphereError("Cannot finalize PROXY transfer - no nametag token", "VALIDATION_ERROR");
7923
+ throw new SphereError("Cannot finalize PROXY transfer - no Unicity ID token", "VALIDATION_ERROR");
7924
7924
  }
7925
7925
  const nametagToken = await SdkToken2.fromJSON(proxyNametag.token);
7926
7926
  const proxy = await ProxyAddress.fromTokenId(nametagToken.id);
@@ -8921,7 +8921,7 @@ var CommunicationsModule = class {
8921
8921
  const nametag = recipient.slice(1);
8922
8922
  const pubkey = await this.deps.transport.resolveNametag?.(nametag);
8923
8923
  if (!pubkey) {
8924
- throw new SphereError(`Nametag not found: ${recipient}`, "INVALID_RECIPIENT");
8924
+ throw new SphereError(`Unicity ID not found: ${recipient}`, "INVALID_RECIPIENT");
8925
8925
  }
8926
8926
  return { pubkey, nametag };
8927
8927
  }
@@ -9184,13 +9184,16 @@ var GroupChatModule = class {
9184
9184
  logger.error("GroupChat", "Max reconnection attempts reached");
9185
9185
  return;
9186
9186
  }
9187
+ const maxDelay = this.config.reconnectDelayMs * 16;
9188
+ const delay = Math.min(this.config.reconnectDelayMs * Math.pow(2, this.reconnectAttempts), maxDelay);
9187
9189
  this.reconnectAttempts++;
9190
+ logger.debug("GroupChat", `Reconnecting in ${delay}ms (attempt ${this.reconnectAttempts}/${this.config.maxReconnectAttempts})`);
9188
9191
  this.reconnectTimer = setTimeout(() => {
9189
9192
  this.reconnectTimer = null;
9190
9193
  if (this.deps) {
9191
9194
  this.connect().catch((err) => logger.error("GroupChat", "Reconnect failed:", err));
9192
9195
  }
9193
- }, this.config.reconnectDelayMs);
9196
+ }, delay);
9194
9197
  }
9195
9198
  // ===========================================================================
9196
9199
  // Subscription Management
@@ -12527,6 +12530,7 @@ var secp256k1 = /* @__PURE__ */ ecdsa(Pointk1, sha2564);
12527
12530
 
12528
12531
  // modules/market/MarketModule.ts
12529
12532
  init_errors();
12533
+ init_logger();
12530
12534
  var DEFAULT_MARKET_API_URL = "https://market-api.unicity.network";
12531
12535
  function hexToBytes3(hex) {
12532
12536
  const len = hex.length >> 1;
@@ -12699,16 +12703,54 @@ var MarketModule = class {
12699
12703
  */
12700
12704
  subscribeFeed(listener) {
12701
12705
  const wsUrl = this.apiUrl.replace(/^http/, "ws") + "/ws/feed";
12702
- const ws2 = new WebSocket(wsUrl);
12703
- ws2.onmessage = (event) => {
12704
- try {
12705
- const raw = JSON.parse(typeof event.data === "string" ? event.data : event.data.toString());
12706
- listener(mapFeedMessage(raw));
12707
- } catch {
12706
+ const BASE_DELAY2 = 2e3;
12707
+ const MAX_DELAY2 = 3e4;
12708
+ const MAX_ATTEMPTS = 10;
12709
+ let ws2 = null;
12710
+ let reconnectAttempts2 = 0;
12711
+ let reconnectTimer = null;
12712
+ let destroyed = false;
12713
+ function connect2() {
12714
+ if (destroyed) return;
12715
+ ws2 = new WebSocket(wsUrl);
12716
+ ws2.onopen = () => {
12717
+ reconnectAttempts2 = 0;
12718
+ logger.debug("Market", "Feed WebSocket connected");
12719
+ };
12720
+ ws2.onmessage = (event) => {
12721
+ try {
12722
+ const raw = JSON.parse(typeof event.data === "string" ? event.data : event.data.toString());
12723
+ listener(mapFeedMessage(raw));
12724
+ } catch {
12725
+ }
12726
+ };
12727
+ ws2.onclose = () => {
12728
+ if (destroyed) return;
12729
+ scheduleReconnect();
12730
+ };
12731
+ ws2.onerror = () => {
12732
+ };
12733
+ }
12734
+ function scheduleReconnect() {
12735
+ if (destroyed) return;
12736
+ if (reconnectAttempts2 >= MAX_ATTEMPTS) {
12737
+ logger.warn("Market", `Feed WebSocket: gave up after ${MAX_ATTEMPTS} reconnect attempts`);
12738
+ return;
12708
12739
  }
12709
- };
12740
+ const delay = Math.min(BASE_DELAY2 * Math.pow(2, reconnectAttempts2), MAX_DELAY2);
12741
+ reconnectAttempts2++;
12742
+ logger.debug("Market", `Feed WebSocket reconnecting in ${delay}ms (attempt ${reconnectAttempts2}/${MAX_ATTEMPTS})`);
12743
+ reconnectTimer = setTimeout(connect2, delay);
12744
+ }
12745
+ connect2();
12710
12746
  return () => {
12711
- ws2.close();
12747
+ destroyed = true;
12748
+ if (reconnectTimer) {
12749
+ clearTimeout(reconnectTimer);
12750
+ reconnectTimer = null;
12751
+ }
12752
+ ws2?.close();
12753
+ ws2 = null;
12712
12754
  };
12713
12755
  }
12714
12756
  // ---------------------------------------------------------------------------
@@ -12878,7 +12920,7 @@ async function scanAddressesImpl(deriveAddress, options = {}) {
12878
12920
  nametagsFoundCount++;
12879
12921
  }
12880
12922
  } catch (err) {
12881
- logger.debug("Sphere", "Nametag resolution failed during scan", err);
12923
+ logger.debug("Sphere", "Unicity ID resolution failed during scan", err);
12882
12924
  }
12883
12925
  }
12884
12926
  foundAddresses.push({
@@ -13990,7 +14032,7 @@ var Sphere = class _Sphere {
13990
14032
  _Sphere.instance = sphere;
13991
14033
  if (sphere._identity?.nametag && !sphere._payments.hasNametag()) {
13992
14034
  progress?.({ step: "registering_nametag", message: "Restoring nametag token..." });
13993
- logger.debug("Sphere", `Nametag @${sphere._identity.nametag} has no token, attempting to mint...`);
14035
+ logger.debug("Sphere", `Unicity ID @${sphere._identity.nametag} has no token, attempting to mint...`);
13994
14036
  try {
13995
14037
  const result = await sphere.mintNametag(sphere._identity.nametag);
13996
14038
  if (result.success) {
@@ -14087,9 +14129,9 @@ var Sphere = class _Sphere {
14087
14129
  logger.debug("Sphere", "Modules initialized");
14088
14130
  if (!options.nametag) {
14089
14131
  progress?.({ step: "recovering_nametag", message: "Recovering nametag..." });
14090
- logger.debug("Sphere", "Recovering nametag from transport...");
14132
+ logger.debug("Sphere", "Recovering Unicity ID from transport...");
14091
14133
  await sphere.recoverNametagFromTransport();
14092
- logger.debug("Sphere", "Nametag recovery done");
14134
+ logger.debug("Sphere", "Unicity ID recovery done");
14093
14135
  progress?.({ step: "syncing_identity", message: "Publishing identity..." });
14094
14136
  await sphere.syncIdentityWithTransport();
14095
14137
  }
@@ -14102,7 +14144,7 @@ var Sphere = class _Sphere {
14102
14144
  await sphere.ensureAddressTracked(0);
14103
14145
  if (options.nametag) {
14104
14146
  progress?.({ step: "registering_nametag", message: "Registering nametag..." });
14105
- logger.debug("Sphere", "Registering nametag...");
14147
+ logger.debug("Sphere", "Registering Unicity ID...");
14106
14148
  await sphere.registerNametag(options.nametag);
14107
14149
  }
14108
14150
  if (sphere._tokenStorageProviders.size > 0) {
@@ -14953,7 +14995,7 @@ var Sphere = class _Sphere {
14953
14995
  }
14954
14996
  const newNametag = options?.nametag ? this.cleanNametag(options.nametag) : void 0;
14955
14997
  if (newNametag && !isValidNametag(newNametag)) {
14956
- throw new SphereError("Invalid nametag format. Use lowercase alphanumeric, underscore, or hyphen (3-20 chars), or a valid phone number.", "VALIDATION_ERROR");
14998
+ throw new SphereError("Invalid Unicity ID format. Use lowercase alphanumeric, underscore, or hyphen (3-20 chars), or a valid phone number.", "VALIDATION_ERROR");
14957
14999
  }
14958
15000
  const addressInfo = this.deriveAddress(index, false);
14959
15001
  const ipnsHash = sha256(addressInfo.publicKey, "hex").slice(0, 40);
@@ -14963,7 +15005,7 @@ var Sphere = class _Sphere {
14963
15005
  if (newNametag) {
14964
15006
  const existing = await this._transport.resolveNametag?.(newNametag);
14965
15007
  if (existing) {
14966
- throw new SphereError(`Nametag @${newNametag} is already taken`, "VALIDATION_ERROR");
15008
+ throw new SphereError(`Unicity ID @${newNametag} is already taken`, "VALIDATION_ERROR");
14967
15009
  }
14968
15010
  let nametags = this._addressNametags.get(addressId);
14969
15011
  if (!nametags) {
@@ -15035,7 +15077,7 @@ var Sphere = class _Sphere {
15035
15077
  addressIndex: index
15036
15078
  });
15037
15079
  } else if (this._identity?.nametag && !this._payments.hasNametag()) {
15038
- logger.debug("Sphere", `Nametag @${this._identity.nametag} has no token after switch, minting...`);
15080
+ logger.debug("Sphere", `Unicity ID @${this._identity.nametag} has no token after switch, minting...`);
15039
15081
  try {
15040
15082
  const result = await this.mintNametag(this._identity.nametag);
15041
15083
  if (result.success) {
@@ -15221,7 +15263,7 @@ var Sphere = class _Sphere {
15221
15263
  const info = await this._transport.resolveAddressInfo(l1Address);
15222
15264
  return info?.nametag ?? null;
15223
15265
  } catch (err) {
15224
- logger.debug("Sphere", "Nametag resolution failed during scan", err);
15266
+ logger.debug("Sphere", "Unicity ID resolution failed during scan", err);
15225
15267
  return null;
15226
15268
  }
15227
15269
  } : void 0);
@@ -15632,10 +15674,10 @@ var Sphere = class _Sphere {
15632
15674
  this.ensureReady();
15633
15675
  const cleanNametag = this.cleanNametag(nametag);
15634
15676
  if (!isValidNametag(cleanNametag)) {
15635
- throw new SphereError("Invalid nametag format. Use lowercase alphanumeric, underscore, or hyphen (3-20 chars), or a valid phone number.", "VALIDATION_ERROR");
15677
+ throw new SphereError("Invalid Unicity ID format. Use lowercase alphanumeric, underscore, or hyphen (3-20 chars), or a valid phone number.", "VALIDATION_ERROR");
15636
15678
  }
15637
15679
  if (this._identity?.nametag) {
15638
- throw new SphereError(`Nametag already registered for address ${this._currentAddressIndex}: @${this._identity.nametag}`, "ALREADY_INITIALIZED");
15680
+ throw new SphereError(`Unicity ID already registered for address ${this._currentAddressIndex}: @${this._identity.nametag}`, "ALREADY_INITIALIZED");
15639
15681
  }
15640
15682
  if (this._transport.publishIdentityBinding) {
15641
15683
  const success = await this._transport.publishIdentityBinding(
@@ -15645,7 +15687,7 @@ var Sphere = class _Sphere {
15645
15687
  cleanNametag
15646
15688
  );
15647
15689
  if (!success) {
15648
- throw new SphereError("Failed to register nametag. It may already be taken.", "VALIDATION_ERROR");
15690
+ throw new SphereError("Failed to register Unicity ID. It may already be taken.", "VALIDATION_ERROR");
15649
15691
  }
15650
15692
  }
15651
15693
  this._identity.nametag = cleanNametag;
@@ -15673,7 +15715,7 @@ var Sphere = class _Sphere {
15673
15715
  nametag: cleanNametag,
15674
15716
  addressIndex: this._currentAddressIndex
15675
15717
  });
15676
- logger.debug("Sphere", `Nametag registered for address ${this._currentAddressIndex}:`, cleanNametag);
15718
+ logger.debug("Sphere", `Unicity ID registered for address ${this._currentAddressIndex}:`, cleanNametag);
15677
15719
  }
15678
15720
  /**
15679
15721
  * Persist tracked addresses to storage (only minimal fields via StorageProvider)
@@ -15913,7 +15955,7 @@ var Sphere = class _Sphere {
15913
15955
  this._identity.directAddress || "",
15914
15956
  recoveredNametag
15915
15957
  );
15916
- logger.debug("Sphere", `Migrated legacy binding with nametag @${recoveredNametag}`);
15958
+ logger.debug("Sphere", `Migrated legacy binding with Unicity ID @${recoveredNametag}`);
15917
15959
  return;
15918
15960
  }
15919
15961
  }
@@ -15944,9 +15986,9 @@ var Sphere = class _Sphere {
15944
15986
  nametag || void 0
15945
15987
  );
15946
15988
  if (success) {
15947
- logger.debug("Sphere", `Identity binding published${nametag ? ` with nametag @${nametag}` : ""}`);
15989
+ logger.debug("Sphere", `Identity binding published${nametag ? ` with Unicity ID @${nametag}` : ""}`);
15948
15990
  } else if (nametag) {
15949
- logger.warn("Sphere", `Nametag @${nametag} is taken by another pubkey`);
15991
+ logger.warn("Sphere", `Unicity ID @${nametag} is taken by another pubkey`);
15950
15992
  }
15951
15993
  } catch (error) {
15952
15994
  logger.warn("Sphere", `Identity binding sync failed:`, error);