@unicitylabs/sphere-sdk 0.4.0 → 0.4.2

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.d.cts CHANGED
@@ -3063,6 +3063,13 @@ declare class PaymentsModule {
3063
3063
  * Remove all nametag data from memory and storage.
3064
3064
  */
3065
3065
  clearNametag(): Promise<void>;
3066
+ /**
3067
+ * Reload nametag data from storage providers into memory.
3068
+ *
3069
+ * Used as a recovery mechanism when `this.nametags` is unexpectedly empty
3070
+ * (e.g., wiped by sync or race condition) but nametag data exists in storage.
3071
+ */
3072
+ private reloadNametagsFromStorage;
3066
3073
  /**
3067
3074
  * Mint a nametag token on-chain (like Sphere wallet and lottery)
3068
3075
  * This creates the nametag token required for receiving tokens via PROXY addresses
package/dist/index.d.ts CHANGED
@@ -3063,6 +3063,13 @@ declare class PaymentsModule {
3063
3063
  * Remove all nametag data from memory and storage.
3064
3064
  */
3065
3065
  clearNametag(): Promise<void>;
3066
+ /**
3067
+ * Reload nametag data from storage providers into memory.
3068
+ *
3069
+ * Used as a recovery mechanism when `this.nametags` is unexpectedly empty
3070
+ * (e.g., wiped by sync or race condition) but nametag data exists in storage.
3071
+ */
3072
+ private reloadNametagsFromStorage;
3066
3073
  /**
3067
3074
  * Mint a nametag token on-chain (like Sphere wallet and lottery)
3068
3075
  * This creates the nametag token required for receiving tokens via PROXY addresses
package/dist/index.js CHANGED
@@ -6575,6 +6575,29 @@ var PaymentsModule = class _PaymentsModule {
6575
6575
  this.nametags = [];
6576
6576
  await this.save();
6577
6577
  }
6578
+ /**
6579
+ * Reload nametag data from storage providers into memory.
6580
+ *
6581
+ * Used as a recovery mechanism when `this.nametags` is unexpectedly empty
6582
+ * (e.g., wiped by sync or race condition) but nametag data exists in storage.
6583
+ */
6584
+ async reloadNametagsFromStorage() {
6585
+ const providers = this.getTokenStorageProviders();
6586
+ for (const [, provider] of providers) {
6587
+ try {
6588
+ const result = await provider.load();
6589
+ if (result.success && result.data) {
6590
+ const parsed = parseTxfStorageData(result.data);
6591
+ if (parsed.nametags.length > 0) {
6592
+ this.nametags = parsed.nametags;
6593
+ this.log(`Reloaded ${parsed.nametags.length} nametag(s) from storage`);
6594
+ return;
6595
+ }
6596
+ }
6597
+ } catch {
6598
+ }
6599
+ }
6600
+ }
6578
6601
  /**
6579
6602
  * Mint a nametag token on-chain (like Sphere wallet and lottery)
6580
6603
  * This creates the nametag token required for receiving tokens via PROXY addresses
@@ -6699,11 +6722,15 @@ var PaymentsModule = class _PaymentsModule {
6699
6722
  const localData = await this.createStorageData();
6700
6723
  let totalAdded = 0;
6701
6724
  let totalRemoved = 0;
6725
+ const savedNametags = [...this.nametags];
6702
6726
  for (const [providerId, provider] of providers) {
6703
6727
  try {
6704
6728
  const result = await provider.sync(localData);
6705
6729
  if (result.success && result.merged) {
6706
6730
  this.loadFromStorageData(result.merged);
6731
+ if (this.nametags.length === 0 && savedNametags.length > 0) {
6732
+ this.nametags = savedNametags;
6733
+ }
6707
6734
  totalAdded += result.added;
6708
6735
  totalRemoved += result.removed;
6709
6736
  }
@@ -7091,7 +7118,12 @@ var PaymentsModule = class _PaymentsModule {
7091
7118
  let nametagTokens = [];
7092
7119
  if (addressScheme === AddressScheme.PROXY) {
7093
7120
  const { ProxyAddress } = await import("@unicitylabs/state-transition-sdk/lib/address/ProxyAddress");
7094
- const proxyNametag = this.getNametag();
7121
+ let proxyNametag = this.getNametag();
7122
+ if (!proxyNametag?.token) {
7123
+ this.log("Nametag missing in memory, attempting reload from storage...");
7124
+ await this.reloadNametagsFromStorage();
7125
+ proxyNametag = this.getNametag();
7126
+ }
7095
7127
  if (!proxyNametag?.token) {
7096
7128
  throw new Error("Cannot finalize PROXY transfer - no nametag token");
7097
7129
  }
@@ -7696,13 +7728,14 @@ var CommunicationsModule = class {
7696
7728
  */
7697
7729
  async sendDM(recipient, content) {
7698
7730
  this.ensureInitialized();
7699
- const recipientPubkey = await this.resolveRecipient(recipient);
7700
- const eventId = await this.deps.transport.sendMessage(recipientPubkey, content);
7731
+ const resolved = await this.resolveRecipient(recipient);
7732
+ const eventId = await this.deps.transport.sendMessage(resolved.pubkey, content);
7701
7733
  const message = {
7702
7734
  id: eventId,
7703
7735
  senderPubkey: this.deps.identity.chainPubkey,
7704
7736
  senderNametag: this.deps.identity.nametag,
7705
- recipientPubkey,
7737
+ recipientPubkey: resolved.pubkey,
7738
+ ...resolved.nametag ? { recipientNametag: resolved.nametag } : {},
7706
7739
  content,
7707
7740
  timestamp: Date.now(),
7708
7741
  isRead: false
@@ -7820,12 +7853,12 @@ var CommunicationsModule = class {
7820
7853
  */
7821
7854
  async sendComposingIndicator(recipientPubkeyOrNametag) {
7822
7855
  this.ensureInitialized();
7823
- const recipientPubkey = await this.resolveRecipient(recipientPubkeyOrNametag);
7856
+ const resolved = await this.resolveRecipient(recipientPubkeyOrNametag);
7824
7857
  const content = JSON.stringify({
7825
7858
  senderNametag: this.deps.identity.nametag,
7826
7859
  expiresIn: 3e4
7827
7860
  });
7828
- await this.deps.transport.sendComposingIndicator?.(recipientPubkey, content);
7861
+ await this.deps.transport.sendComposingIndicator?.(resolved.pubkey, content);
7829
7862
  }
7830
7863
  /**
7831
7864
  * Subscribe to incoming composing indicators
@@ -8013,13 +8046,14 @@ var CommunicationsModule = class {
8013
8046
  // ===========================================================================
8014
8047
  async resolveRecipient(recipient) {
8015
8048
  if (recipient.startsWith("@")) {
8016
- const pubkey = await this.deps.transport.resolveNametag?.(recipient.slice(1));
8049
+ const nametag = recipient.slice(1);
8050
+ const pubkey = await this.deps.transport.resolveNametag?.(nametag);
8017
8051
  if (!pubkey) {
8018
8052
  throw new Error(`Nametag not found: ${recipient}`);
8019
8053
  }
8020
- return pubkey;
8054
+ return { pubkey, nametag };
8021
8055
  }
8022
- return recipient;
8056
+ return { pubkey: recipient };
8023
8057
  }
8024
8058
  ensureInitialized() {
8025
8059
  if (!this.deps) {
@@ -13034,45 +13068,22 @@ var Sphere = class _Sphere {
13034
13068
  await _Sphere.instance.destroy();
13035
13069
  console.log("[Sphere.clear] Sphere instance destroyed");
13036
13070
  }
13071
+ console.log("[Sphere.clear] Clearing L1 vesting cache...");
13037
13072
  await vestingClassifier.destroy();
13038
- if (typeof indexedDB !== "undefined" && typeof indexedDB.databases === "function") {
13039
- console.log("[Sphere.clear] Deleting all sphere IndexedDB databases...");
13040
- try {
13041
- const dbs = await Promise.race([
13042
- indexedDB.databases(),
13043
- new Promise(
13044
- (_, reject) => setTimeout(() => reject(new Error("timeout")), 2e3)
13045
- )
13046
- ]);
13047
- const sphereDbs = dbs.filter((db) => db.name?.startsWith("sphere"));
13048
- if (sphereDbs.length > 0) {
13049
- await Promise.all(sphereDbs.map(
13050
- (db) => new Promise((resolve) => {
13051
- const req = indexedDB.deleteDatabase(db.name);
13052
- req.onsuccess = () => {
13053
- console.log(`[Sphere.clear] Deleted ${db.name}`);
13054
- resolve();
13055
- };
13056
- req.onerror = () => resolve();
13057
- req.onblocked = () => {
13058
- console.warn(`[Sphere.clear] deleteDatabase blocked: ${db.name}`);
13059
- resolve();
13060
- };
13061
- })
13062
- ));
13063
- }
13064
- console.log("[Sphere.clear] IndexedDB cleanup done");
13065
- } catch {
13066
- console.warn("[Sphere.clear] IndexedDB enumeration failed");
13067
- }
13068
- }
13073
+ console.log("[Sphere.clear] Yielding 50ms for IDB transaction settlement...");
13074
+ await new Promise((r) => setTimeout(r, 50));
13069
13075
  if (tokenStorage?.clear) {
13076
+ console.log("[Sphere.clear] Clearing token storage...");
13070
13077
  try {
13071
13078
  await tokenStorage.clear();
13079
+ console.log("[Sphere.clear] Token storage cleared");
13072
13080
  } catch (err) {
13073
13081
  console.warn("[Sphere.clear] Token storage clear failed:", err);
13074
13082
  }
13083
+ } else {
13084
+ console.log("[Sphere.clear] No token storage provider to clear");
13075
13085
  }
13086
+ console.log("[Sphere.clear] Clearing KV storage...");
13076
13087
  if (!storage.isConnected()) {
13077
13088
  try {
13078
13089
  await storage.connect();
@@ -13081,7 +13092,11 @@ var Sphere = class _Sphere {
13081
13092
  }
13082
13093
  if (storage.isConnected()) {
13083
13094
  await storage.clear();
13095
+ console.log("[Sphere.clear] KV storage cleared");
13096
+ } else {
13097
+ console.log("[Sphere.clear] KV storage not connected, skipping");
13084
13098
  }
13099
+ console.log("[Sphere.clear] Done");
13085
13100
  }
13086
13101
  /**
13087
13102
  * Get current instance
@@ -13929,8 +13944,12 @@ var Sphere = class _Sphere {
13929
13944
  await this._storage.set(STORAGE_KEYS_GLOBAL.CURRENT_ADDRESS_INDEX, index.toString());
13930
13945
  this._storage.setIdentity(this._identity);
13931
13946
  await this._transport.setIdentity(this._identity);
13932
- for (const provider of this._tokenStorageProviders.values()) {
13947
+ console.log(`[Sphere] switchToAddress(${index}): re-initializing ${this._tokenStorageProviders.size} token storage provider(s)`);
13948
+ for (const [providerId, provider] of this._tokenStorageProviders.entries()) {
13949
+ console.log(`[Sphere] switchToAddress(${index}): shutdown provider=${providerId}`);
13950
+ await provider.shutdown();
13933
13951
  provider.setIdentity(this._identity);
13952
+ console.log(`[Sphere] switchToAddress(${index}): initialize provider=${providerId}`);
13934
13953
  await provider.initialize();
13935
13954
  }
13936
13955
  await this.reinitializeModulesForNewAddress();
@@ -14012,6 +14031,9 @@ var Sphere = class _Sphere {
14012
14031
  await this._communications.load();
14013
14032
  await this._groupChat?.load();
14014
14033
  await this._market?.load();
14034
+ this._payments.sync().catch((err) => {
14035
+ console.warn("[Sphere] Post-switch sync failed:", err);
14036
+ });
14015
14037
  }
14016
14038
  /**
14017
14039
  * Derive address at a specific index