validate events on receive

This commit is contained in:
Kieran 2023-10-11 18:28:28 +01:00
parent 022296fa18
commit 88ac4063cd
Signed by: Kieran
GPG Key ID: DE71CEB3925BE941
3 changed files with 39 additions and 13 deletions

View File

@ -4,6 +4,7 @@ import { getPublicKey, sha256, unixNow } from "@snort/shared";
import { EventKind, HexKey, NostrEvent, NotSignedNostrEvent } from "."; import { EventKind, HexKey, NostrEvent, NotSignedNostrEvent } from ".";
import { minePow } from "./pow-util"; import { minePow } from "./pow-util";
import { findTag } from "./utils";
export interface Tag { export interface Tag {
key: string; key: string;
@ -19,6 +20,12 @@ export interface Thread {
pubKeys: Array<HexKey>; pubKeys: Array<HexKey>;
} }
export const enum EventType {
Regular,
Replaceable,
ParameterizedReplaceable,
}
export abstract class EventExt { export abstract class EventExt {
/** /**
* Get the pub key of the creator of this event NIP-26 * Get the pub key of the creator of this event NIP-26
@ -149,4 +156,25 @@ export abstract class EventExt {
e.pubkey ??= ""; e.pubkey ??= "";
e.sig ??= ""; e.sig ??= "";
} }
static getType(kind: number) {
const legacyReplaceable = [0, 3, 41];
if (kind >= 30_000 && kind < 40_000) {
return EventType.ParameterizedReplaceable;
} else if (kind >= 10_000 && kind < 20_000) {
return EventType.Replaceable;
} else if (legacyReplaceable.includes(kind)) {
return EventType.Replaceable;
} else {
return EventType.Regular;
}
}
static isValid(ev: NostrEvent) {
const type = EventExt.getType(ev.kind);
if (type === EventType.ParameterizedReplaceable) {
if (!findTag(ev, "d")) return false;
}
return true;
}
} }

View File

@ -18,6 +18,7 @@ import {
RelayMetricCache, RelayMetricCache,
UsersRelays, UsersRelays,
SnortSystemDb, SnortSystemDb,
EventExt,
} from "."; } from ".";
import { EventsCache } from "./cache/events"; import { EventsCache } from "./cache/events";
import { RelayCache } from "./gossip-model"; import { RelayCache } from "./gossip-model";
@ -178,6 +179,8 @@ export class NostrSystem extends ExternalStore<SystemSnapshot> implements System
} }
#onEvent(sub: string, ev: TaggedNostrEvent) { #onEvent(sub: string, ev: TaggedNostrEvent) {
if (!EventExt.isValid(ev)) return;
for (const [, v] of this.Queries) { for (const [, v] of this.Queries) {
v.handleEvent(sub, ev); v.handleEvent(sub, ev);
} }

View File

@ -1,5 +1,5 @@
import { appendDedupe } from "@snort/shared"; import { appendDedupe } from "@snort/shared";
import { TaggedNostrEvent, u256 } from "."; import { EventExt, EventType, TaggedNostrEvent, u256 } from ".";
import { findTag } from "./utils"; import { findTag } from "./utils";
export interface StoreSnapshot<TSnapshot> { export interface StoreSnapshot<TSnapshot> {
@ -268,17 +268,12 @@ export class ReplaceableNoteStore extends HookedNoteStore<Readonly<TaggedNostrEv
export class NoteCollection extends KeyedReplaceableNoteStore { export class NoteCollection extends KeyedReplaceableNoteStore {
constructor() { constructor() {
super(e => { super(e => {
const legacyReplaceable = [0, 3, 41]; switch (EventExt.getType(e.kind)) {
if (e.kind >= 30_000 && e.kind < 40_000) { case EventType.ParameterizedReplaceable:
return `${e.kind}:${e.pubkey}:${findTag(e, "d")}`; // Parameterized replaceable return `${e.kind}:${e.pubkey}:${findTag(e, "d")}`;
} else if (e.kind >= 10_000 && e.kind < 20_000) { case EventType.Replaceable:
return `${e.kind}:${e.pubkey}`; // Replaceable event return `${e.kind}:${e.pubkey}`;
} else if (legacyReplaceable.includes(e.kind)) { default:
return `${e.kind}:${e.pubkey}`; // Replaceable event
} else {
// Regular event
// ephemeral events are like regular events
// unknown kind
return e.id; return e.id;
} }
}); });