story-research-zapwall/lib/contentDeliveryVerification.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

157 lines
4.1 KiB
TypeScript

import { nostrService } from './nostr'
import type { SimplePoolWithSub } from '@/types/nostr-tools-extended'
import { getPrimaryRelaySync } from './config'
import type { Event } from 'nostr-tools'
export interface ContentDeliveryStatus {
messageEventId: string | null
published: boolean
verifiedOnRelay: boolean
retrievable: boolean
error?: string
}
/**
* Verify that private content was successfully delivered to recipient
* Checks multiple aspects to ensure delivery certainty
*/
function createContentDeliveryFilters(authorPubkey: string, recipientPubkey: string, articleId: string, messageEventId: string) {
const filters: Array<{
kinds: number[]
ids?: string[]
authors: string[]
'#p': string[]
'#e': string[]
limit: number
}> = [
{
kinds: [4],
authors: [authorPubkey],
'#p': [recipientPubkey],
'#e': [articleId],
limit: 1,
},
]
if (messageEventId && filters[0]) {
filters[0].ids = [messageEventId]
}
return filters
}
function setupContentDeliveryHandlers(
sub: SimplePoolWithSub['sub'] extends (...args: any[]) => infer R ? R : never,
status: ContentDeliveryStatus,
finalize: (result: ContentDeliveryStatus) => void,
isResolved: () => boolean
): void {
sub.on('event', (event: Event) => {
status.published = true
status.verifiedOnRelay = true
status.messageEventId = event.id
status.retrievable = true
finalize(status)
})
sub.on('eose', () => {
if (!status.published) {
status.error = 'Message not found on relay'
}
finalize(status)
})
setTimeout(() => {
if (!isResolved()) {
if (!status.published) {
status.error = 'Timeout waiting for message verification'
}
finalize(status)
}
}, 5000)
}
function createContentDeliverySubscription(
pool: SimplePoolWithSub,
authorPubkey: string,
recipientPubkey: string,
articleId: string,
messageEventId: string
) {
const filters = createContentDeliveryFilters(authorPubkey, recipientPubkey, articleId, messageEventId)
const relayUrl = getPrimaryRelaySync()
return pool.sub([relayUrl], filters)
}
function createContentDeliveryPromise(
sub: SimplePoolWithSub['sub'] extends (...args: any[]) => infer R ? R : never,
status: ContentDeliveryStatus
): Promise<ContentDeliveryStatus> {
return new Promise((resolve) => {
let resolved = false
const finalize = (result: ContentDeliveryStatus) => {
if (resolved) {
return
}
resolved = true
sub.unsub()
resolve(result)
}
setupContentDeliveryHandlers(sub, status, finalize, () => resolved)
})
}
export function verifyContentDelivery(
articleId: string,
authorPubkey: string,
recipientPubkey: string,
messageEventId: string
): Promise<ContentDeliveryStatus> {
const status: ContentDeliveryStatus = {
messageEventId,
published: false,
verifiedOnRelay: false,
retrievable: false,
}
try {
const pool = nostrService.getPool()
if (!pool) {
status.error = 'Pool not initialized'
return Promise.resolve(status)
}
const poolWithSub = pool as SimplePoolWithSub
const sub = createContentDeliverySubscription(poolWithSub, authorPubkey, recipientPubkey, articleId, messageEventId)
return createContentDeliveryPromise(sub, status)
} catch (error) {
status.error = error instanceof Error ? error.message : 'Unknown error'
return Promise.resolve(status)
}
}
/**
* Check if recipient can retrieve the private message
* This verifies that the message is accessible with the recipient's key
*/
export async function verifyRecipientCanRetrieve(
articleId: string,
authorPubkey: string,
recipientPubkey: string,
messageEventId: string
): Promise<boolean> {
try {
const status = await verifyContentDelivery(articleId, authorPubkey, recipientPubkey, messageEventId)
return status.retrievable && status.verifiedOnRelay
} catch (error) {
console.error('Error verifying recipient can retrieve', {
articleId,
recipientPubkey,
messageEventId,
error: error instanceof Error ? error.message : 'Unknown error',
})
return false
}
}