@unicitylabs/sphere-sdk 0.6.8-dev.1 → 0.6.8-dev.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.
Files changed (44) hide show
  1. package/README.md +30 -0
  2. package/dist/connect/index.cjs +2 -0
  3. package/dist/connect/index.cjs.map +1 -1
  4. package/dist/connect/index.js +2 -0
  5. package/dist/connect/index.js.map +1 -1
  6. package/dist/core/index.cjs +140 -6
  7. package/dist/core/index.cjs.map +1 -1
  8. package/dist/core/index.d.cts +18 -0
  9. package/dist/core/index.d.ts +18 -0
  10. package/dist/core/index.js +140 -6
  11. package/dist/core/index.js.map +1 -1
  12. package/dist/impl/browser/connect/index.cjs +2 -0
  13. package/dist/impl/browser/connect/index.cjs.map +1 -1
  14. package/dist/impl/browser/connect/index.js +2 -0
  15. package/dist/impl/browser/connect/index.js.map +1 -1
  16. package/dist/impl/browser/index.cjs +66 -0
  17. package/dist/impl/browser/index.cjs.map +1 -1
  18. package/dist/impl/browser/index.js +66 -0
  19. package/dist/impl/browser/index.js.map +1 -1
  20. package/dist/impl/browser/ipfs.cjs +2 -0
  21. package/dist/impl/browser/ipfs.cjs.map +1 -1
  22. package/dist/impl/browser/ipfs.js +2 -0
  23. package/dist/impl/browser/ipfs.js.map +1 -1
  24. package/dist/impl/nodejs/connect/index.cjs +2 -0
  25. package/dist/impl/nodejs/connect/index.cjs.map +1 -1
  26. package/dist/impl/nodejs/connect/index.js +2 -0
  27. package/dist/impl/nodejs/connect/index.js.map +1 -1
  28. package/dist/impl/nodejs/index.cjs +66 -0
  29. package/dist/impl/nodejs/index.cjs.map +1 -1
  30. package/dist/impl/nodejs/index.d.cts +15 -0
  31. package/dist/impl/nodejs/index.d.ts +15 -0
  32. package/dist/impl/nodejs/index.js +66 -0
  33. package/dist/impl/nodejs/index.js.map +1 -1
  34. package/dist/index.cjs +140 -6
  35. package/dist/index.cjs.map +1 -1
  36. package/dist/index.d.cts +18 -0
  37. package/dist/index.d.ts +18 -0
  38. package/dist/index.js +140 -6
  39. package/dist/index.js.map +1 -1
  40. package/dist/l1/index.cjs +2 -0
  41. package/dist/l1/index.cjs.map +1 -1
  42. package/dist/l1/index.js +2 -0
  43. package/dist/l1/index.js.map +1 -1
  44. package/package.json +1 -1
@@ -131,6 +131,8 @@ var STORAGE_KEYS_GLOBAL = {
131
131
  TRACKED_ADDRESSES: "tracked_addresses",
132
132
  /** Last processed Nostr wallet event timestamp (unix seconds), keyed per pubkey */
133
133
  LAST_WALLET_EVENT_TS: "last_wallet_event_ts",
134
+ /** Last processed Nostr DM (gift-wrap) event timestamp (unix seconds), keyed per pubkey */
135
+ LAST_DM_EVENT_TS: "last_dm_event_ts",
134
136
  /** Group chat: last used relay URL (stale data detection) — global, same relay for all addresses */
135
137
  GROUP_CHAT_RELAY_URL: "group_chat_relay_url",
136
138
  /** Cached token registry JSON (fetched from remote) */
@@ -1644,8 +1646,12 @@ var NostrTransportProvider = class _NostrTransportProvider {
1644
1646
  storage = null;
1645
1647
  /** In-memory max event timestamp to avoid read-before-write races in updateLastEventTimestamp. */
1646
1648
  lastEventTs = 0;
1649
+ /** In-memory max DM (gift-wrap) event timestamp. */
1650
+ lastDmEventTs = 0;
1647
1651
  /** Fallback 'since' timestamp for first-time address subscriptions (consumed once). */
1648
1652
  fallbackSince = null;
1653
+ /** Fallback 'since' timestamp for DM (gift-wrap) subscriptions (consumed once). */
1654
+ fallbackDmSince = null;
1649
1655
  identity = null;
1650
1656
  keyManager = null;
1651
1657
  status = "disconnected";
@@ -1900,6 +1906,8 @@ var NostrTransportProvider = class _NostrTransportProvider {
1900
1906
  this.identity = identity;
1901
1907
  this.processedEventIds.clear();
1902
1908
  this.lastEventTs = 0;
1909
+ this.lastDmEventTs = 0;
1910
+ this.fallbackDmSince = null;
1903
1911
  const secretKey = Buffer2.from(identity.privateKey, "hex");
1904
1912
  this.keyManager = NostrKeyManager.fromPrivateKey(secretKey);
1905
1913
  const nostrPubkey = this.keyManager.getPublicKeyHex();
@@ -1945,6 +1953,9 @@ var NostrTransportProvider = class _NostrTransportProvider {
1945
1953
  setFallbackSince(sinceSeconds) {
1946
1954
  this.fallbackSince = sinceSeconds;
1947
1955
  }
1956
+ setFallbackDmSince(sinceSeconds) {
1957
+ this.fallbackDmSince = sinceSeconds;
1958
+ }
1948
1959
  /**
1949
1960
  * Get the Nostr-format public key (32 bytes / 64 hex chars)
1950
1961
  * This is the x-coordinate only, without the 02/03 prefix.
@@ -2479,6 +2490,17 @@ var NostrTransportProvider = class _NostrTransportProvider {
2479
2490
  logger.debug("Nostr", "Failed to save last event timestamp:", err);
2480
2491
  });
2481
2492
  }
2493
+ /** Persist the max DM (gift-wrap) event timestamp for the since filter on next connect. */
2494
+ updateLastDmEventTimestamp(createdAt) {
2495
+ if (!this.storage || !this.keyManager) return;
2496
+ if (createdAt <= this.lastDmEventTs) return;
2497
+ this.lastDmEventTs = createdAt;
2498
+ const pubkey = this.keyManager.getPublicKeyHex();
2499
+ const storageKey = `${STORAGE_KEYS_GLOBAL.LAST_DM_EVENT_TS}_${pubkey.slice(0, 16)}`;
2500
+ this.storage.set(storageKey, createdAt.toString()).catch((err) => {
2501
+ logger.debug("Nostr", "Failed to save last DM event timestamp:", err);
2502
+ });
2503
+ }
2482
2504
  async handleDirectMessage(event) {
2483
2505
  logger.debug("Nostr", "Ignoring NIP-04 kind 4 event (DMs use NIP-17):", event.id?.slice(0, 12));
2484
2506
  }
@@ -2489,6 +2511,9 @@ var NostrTransportProvider = class _NostrTransportProvider {
2489
2511
  }
2490
2512
  try {
2491
2513
  const pm = NIP17.unwrap(event, this.keyManager);
2514
+ if (event.created_at) {
2515
+ this.updateLastDmEventTimestamp(event.created_at);
2516
+ }
2492
2517
  logger.debug("Nostr", "Gift wrap unwrapped, sender:", pm.senderPubkey?.slice(0, 16), "kind:", pm.kind);
2493
2518
  if (pm.senderPubkey === this.keyManager.getPublicKeyHex()) {
2494
2519
  try {
@@ -2935,9 +2960,50 @@ var NostrTransportProvider = class _NostrTransportProvider {
2935
2960
  }
2936
2961
  });
2937
2962
  logger.debug("Nostr", "Wallet subscription created, subId:", this.walletSubscriptionId);
2963
+ let dmSince;
2964
+ if (this.storage) {
2965
+ const dmStorageKey = `${STORAGE_KEYS_GLOBAL.LAST_DM_EVENT_TS}_${nostrPubkey.slice(0, 16)}`;
2966
+ try {
2967
+ const stored = await this.storage.get(dmStorageKey);
2968
+ const parsed = stored ? parseInt(stored, 10) : NaN;
2969
+ if (Number.isFinite(parsed)) {
2970
+ dmSince = parsed;
2971
+ this.lastDmEventTs = dmSince;
2972
+ this.fallbackDmSince = null;
2973
+ logger.debug("Nostr", "DM resuming from stored timestamp:", dmSince);
2974
+ } else if (this.fallbackDmSince !== null) {
2975
+ dmSince = this.fallbackDmSince;
2976
+ this.lastDmEventTs = dmSince;
2977
+ this.fallbackDmSince = null;
2978
+ logger.debug("Nostr", "DM using fallback since timestamp:", dmSince);
2979
+ } else {
2980
+ dmSince = Math.floor(Date.now() / 1e3);
2981
+ logger.debug("Nostr", "No stored DM timestamp, starting from now:", dmSince);
2982
+ }
2983
+ } catch (err) {
2984
+ if (this.fallbackDmSince !== null) {
2985
+ dmSince = this.fallbackDmSince;
2986
+ this.lastDmEventTs = dmSince;
2987
+ this.fallbackDmSince = null;
2988
+ logger.debug("Nostr", "Storage read failed, using DM fallback since:", dmSince, err);
2989
+ } else {
2990
+ dmSince = Math.floor(Date.now() / 1e3);
2991
+ logger.debug("Nostr", "Failed to read last DM event timestamp, falling back to now:", err);
2992
+ }
2993
+ }
2994
+ } else if (this.fallbackDmSince !== null) {
2995
+ dmSince = this.fallbackDmSince;
2996
+ this.lastDmEventTs = dmSince;
2997
+ this.fallbackDmSince = null;
2998
+ logger.debug("Nostr", "No storage adapter for DM, using fallback since:", dmSince);
2999
+ } else {
3000
+ dmSince = Math.floor(Date.now() / 1e3);
3001
+ logger.debug("Nostr", "No storage adapter for DM, starting from now:", dmSince);
3002
+ }
2938
3003
  const chatFilter = new Filter();
2939
3004
  chatFilter.kinds = [EventKinds.GIFT_WRAP];
2940
3005
  chatFilter["#p"] = [nostrPubkey];
3006
+ chatFilter.since = dmSince;
2941
3007
  this.chatSubscriptionId = this.nostrClient.subscribe(chatFilter, {
2942
3008
  onEvent: (event) => {
2943
3009
  logger.debug("Nostr", "Received chat event kind:", event.kind, "id:", event.id?.slice(0, 12));