Attempt to speed up signature verification
This commit is contained in:
Generated
+8
-6
@@ -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",
|
||||
|
||||
@@ -25,6 +25,8 @@ export const identity = <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 add = (x: Maybe<number>, y: Maybe<number>) => num(x) + num(y)
|
||||
|
||||
@@ -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"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<string, boolean, [SignedEvent]>({
|
||||
const _hasValidSignature = cached<string, boolean, [SignedEvent]>({
|
||||
maxSize: 10000,
|
||||
getKey: ([e]: [SignedEvent]) => {
|
||||
try {
|
||||
@@ -103,7 +109,7 @@ export const hasValidSignature = cached<string, boolean, [SignedEvent]>({
|
||||
},
|
||||
getValue: ([e]: [SignedEvent]) => {
|
||||
try {
|
||||
verifyEvent(e)
|
||||
nw ? nw.verifyEvent(e) : verifyEvent(e)
|
||||
} catch (err) {
|
||||
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 getIdOrAddress = (e: HashedEvent) => isReplaceable(e) ? getAddress(e) : e.id
|
||||
|
||||
Reference in New Issue
Block a user