story-research-zapwall/lib/nostrPrivateMessages.ts
Nicolas Cantu 42e3e7e692 Update all dependencies to latest versions and fix compatibility issues
**Motivations:**
- Keep dependencies up to date for security and features
- Automate dependency updates in deployment script
- Fix compatibility issues with major version updates (React 19, Next.js 16, nostr-tools 2.x)

**Root causes:**
- Dependencies were outdated
- Deployment script did not update dependencies before deploying
- Major version updates introduced breaking API changes

**Correctifs:**
- Updated all dependencies to latest versions using npm-check-updates
- Modified deploy.sh to run npm-check-updates before installing dependencies
- Fixed nostr-tools 2.x API changes (generatePrivateKey -> generateSecretKey, signEvent -> finalizeEvent, verifySignature -> verifyEvent)
- Fixed React 19 ref types to accept null
- Fixed JSX namespace issues (JSX.Element -> React.ReactElement)
- Added proper types for event callbacks
- Fixed SimplePool.sub typing issues with type assertions

**Evolutions:**
- Deployment script now automatically updates dependencies to latest versions before deploying
- All dependencies updated to latest versions (Next.js 14->16, React 18->19, nostr-tools 1->2, etc.)

**Pages affectées:**
- package.json
- deploy.sh
- lib/keyManagement.ts
- lib/nostr.ts
- lib/nostrRemoteSigner.ts
- lib/zapVerification.ts
- lib/platformTrackingEvents.ts
- lib/sponsoringTracking.ts
- lib/articlePublisherHelpersVerification.ts
- lib/contentDeliveryVerification.ts
- lib/paymentPollingZapReceipt.ts
- lib/nostrPrivateMessages.ts
- lib/nostrSubscription.ts
- lib/nostrZapVerification.ts
- lib/markdownRenderer.tsx
- components/AuthorFilter.tsx
- components/AuthorFilterButton.tsx
- components/UserArticlesList.tsx
- types/nostr-tools-extended.ts
2025-12-28 21:49:19 +01:00

147 lines
4.2 KiB
TypeScript

import { Event, nip04 } from 'nostr-tools'
import { SimplePool } from 'nostr-tools'
import type { SimplePoolWithSub } from '@/types/nostr-tools-extended'
import { decryptArticleContent, type DecryptionKey } from './articleEncryption'
import { getPrimaryRelaySync } from './config'
function createPrivateMessageFilters(eventId: string, publicKey: string, authorPubkey: string) {
return [
{
kinds: [4], // Encrypted direct messages
'#p': [publicKey],
'#e': [eventId], // Filter by event ID to find relevant private messages
authors: [authorPubkey], // Filter by author of the original article
limit: 10, // Limit to recent messages
},
]
}
function decryptContent(privateKey: string, event: Event): Promise<string | null> {
return Promise.resolve(nip04.decrypt(privateKey, event.pubkey, event.content)).then((decrypted) =>
decrypted ? decrypted : null
)
}
/**
* Get private content for an article (encrypted message from author)
* This function now returns the decryption key instead of the full content
*/
export function getPrivateContent(
pool: SimplePool,
eventId: string,
authorPubkey: string,
privateKey: string,
publicKey: string
): Promise<string | null> {
if (!privateKey || !pool || !publicKey) {
throw new Error('Private key not set or pool not initialized')
}
return new Promise((resolve) => {
let resolved = false
const relayUrl = getPrimaryRelaySync()
const sub = (pool as SimplePoolWithSub).sub([relayUrl], createPrivateMessageFilters(eventId, publicKey, authorPubkey))
const finalize = (result: string | null) => {
if (resolved) {
return
}
resolved = true
sub.unsub()
resolve(result)
}
sub.on('event', (event: Event) => {
void decryptContent(privateKey, event)
.then((content) => {
if (content) {
finalize(content)
}
})
.catch((e) => {
console.error('Error decrypting content:', e)
})
})
sub.on('eose', () => finalize(null))
setTimeout(() => finalize(null), 5000)
})
}
/**
* Get decryption key for an article from private messages
* Returns the decryption key and IV if found
*/
function parseDecryptionKey(decryptedContent: string): DecryptionKey | null {
try {
const keyData = JSON.parse(decryptedContent) as DecryptionKey
if (keyData.key && keyData.iv) {
return keyData
}
} catch {
// If parsing fails, it might be old format (full content)
}
return null
}
function handleDecryptionKeyEvent(
event: Event,
recipientPrivateKey: string,
finalize: (result: DecryptionKey | null) => void
): void {
void decryptContent(recipientPrivateKey, event)
.then((decryptedContent) => {
if (decryptedContent) {
const keyData = parseDecryptionKey(decryptedContent)
if (keyData) {
finalize(keyData)
}
}
})
.catch((e) => {
console.error('Error decrypting decryption key:', e)
})
}
export async function getDecryptionKey(
pool: SimplePool,
eventId: string,
authorPubkey: string,
recipientPrivateKey: string,
recipientPublicKey: string
): Promise<DecryptionKey | null> {
if (!recipientPrivateKey || !pool || !recipientPublicKey) {
throw new Error('Private key not set or pool not initialized')
}
return new Promise((resolve) => {
let resolved = false
const relayUrl = getPrimaryRelaySync()
const sub = (pool as SimplePoolWithSub).sub([relayUrl], createPrivateMessageFilters(eventId, recipientPublicKey, authorPubkey))
const finalize = (result: DecryptionKey | null) => {
if (resolved) {
return
}
resolved = true
sub.unsub()
resolve(result)
}
sub.on('event', (event: Event) => {
handleDecryptionKeyEvent(event, recipientPrivateKey, finalize)
})
sub.on('eose', () => finalize(null))
setTimeout(() => finalize(null), 5000)
})
}
/**
* Decrypt article content using the decryption key from private message
*/
export async function decryptArticleContentWithKey(
encryptedContent: string,
decryptionKey: DecryptionKey
): Promise<string> {
return decryptArticleContent(encryptedContent, decryptionKey.key, decryptionKey.iv)
}