Skip to content

Commit

Permalink
feat: add eventVerifier to FetcherOptions, to make the event verifica…
Browse files Browse the repository at this point in the history
…tion logic customizable
  • Loading branch information
jiftechnify committed Oct 4, 2024
1 parent d587d2b commit 75810b3
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
1 change: 1 addition & 0 deletions packages/kernel/src/fetcherBackend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export type EnsureRelaysOptions = {

export type FetchTillEoseOptions = {
subId?: string;
eventVerifier: (event: NostrEvent) => boolean;
skipVerification: boolean;
skipFilterMatching: boolean;
connectTimeoutMs: number;
Expand Down
26 changes: 26 additions & 0 deletions packages/nostr-fetch/src/fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,31 @@ export type NostrEventWithAuthor<SeenOn extends boolean> = {
* Common options for all the fetch methods.
*/
export type FetchOptions<SeenOn extends boolean = false> = {
/**
* If specified, the fetcher uses the given function as an event signature verifier instead of the default one.
*
* The function must return `true` if the event signature is valid. Otherwise, it should return `false`.
*
* @example
* // How to use nostr-wasm's verifyEvent() with nostr-fetch
* import { NostrFetcher, type NostrEvent } from "nostr-fetch";
* import { initNostrWasm } from "nostr-wasm";
*
* const nw = await initNostrWasm();
* const nwVerifyEvent = (ev: NostrEvent) => {
* try {
* nw.verifyEvent(ev);
* return true;
* } catch {
* return false;
* }
* };
* const fetcher = NostrFetcher.init({
* eventVerifier: nwVerifyEvent,
* });
*/
eventVerifier?: (event: NostrEvent) => boolean;

/**
* If true, the fetcher skips event signature verification.
*
Expand Down Expand Up @@ -173,6 +198,7 @@ export type FetchOptions<SeenOn extends boolean = false> = {
};

const defaultFetchOptions: Required<FetchOptions> = {
eventVerifier: verifyEventSig,
skipVerification: false,
skipFilterMatching: false,
withSeenOn: false,
Expand Down
4 changes: 2 additions & 2 deletions packages/nostr-fetch/src/relay.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { verifyEventSig } from "@nostr-fetch/kernel/crypto";
import type { C2RMessage, Filter, NostrEvent } from "@nostr-fetch/kernel/nostr";
import { FilterMatcher, generateSubId, parseR2CMessage } from "@nostr-fetch/kernel/nostr";
import {
Expand Down Expand Up @@ -241,6 +240,7 @@ export interface Subscription {

export interface SubscriptionOptions {
subId?: string;
eventVerifier: (event: NostrEvent) => boolean;
skipVerification: boolean;
skipFilterMatching: boolean;
abortSubBeforeEoseTimeoutMs: number;
Expand Down Expand Up @@ -317,7 +317,7 @@ class RelaySubscription implements Subscription {
_forwardEvent(ev: NostrEvent) {
this.#resetAbortSubTimer();

if (!this.#options.skipVerification && !verifyEventSig(ev)) {
if (!this.#options.skipVerification && !this.#options.eventVerifier(ev)) {
return;
}
if (!this.#options.skipFilterMatching && !this.#filterMatcher.match(ev)) {
Expand Down
3 changes: 1 addition & 2 deletions packages/nostr-fetch/src/testutil/fakedFetcher.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Channel } from "@nostr-fetch/kernel/channel";
import { verifyEventSig } from "@nostr-fetch/kernel/crypto";
import type {
EnsureRelaysOptions,
FetchTillEoseOptions,
Expand Down Expand Up @@ -177,7 +176,7 @@ class FakeFetcherBackend implements NostrFetcherBackend {

const [tx, iter] = Channel.make<NostrEvent>();
const onEvent = (ev: NostrEvent) => {
if (options.skipVerification || verifyEventSig(ev)) {
if (options.skipVerification || options.eventVerifier(ev)) {
tx.send(ev);
}
};
Expand Down

0 comments on commit 75810b3

Please sign in to comment.