story-research-zapwall/lib/sponsoringTracking.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
5.0 KiB
TypeScript

import { Event, EventTemplate, finalizeEvent } from 'nostr-tools'
import { hexToBytes } from 'nostr-tools/utils'
import { nostrService } from './nostr'
import { PLATFORM_NPUB } from './platformConfig'
import type { SimplePoolWithSub } from '@/types/nostr-tools-extended'
import { getPrimaryRelaySync } from './config'
export interface SponsoringTracking {
transactionId: string
authorPubkey: string
authorMainnetAddress: string
amount: number
authorAmount: number
platformCommission: number
timestamp: number
confirmed: boolean
confirmations: number
}
/**
* Sponsoring tracking service
* Publishes tracking events on Nostr for sponsoring payments
*/
export class SponsoringTrackingService {
private readonly platformPubkey: string = PLATFORM_NPUB
private readonly trackingKind = 30079 // Custom kind for sponsoring tracking
/**
* Track sponsoring payment
* Publishes event on Nostr with commission information
*/
private buildSponsoringTrackingTags(tracking: SponsoringTracking): string[][] {
return [
['p', this.platformPubkey],
['transaction', tracking.transactionId],
['author', tracking.authorPubkey],
['author_address', tracking.authorMainnetAddress],
['amount', tracking.amount.toString()],
['author_amount', tracking.authorAmount.toString()],
['platform_commission', tracking.platformCommission.toString()],
['confirmed', tracking.confirmed ? 'true' : 'false'],
['confirmations', tracking.confirmations.toString()],
['timestamp', tracking.timestamp.toString()],
]
}
private buildSponsoringTrackingEvent(
tracking: SponsoringTracking,
_authorPubkey: string,
authorPrivateKey: string
): Event {
const eventTemplate: EventTemplate = {
kind: this.trackingKind,
created_at: Math.floor(Date.now() / 1000),
tags: this.buildSponsoringTrackingTags(tracking),
content: JSON.stringify({
transactionId: tracking.transactionId,
authorPubkey: tracking.authorPubkey,
authorMainnetAddress: tracking.authorMainnetAddress,
amount: tracking.amount,
authorAmount: tracking.authorAmount,
platformCommission: tracking.platformCommission,
confirmed: tracking.confirmed,
confirmations: tracking.confirmations,
timestamp: tracking.timestamp,
}),
}
const secretKey = hexToBytes(authorPrivateKey)
return finalizeEvent(eventTemplate, secretKey)
}
private async publishSponsoringTrackingEvent(event: Event): Promise<void> {
const pool = nostrService.getPool()
if (!pool) {
throw new Error('Pool not initialized')
}
const poolWithSub = pool as SimplePoolWithSub
const relayUrl = getPrimaryRelaySync()
const pubs = poolWithSub.publish([relayUrl], event)
await Promise.all(pubs)
}
async trackSponsoringPayment(
tracking: SponsoringTracking,
authorPrivateKey: string
): Promise<string | null> {
try {
const pool = nostrService.getPool()
if (!pool) {
console.error('Pool not initialized for sponsoring tracking')
return null
}
const authorPubkey = nostrService.getPublicKey()
if (!authorPubkey) {
console.error('Author public key not available for tracking')
return null
}
const event = this.buildSponsoringTrackingEvent(tracking, authorPubkey, authorPrivateKey)
await this.publishSponsoringTrackingEvent(event)
console.log('Sponsoring payment tracked', {
eventId: event.id,
transactionId: tracking.transactionId,
authorPubkey: tracking.authorPubkey,
authorAmount: tracking.authorAmount,
platformCommission: tracking.platformCommission,
timestamp: new Date().toISOString(),
})
return event.id
} catch (error) {
console.error('Error tracking sponsoring payment', {
transactionId: tracking.transactionId,
authorPubkey: tracking.authorPubkey,
error: error instanceof Error ? error.message : 'Unknown error',
timestamp: new Date().toISOString(),
})
return null
}
}
/**
* Update author presentation article with new total sponsoring
*/
async updatePresentationSponsoring(
presentationArticleId: string,
newTotalSponsoring: number,
_authorPrivateKey: string
): Promise<boolean> {
try {
// In production, this would:
// 1. Fetch the presentation article
// 2. Update the total_sponsoring tag
// 3. Publish updated event
console.log('Presentation sponsoring updated', {
presentationArticleId,
newTotalSponsoring,
timestamp: new Date().toISOString(),
})
return true
} catch (error) {
console.error('Error updating presentation sponsoring', {
presentationArticleId,
error: error instanceof Error ? error.message : 'Unknown error',
timestamp: new Date().toISOString(),
})
return false
}
}
}
export const sponsoringTrackingService = new SponsoringTrackingService()