pass the relay URL to verifyEvent() so trusted-relay policies can be implemented.

This commit is contained in:
fiatjaf
2026-07-20 13:01:28 -03:00
parent c66469dc75
commit bd02fc3287
4 changed files with 8 additions and 17 deletions

View File

@@ -8,9 +8,8 @@ import {
} from './abstract-relay.ts'
import { normalizeURL } from './utils.ts'
import type { Event, EventTemplate, Nostr, VerifiedEvent } from './core.ts'
import type { Event, EventTemplate, VerifiedEvent } from './core.ts'
import { type Filter } from './filter.ts'
import { alwaysTrue } from './helpers.ts'
import { getCountManyFilter, hllDecode, hllEncode, mergeHll, type CountManyDirective } from './nip45.ts'
import { Relay } from './relay.ts'
@@ -46,12 +45,11 @@ export class AbstractSimplePool {
public seenOn: Map<string, Set<AbstractRelay>> = new Map()
public trackRelays: boolean = false
public verifyEvent: Nostr['verifyEvent']
public verifyEvent: (event: Event, url: string) => boolean
public enablePing: boolean | undefined
public enableReconnect: boolean
public idleTimeout: number = 20000
public automaticallyAuth?: (relayURL: string) => null | ((event: EventTemplate) => Promise<VerifiedEvent>)
public trustedRelayURLs: Set<string> = new Set()
public onRelayConnectionFailure?: (url: string) => void
public onRelayConnectionSuccess?: (url: string) => void
public allowConnectingToRelay?: (url: string, operation: ['read', Filter[]] | ['write', Event]) => boolean
@@ -84,7 +82,7 @@ export class AbstractSimplePool {
let relay = this.relays.get(url)
if (!relay) {
relay = new AbstractRelay(url, {
verifyEvent: this.trustedRelayURLs.has(url) ? alwaysTrue : this.verifyEvent,
verifyEvent: this.verifyEvent,
websocketImplementation: this._WebSocket,
enablePing: this.enablePing,
enableReconnect: this.enableReconnect,

View File

@@ -1,6 +1,6 @@
/* global WebSocket */
import type { Event, EventTemplate, VerifiedEvent, Nostr, NostrEvent } from './core.ts'
import type { Event, EventTemplate, VerifiedEvent, NostrEvent } from './core.ts'
import { matchFilters, type Filter } from './filter.ts'
import { getHex64, getSubscriptionId } from './fakejson.ts'
import { normalizeURL } from './utils.ts'
@@ -12,7 +12,7 @@ type RelayWebSocket = WebSocket & {
}
export type AbstractRelayConstructorOptions = {
verifyEvent: Nostr['verifyEvent']
verifyEvent: (event: Event, url: string) => boolean
websocketImplementation?: typeof WebSocket
enablePing?: boolean
enableReconnect?: boolean
@@ -58,7 +58,7 @@ export class AbstractRelay {
private challenge: string | undefined
private authPromise: Promise<string> | undefined
private serial: number = 0
private verifyEvent: Nostr['verifyEvent']
private verifyEvent: (event: Event, url: string) => boolean
private _WebSocket: typeof WebSocket
@@ -485,7 +485,7 @@ export class AbstractRelay {
case 'EVENT': {
const so = this.openSubs.get(data[1] as string) as Subscription
const event = data[2] as NostrEvent
if (this.verifyEvent(event) && matchFilters(so.filters, event)) {
if (matchFilters(so.filters, event) && this.verifyEvent(event, this.url)) {
so.onevent(event)
} else {
so.oninvalidevent?.(event)

View File

@@ -5,7 +5,6 @@ import { finalizeEvent, generateSecretKey } from './pure.ts'
import { setNostrWasm, verifyEvent } from './wasm.ts'
import { AbstractRelay } from './abstract-relay.ts'
import { Relay as PureRelay } from './relay.ts'
import { alwaysTrue } from './helpers.ts'
// benchmarking relay reads with verifyEvent
const EVENTS = 200
@@ -28,7 +27,7 @@ for (let i = 0; i < EVENTS; i++) {
setNostrWasm(await initNostrWasm())
const pureRelay = new PureRelay('wss://pure.com/')
const trustedRelay = new AbstractRelay('wss://trusted.com/', { verifyEvent: alwaysTrue })
const trustedRelay = new AbstractRelay('wss://trusted.com/', { verifyEvent: () => true })
const wasmRelay = new AbstractRelay('wss://wasm.com/', { verifyEvent })
const runWith = (relay: AbstractRelay) => async () => {

View File

@@ -1,6 +0,0 @@
import { verifiedSymbol, type Event, type Nostr, VerifiedEvent } from './core.ts'
export const alwaysTrue: Nostr['verifyEvent'] = (t: Event): t is VerifiedEvent => {
t[verifiedSymbol] = true
return true
}