From 2fbcfc59a10f9c9c04f7e25606c3630f81da7235 Mon Sep 17 00:00:00 2001 From: Jon Staab Date: Thu, 21 Nov 2024 16:03:34 -0800 Subject: [PATCH] Remove nostr-wasm, add event validation sampling --- package-lock.json | 6 +++--- packages/lib/src/Tools.ts | 2 +- packages/net/src/Context.ts | 27 ++++++++++++++++++++++++--- packages/util/package.json | 3 +-- packages/util/src/Events.ts | 9 +-------- 5 files changed, 30 insertions(+), 17 deletions(-) diff --git a/package-lock.json b/package-lock.json index 4843f1b..d8bf691 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2487,7 +2487,8 @@ "version": "0.1.0", "resolved": "https://registry.npmjs.org/nostr-wasm/-/nostr-wasm-0.1.0.tgz", "integrity": "sha512-78BTryCLcLYv96ONU8Ws3Q1JzjlAt+43pWQhIl86xZmWeegYCNLPml7yQ+gG3vR6V5h4XGj+TxO+SS5dsThQIA==", - "license": "MIT" + "license": "MIT", + "optional": true }, "node_modules/npm-run-path": { "version": "4.0.1", @@ -3782,8 +3783,7 @@ "license": "MIT", "dependencies": { "@welshman/lib": "~0.0.25", - "nostr-tools": "^2.7.2", - "nostr-wasm": "^0.1.0" + "nostr-tools": "^2.7.2" }, "devDependencies": { "gts": "^5.0.1", diff --git a/packages/lib/src/Tools.ts b/packages/lib/src/Tools.ts index 0866a0b..b3fec8e 100644 --- a/packages/lib/src/Tools.ts +++ b/packages/lib/src/Tools.ts @@ -127,7 +127,7 @@ export const mergeRight = >(a: T, b: T) => ({...a, export const between = ([low, high]: [number, number], n: number) => n > low && n < high -export const randomInt = (min = 0, max = 9) => min + Math.round(Math.random()) * (max - min) +export const randomInt = (min = 0, max = 9) => min + Math.round(Math.random() * (max - min)) export const randomId = (): string => Math.random().toString().slice(2) diff --git a/packages/net/src/Context.ts b/packages/net/src/Context.ts index dd53133..276e06b 100644 --- a/packages/net/src/Context.ts +++ b/packages/net/src/Context.ts @@ -1,5 +1,5 @@ -import {ctx, uniq, noop, always} from '@welshman/lib' -import {matchFilters, unionFilters, isSignedEvent, hasValidSignature} from '@welshman/util' +import {ctx, randomInt, uniq, noop, always} from '@welshman/lib' +import {LOCAL_RELAY_URL, matchFilters, unionFilters, isSignedEvent, hasValidSignature} from '@welshman/util' import type {StampedEvent, SignedEvent, Filter, TrustedEvent} from '@welshman/util' import {Pool} from "./Pool" import {Executor} from "./Executor" @@ -28,13 +28,34 @@ export const defaultOptimizeSubscriptions = (subs: Subscription[]) => return {relays: [relay], filters} }) +export const eventValidationScores = new Map() + +export const isEventValid = (url: string, event: TrustedEvent) => { + if (url === LOCAL_RELAY_URL) return true + + const validCount = eventValidationScores.get(url) || 0 + + // The more events we've actually validated from this relay, the more we can trust it. + if (validCount > randomInt(100, 1000)) true + + const isValid = isSignedEvent(event) && hasValidSignature(event) + + // If the event was valid, increase the relay's score. If not, reset it + // Never validate less than 10% to make sure we're never totally checking out + if (!isValid || validCount < 900) { + eventValidationScores.set(url, isValid ? validCount + 1 : 0) + } + + return isValid +} + export const getDefaultNetContext = (overrides: Partial = {}) => ({ pool: new Pool(), authMode: AuthMode.Implicit, onEvent: noop, signEvent: noop, isDeleted: always(false), - isValid: (url: string, event: TrustedEvent) => isSignedEvent(event) && hasValidSignature(event), + isValid: isEventValid, getExecutor: (relays: string[]) => new Executor(new Relays(relays.map((relay: string) => ctx.net.pool.get(relay)))), matchFilters: (url: string, filters: Filter[], event: TrustedEvent) => matchFilters(filters, event), optimizeSubscriptions: defaultOptimizeSubscriptions, diff --git a/packages/util/package.json b/packages/util/package.json index 4349c4d..868604a 100644 --- a/packages/util/package.json +++ b/packages/util/package.json @@ -32,7 +32,6 @@ }, "dependencies": { "@welshman/lib": "~0.0.25", - "nostr-tools": "^2.7.2", - "nostr-wasm": "^0.1.0" + "nostr-tools": "^2.7.2" } } diff --git a/packages/util/src/Events.ts b/packages/util/src/Events.ts index 9066e30..4e01252 100644 --- a/packages/util/src/Events.ts +++ b/packages/util/src/Events.ts @@ -1,16 +1,9 @@ -import {initNostrWasm, type Nostr} from 'nostr-wasm' import {verifiedSymbol, getEventHash, verifyEvent} from 'nostr-tools' import {cached, pick, now} from '@welshman/lib' import {Tags} from './Tags' import {getAddress} from './Address' import {isEphemeralKind, isReplaceableKind, isPlainReplaceableKind, isParameterizedReplaceableKind} from './Kinds' -let nw: Nostr - -initNostrWasm().then(nostrWasm => { - nw = nostrWasm -}) - export type EventContent = { tags: string[][] content: string @@ -109,7 +102,7 @@ const _hasValidSignature = cached({ }, getValue: ([e]: [SignedEvent]) => { try { - nw ? nw.verifyEvent(e) : verifyEvent(e) + verifyEvent(e) } catch (err) { return false }