Attempt to speed up signature verification

This commit is contained in:
Jon Staab
2024-11-21 15:33:13 -08:00
parent 81bf3bec10
commit 2bc575dc21
4 changed files with 24 additions and 11 deletions
+8 -6
View File
@@ -2485,8 +2485,9 @@
}, },
"node_modules/nostr-wasm": { "node_modules/nostr-wasm": {
"version": "0.1.0", "version": "0.1.0",
"license": "MIT", "resolved": "https://registry.npmjs.org/nostr-wasm/-/nostr-wasm-0.1.0.tgz",
"optional": true "integrity": "sha512-78BTryCLcLYv96ONU8Ws3Q1JzjlAt+43pWQhIl86xZmWeegYCNLPml7yQ+gG3vR6V5h4XGj+TxO+SS5dsThQIA==",
"license": "MIT"
}, },
"node_modules/npm-run-path": { "node_modules/npm-run-path": {
"version": "4.0.1", "version": "4.0.1",
@@ -3635,13 +3636,13 @@
}, },
"packages/app": { "packages/app": {
"name": "@welshman/app", "name": "@welshman/app",
"version": "0.0.28", "version": "0.0.29",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@welshman/dvm": "~0.0.10", "@welshman/dvm": "~0.0.10",
"@welshman/feeds": "~0.0.25", "@welshman/feeds": "~0.0.25",
"@welshman/lib": "~0.0.25", "@welshman/lib": "~0.0.25",
"@welshman/net": "~0.0.37", "@welshman/net": "~0.0.38",
"@welshman/signer": "~0.0.14", "@welshman/signer": "~0.0.14",
"@welshman/store": "~0.0.12", "@welshman/store": "~0.0.12",
"@welshman/util": "~0.0.45", "@welshman/util": "~0.0.45",
@@ -3726,7 +3727,7 @@
}, },
"packages/net": { "packages/net": {
"name": "@welshman/net", "name": "@welshman/net",
"version": "0.0.37", "version": "0.0.38",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@welshman/lib": "~0.0.25", "@welshman/lib": "~0.0.25",
@@ -3781,7 +3782,8 @@
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@welshman/lib": "~0.0.25", "@welshman/lib": "~0.0.25",
"nostr-tools": "^2.7.2" "nostr-tools": "^2.7.2",
"nostr-wasm": "^0.1.0"
}, },
"devDependencies": { "devDependencies": {
"gts": "^5.0.1", "gts": "^5.0.1",
+2
View File
@@ -25,6 +25,8 @@ export const identity = <T>(x: T, ...args: unknown[]) => x
export const always = <T>(x: T, ...args: unknown[]) => () => x export const always = <T>(x: T, ...args: unknown[]) => () => x
export const not = (x: boolean, ...args: unknown[]) => !x
export const num = (x: Maybe<number>) => x || 0 export const num = (x: Maybe<number>) => x || 0
export const add = (x: Maybe<number>, y: Maybe<number>) => num(x) + num(y) export const add = (x: Maybe<number>, y: Maybe<number>) => num(x) + num(y)
+2 -1
View File
@@ -32,6 +32,7 @@
}, },
"dependencies": { "dependencies": {
"@welshman/lib": "~0.0.25", "@welshman/lib": "~0.0.25",
"nostr-tools": "^2.7.2" "nostr-tools": "^2.7.2",
"nostr-wasm": "^0.1.0"
} }
} }
+12 -4
View File
@@ -1,10 +1,16 @@
import {verifiedSymbol} from 'nostr-tools' import {initNostrWasm, type Nostr} from 'nostr-wasm'
import {verifyEvent, getEventHash} from 'nostr-tools' import {verifiedSymbol, getEventHash, verifyEvent} from 'nostr-tools'
import {cached, pick, now} from '@welshman/lib' import {cached, pick, now} from '@welshman/lib'
import {Tags} from './Tags' import {Tags} from './Tags'
import {getAddress} from './Address' import {getAddress} from './Address'
import {isEphemeralKind, isReplaceableKind, isPlainReplaceableKind, isParameterizedReplaceableKind} from './Kinds' import {isEphemeralKind, isReplaceableKind, isPlainReplaceableKind, isParameterizedReplaceableKind} from './Kinds'
let nw: Nostr
initNostrWasm().then(nostrWasm => {
nw = nostrWasm
})
export type EventContent = { export type EventContent = {
tags: string[][] tags: string[][]
content: string content: string
@@ -92,7 +98,7 @@ export const asUnwrappedEvent = (e: UnwrappedEvent): UnwrappedEvent =>
export const asTrustedEvent = (e: TrustedEvent): TrustedEvent => export const asTrustedEvent = (e: TrustedEvent): TrustedEvent =>
pick(['kind', 'tags', 'content', 'created_at', 'pubkey', 'id', 'sig', 'wrap'], e) pick(['kind', 'tags', 'content', 'created_at', 'pubkey', 'id', 'sig', 'wrap'], e)
export const hasValidSignature = cached<string, boolean, [SignedEvent]>({ const _hasValidSignature = cached<string, boolean, [SignedEvent]>({
maxSize: 10000, maxSize: 10000,
getKey: ([e]: [SignedEvent]) => { getKey: ([e]: [SignedEvent]) => {
try { try {
@@ -103,7 +109,7 @@ export const hasValidSignature = cached<string, boolean, [SignedEvent]>({
}, },
getValue: ([e]: [SignedEvent]) => { getValue: ([e]: [SignedEvent]) => {
try { try {
verifyEvent(e) nw ? nw.verifyEvent(e) : verifyEvent(e)
} catch (err) { } catch (err) {
return false return false
} }
@@ -112,6 +118,8 @@ export const hasValidSignature = cached<string, boolean, [SignedEvent]>({
}, },
}) })
export const hasValidSignature = (e: SignedEvent) => e[verifiedSymbol] || _hasValidSignature(e)
export const getIdentifier = (e: EventTemplate) => e.tags.find(t => t[0] === 'd')?.[1] export const getIdentifier = (e: EventTemplate) => e.tags.find(t => t[0] === 'd')?.[1]
export const getIdOrAddress = (e: HashedEvent) => isReplaceable(e) ? getAddress(e) : e.id export const getIdOrAddress = (e: HashedEvent) => isReplaceable(e) ? getAddress(e) : e.id