**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
85 lines
2.4 KiB
TypeScript
85 lines
2.4 KiB
TypeScript
import { nostrService } from './nostr'
|
|
import { getPrimaryRelaySync } from './config'
|
|
import type { Event } from 'nostr-tools'
|
|
|
|
export function parseZapAmount(event: import('nostr-tools').Event): number {
|
|
const amountTag = event.tags.find((tag) => tag[0] === 'amount')?.[1]
|
|
return amountTag ? Math.floor(parseInt(amountTag, 10) / 1000) : 0
|
|
}
|
|
|
|
export function createZapReceiptSubscription(poolWithSub: import('@/types/nostr-tools-extended').SimplePoolWithSub, articlePubkey: string, articleId: string) {
|
|
const filters = [
|
|
{
|
|
kinds: [9735],
|
|
'#p': [articlePubkey],
|
|
'#e': [articleId],
|
|
limit: 1,
|
|
},
|
|
]
|
|
const relayUrl = getPrimaryRelaySync()
|
|
return poolWithSub.sub([relayUrl], filters)
|
|
}
|
|
|
|
export function handleZapReceiptEvent(
|
|
event: import('nostr-tools').Event,
|
|
amount: number,
|
|
recipientPubkey: string,
|
|
finalize: (value: string | undefined) => void
|
|
): void {
|
|
const amountInSats = parseZapAmount(event)
|
|
if (amountInSats === amount && event.pubkey === recipientPubkey) {
|
|
finalize(event.id)
|
|
}
|
|
}
|
|
|
|
export function createZapReceiptPromise(
|
|
sub: import('@/types/nostr-tools-extended').SimplePoolWithSub['sub'] extends (...args: any[]) => infer R ? R : never,
|
|
amount: number,
|
|
recipientPubkey: string
|
|
): Promise<string | undefined> {
|
|
return new Promise((resolve) => {
|
|
let resolved = false
|
|
|
|
const finalize = (value: string | undefined) => {
|
|
if (resolved) {
|
|
return
|
|
}
|
|
resolved = true
|
|
sub.unsub()
|
|
resolve(value)
|
|
}
|
|
|
|
sub.on('event', (event: Event) => {
|
|
handleZapReceiptEvent(event, amount, recipientPubkey, finalize)
|
|
})
|
|
|
|
sub.on('eose', () => finalize(undefined))
|
|
setTimeout(() => finalize(undefined), 3000)
|
|
})
|
|
}
|
|
|
|
export async function getZapReceiptId(
|
|
articlePubkey: string,
|
|
articleId: string,
|
|
amount: number,
|
|
recipientPubkey: string
|
|
): Promise<string | undefined> {
|
|
try {
|
|
const pool = nostrService.getPool()
|
|
if (!pool) {
|
|
return undefined
|
|
}
|
|
|
|
const poolWithSub = pool as import('@/types/nostr-tools-extended').SimplePoolWithSub
|
|
const sub = createZapReceiptSubscription(poolWithSub, articlePubkey, articleId)
|
|
return createZapReceiptPromise(sub, amount, recipientPubkey)
|
|
} catch (error) {
|
|
console.error('Error getting zap receipt ID', {
|
|
articleId,
|
|
recipientPubkey,
|
|
error: error instanceof Error ? error.message : 'Unknown error',
|
|
})
|
|
return undefined
|
|
}
|
|
}
|