Files
bitvid/js/nostr/decryptionSchemeCache.js
Claude 8bf8da03b9 Speed up Nostr login and fix list sync, signer persistence, and feed registration
The NIP-07 login flow was extremely slow because list decryption (blocks,
subscriptions, hashtag preferences) started before the signer was ready.
On page refresh, all three services failed with "permission-required" and
scheduled 3-second retry delays, adding 6-10 seconds to every login.

Key fixes:
- List loading now waits for the permission pre-grant to complete before
  attempting decryption, ensuring the signer is available on first try
- Extension wait timeout increased from 3s to 5s for slower extensions
- Decrypt timeouts reduced from 20s to 10s (signer is guaranteed ready)
- Auto-login now forwards authType/providerId from saved profiles
- Storage tab unlock falls back to ensureActiveSignerForPubkey when the
  global signer is not yet registered
- Watch history feed registration adds defensive re-registration and
  try/catch fallback to prevent "Feed not registered" errors
- Decryption scheme cache TTL extended from 10 minutes to 2 hours
- ensureExtensionPermissions waits for extension injection when there
  are outstanding permissions to request (was returning immediately)

https://claude.ai/code/session_01T4wwPu7m5qp1Nxx3H2v57j
2026-02-08 02:02:51 +00:00

71 lines
2.0 KiB
JavaScript

// js/nostr/decryptionSchemeCache.js
/**
* Shared decryption scheme cache across all list services (blocks,
* subscriptions, hashtag preferences). When one service discovers the
* correct decryption scheme for a pubkey, the others can reuse it
* immediately instead of independently probing the NIP-07 extension.
*
* This eliminates redundant extension decrypt calls during the critical
* login path where all three services decrypt simultaneously.
*/
// Extended from 10 minutes to 2 hours. The cache is cleared on logout, so
// there's no privacy concern with a longer TTL. A short TTL caused redundant
// scheme probing during normal browsing sessions, triggering unnecessary
// NIP-07 extension calls that slow down background list re-syncs.
const DECRYPTION_CACHE_TTL_MS = 2 * 60 * 60 * 1000; // 2 hours
/** @type {Map<string, { scheme: string, timestamp: number }>} */
const cache = new Map();
/**
* Retrieve the last successfully-used decryption scheme for a pubkey.
* Returns null if the entry is missing or expired.
*
* @param {string} pubkey - Hex pubkey to look up.
* @returns {string|null} The scheme identifier (e.g. "nip44_v2") or null.
*/
export function getLastSuccessfulScheme(pubkey) {
if (typeof pubkey !== "string" || !pubkey) {
return null;
}
const entry = cache.get(pubkey);
if (!entry) {
return null;
}
if (Date.now() - entry.timestamp > DECRYPTION_CACHE_TTL_MS) {
cache.delete(pubkey);
return null;
}
return entry.scheme;
}
/**
* Record a successful decryption scheme for a pubkey so other services
* can skip scheme discovery.
*
* @param {string} pubkey - Hex pubkey.
* @param {string} scheme - The scheme that succeeded (e.g. "nip44_v2").
*/
export function setLastSuccessfulScheme(pubkey, scheme) {
if (typeof pubkey !== "string" || !pubkey) {
return;
}
if (typeof scheme !== "string" || !scheme) {
return;
}
cache.set(pubkey, { scheme, timestamp: Date.now() });
}
/**
* Clear all cached entries. Useful on logout.
*/
export function clearDecryptionSchemeCache() {
cache.clear();
}