mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2025-12-09 00:28:51 +00:00
Merge pull request #289 from alexgleason/verified
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: [],
|
||||||
@@ -247,7 +248,7 @@ describe('Event', () => {
|
|||||||
)
|
)
|
||||||
|
|
||||||
// tamper with the signature
|
// tamper with the signature
|
||||||
event.sig = event.sig.replace(/0/g, '1')
|
event.sig = event.sig.replace(/^.{3}/g, '666')
|
||||||
|
|
||||||
const isValid = verifySignature(event)
|
const isValid = verifySignature(event)
|
||||||
|
|
||||||
@@ -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: [],
|
||||||
@@ -278,6 +279,27 @@ describe('Event', () => {
|
|||||||
|
|
||||||
expect(isValid).toEqual(false)
|
expect(isValid).toEqual(false)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should return false for an invalid event id', () => {
|
||||||
|
const privateKey = 'd217c1ff2f8a65c3e3a1740db3b9f58b8c848bb45e26d00ed4714e4a0f4ceecf'
|
||||||
|
|
||||||
|
const { [verifiedSymbol]: _, ...event } = finishEvent(
|
||||||
|
{
|
||||||
|
kind: 1,
|
||||||
|
tags: [],
|
||||||
|
content: 'Hello, world!',
|
||||||
|
created_at: 1617932115,
|
||||||
|
},
|
||||||
|
privateKey,
|
||||||
|
)
|
||||||
|
|
||||||
|
// tamper with the id
|
||||||
|
event.id = event.id.replace(/^.{3}/g, '666')
|
||||||
|
|
||||||
|
const isValid = verifySignature(event)
|
||||||
|
|
||||||
|
expect(isValid).toEqual(false)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('getSignature', () => {
|
describe('getSignature', () => {
|
||||||
@@ -296,9 +318,9 @@ describe('Event', () => {
|
|||||||
const sig = getSignature(unsignedEvent, privateKey)
|
const sig = getSignature(unsignedEvent, privateKey)
|
||||||
|
|
||||||
// verify the signature
|
// verify the signature
|
||||||
// @ts-expect-error
|
|
||||||
const isValid = verifySignature({
|
const isValid = verifySignature({
|
||||||
...unsignedEvent,
|
...unsignedEvent,
|
||||||
|
id: getEventHash(unsignedEvent),
|
||||||
sig,
|
sig,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
16
event.ts
16
event.ts
@@ -71,11 +71,12 @@ export function getBlankEvent<K>(kind: K | Kind.Blank = Kind.Blank) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function finishEvent<K extends number = number>(t: EventTemplate<K>, privateKey: string): Event<K> {
|
export function finishEvent<K extends number = number>(t: EventTemplate<K>, privateKey: string): VerifiedEvent<K> {
|
||||||
let event = t as Event<K>
|
const event = t as VerifiedEvent<K>
|
||||||
event.pubkey = getPublicKey(privateKey)
|
event.pubkey = getPublicKey(privateKey)
|
||||||
event.id = getEventHash(event)
|
event.id = getEventHash(event)
|
||||||
event.sig = getSignature(event, privateKey)
|
event.sig = getSignature(event, privateKey)
|
||||||
|
event[verifiedSymbol] = true
|
||||||
return event
|
return event
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -115,11 +116,16 @@ export function validateEvent<T>(event: T): event is T & UnsignedEvent<number> {
|
|||||||
/** Verify the event's signature. This function mutates the event with a `verified` symbol, making it idempotent. */
|
/** Verify the event's signature. This function mutates the event with a `verified` symbol, making it idempotent. */
|
||||||
export function verifySignature<K extends number>(event: Event<K>): event is VerifiedEvent<K> {
|
export function verifySignature<K extends number>(event: Event<K>): event is VerifiedEvent<K> {
|
||||||
if (typeof event[verifiedSymbol] === 'boolean') return event[verifiedSymbol]
|
if (typeof event[verifiedSymbol] === 'boolean') return event[verifiedSymbol]
|
||||||
|
|
||||||
|
const hash = getEventHash(event)
|
||||||
|
if (hash !== event.id) {
|
||||||
|
return (event[verifiedSymbol] = false)
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
event[verifiedSymbol] = schnorr.verify(event.sig, getEventHash(event), 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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
11
pool.ts
11
pool.ts
@@ -79,10 +79,13 @@ export class SimplePool {
|
|||||||
let eosesMissing = relays.length
|
let eosesMissing = relays.length
|
||||||
|
|
||||||
let eoseSent = false
|
let eoseSent = false
|
||||||
let eoseTimeout = setTimeout(() => {
|
let eoseTimeout = setTimeout(
|
||||||
eoseSent = true
|
() => {
|
||||||
for (let cb of eoseListeners.values()) cb()
|
eoseSent = true
|
||||||
}, opts?.eoseSubTimeout || this.eoseSubTimeout)
|
for (let cb of eoseListeners.values()) cb()
|
||||||
|
},
|
||||||
|
opts?.eoseSubTimeout || this.eoseSubTimeout,
|
||||||
|
)
|
||||||
|
|
||||||
relays
|
relays
|
||||||
.filter((r, i, a) => a.indexOf(r) === i)
|
.filter((r, i, a) => a.indexOf(r) === i)
|
||||||
|
|||||||
Reference in New Issue
Block a user