2025-12-22 09:48:57 +01:00

286 lines
6.7 KiB
TypeScript

import {
Event,
EventTemplate,
getEventHash,
signEvent,
nip19,
SimplePool,
nip04
} from 'nostr-tools'
import type { Article, NostrProfile } from '@/types/nostr'
import { parseArticleFromEvent } from './nostrEventParsing'
import { getPrivateContent } from './nostrPrivateMessages'
import { checkZapReceipt as checkZapReceiptHelper } from './nostrZapVerification'
const RELAY_URL = process.env.NEXT_PUBLIC_NOSTR_RELAY_URL || 'wss://relay.damus.io'
class NostrService {
private pool: SimplePool | null = null
private privateKey: string | null = null
private publicKey: string | null = null
constructor() {
if (typeof window !== 'undefined') {
this.initializePool()
}
}
private initializePool() {
this.pool = new SimplePool()
}
setPrivateKey(privateKey: string) {
this.privateKey = privateKey
try {
const decoded = nip19.decode(privateKey)
if (decoded.type === 'nsec') {
this.privateKey = decoded.data as string
}
} catch (e) {
// Assume it's already a hex string
}
}
getPrivateKey(): string | null {
return this.privateKey
}
getPublicKey(): string | null {
return this.publicKey
}
setPublicKey(publicKey: string) {
this.publicKey = publicKey
try {
const decoded = nip19.decode(publicKey)
if (decoded.type === 'npub') {
this.publicKey = decoded.data as string
}
} catch (e) {
// Assume it's already a hex string
}
}
async publishEvent(eventTemplate: EventTemplate): Promise<Event | null> {
if (!this.privateKey || !this.pool) {
throw new Error('Private key not set or pool not initialized')
}
const event = {
...eventTemplate,
id: getEventHash(eventTemplate),
sig: signEvent(eventTemplate, this.privateKey),
} as Event
try {
const pubs = this.pool.publish([RELAY_URL], event)
await Promise.all(pubs)
return event
} catch (e) {
throw new Error(`Publish failed: ${e}`)
}
}
async subscribeToArticles(
callback: (article: Article) => void,
limit: number = 100
): Promise<() => void> {
if (typeof window === 'undefined') {
throw new Error('Cannot subscribe on server side')
}
if (!this.pool) {
this.initializePool()
}
if (!this.pool) {
throw new Error('Pool not initialized')
}
const filters = [
{
kinds: [1], // Text notes
limit,
},
]
const sub = (this.pool as any).sub([RELAY_URL], filters)
sub.on('event', (event: Event) => {
try {
const article = parseArticleFromEvent(event)
if (article) {
callback(article)
}
} catch (e) {
console.error('Error parsing article:', e)
}
})
return () => {
sub.unsub()
}
}
async getArticleById(eventId: string): Promise<Article | null> {
if (!this.pool) {
throw new Error('Pool not initialized')
}
const filters = [{ ids: [eventId], kinds: [1] }]
return subscribeWithTimeout(this.pool, filters, parseArticleFromEvent, 5000)
}
async getPrivateContent(eventId: string, authorPubkey: string): Promise<string | null> {
if (!this.privateKey || !this.pool || !this.publicKey) {
throw new Error('Private key not set or pool not initialized')
}
return new Promise(async (resolve) => {
const filters = [
{
kinds: [4], // Encrypted direct messages
'#p': [this.publicKey],
limit: 100,
},
]
let resolved = false
const sub = (this.pool as any).sub([RELAY_URL], filters)
sub.on('event', async (event: Event) => {
if (!resolved && event.tags.some((tag) => tag[0] === 'e' && tag[1] === eventId)) {
try {
// Decrypt the content using nip04
const content = await nip04.decrypt(this.privateKey!, authorPubkey, event.content)
if (content) {
resolved = true
sub.unsub()
resolve(content)
}
} catch (e) {
console.error('Error decrypting content:', e)
}
}
})
sub.on('eose', () => {
if (!resolved) {
resolved = true
sub.unsub()
resolve(null)
}
})
setTimeout(() => {
if (!resolved) {
resolved = true
sub.unsub()
resolve(null)
}
}, 5000)
})
}
async getProfile(pubkey: string): Promise<NostrProfile | null> {
if (!this.pool) {
throw new Error('Pool not initialized')
}
return new Promise((resolve) => {
const filters = [
{
kinds: [0],
authors: [pubkey],
limit: 1,
},
]
let resolved = false
const sub = (this.pool as any).sub([RELAY_URL], filters)
sub.on('event', (event: Event) => {
if (!resolved) {
resolved = true
try {
const profile = JSON.parse(event.content) as NostrProfile
profile.pubkey = pubkey
sub.unsub()
resolve(profile)
} catch (e) {
sub.unsub()
resolve(null)
}
}
})
sub.on('eose', () => {
if (!resolved) {
resolved = true
sub.unsub()
resolve(null)
}
})
setTimeout(() => {
if (!resolved) {
resolved = true
sub.unsub()
resolve(null)
}
}, 5000)
})
}
async createZapRequest(targetPubkey: string, targetEventId: string, amount: number): Promise<Event> {
if (!this.privateKey) {
throw new Error('Private key not set')
}
const zapRequest: EventTemplate = {
kind: 9734, // Zap request
created_at: Math.floor(Date.now() / 1000),
tags: [
['p', targetPubkey],
['e', targetEventId],
['amount', amount.toString()],
['relays', RELAY_URL],
],
content: '',
}
const event = await this.publishEvent(zapRequest)
if (!event) {
throw new Error('Failed to create zap request')
}
return event
}
// Check if user has paid for an article by looking for zap receipts
async checkZapReceipt(
targetPubkey: string,
targetEventId: string,
amount: number,
userPubkey?: string
): Promise<boolean> {
if (!this.publicKey || !this.pool) {
return false
}
// Use provided userPubkey or fall back to current public key
const checkPubkey = userPubkey || this.publicKey
return checkZapReceiptHelper(this.pool, targetPubkey, targetEventId, amount, checkPubkey)
}
/**
* Get the pool instance (for use by other services)
*/
getPool(): SimplePool | null {
return this.pool
}
}
export const nostrService = new NostrService()