@unicitylabs/sphere-sdk 0.4.6 → 0.4.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.
@@ -8155,10 +8155,12 @@ var GroupChatModule = class {
8155
8155
  if (!this.client) return;
8156
8156
  const groupIds = Array.from(this.groups.keys());
8157
8157
  if (groupIds.length === 0) return;
8158
+ const latestTimestamp = this.getLatestMessageTimestamp(groupIds);
8158
8159
  this.trackSubscription(
8159
8160
  createNip29Filter({
8160
8161
  kinds: [NIP29_KINDS.CHAT_MESSAGE, NIP29_KINDS.THREAD_ROOT, NIP29_KINDS.THREAD_REPLY],
8161
- "#h": groupIds
8162
+ "#h": groupIds,
8163
+ ...latestTimestamp ? { since: latestTimestamp } : {}
8162
8164
  }),
8163
8165
  { onEvent: (event) => this.handleGroupEvent(event) }
8164
8166
  );
@@ -8179,10 +8181,12 @@ var GroupChatModule = class {
8179
8181
  }
8180
8182
  subscribeToGroup(groupId) {
8181
8183
  if (!this.client) return;
8184
+ const latestTimestamp = this.getLatestMessageTimestamp([groupId]);
8182
8185
  this.trackSubscription(
8183
8186
  createNip29Filter({
8184
8187
  kinds: [NIP29_KINDS.CHAT_MESSAGE, NIP29_KINDS.THREAD_ROOT, NIP29_KINDS.THREAD_REPLY],
8185
- "#h": [groupId]
8188
+ "#h": [groupId],
8189
+ ...latestTimestamp ? { since: latestTimestamp } : {}
8186
8190
  }),
8187
8191
  { onEvent: (event) => this.handleGroupEvent(event) }
8188
8192
  );
@@ -8874,6 +8878,23 @@ var GroupChatModule = class {
8874
8878
  getMyPublicKey() {
8875
8879
  return this.keyManager?.getPublicKeyHex() || null;
8876
8880
  }
8881
+ /**
8882
+ * Returns the latest message timestamp (in Nostr seconds) across the given groups,
8883
+ * or 0 if no messages exist. Used to set `since` on subscriptions so the relay
8884
+ * only sends events we don't already have.
8885
+ */
8886
+ getLatestMessageTimestamp(groupIds) {
8887
+ let latest = 0;
8888
+ for (const gid of groupIds) {
8889
+ const msgs = this.messages.get(gid);
8890
+ if (!msgs) continue;
8891
+ for (const m of msgs) {
8892
+ const ts = Math.floor(m.timestamp / 1e3);
8893
+ if (ts > latest) latest = ts;
8894
+ }
8895
+ }
8896
+ return latest;
8897
+ }
8877
8898
  // ===========================================================================
8878
8899
  // Private — Relay Admin
8879
8900
  // ===========================================================================
@@ -13405,55 +13426,44 @@ var Sphere = class _Sphere {
13405
13426
  * ```
13406
13427
  */
13407
13428
  static async importFromJSON(options) {
13429
+ const { jsonContent, password, ...baseOptions } = options;
13408
13430
  try {
13409
- const data = JSON.parse(options.jsonContent);
13431
+ const data = JSON.parse(jsonContent);
13410
13432
  if (data.version !== "1.0" || data.type !== "sphere-wallet") {
13411
13433
  return { success: false, error: "Invalid wallet format" };
13412
13434
  }
13413
13435
  let mnemonic = data.mnemonic;
13414
13436
  let masterKey = data.wallet.masterPrivateKey;
13415
- if (data.encrypted && options.password) {
13437
+ if (data.encrypted && password) {
13416
13438
  if (mnemonic) {
13417
- const decrypted = decryptSimple(mnemonic, options.password);
13439
+ const decrypted = decryptSimple(mnemonic, password);
13418
13440
  if (!decrypted) {
13419
13441
  return { success: false, error: "Failed to decrypt mnemonic - wrong password?" };
13420
13442
  }
13421
13443
  mnemonic = decrypted;
13422
13444
  }
13423
13445
  if (masterKey) {
13424
- const decrypted = decryptSimple(masterKey, options.password);
13446
+ const decrypted = decryptSimple(masterKey, password);
13425
13447
  if (!decrypted) {
13426
13448
  return { success: false, error: "Failed to decrypt master key - wrong password?" };
13427
13449
  }
13428
13450
  masterKey = decrypted;
13429
13451
  }
13430
- } else if (data.encrypted && !options.password) {
13452
+ } else if (data.encrypted && !password) {
13431
13453
  return { success: false, error: "Password required for encrypted wallet" };
13432
13454
  }
13433
13455
  const basePath = data.wallet.descriptorPath ? `m/${data.wallet.descriptorPath}` : DEFAULT_BASE_PATH;
13434
13456
  if (mnemonic) {
13435
- await _Sphere.import({
13436
- mnemonic,
13437
- basePath,
13438
- storage: options.storage,
13439
- transport: options.transport,
13440
- oracle: options.oracle,
13441
- tokenStorage: options.tokenStorage,
13442
- l1: options.l1
13443
- });
13457
+ await _Sphere.import({ ...baseOptions, mnemonic, basePath });
13444
13458
  return { success: true, mnemonic };
13445
13459
  }
13446
13460
  if (masterKey) {
13447
13461
  await _Sphere.import({
13462
+ ...baseOptions,
13448
13463
  masterKey,
13449
13464
  chainCode: data.wallet.chainCode,
13450
13465
  basePath,
13451
- derivationMode: data.derivationMode || (data.wallet.isBIP32 ? "bip32" : "wif_hmac"),
13452
- storage: options.storage,
13453
- transport: options.transport,
13454
- oracle: options.oracle,
13455
- tokenStorage: options.tokenStorage,
13456
- l1: options.l1
13466
+ derivationMode: data.derivationMode || (data.wallet.isBIP32 ? "bip32" : "wif_hmac")
13457
13467
  });
13458
13468
  return { success: true };
13459
13469
  }
@@ -13496,7 +13506,7 @@ var Sphere = class _Sphere {
13496
13506
  * ```
13497
13507
  */
13498
13508
  static async importFromLegacyFile(options) {
13499
- const { fileContent, fileName, password, onDecryptProgress } = options;
13509
+ const { fileContent, fileName, password, onDecryptProgress, ...baseOptions } = options;
13500
13510
  const fileType = _Sphere.detectLegacyFileType(fileName, fileContent);
13501
13511
  if (fileType === "unknown") {
13502
13512
  return { success: false, error: "Unknown file format" };
@@ -13506,15 +13516,7 @@ var Sphere = class _Sphere {
13506
13516
  if (!_Sphere.validateMnemonic(mnemonic)) {
13507
13517
  return { success: false, error: "Invalid mnemonic phrase" };
13508
13518
  }
13509
- const sphere = await _Sphere.import({
13510
- mnemonic,
13511
- storage: options.storage,
13512
- transport: options.transport,
13513
- oracle: options.oracle,
13514
- tokenStorage: options.tokenStorage,
13515
- nametag: options.nametag,
13516
- l1: options.l1
13517
- });
13519
+ const sphere = await _Sphere.import({ ...baseOptions, mnemonic });
13518
13520
  return { success: true, sphere, mnemonic };
13519
13521
  }
13520
13522
  if (fileType === "dat") {
@@ -13534,16 +13536,11 @@ var Sphere = class _Sphere {
13534
13536
  const { masterKey, chainCode, descriptorPath, derivationMode } = parseResult.data;
13535
13537
  const basePath = descriptorPath ? `m/${descriptorPath}` : DEFAULT_BASE_PATH;
13536
13538
  const sphere = await _Sphere.import({
13539
+ ...baseOptions,
13537
13540
  masterKey,
13538
13541
  chainCode,
13539
13542
  basePath,
13540
- derivationMode: derivationMode || (chainCode ? "bip32" : "wif_hmac"),
13541
- storage: options.storage,
13542
- transport: options.transport,
13543
- oracle: options.oracle,
13544
- tokenStorage: options.tokenStorage,
13545
- nametag: options.nametag,
13546
- l1: options.l1
13543
+ derivationMode: derivationMode || (chainCode ? "bip32" : "wif_hmac")
13547
13544
  });
13548
13545
  return { success: true, sphere };
13549
13546
  }
@@ -13566,16 +13563,11 @@ var Sphere = class _Sphere {
13566
13563
  const { masterKey, chainCode, descriptorPath, derivationMode } = parseResult.data;
13567
13564
  const basePath = descriptorPath ? `m/${descriptorPath}` : DEFAULT_BASE_PATH;
13568
13565
  const sphere = await _Sphere.import({
13566
+ ...baseOptions,
13569
13567
  masterKey,
13570
13568
  chainCode,
13571
13569
  basePath,
13572
- derivationMode: derivationMode || (chainCode ? "bip32" : "wif_hmac"),
13573
- storage: options.storage,
13574
- transport: options.transport,
13575
- oracle: options.oracle,
13576
- tokenStorage: options.tokenStorage,
13577
- nametag: options.nametag,
13578
- l1: options.l1
13570
+ derivationMode: derivationMode || (chainCode ? "bip32" : "wif_hmac")
13579
13571
  });
13580
13572
  return { success: true, sphere };
13581
13573
  }
@@ -13589,13 +13581,9 @@ var Sphere = class _Sphere {
13589
13581
  }
13590
13582
  if (parsed.type === "sphere-wallet") {
13591
13583
  const result = await _Sphere.importFromJSON({
13584
+ ...baseOptions,
13592
13585
  jsonContent: content,
13593
- password,
13594
- storage: options.storage,
13595
- transport: options.transport,
13596
- oracle: options.oracle,
13597
- tokenStorage: options.tokenStorage,
13598
- l1: options.l1
13586
+ password
13599
13587
  });
13600
13588
  if (result.success) {
13601
13589
  const sphere2 = _Sphere.getInstance();
@@ -13637,29 +13625,15 @@ var Sphere = class _Sphere {
13637
13625
  const isBIP32 = derivationMode === "bip32" || !!chainCode;
13638
13626
  const basePath = descriptorPath ? `m/${descriptorPath}` : isBIP32 ? "m/84'/1'/0'" : DEFAULT_BASE_PATH;
13639
13627
  if (mnemonic) {
13640
- const sphere2 = await _Sphere.import({
13641
- mnemonic,
13642
- basePath,
13643
- storage: options.storage,
13644
- transport: options.transport,
13645
- oracle: options.oracle,
13646
- tokenStorage: options.tokenStorage,
13647
- nametag: options.nametag,
13648
- l1: options.l1
13649
- });
13628
+ const sphere2 = await _Sphere.import({ ...baseOptions, mnemonic, basePath });
13650
13629
  return { success: true, sphere: sphere2, mnemonic };
13651
13630
  }
13652
13631
  const sphere = await _Sphere.import({
13632
+ ...baseOptions,
13653
13633
  masterKey,
13654
13634
  chainCode,
13655
13635
  basePath,
13656
- derivationMode: derivationMode || (chainCode ? "bip32" : "wif_hmac"),
13657
- storage: options.storage,
13658
- transport: options.transport,
13659
- oracle: options.oracle,
13660
- tokenStorage: options.tokenStorage,
13661
- nametag: options.nametag,
13662
- l1: options.l1
13636
+ derivationMode: derivationMode || (chainCode ? "bip32" : "wif_hmac")
13663
13637
  });
13664
13638
  return { success: true, sphere };
13665
13639
  }