From 2ed1269eb5bf231edcc2cdcc839156521cd9f249 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ant=C3=B3nio=20Conselheiro?= Date: Sun, 12 Nov 2023 16:05:14 -0300 Subject: [PATCH] =?UTF-8?q?mantendo=20a=20conex=C3=A3o=20ativa?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app/shared/nostr-api/nostr-api.module.ts | 6 ++- src/app/shared/nostr-api/nostr.service.ts | 40 +++++++++++++++----- src/app/shared/omegle/omegle.nostr.ts | 11 ------ 3 files changed, 34 insertions(+), 23 deletions(-) diff --git a/src/app/shared/nostr-api/nostr-api.module.ts b/src/app/shared/nostr-api/nostr-api.module.ts index 0aa0bbe..25763ca 100644 --- a/src/app/shared/nostr-api/nostr-api.module.ts +++ b/src/app/shared/nostr-api/nostr-api.module.ts @@ -1,5 +1,6 @@ -import { NgModule } from '@angular/core'; import { CommonModule } from '@angular/common'; +import { NgModule } from '@angular/core'; +import { NostrEventFactory } from './nostr-event.factory'; import { NostrService } from './nostr.service'; @NgModule({ @@ -7,7 +8,8 @@ import { NostrService } from './nostr.service'; CommonModule ], providers: [ - NostrService + NostrService, + NostrEventFactory ] }) export class NostrApiModule { } diff --git a/src/app/shared/nostr-api/nostr.service.ts b/src/app/shared/nostr-api/nostr.service.ts index a7504bb..9bd818a 100644 --- a/src/app/shared/nostr-api/nostr.service.ts +++ b/src/app/shared/nostr-api/nostr.service.ts @@ -1,18 +1,26 @@ import { Injectable } from '@angular/core'; -import { Event, Filter, SimplePool } from 'nostr-tools'; +import { Event, Filter, SimplePool, validateEvent, verifySignature } from 'nostr-tools'; import { defaultRelays } from '../../default-relays.const'; -@Injectable({ - providedIn: 'root' -}) +@Injectable() export class NostrService { - private readonly relays = defaultRelays; + private static instance: NostrService | null = null; + + pool = new SimplePool(); + + constructor() { + if (!NostrService.instance) { + NostrService.instance = this; + } + + return NostrService.instance; + } + get(filters: Filter[]): Promise>> { - const pool = new SimplePool(); const events = new Array>(); - const sub = pool.sub( - this.relays, filters + const sub = this.pool.sub( + defaultRelays, filters ); sub.on('event', event => { @@ -22,9 +30,21 @@ export class NostrService { return new Promise(resolve => { sub.on('eose', () => { resolve(events); - sub.unsub(); - pool.close(this.relays); }); }); } + + async publish(event: Event): Promise { + const ok = validateEvent(event); + const veryOk = verifySignature(event); + + if (!ok || !veryOk) { + console.error(' :: event is not valid... aborting...'); + return Promise.resolve(); + } + + await this.pool.publish(defaultRelays, event); + + return Promise.resolve(); + } } diff --git a/src/app/shared/omegle/omegle.nostr.ts b/src/app/shared/omegle/omegle.nostr.ts index 2c0b3fb..06d2ee7 100644 --- a/src/app/shared/omegle/omegle.nostr.ts +++ b/src/app/shared/omegle/omegle.nostr.ts @@ -2,17 +2,6 @@ import { Injectable } from '@angular/core'; import { NostrService } from '../nostr-api/nostr.service'; import { NostrEventKind } from '@domain/nostr-event-kind.enum'; -/** - * Nip 38 e nip 4 vou usar - * O nip 38 indica um status em que o usuário está (online, offline, ocupado e variações) - * O 4 é chat direto criptografado - * 1. O nsec é gerado - * 2. Aplico um user status de interessado em bate papo - * 3. Filtra nos relays eventos de user status interessado em bate papo - * 4. Conecta os dois em chat criptografado e atualiza o user status pra ocupado - * Cada nova conversa por ser um novo nsec - */ - @Injectable() export class OmegleNostr {