better socket

This commit is contained in:
boufni95 2023-12-08 20:39:34 +01:00
parent 2705d6957e
commit a22bec7450

View file

@ -84,46 +84,35 @@ export default class Handler {
constructor(settings: NostrSettings, eventCallback: (event: NostrEvent) => void) { constructor(settings: NostrSettings, eventCallback: (event: NostrEvent) => void) {
this.settings = settings this.settings = settings
console.log(settings) console.log(settings)
const apps: Record<string, AppInfo> = {}
this.settings.apps.forEach(app => { this.settings.apps.forEach(app => {
this.SubForApp(app, eventCallback) apps[app.publicKey] = app
}) })
} const sub = this.pool.sub(this.settings.relays, [
async SubForApp(appInfo: AppInfo, eventCallback: (event: NostrEvent) => void) {
const relay = relayInit(this.settings.relays[0])
relay.on('connect', () => {
console.log(`connected to ${relay.url}`)
})
relay.on('error', () => {
console.log(`failed to connect to ${relay.url}`)
})
await relay.connect()
console.log("relay connected")
const sub = relay.sub([
{ {
since: Math.ceil(Date.now() / 1000), since: Math.ceil(Date.now() / 1000),
kinds: [21000], kinds: [21000],
'#p': [appInfo.publicKey], '#p': Object.keys(apps),
} }
]) ])
sub.on("event", async (e) => { sub.on('event', async (e) => {
if (e.kind !== 21000 || !e.pubkey) { if (e.kind !== 21000 || !e.pubkey) {
return return
} }
//@ts-ignore const app = apps[e.pubkey]
const eventId = e.id as string if (!app) {
return
}
const eventId = e.id
if (handledEvents.includes(eventId)) { if (handledEvents.includes(eventId)) {
console.log("event already handled") console.log("event already handled")
return return
} }
handledEvents.push(eventId) handledEvents.push(eventId)
const decoded = decodePayload(e.content) const decoded = decodePayload(e.content)
const content = await decryptData(decoded, getSharedSecret(appInfo.privateKey, e.pubkey)) const content = await decryptData(decoded, getSharedSecret(app.privateKey, e.pubkey))
eventCallback({ id: eventId, content, pub: e.pubkey, appId: appInfo.appId }) eventCallback({ id: eventId, content, pub: e.pubkey, appId: app.appId })
//eventCallback({ id: eventId, content: await nip04.decrypt(appInfo.privateKey, e.pubkey, e.content), pub: e.pubkey, appId: appInfo.appId })
}) })
this.subs.push(sub)
} }
async Send(appId: string, data: SendData, relays?: string[]) { async Send(appId: string, data: SendData, relays?: string[]) {