mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2026-10-05 14:38:23 +00:00
pass the relay URL to verifyEvent() so trusted-relay policies can be implemented.
This commit is contained in:
+3
-5
@@ -8,9 +8,8 @@ import {
|
|||||||
} from './abstract-relay.ts'
|
} from './abstract-relay.ts'
|
||||||
import { normalizeURL } from './utils.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 { type Filter } from './filter.ts'
|
||||||
import { alwaysTrue } from './helpers.ts'
|
|
||||||
import { getCountManyFilter, hllDecode, hllEncode, mergeHll, type CountManyDirective } from './nip45.ts'
|
import { getCountManyFilter, hllDecode, hllEncode, mergeHll, type CountManyDirective } from './nip45.ts'
|
||||||
import { Relay } from './relay.ts'
|
import { Relay } from './relay.ts'
|
||||||
|
|
||||||
@@ -46,12 +45,11 @@ export class AbstractSimplePool {
|
|||||||
public seenOn: Map<string, Set<AbstractRelay>> = new Map()
|
public seenOn: Map<string, Set<AbstractRelay>> = new Map()
|
||||||
public trackRelays: boolean = false
|
public trackRelays: boolean = false
|
||||||
|
|
||||||
public verifyEvent: Nostr['verifyEvent']
|
public verifyEvent: (event: Event, url: string) => boolean
|
||||||
public enablePing: boolean | undefined
|
public enablePing: boolean | undefined
|
||||||
public enableReconnect: boolean
|
public enableReconnect: boolean
|
||||||
public idleTimeout: number = 20000
|
public idleTimeout: number = 20000
|
||||||
public automaticallyAuth?: (relayURL: string) => null | ((event: EventTemplate) => Promise<VerifiedEvent>)
|
public automaticallyAuth?: (relayURL: string) => null | ((event: EventTemplate) => Promise<VerifiedEvent>)
|
||||||
public trustedRelayURLs: Set<string> = new Set()
|
|
||||||
public onRelayConnectionFailure?: (url: string) => void
|
public onRelayConnectionFailure?: (url: string) => void
|
||||||
public onRelayConnectionSuccess?: (url: string) => void
|
public onRelayConnectionSuccess?: (url: string) => void
|
||||||
public allowConnectingToRelay?: (url: string, operation: ['read', Filter[]] | ['write', Event]) => boolean
|
public allowConnectingToRelay?: (url: string, operation: ['read', Filter[]] | ['write', Event]) => boolean
|
||||||
@@ -84,7 +82,7 @@ export class AbstractSimplePool {
|
|||||||
let relay = this.relays.get(url)
|
let relay = this.relays.get(url)
|
||||||
if (!relay) {
|
if (!relay) {
|
||||||
relay = new AbstractRelay(url, {
|
relay = new AbstractRelay(url, {
|
||||||
verifyEvent: this.trustedRelayURLs.has(url) ? alwaysTrue : this.verifyEvent,
|
verifyEvent: this.verifyEvent,
|
||||||
websocketImplementation: this._WebSocket,
|
websocketImplementation: this._WebSocket,
|
||||||
enablePing: this.enablePing,
|
enablePing: this.enablePing,
|
||||||
enableReconnect: this.enableReconnect,
|
enableReconnect: this.enableReconnect,
|
||||||
|
|||||||
+4
-4
@@ -1,6 +1,6 @@
|
|||||||
/* global WebSocket */
|
/* 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 { matchFilters, type Filter } from './filter.ts'
|
||||||
import { getHex64, getSubscriptionId } from './fakejson.ts'
|
import { getHex64, getSubscriptionId } from './fakejson.ts'
|
||||||
import { normalizeURL } from './utils.ts'
|
import { normalizeURL } from './utils.ts'
|
||||||
@@ -12,7 +12,7 @@ type RelayWebSocket = WebSocket & {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export type AbstractRelayConstructorOptions = {
|
export type AbstractRelayConstructorOptions = {
|
||||||
verifyEvent: Nostr['verifyEvent']
|
verifyEvent: (event: Event, url: string) => boolean
|
||||||
websocketImplementation?: typeof WebSocket
|
websocketImplementation?: typeof WebSocket
|
||||||
enablePing?: boolean
|
enablePing?: boolean
|
||||||
enableReconnect?: boolean
|
enableReconnect?: boolean
|
||||||
@@ -58,7 +58,7 @@ export class AbstractRelay {
|
|||||||
private challenge: string | undefined
|
private challenge: string | undefined
|
||||||
private authPromise: Promise<string> | undefined
|
private authPromise: Promise<string> | undefined
|
||||||
private serial: number = 0
|
private serial: number = 0
|
||||||
private verifyEvent: Nostr['verifyEvent']
|
private verifyEvent: (event: Event, url: string) => boolean
|
||||||
|
|
||||||
private _WebSocket: typeof WebSocket
|
private _WebSocket: typeof WebSocket
|
||||||
|
|
||||||
@@ -485,7 +485,7 @@ export class AbstractRelay {
|
|||||||
case 'EVENT': {
|
case 'EVENT': {
|
||||||
const so = this.openSubs.get(data[1] as string) as Subscription
|
const so = this.openSubs.get(data[1] as string) as Subscription
|
||||||
const event = data[2] as NostrEvent
|
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)
|
so.onevent(event)
|
||||||
} else {
|
} else {
|
||||||
so.oninvalidevent?.(event)
|
so.oninvalidevent?.(event)
|
||||||
|
|||||||
+1
-2
@@ -5,7 +5,6 @@ import { finalizeEvent, generateSecretKey } from './pure.ts'
|
|||||||
import { setNostrWasm, verifyEvent } from './wasm.ts'
|
import { setNostrWasm, verifyEvent } from './wasm.ts'
|
||||||
import { AbstractRelay } from './abstract-relay.ts'
|
import { AbstractRelay } from './abstract-relay.ts'
|
||||||
import { Relay as PureRelay } from './relay.ts'
|
import { Relay as PureRelay } from './relay.ts'
|
||||||
import { alwaysTrue } from './helpers.ts'
|
|
||||||
|
|
||||||
// benchmarking relay reads with verifyEvent
|
// benchmarking relay reads with verifyEvent
|
||||||
const EVENTS = 200
|
const EVENTS = 200
|
||||||
@@ -28,7 +27,7 @@ for (let i = 0; i < EVENTS; i++) {
|
|||||||
setNostrWasm(await initNostrWasm())
|
setNostrWasm(await initNostrWasm())
|
||||||
|
|
||||||
const pureRelay = new PureRelay('wss://pure.com/')
|
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 wasmRelay = new AbstractRelay('wss://wasm.com/', { verifyEvent })
|
||||||
|
|
||||||
const runWith = (relay: AbstractRelay) => async () => {
|
const runWith = (relay: AbstractRelay) => async () => {
|
||||||
|
|||||||
@@ -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
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user