From 2bc575dc2112c033ae8687281dfd695a0f774dba Mon Sep 17 00:00:00 2001 From: Jon Staab Date: Thu, 21 Nov 2024 15:33:13 -0800 Subject: [PATCH] Attempt to speed up signature verification --- package-lock.json | 14 ++++++++------ packages/lib/src/Tools.ts | 2 ++ packages/util/package.json | 3 ++- packages/util/src/Events.ts | 16 ++++++++++++---- 4 files changed, 24 insertions(+), 11 deletions(-) diff --git a/package-lock.json b/package-lock.json index 3c87bd8..4843f1b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2485,8 +2485,9 @@ }, "node_modules/nostr-wasm": { "version": "0.1.0", - "license": "MIT", - "optional": true + "resolved": "https://registry.npmjs.org/nostr-wasm/-/nostr-wasm-0.1.0.tgz", + "integrity": "sha512-78BTryCLcLYv96ONU8Ws3Q1JzjlAt+43pWQhIl86xZmWeegYCNLPml7yQ+gG3vR6V5h4XGj+TxO+SS5dsThQIA==", + "license": "MIT" }, "node_modules/npm-run-path": { "version": "4.0.1", @@ -3635,13 +3636,13 @@ }, "packages/app": { "name": "@welshman/app", - "version": "0.0.28", + "version": "0.0.29", "license": "MIT", "dependencies": { "@welshman/dvm": "~0.0.10", "@welshman/feeds": "~0.0.25", "@welshman/lib": "~0.0.25", - "@welshman/net": "~0.0.37", + "@welshman/net": "~0.0.38", "@welshman/signer": "~0.0.14", "@welshman/store": "~0.0.12", "@welshman/util": "~0.0.45", @@ -3726,7 +3727,7 @@ }, "packages/net": { "name": "@welshman/net", - "version": "0.0.37", + "version": "0.0.38", "license": "MIT", "dependencies": { "@welshman/lib": "~0.0.25", @@ -3781,7 +3782,8 @@ "license": "MIT", "dependencies": { "@welshman/lib": "~0.0.25", - "nostr-tools": "^2.7.2" + "nostr-tools": "^2.7.2", + "nostr-wasm": "^0.1.0" }, "devDependencies": { "gts": "^5.0.1", diff --git a/packages/lib/src/Tools.ts b/packages/lib/src/Tools.ts index 7516483..0866a0b 100644 --- a/packages/lib/src/Tools.ts +++ b/packages/lib/src/Tools.ts @@ -25,6 +25,8 @@ export const identity = (x: T, ...args: unknown[]) => x export const always = (x: T, ...args: unknown[]) => () => x +export const not = (x: boolean, ...args: unknown[]) => !x + export const num = (x: Maybe) => x || 0 export const add = (x: Maybe, y: Maybe) => num(x) + num(y) diff --git a/packages/util/package.json b/packages/util/package.json index 868604a..4349c4d 100644 --- a/packages/util/package.json +++ b/packages/util/package.json @@ -32,6 +32,7 @@ }, "dependencies": { "@welshman/lib": "~0.0.25", - "nostr-tools": "^2.7.2" + "nostr-tools": "^2.7.2", + "nostr-wasm": "^0.1.0" } } diff --git a/packages/util/src/Events.ts b/packages/util/src/Events.ts index 18debc5..9066e30 100644 --- a/packages/util/src/Events.ts +++ b/packages/util/src/Events.ts @@ -1,10 +1,16 @@ -import {verifiedSymbol} from 'nostr-tools' -import {verifyEvent, getEventHash} from 'nostr-tools' +import {initNostrWasm, type Nostr} from 'nostr-wasm' +import {verifiedSymbol, getEventHash, verifyEvent} from 'nostr-tools' import {cached, pick, now} from '@welshman/lib' import {Tags} from './Tags' import {getAddress} from './Address' import {isEphemeralKind, isReplaceableKind, isPlainReplaceableKind, isParameterizedReplaceableKind} from './Kinds' +let nw: Nostr + +initNostrWasm().then(nostrWasm => { + nw = nostrWasm +}) + export type EventContent = { tags: string[][] content: string @@ -92,7 +98,7 @@ export const asUnwrappedEvent = (e: UnwrappedEvent): UnwrappedEvent => export const asTrustedEvent = (e: TrustedEvent): TrustedEvent => pick(['kind', 'tags', 'content', 'created_at', 'pubkey', 'id', 'sig', 'wrap'], e) -export const hasValidSignature = cached({ +const _hasValidSignature = cached({ maxSize: 10000, getKey: ([e]: [SignedEvent]) => { try { @@ -103,7 +109,7 @@ export const hasValidSignature = cached({ }, getValue: ([e]: [SignedEvent]) => { try { - verifyEvent(e) + nw ? nw.verifyEvent(e) : verifyEvent(e) } catch (err) { return false } @@ -112,6 +118,8 @@ export const hasValidSignature = cached({ }, }) +export const hasValidSignature = (e: SignedEvent) => e[verifiedSymbol] || _hasValidSignature(e) + export const getIdentifier = (e: EventTemplate) => e.tags.find(t => t[0] === 'd')?.[1] export const getIdOrAddress = (e: HashedEvent) => isReplaceable(e) ? getAddress(e) : e.id