@unicitylabs/sphere-sdk 0.4.4 → 0.4.5

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.
Files changed (37) hide show
  1. package/dist/connect/index.cjs +10 -10
  2. package/dist/connect/index.cjs.map +1 -1
  3. package/dist/connect/index.js +10 -10
  4. package/dist/connect/index.js.map +1 -1
  5. package/dist/core/index.cjs +55 -25
  6. package/dist/core/index.cjs.map +1 -1
  7. package/dist/core/index.d.cts +6 -0
  8. package/dist/core/index.d.ts +6 -0
  9. package/dist/core/index.js +55 -25
  10. package/dist/core/index.js.map +1 -1
  11. package/dist/impl/browser/connect/index.cjs +10 -10
  12. package/dist/impl/browser/connect/index.cjs.map +1 -1
  13. package/dist/impl/browser/connect/index.js +10 -10
  14. package/dist/impl/browser/connect/index.js.map +1 -1
  15. package/dist/impl/browser/index.cjs +10 -10
  16. package/dist/impl/browser/index.cjs.map +1 -1
  17. package/dist/impl/browser/index.js +10 -10
  18. package/dist/impl/browser/index.js.map +1 -1
  19. package/dist/impl/browser/ipfs.cjs +10 -10
  20. package/dist/impl/browser/ipfs.cjs.map +1 -1
  21. package/dist/impl/browser/ipfs.js +10 -10
  22. package/dist/impl/browser/ipfs.js.map +1 -1
  23. package/dist/impl/nodejs/connect/index.cjs +10 -10
  24. package/dist/impl/nodejs/connect/index.cjs.map +1 -1
  25. package/dist/impl/nodejs/connect/index.js +10 -10
  26. package/dist/impl/nodejs/connect/index.js.map +1 -1
  27. package/dist/impl/nodejs/index.cjs +10 -10
  28. package/dist/impl/nodejs/index.cjs.map +1 -1
  29. package/dist/impl/nodejs/index.js +10 -10
  30. package/dist/impl/nodejs/index.js.map +1 -1
  31. package/dist/index.cjs +55 -25
  32. package/dist/index.cjs.map +1 -1
  33. package/dist/index.d.cts +24 -18
  34. package/dist/index.d.ts +24 -18
  35. package/dist/index.js +55 -25
  36. package/dist/index.js.map +1 -1
  37. package/package.json +1 -1
package/dist/index.cjs CHANGED
@@ -2664,15 +2664,7 @@ var STORAGE_KEYS_GLOBAL = {
2664
2664
  TRACKED_ADDRESSES: "tracked_addresses",
2665
2665
  /** Last processed Nostr wallet event timestamp (unix seconds), keyed per pubkey */
2666
2666
  LAST_WALLET_EVENT_TS: "last_wallet_event_ts",
2667
- /** Group chat: joined groups */
2668
- GROUP_CHAT_GROUPS: "group_chat_groups",
2669
- /** Group chat: messages */
2670
- GROUP_CHAT_MESSAGES: "group_chat_messages",
2671
- /** Group chat: members */
2672
- GROUP_CHAT_MEMBERS: "group_chat_members",
2673
- /** Group chat: processed event IDs for deduplication */
2674
- GROUP_CHAT_PROCESSED_EVENTS: "group_chat_processed_events",
2675
- /** Group chat: last used relay URL (stale data detection) */
2667
+ /** Group chat: last used relay URL (stale data detection) — global, same relay for all addresses */
2676
2668
  GROUP_CHAT_RELAY_URL: "group_chat_relay_url",
2677
2669
  /** Cached token registry JSON (fetched from remote) */
2678
2670
  TOKEN_REGISTRY_CACHE: "token_registry_cache",
@@ -2695,7 +2687,15 @@ var STORAGE_KEYS_ADDRESS = {
2695
2687
  /** Transaction history for this address */
2696
2688
  TRANSACTION_HISTORY: "transaction_history",
2697
2689
  /** Pending V5 finalization tokens (unconfirmed instant split tokens) */
2698
- PENDING_V5_TOKENS: "pending_v5_tokens"
2690
+ PENDING_V5_TOKENS: "pending_v5_tokens",
2691
+ /** Group chat: joined groups for this address */
2692
+ GROUP_CHAT_GROUPS: "group_chat_groups",
2693
+ /** Group chat: messages for this address */
2694
+ GROUP_CHAT_MESSAGES: "group_chat_messages",
2695
+ /** Group chat: members for this address */
2696
+ GROUP_CHAT_MEMBERS: "group_chat_members",
2697
+ /** Group chat: processed event IDs for deduplication */
2698
+ GROUP_CHAT_PROCESSED_EVENTS: "group_chat_processed_events"
2699
2699
  };
2700
2700
  var STORAGE_KEYS = {
2701
2701
  ...STORAGE_KEYS_GLOBAL,
@@ -7806,6 +7806,8 @@ var CommunicationsModule = class {
7806
7806
  dmHandlers = /* @__PURE__ */ new Set();
7807
7807
  composingHandlers = /* @__PURE__ */ new Set();
7808
7808
  broadcastHandlers = /* @__PURE__ */ new Set();
7809
+ // Timestamp of module initialization — messages older than this are historical
7810
+ initializedAt = 0;
7809
7811
  constructor(config) {
7810
7812
  this.config = {
7811
7813
  autoSave: config?.autoSave ?? true,
@@ -7824,6 +7826,7 @@ var CommunicationsModule = class {
7824
7826
  this.unsubscribeMessages?.();
7825
7827
  this.unsubscribeComposing?.();
7826
7828
  this.deps = deps;
7829
+ this.initializedAt = Date.now();
7827
7830
  this.unsubscribeMessages = deps.transport.onMessage((msg) => {
7828
7831
  this.handleIncomingMessage(msg);
7829
7832
  });
@@ -8115,6 +8118,7 @@ var CommunicationsModule = class {
8115
8118
  // Private: Message Handling
8116
8119
  // ===========================================================================
8117
8120
  handleIncomingMessage(msg) {
8121
+ const isHistorical = msg.timestamp < this.initializedAt;
8118
8122
  if (msg.isSelfWrap && msg.recipientTransportPubkey) {
8119
8123
  if (this.messages.has(msg.id)) return;
8120
8124
  const message2 = {
@@ -8124,7 +8128,7 @@ var CommunicationsModule = class {
8124
8128
  recipientPubkey: msg.recipientTransportPubkey,
8125
8129
  content: msg.content,
8126
8130
  timestamp: msg.timestamp,
8127
- isRead: false
8131
+ isRead: isHistorical
8128
8132
  };
8129
8133
  this.messages.set(message2.id, message2);
8130
8134
  this.deps.emitEvent("message:dm", message2);
@@ -8142,7 +8146,7 @@ var CommunicationsModule = class {
8142
8146
  recipientPubkey: this.deps.identity.chainPubkey,
8143
8147
  content: msg.content,
8144
8148
  timestamp: msg.timestamp,
8145
- isRead: false
8149
+ isRead: isHistorical
8146
8150
  };
8147
8151
  this.messages.set(message.id, message);
8148
8152
  this.deps.emitEvent("message:dm", message);
@@ -8314,22 +8318,24 @@ var GroupChatModule = class {
8314
8318
  async load() {
8315
8319
  this.ensureInitialized();
8316
8320
  const storage = this.deps.storage;
8317
- const groupsJson = await storage.get(STORAGE_KEYS_GLOBAL.GROUP_CHAT_GROUPS);
8321
+ this.groups.clear();
8322
+ this.messages.clear();
8323
+ this.members.clear();
8324
+ this.processedEventIds.clear();
8325
+ const groupsJson = await storage.get(STORAGE_KEYS_ADDRESS.GROUP_CHAT_GROUPS);
8318
8326
  if (groupsJson) {
8319
8327
  try {
8320
8328
  const parsed = JSON.parse(groupsJson);
8321
- this.groups.clear();
8322
8329
  for (const g of parsed) {
8323
8330
  this.groups.set(g.id, g);
8324
8331
  }
8325
8332
  } catch {
8326
8333
  }
8327
8334
  }
8328
- const messagesJson = await storage.get(STORAGE_KEYS_GLOBAL.GROUP_CHAT_MESSAGES);
8335
+ const messagesJson = await storage.get(STORAGE_KEYS_ADDRESS.GROUP_CHAT_MESSAGES);
8329
8336
  if (messagesJson) {
8330
8337
  try {
8331
8338
  const parsed = JSON.parse(messagesJson);
8332
- this.messages.clear();
8333
8339
  for (const m of parsed) {
8334
8340
  const groupId = m.groupId;
8335
8341
  if (!this.messages.has(groupId)) {
@@ -8340,11 +8346,10 @@ var GroupChatModule = class {
8340
8346
  } catch {
8341
8347
  }
8342
8348
  }
8343
- const membersJson = await storage.get(STORAGE_KEYS_GLOBAL.GROUP_CHAT_MEMBERS);
8349
+ const membersJson = await storage.get(STORAGE_KEYS_ADDRESS.GROUP_CHAT_MEMBERS);
8344
8350
  if (membersJson) {
8345
8351
  try {
8346
8352
  const parsed = JSON.parse(membersJson);
8347
- this.members.clear();
8348
8353
  for (const m of parsed) {
8349
8354
  const groupId = m.groupId;
8350
8355
  if (!this.members.has(groupId)) {
@@ -8355,7 +8360,7 @@ var GroupChatModule = class {
8355
8360
  } catch {
8356
8361
  }
8357
8362
  }
8358
- const processedJson = await storage.get(STORAGE_KEYS_GLOBAL.GROUP_CHAT_PROCESSED_EVENTS);
8363
+ const processedJson = await storage.get(STORAGE_KEYS_ADDRESS.GROUP_CHAT_PROCESSED_EVENTS);
8359
8364
  if (processedJson) {
8360
8365
  try {
8361
8366
  const parsed = JSON.parse(processedJson);
@@ -8409,7 +8414,10 @@ var GroupChatModule = class {
8409
8414
  // Connection
8410
8415
  // ===========================================================================
8411
8416
  async connect() {
8412
- if (this.connected) return;
8417
+ if (this.connected) {
8418
+ await this.refreshSubscriptions();
8419
+ return;
8420
+ }
8413
8421
  if (this.connectPromise) {
8414
8422
  return this.connectPromise;
8415
8423
  }
@@ -8423,6 +8431,28 @@ var GroupChatModule = class {
8423
8431
  getConnectionStatus() {
8424
8432
  return this.connected;
8425
8433
  }
8434
+ /**
8435
+ * Refresh subscriptions after load() switched to a different address.
8436
+ * Clears old subscriptions, restores groups if needed, and re-subscribes.
8437
+ */
8438
+ async refreshSubscriptions() {
8439
+ if (!this.client) return;
8440
+ for (const subId of this.subscriptionIds) {
8441
+ try {
8442
+ this.client.unsubscribe(subId);
8443
+ } catch {
8444
+ }
8445
+ }
8446
+ this.subscriptionIds = [];
8447
+ const secretKey = Buffer.from(this.deps.identity.privateKey, "hex");
8448
+ this.keyManager = import_nostr_js_sdk2.NostrKeyManager.fromPrivateKey(secretKey);
8449
+ if (this.groups.size === 0) {
8450
+ await this.restoreJoinedGroups();
8451
+ } else {
8452
+ await this.subscribeToJoinedGroups();
8453
+ }
8454
+ this.deps.emitEvent("groupchat:connection", { connected: true });
8455
+ }
8426
8456
  async doConnect() {
8427
8457
  this.ensureInitialized();
8428
8458
  if (!this.keyManager) {
@@ -8438,12 +8468,12 @@ var GroupChatModule = class {
8438
8468
  await this.client.connect(...this.config.relays);
8439
8469
  this.connected = true;
8440
8470
  this.reconnectAttempts = 0;
8441
- this.deps.emitEvent("groupchat:connection", { connected: true });
8442
8471
  if (this.groups.size === 0) {
8443
8472
  await this.restoreJoinedGroups();
8444
8473
  } else {
8445
8474
  await this.subscribeToJoinedGroups();
8446
8475
  }
8476
+ this.deps.emitEvent("groupchat:connection", { connected: true });
8447
8477
  } catch (error) {
8448
8478
  console.error("[GroupChat] Failed to connect to relays", error);
8449
8479
  this.deps.emitEvent("groupchat:connection", { connected: false });
@@ -9426,7 +9456,7 @@ var GroupChatModule = class {
9426
9456
  async persistGroups() {
9427
9457
  if (!this.deps) return;
9428
9458
  const data = Array.from(this.groups.values());
9429
- await this.deps.storage.set(STORAGE_KEYS_GLOBAL.GROUP_CHAT_GROUPS, JSON.stringify(data));
9459
+ await this.deps.storage.set(STORAGE_KEYS_ADDRESS.GROUP_CHAT_GROUPS, JSON.stringify(data));
9430
9460
  }
9431
9461
  async persistMessages() {
9432
9462
  if (!this.deps) return;
@@ -9434,7 +9464,7 @@ var GroupChatModule = class {
9434
9464
  for (const msgs of this.messages.values()) {
9435
9465
  allMessages.push(...msgs);
9436
9466
  }
9437
- await this.deps.storage.set(STORAGE_KEYS_GLOBAL.GROUP_CHAT_MESSAGES, JSON.stringify(allMessages));
9467
+ await this.deps.storage.set(STORAGE_KEYS_ADDRESS.GROUP_CHAT_MESSAGES, JSON.stringify(allMessages));
9438
9468
  }
9439
9469
  async persistMembers() {
9440
9470
  if (!this.deps) return;
@@ -9442,12 +9472,12 @@ var GroupChatModule = class {
9442
9472
  for (const mems of this.members.values()) {
9443
9473
  allMembers.push(...mems);
9444
9474
  }
9445
- await this.deps.storage.set(STORAGE_KEYS_GLOBAL.GROUP_CHAT_MEMBERS, JSON.stringify(allMembers));
9475
+ await this.deps.storage.set(STORAGE_KEYS_ADDRESS.GROUP_CHAT_MEMBERS, JSON.stringify(allMembers));
9446
9476
  }
9447
9477
  async persistProcessedEvents() {
9448
9478
  if (!this.deps) return;
9449
9479
  const arr = Array.from(this.processedEventIds);
9450
- await this.deps.storage.set(STORAGE_KEYS_GLOBAL.GROUP_CHAT_PROCESSED_EVENTS, JSON.stringify(arr));
9480
+ await this.deps.storage.set(STORAGE_KEYS_ADDRESS.GROUP_CHAT_PROCESSED_EVENTS, JSON.stringify(arr));
9451
9481
  }
9452
9482
  // ===========================================================================
9453
9483
  // Private — Relay URL Change Detection