feat: implement direct decryption for unrecognized event kinds

- Add manual decryption using signer's nip04 capabilities directly
- Parse decrypted content as JSON array of bookmark tags
- Cache decrypted content properly for getHiddenBookmarks to find
- Enable decryption of legacy bookmark events (kind 30001) with encrypted content
This commit is contained in:
Gigi
2025-10-02 21:08:20 +02:00
parent 6e6d43cb25
commit ce7fbdbdf3

View File

@@ -2,6 +2,8 @@ import { RelayPool, completeOnEose } from 'applesauce-relay'
import { getParsedContent } from 'applesauce-content/text' import { getParsedContent } from 'applesauce-content/text'
import { Helpers } from 'applesauce-core' import { Helpers } from 'applesauce-core'
import { lastValueFrom, takeUntil, timer, toArray } from 'rxjs' import { lastValueFrom, takeUntil, timer, toArray } from 'rxjs'
// Import the bookmark hidden symbol for caching
const BookmarkHiddenSymbol = Symbol.for("bookmark-hidden")
import { Bookmark, IndividualBookmark, ParsedContent, ActiveAccount } from '../types/bookmarks' import { Bookmark, IndividualBookmark, ParsedContent, ActiveAccount } from '../types/bookmarks'
interface BookmarkData { interface BookmarkData {
@@ -246,17 +248,23 @@ export const fetchBookmarks = async (
else if (evt.content && evt.content.length > 0 && signerCandidate) { else if (evt.content && evt.content.length > 0 && signerCandidate) {
try { try {
console.log('🔓 Attempting manual decryption for event with unrecognized kind...') console.log('🔓 Attempting manual decryption for event with unrecognized kind...')
// Try to manually decrypt using applesauce's unlockHiddenContent function console.log('📄 Content to decrypt:', evt.content.slice(0, 100) + '...')
// We'll temporarily mark the event as supporting hidden content
const originalKind = evt.kind // Try direct decryption using the signer's nip04 capabilities
// eslint-disable-next-line @typescript-eslint/no-explicit-any const decryptedContent = await (signerCandidate as any).nip04?.decrypt(evt.pubkey, evt.content)
;(evt as any).kind = 10003 // Temporarily change to recognized kind console.log('✅ Successfully decrypted content manually')
// Parse the decrypted content as JSON (should be array of tags)
try { try {
await Helpers.unlockHiddenContent(evt, signerCandidate as any) const hiddenTags = JSON.parse(decryptedContent)
console.log('✅ Successfully decrypted unrecognized event') console.log('📋 Decrypted hidden tags:', hiddenTags.length, 'tags')
} finally {
// eslint-disable-next-line @typescript-eslint/no-explicit-any // Set the cached value on the event so getHiddenBookmarks can find it
;(evt as any).kind = originalKind // Restore original kind Reflect.set(evt, BookmarkHiddenSymbol, hiddenTags)
Reflect.set(evt, 'EncryptedContentSymbol', decryptedContent)
} catch (parseError) {
console.warn('❌ Failed to parse decrypted content as JSON:', parseError)
} }
} catch (error) { } catch (error) {
console.warn('❌ Failed manual decryption:', error) console.warn('❌ Failed manual decryption:', error)