story-research-zapwall/lib/nostrZapVerification.ts
Nicolas Cantu 3000872dbc refactoring
- **Motivations :** Assurer passage du lint strict et clarifier la logique paiements/publications.

- **Root causes :** Fonctions trop longues, promesses non gérées et typages WebLN/Nostr incomplets.

- **Correctifs :** Refactor PaymentModal (handlers void), extraction helpers articlePublisher, simplification polling sponsoring/zap, corrections curly et awaits.

- **Evolutions :** Nouveau module articlePublisherHelpers pour présentation/aiguillage contenu privé.

- **Page affectées :** components/PaymentModal.tsx, lib/articlePublisher.ts, lib/articlePublisherHelpers.ts, lib/paymentPolling.ts, lib/sponsoring.ts, lib/nostrZapVerification.ts et dépendances liées.
2025-12-22 17:56:00 +01:00

73 lines
1.8 KiB
TypeScript

import type { Event } from 'nostr-tools'
import { SimplePool } from 'nostr-tools'
const RELAY_URL = process.env.NEXT_PUBLIC_NOSTR_RELAY_URL ?? 'wss://relay.damus.io'
function createZapFilters(targetPubkey: string, targetEventId: string, userPubkey: string) {
return [
{
kinds: [9735], // Zap receipt
'#p': [targetPubkey],
'#e': [targetEventId],
authors: [userPubkey], // Filter by the payer's pubkey
},
]
}
async function isValidZapReceipt(
event: Event,
targetEventId: string,
targetPubkey: string,
userPubkey: string,
amount: number
): Promise<boolean> {
// Import verification service dynamically to avoid circular dependencies
const { zapVerificationService } = await import('./zapVerification')
return zapVerificationService.verifyZapReceiptForArticle(event, targetEventId, targetPubkey, userPubkey, amount)
}
/**
* Check if user has paid for an article by looking for zap receipts
*/
export function checkZapReceipt(
pool: SimplePool,
targetPubkey: string,
targetEventId: string,
amount: number,
userPubkey: string
): Promise<boolean> {
if (!pool) {
return false
}
return new Promise((resolve) => {
let resolved = false
const sub = pool.sub([RELAY_URL], createZapFilters(targetPubkey, targetEventId, userPubkey))
const finalize = (value: boolean) => {
if (resolved) {
return
}
resolved = true
sub.unsub()
resolve(value)
}
sub.on('event', (event: Event) => {
if (resolved) {
return
}
void isValidZapReceipt(event, targetEventId, targetPubkey, userPubkey, amount).then((isValid) => {
if (isValid) {
finalize(true)
}
})
})
const end = () => finalize(false)
sub.on('eose', end)
setTimeout(end, 3000)
})
}