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

158 lines
4.4 KiB
TypeScript

import { nostrService } from './nostr'
import { getPrimaryRelaySync } from './config'
import type { Event } from 'nostr-tools'
export function createMessageVerificationFilters(messageEventId: string, authorPubkey: string, recipientPubkey: string, articleId: string) {
return [
{
kinds: [4],
ids: [messageEventId],
authors: [authorPubkey],
'#p': [recipientPubkey],
'#e': [articleId],
limit: 1,
},
]
}
export function handleMessageVerificationEvent(
event: import('nostr-tools').Event,
articleId: string,
recipientPubkey: string,
authorPubkey: string,
finalize: (value: boolean) => void
): void {
console.log('Private message verified on relay', {
messageEventId: event.id,
articleId,
recipientPubkey,
authorPubkey,
timestamp: new Date().toISOString(),
})
finalize(true)
}
export function setupMessageVerificationHandlers(
sub: import('@/types/nostr-tools-extended').SimplePoolWithSub['sub'] extends (...args: any[]) => infer R ? R : never,
messageEventId: string,
articleId: string,
recipientPubkey: string,
authorPubkey: string,
finalize: (value: boolean) => void,
isResolved: () => boolean
): void {
sub.on('event', (event: Event) => {
handleMessageVerificationEvent(event, articleId, recipientPubkey, authorPubkey, finalize)
})
sub.on('eose', () => {
console.warn('Private message not found on relay after EOSE', {
messageEventId,
articleId,
recipientPubkey,
timestamp: new Date().toISOString(),
})
finalize(false)
})
setTimeout(() => {
if (!isResolved()) {
console.warn('Timeout verifying private message on relay', {
messageEventId,
articleId,
recipientPubkey,
timestamp: new Date().toISOString(),
})
finalize(false)
}
}, 5000)
}
function createMessageVerificationSubscription(
pool: import('@/types/nostr-tools-extended').SimplePoolWithSub,
messageEventId: string,
authorPubkey: string,
recipientPubkey: string,
articleId: string
) {
const filters = createMessageVerificationFilters(messageEventId, authorPubkey, recipientPubkey, articleId)
const relayUrl = getPrimaryRelaySync()
return pool.sub([relayUrl], filters)
}
function createVerificationPromise(
sub: import('@/types/nostr-tools-extended').SimplePoolWithSub['sub'] extends (...args: any[]) => infer R ? R : never,
messageEventId: string,
articleId: string,
recipientPubkey: string,
authorPubkey: string
): Promise<boolean> {
return new Promise((resolve) => {
let resolved = false
const finalize = (value: boolean) => {
if (resolved) {
return
}
resolved = true
sub.unsub()
resolve(value)
}
setupMessageVerificationHandlers(sub, messageEventId, articleId, recipientPubkey, authorPubkey, finalize, () => resolved)
})
}
export function verifyPrivateMessagePublished(
messageEventId: string,
authorPubkey: string,
recipientPubkey: string,
articleId: string
): Promise<boolean> {
try {
const pool = nostrService.getPool()
if (!pool) {
console.error('Pool not initialized for message verification', {
messageEventId,
articleId,
recipientPubkey,
})
return Promise.resolve(false)
}
const sub = createMessageVerificationSubscription(
pool as import('@/types/nostr-tools-extended').SimplePoolWithSub,
messageEventId,
authorPubkey,
recipientPubkey,
articleId
)
return createVerificationPromise(sub, messageEventId, articleId, recipientPubkey, authorPubkey)
} catch (error) {
console.error('Error verifying private message', {
messageEventId,
articleId,
recipientPubkey,
error: error instanceof Error ? error.message : 'Unknown error',
timestamp: new Date().toISOString(),
})
return Promise.resolve(false)
}
}
export async function publishAndVerifyMessage(
articleId: string,
recipientPubkey: string,
authorPubkey: string,
messageEventId: string
): Promise<boolean> {
const verified = await verifyPrivateMessagePublished(messageEventId, authorPubkey, recipientPubkey, articleId)
if (verified) {
console.log('Private message verified on relay', { messageEventId, articleId, recipientPubkey })
} else {
console.warn('Private message published but not yet verified on relay', { messageEventId, articleId, recipientPubkey })
}
return verified
}