@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
@@ -77,6 +77,8 @@ var STORAGE_KEYS_GLOBAL = {
77
77
  TRACKED_ADDRESSES: "tracked_addresses",
78
78
  /** Last processed Nostr wallet event timestamp (unix seconds), keyed per pubkey */
79
79
  LAST_WALLET_EVENT_TS: "last_wallet_event_ts",
80
+ /** Last processed Nostr DM (gift-wrap) event timestamp (unix seconds), keyed per pubkey */
81
+ LAST_DM_EVENT_TS: "last_dm_event_ts",
80
82
  /** Group chat: last used relay URL (stale data detection) — global, same relay for all addresses */
81
83
  GROUP_CHAT_RELAY_URL: "group_chat_relay_url",
82
84
  /** Cached token registry JSON (fetched from remote) */
@@ -1124,8 +1126,12 @@ var NostrTransportProvider = class _NostrTransportProvider {
1124
1126
  storage = null;
1125
1127
  /** In-memory max event timestamp to avoid read-before-write races in updateLastEventTimestamp. */
1126
1128
  lastEventTs = 0;
1129
+ /** In-memory max DM (gift-wrap) event timestamp. */
1130
+ lastDmEventTs = 0;
1127
1131
  /** Fallback 'since' timestamp for first-time address subscriptions (consumed once). */
1128
1132
  fallbackSince = null;
1133
+ /** Fallback 'since' timestamp for DM (gift-wrap) subscriptions (consumed once). */
1134
+ fallbackDmSince = null;
1129
1135
  identity = null;
1130
1136
  keyManager = null;
1131
1137
  status = "disconnected";
@@ -1380,6 +1386,8 @@ var NostrTransportProvider = class _NostrTransportProvider {
1380
1386
  this.identity = identity;
1381
1387
  this.processedEventIds.clear();
1382
1388
  this.lastEventTs = 0;
1389
+ this.lastDmEventTs = 0;
1390
+ this.fallbackDmSince = null;
1383
1391
  const secretKey = import_buffer.Buffer.from(identity.privateKey, "hex");
1384
1392
  this.keyManager = import_nostr_js_sdk.NostrKeyManager.fromPrivateKey(secretKey);
1385
1393
  const nostrPubkey = this.keyManager.getPublicKeyHex();
@@ -1425,6 +1433,9 @@ var NostrTransportProvider = class _NostrTransportProvider {
1425
1433
  setFallbackSince(sinceSeconds) {
1426
1434
  this.fallbackSince = sinceSeconds;
1427
1435
  }
1436
+ setFallbackDmSince(sinceSeconds) {
1437
+ this.fallbackDmSince = sinceSeconds;
1438
+ }
1428
1439
  /**
1429
1440
  * Get the Nostr-format public key (32 bytes / 64 hex chars)
1430
1441
  * This is the x-coordinate only, without the 02/03 prefix.
@@ -1959,6 +1970,17 @@ var NostrTransportProvider = class _NostrTransportProvider {
1959
1970
  logger.debug("Nostr", "Failed to save last event timestamp:", err);
1960
1971
  });
1961
1972
  }
1973
+ /** Persist the max DM (gift-wrap) event timestamp for the since filter on next connect. */
1974
+ updateLastDmEventTimestamp(createdAt) {
1975
+ if (!this.storage || !this.keyManager) return;
1976
+ if (createdAt <= this.lastDmEventTs) return;
1977
+ this.lastDmEventTs = createdAt;
1978
+ const pubkey = this.keyManager.getPublicKeyHex();
1979
+ const storageKey = `${STORAGE_KEYS_GLOBAL.LAST_DM_EVENT_TS}_${pubkey.slice(0, 16)}`;
1980
+ this.storage.set(storageKey, createdAt.toString()).catch((err) => {
1981
+ logger.debug("Nostr", "Failed to save last DM event timestamp:", err);
1982
+ });
1983
+ }
1962
1984
  async handleDirectMessage(event) {
1963
1985
  logger.debug("Nostr", "Ignoring NIP-04 kind 4 event (DMs use NIP-17):", event.id?.slice(0, 12));
1964
1986
  }
@@ -1969,6 +1991,9 @@ var NostrTransportProvider = class _NostrTransportProvider {
1969
1991
  }
1970
1992
  try {
1971
1993
  const pm = import_nostr_js_sdk.NIP17.unwrap(event, this.keyManager);
1994
+ if (event.created_at) {
1995
+ this.updateLastDmEventTimestamp(event.created_at);
1996
+ }
1972
1997
  logger.debug("Nostr", "Gift wrap unwrapped, sender:", pm.senderPubkey?.slice(0, 16), "kind:", pm.kind);
1973
1998
  if (pm.senderPubkey === this.keyManager.getPublicKeyHex()) {
1974
1999
  try {
@@ -2415,9 +2440,50 @@ var NostrTransportProvider = class _NostrTransportProvider {
2415
2440
  }
2416
2441
  });
2417
2442
  logger.debug("Nostr", "Wallet subscription created, subId:", this.walletSubscriptionId);
2443
+ let dmSince;
2444
+ if (this.storage) {
2445
+ const dmStorageKey = `${STORAGE_KEYS_GLOBAL.LAST_DM_EVENT_TS}_${nostrPubkey.slice(0, 16)}`;
2446
+ try {
2447
+ const stored = await this.storage.get(dmStorageKey);
2448
+ const parsed = stored ? parseInt(stored, 10) : NaN;
2449
+ if (Number.isFinite(parsed)) {
2450
+ dmSince = parsed;
2451
+ this.lastDmEventTs = dmSince;
2452
+ this.fallbackDmSince = null;
2453
+ logger.debug("Nostr", "DM resuming from stored timestamp:", dmSince);
2454
+ } else if (this.fallbackDmSince !== null) {
2455
+ dmSince = this.fallbackDmSince;
2456
+ this.lastDmEventTs = dmSince;
2457
+ this.fallbackDmSince = null;
2458
+ logger.debug("Nostr", "DM using fallback since timestamp:", dmSince);
2459
+ } else {
2460
+ dmSince = Math.floor(Date.now() / 1e3);
2461
+ logger.debug("Nostr", "No stored DM timestamp, starting from now:", dmSince);
2462
+ }
2463
+ } catch (err) {
2464
+ if (this.fallbackDmSince !== null) {
2465
+ dmSince = this.fallbackDmSince;
2466
+ this.lastDmEventTs = dmSince;
2467
+ this.fallbackDmSince = null;
2468
+ logger.debug("Nostr", "Storage read failed, using DM fallback since:", dmSince, err);
2469
+ } else {
2470
+ dmSince = Math.floor(Date.now() / 1e3);
2471
+ logger.debug("Nostr", "Failed to read last DM event timestamp, falling back to now:", err);
2472
+ }
2473
+ }
2474
+ } else if (this.fallbackDmSince !== null) {
2475
+ dmSince = this.fallbackDmSince;
2476
+ this.lastDmEventTs = dmSince;
2477
+ this.fallbackDmSince = null;
2478
+ logger.debug("Nostr", "No storage adapter for DM, using fallback since:", dmSince);
2479
+ } else {
2480
+ dmSince = Math.floor(Date.now() / 1e3);
2481
+ logger.debug("Nostr", "No storage adapter for DM, starting from now:", dmSince);
2482
+ }
2418
2483
  const chatFilter = new import_nostr_js_sdk.Filter();
2419
2484
  chatFilter.kinds = [import_nostr_js_sdk.EventKinds.GIFT_WRAP];
2420
2485
  chatFilter["#p"] = [nostrPubkey];
2486
+ chatFilter.since = dmSince;
2421
2487
  this.chatSubscriptionId = this.nostrClient.subscribe(chatFilter, {
2422
2488
  onEvent: (event) => {
2423
2489
  logger.debug("Nostr", "Received chat event kind:", event.kind, "id:", event.id?.slice(0, 12));