Merge branch 'verify-worker' into 'main'

Add verifySignatureWorker

See merge request soapbox-pub/ditto!65
This commit is contained in:
Alex Gleason 2023-12-04 18:16:27 +00:00
commit caf671983b
4 changed files with 25 additions and 1 deletions

View File

@ -12,6 +12,7 @@ import { Sub } from '@/subs.ts';
import { getTagSet } from '@/tags.ts';
import { trends } from '@/trends.ts';
import { eventAge, isRelay, nostrDate, Time } from '@/utils.ts';
import { verifySignatureWorker } from '@/workers/verify.ts';
import type { EventData } from '@/types.ts';
@ -20,6 +21,7 @@ import type { EventData } from '@/types.ts';
* It is idempotent, so it can be called multiple times for the same event.
*/
async function handleEvent(event: Event): Promise<void> {
if (!(await verifySignatureWorker(event))) return;
if (encounterEvent(event)) return;
const data = await getEventData(event);

View File

@ -2,7 +2,7 @@ import { getActiveRelays } from '@/db/relays.ts';
import { type Event, RelayPool } from '@/deps.ts';
const allRelays = await getActiveRelays();
const pool = new RelayPool(allRelays);
const pool = new RelayPool(allRelays, { skipVerification: true });
/** Publish an event to the given relays, or the entire pool. */
function publish(event: Event, relays: string[] = allRelays) {

13
src/workers/verify.ts Normal file
View File

@ -0,0 +1,13 @@
import { Comlink, type Event } from '@/deps.ts';
import type { VerifyWorker } from './verify.worker.ts';
const worker = Comlink.wrap<typeof VerifyWorker>(
new Worker(new URL('./verify.worker.ts', import.meta.url), { type: 'module' }),
);
function verifySignatureWorker<K extends number>(event: Event<K>): Promise<boolean> {
return worker.verifySignature(event);
}
export { verifySignatureWorker };

View File

@ -0,0 +1,9 @@
import { Comlink, type Event, type VerifiedEvent, verifySignature } from '@/deps.ts';
export const VerifyWorker = {
verifySignature<K extends number>(event: Event<K>): event is VerifiedEvent<K> {
return verifySignature(event);
},
};
Comlink.expose(VerifyWorker);