Remove nostr-wasm, add event validation sampling
This commit is contained in:
Generated
+3
-3
@@ -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",
|
||||
|
||||
@@ -127,7 +127,7 @@ export const mergeRight = <T extends Record<string, any>>(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)
|
||||
|
||||
|
||||
@@ -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<string, number>()
|
||||
|
||||
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<NetContext> = {}) => ({
|
||||
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,
|
||||
|
||||
@@ -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"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<string, boolean, [SignedEvent]>({
|
||||
},
|
||||
getValue: ([e]: [SignedEvent]) => {
|
||||
try {
|
||||
nw ? nw.verifyEvent(e) : verifyEvent(e)
|
||||
verifyEvent(e)
|
||||
} catch (err) {
|
||||
return false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user