- **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.
107 lines
2.9 KiB
TypeScript
107 lines
2.9 KiB
TypeScript
import type { NostrConnectState } from '@/types/nostr'
|
|
import { nostrService } from './nostr'
|
|
|
|
const NOSTRCONNECT_BRIDGE = process.env.NEXT_PUBLIC_NOSTRCONNECT_BRIDGE ?? 'https://use.nsec.app'
|
|
|
|
interface MessageData {
|
|
type?: string
|
|
method?: string
|
|
action?: string
|
|
pubkey?: string
|
|
publicKey?: string
|
|
privateKey?: string
|
|
secretKey?: string
|
|
message?: string
|
|
error?: string
|
|
params?: {
|
|
pubkey?: string
|
|
publicKey?: string
|
|
privateKey?: string
|
|
secretKey?: string
|
|
}
|
|
}
|
|
|
|
function handleConnectMessage(
|
|
data: MessageData,
|
|
onSuccess: (pubkey: string, privateKey?: string) => void,
|
|
onError: (error: Error) => void
|
|
): boolean {
|
|
const pubkey = data.pubkey ?? data.publicKey
|
|
const privateKey = data.privateKey ?? data.secretKey
|
|
|
|
if (!pubkey) {
|
|
console.error('No pubkey in message data:', data)
|
|
onError(new Error('No pubkey received'))
|
|
return false
|
|
}
|
|
nostrService.setPublicKey(pubkey)
|
|
if (privateKey) {
|
|
nostrService.setPrivateKey(privateKey)
|
|
}
|
|
|
|
onSuccess(pubkey, privateKey)
|
|
return true
|
|
}
|
|
|
|
function handleAlternativeConnectMessage(
|
|
data: MessageData,
|
|
onSuccess: (pubkey: string, privateKey?: string) => void,
|
|
onError: (error: Error) => void
|
|
): boolean {
|
|
const pubkey =
|
|
data.pubkey ?? data.publicKey ?? data.params?.pubkey ?? data.params?.publicKey
|
|
const privateKey =
|
|
data.privateKey ?? data.secretKey ?? data.params?.privateKey ?? data.params?.secretKey
|
|
|
|
if (!pubkey) {
|
|
console.error('No pubkey in message data:', data)
|
|
onError(new Error('No pubkey received'))
|
|
return false
|
|
}
|
|
nostrService.setPublicKey(pubkey)
|
|
if (privateKey) {
|
|
nostrService.setPrivateKey(privateKey)
|
|
}
|
|
|
|
onSuccess(pubkey, privateKey)
|
|
return true
|
|
}
|
|
|
|
function handleErrorMessage(data: MessageData, onError: (error: Error) => void): void {
|
|
const errorMessage = data.message ?? data.error ?? 'Connection failed'
|
|
console.error('Connection error:', errorMessage)
|
|
onError(new Error(errorMessage))
|
|
}
|
|
|
|
/**
|
|
* Handle NostrConnect connection message
|
|
*/
|
|
export function handleNostrConnectMessage(
|
|
event: MessageEvent,
|
|
_state: NostrConnectState,
|
|
onSuccess: (pubkey: string, privateKey?: string) => void,
|
|
onError: (error: Error) => void
|
|
): void {
|
|
const bridgeOrigin = new URL(NOSTRCONNECT_BRIDGE).origin
|
|
|
|
if (event.origin !== bridgeOrigin) {
|
|
console.warn('Origin mismatch:', event.origin, 'expected:', bridgeOrigin)
|
|
return
|
|
}
|
|
|
|
const data = event.data as MessageData | undefined
|
|
if (!data) {
|
|
return
|
|
}
|
|
|
|
const messageType = data.type ?? data.method ?? data.action
|
|
|
|
if (messageType === 'nostrconnect:connect' || messageType === 'connect') {
|
|
handleConnectMessage(data, onSuccess, onError)
|
|
} else if (messageType === 'nostrconnect:error' || messageType === 'error') {
|
|
handleErrorMessage(data, onError)
|
|
} else if (data.method === 'connect' || data.action === 'connect') {
|
|
handleAlternativeConnectMessage(data, onSuccess, onError)
|
|
}
|
|
}
|