Allow opting out of signature validation

This commit is contained in:
Jon Staab
2023-12-05 12:34:34 -08:00
parent 51bd938913
commit bdaf42f213
3 changed files with 13 additions and 4 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "paravel", "name": "paravel",
"version": "0.4.12", "version": "0.4.13",
"description": "Yet another toolkit for nostr", "description": "Yet another toolkit for nostr",
"author": "hodlbod", "author": "hodlbod",
"license": "MIT", "license": "MIT",
+10 -1
View File
@@ -10,6 +10,7 @@ export type SubscriptionOpts = {
timeout?: number timeout?: number
closeOnEose?: boolean closeOnEose?: boolean
hasSeen?: (e: Event, url: string) => boolean hasSeen?: (e: Event, url: string) => boolean
shouldValidate?: (e: Event, url: string) => boolean
} }
export class Subscription extends EventEmitter { export class Subscription extends EventEmitter {
@@ -69,13 +70,21 @@ export class Subscription extends EventEmitter {
return false return false
} }
hasValidSignature = (event: Event, url: string) => {
if (this.opts.shouldValidate && !this.opts.shouldValidate(event, url)) {
return true
}
return hasValidSignature(event)
}
onEvent = (url: string, event: Event) => { onEvent = (url: string, event: Event) => {
// If we've seen this event, don't re-validate // If we've seen this event, don't re-validate
// Otherwise, check the signature and filters // Otherwise, check the signature and filters
if (this.hasSeen(event, url)) { if (this.hasSeen(event, url)) {
this.emit("duplicate", event, url) this.emit("duplicate", event, url)
} else { } else {
if (!hasValidSignature(event)) { if (!this.hasValidSignature(event, url)) {
this.emit("invalid-signature", event, url) this.emit("invalid-signature", event, url)
} else if (!matchFilters(this.opts.filters, event)) { } else if (!matchFilters(this.opts.filters, event)) {
this.emit("failed-filter", event, url) this.emit("failed-filter", event, url)
+2 -2
View File
@@ -217,6 +217,8 @@ export class Tags extends Fluent<string[]> {
replies = (type = null) => this.getAncestors(type).replies replies = (type = null) => this.getAncestors(type).replies
communities = () => this.type("a").values().filter(a => a.startsWith('34550:'))
getReply = (type = null) => this.replies(type).values().first() getReply = (type = null) => this.replies(type).values().first()
getRoot = (type = null) => this.roots(type).values().first() getRoot = (type = null) => this.roots(type).values().first()
@@ -224,8 +226,6 @@ export class Tags extends Fluent<string[]> {
getReplyHints = (type = null) => this.replies(type).relays().all() getReplyHints = (type = null) => this.replies(type).relays().all()
getRootHints = (type = null) => this.roots(type).relays().all() getRootHints = (type = null) => this.roots(type).relays().all()
communities = () => this.type("a").values().filter(a => a.startsWith('34550:'))
} }
// =========================================================================== // ===========================================================================