mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2025-12-08 16:28:49 +00:00
verifySignature: set verifiedSymbol to false on failure, DRY return values
This commit is contained in:
@@ -7,6 +7,7 @@ import {
|
||||
verifySignature,
|
||||
getSignature,
|
||||
Kind,
|
||||
verifiedSymbol,
|
||||
} from './event.ts'
|
||||
import { getPublicKey } from './keys.ts'
|
||||
|
||||
@@ -236,7 +237,7 @@ describe('Event', () => {
|
||||
it('should return false for an invalid event signature', () => {
|
||||
const privateKey = 'd217c1ff2f8a65c3e3a1740db3b9f58b8c848bb45e26d00ed4714e4a0f4ceecf'
|
||||
|
||||
const event = finishEvent(
|
||||
const { [verifiedSymbol]: _, ...event } = finishEvent(
|
||||
{
|
||||
kind: Kind.Text,
|
||||
tags: [],
|
||||
@@ -260,7 +261,7 @@ describe('Event', () => {
|
||||
const privateKey2 = '5b4a34f4e4b23c63ad55a35e3f84a3b53d96dbf266edf521a8358f71d19cbf67'
|
||||
const publicKey2 = getPublicKey(privateKey2)
|
||||
|
||||
const event = finishEvent(
|
||||
const { [verifiedSymbol]: _, ...event } = finishEvent(
|
||||
{
|
||||
kind: Kind.Text,
|
||||
tags: [],
|
||||
@@ -282,7 +283,7 @@ describe('Event', () => {
|
||||
it('should return false for an invalid event id', () => {
|
||||
const privateKey = 'd217c1ff2f8a65c3e3a1740db3b9f58b8c848bb45e26d00ed4714e4a0f4ceecf'
|
||||
|
||||
const event = finishEvent(
|
||||
const { [verifiedSymbol]: _, ...event } = finishEvent(
|
||||
{
|
||||
kind: 1,
|
||||
tags: [],
|
||||
|
||||
7
event.ts
7
event.ts
@@ -119,14 +119,13 @@ export function verifySignature<K extends number>(event: Event<K>): event is Ver
|
||||
|
||||
const hash = getEventHash(event)
|
||||
if (hash !== event.id) {
|
||||
return false
|
||||
return (event[verifiedSymbol] = false)
|
||||
}
|
||||
|
||||
try {
|
||||
event[verifiedSymbol] = schnorr.verify(event.sig, hash, event.pubkey)
|
||||
return event[verifiedSymbol]
|
||||
return (event[verifiedSymbol] = schnorr.verify(event.sig, hash, event.pubkey))
|
||||
} catch (err) {
|
||||
return false
|
||||
return (event[verifiedSymbol] = false)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user