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 { minePow } from "./pow-util";
import { findTag } from "./utils";
export interface Tag {
key: string;
@ -19,6 +20,12 @@ export interface Thread {
pubKeys: Array<HexKey>;
}
export const enum EventType {
Regular,
Replaceable,
ParameterizedReplaceable,
}
export abstract class EventExt {
/**
* Get the pub key of the creator of this event NIP-26
@ -149,4 +156,25 @@ export abstract class EventExt {
e.pubkey ??= "";
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,
UsersRelays,
SnortSystemDb,
EventExt,
} from ".";
import { EventsCache } from "./cache/events";
import { RelayCache } from "./gossip-model";
@ -178,6 +179,8 @@ export class NostrSystem extends ExternalStore<SystemSnapshot> implements System
}
#onEvent(sub: string, ev: TaggedNostrEvent) {
if (!EventExt.isValid(ev)) return;
for (const [, v] of this.Queries) {
v.handleEvent(sub, ev);
}

View File

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