@bsv/wallet-toolbox 1.6.17 → 1.6.19

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bsv/wallet-toolbox",
3
- "version": "1.6.17",
3
+ "version": "1.6.19",
4
4
  "description": "BRC100 conforming wallet, wallet storage and wallet signer components",
5
5
  "main": "./out/src/index.js",
6
6
  "types": "./out/src/index.d.ts",
package/src/Wallet.ts CHANGED
@@ -615,6 +615,15 @@ export class Wallet implements WalletInterface, ProtoWallet {
615
615
  return r
616
616
  }
617
617
 
618
+ /** 2-minute cache of trust settings for identity resolution paths */
619
+ private _trustSettingsCache?: {
620
+ expiresAt: number
621
+ trustSettings: Awaited<ReturnType<WalletSettingsManager['get']>>['trustSettings']
622
+ }
623
+
624
+ /** 2-minute cache of queryOverlay() results keyed by normalized query */
625
+ private _overlayCache: Map<string, { expiresAt: number; value: unknown }> = new Map()
626
+
618
627
  async discoverByIdentityKey(
619
628
  args: DiscoverByIdentityKeyArgs,
620
629
  originator?: OriginatorDomainNameStringUnder250Bytes
@@ -622,21 +631,42 @@ export class Wallet implements WalletInterface, ProtoWallet {
622
631
  validateOriginator(originator)
623
632
  this.validateAuthAndArgs(args, validateDiscoverByIdentityKeyArgs)
624
633
 
625
- const trustSettings = (await this.settingsManager.get()).trustSettings
626
- const results = await queryOverlay(
627
- {
628
- identityKey: args.identityKey,
629
- certifiers: trustSettings.trustedCertifiers.map(certifier => certifier.identityKey)
630
- },
631
- this.lookupResolver
632
- )
633
- if (!results) {
634
- return {
635
- totalCertificates: 0,
636
- certificates: []
637
- }
634
+ const TTL_MS = 2 * 60 * 1000
635
+ const now = Date.now()
636
+
637
+ // --- trustSettings cache (2 minutes) ---
638
+ let trustSettings =
639
+ this._trustSettingsCache && this._trustSettingsCache.expiresAt > now
640
+ ? this._trustSettingsCache.trustSettings
641
+ : undefined
642
+
643
+ if (!trustSettings) {
644
+ const settings = await this.settingsManager.get()
645
+ trustSettings = settings.trustSettings
646
+ this._trustSettingsCache = { trustSettings, expiresAt: now + TTL_MS }
638
647
  }
639
- return transformVerifiableCertificatesWithTrust(trustSettings, results)
648
+
649
+ const certifiers = trustSettings.trustedCertifiers.map(c => c.identityKey).sort()
650
+
651
+ // --- queryOverlay cache (2 minutes) ---
652
+ const cacheKey = JSON.stringify({
653
+ fn: 'discoverByIdentityKey',
654
+ identityKey: args.identityKey,
655
+ certifiers
656
+ })
657
+
658
+ let cached = this._overlayCache.get(cacheKey)
659
+ if (!cached || cached.expiresAt <= now) {
660
+ const value = await queryOverlay({ identityKey: args.identityKey, certifiers }, this.lookupResolver)
661
+ cached = { value, expiresAt: now + TTL_MS }
662
+ this._overlayCache.set(cacheKey, cached)
663
+ }
664
+
665
+ if (!cached.value) {
666
+ return { totalCertificates: 0, certificates: [] }
667
+ }
668
+
669
+ return transformVerifiableCertificatesWithTrust(trustSettings, cached.value as any)
640
670
  }
641
671
 
642
672
  async discoverByAttributes(
@@ -646,21 +676,50 @@ export class Wallet implements WalletInterface, ProtoWallet {
646
676
  validateOriginator(originator)
647
677
  this.validateAuthAndArgs(args, validateDiscoverByAttributesArgs)
648
678
 
649
- const trustSettings = (await this.settingsManager.get()).trustSettings
650
- const results = await queryOverlay(
651
- {
652
- attributes: args.attributes,
653
- certifiers: trustSettings.trustedCertifiers.map(certifier => certifier.identityKey)
654
- },
655
- this.lookupResolver
656
- )
657
- if (!results) {
658
- return {
659
- totalCertificates: 0,
660
- certificates: []
661
- }
679
+ const TTL_MS = 2 * 60 * 1000
680
+ const now = Date.now()
681
+
682
+ // --- trustSettings cache (2 minutes) ---
683
+ let trustSettings =
684
+ this._trustSettingsCache && this._trustSettingsCache.expiresAt > now
685
+ ? this._trustSettingsCache.trustSettings
686
+ : undefined
687
+
688
+ if (!trustSettings) {
689
+ const settings = await this.settingsManager.get()
690
+ trustSettings = settings.trustSettings
691
+ this._trustSettingsCache = { trustSettings, expiresAt: now + TTL_MS }
692
+ }
693
+
694
+ const certifiers = trustSettings.trustedCertifiers.map(c => c.identityKey).sort()
695
+
696
+ // Normalize attributes for a stable cache key.
697
+ // If attributes is an object, sort its top-level keys; if it's an array, sort a shallow copy.
698
+ let attributesKey: unknown = args.attributes
699
+ if (args.attributes && typeof args.attributes === 'object') {
700
+ const keys = Object.keys(args.attributes as Record<string, unknown>).sort()
701
+ attributesKey = JSON.stringify(args.attributes, keys)
662
702
  }
663
- return transformVerifiableCertificatesWithTrust(trustSettings, results)
703
+
704
+ // --- queryOverlay cache (2 minutes) ---
705
+ const cacheKey = JSON.stringify({
706
+ fn: 'discoverByAttributes',
707
+ attributes: attributesKey,
708
+ certifiers
709
+ })
710
+
711
+ let cached = this._overlayCache.get(cacheKey)
712
+ if (!cached || cached.expiresAt <= now) {
713
+ const value = await queryOverlay({ attributes: args.attributes, certifiers }, this.lookupResolver)
714
+ cached = { value, expiresAt: now + TTL_MS }
715
+ this._overlayCache.set(cacheKey, cached)
716
+ }
717
+
718
+ if (!cached.value) {
719
+ return { totalCertificates: 0, certificates: [] }
720
+ }
721
+
722
+ return transformVerifiableCertificatesWithTrust(trustSettings, cached.value as any)
664
723
  }
665
724
 
666
725
  verifyReturnedTxidOnly(beef: Beef, knownTxids?: string[]): Beef {
@@ -349,6 +349,9 @@ export class Chaintracks implements ChaintracksManagementApi {
349
349
  } catch (eu: unknown) {
350
350
  const e = (bulkSyncError = WalletError.fromUnknown(eu))
351
351
  this.log(`bulk sync error: ${e.message}`)
352
+ if (!this.available)
353
+ // During initial startup, bulk ingestors must be available.
354
+ break
352
355
  }
353
356
  }
354
357
  if (!bulkDone && !this.available && bulkSyncError) {