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