mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2025-12-08 16:28:49 +00:00
initial commit.
This commit is contained in:
39
event.js
Normal file
39
event.js
Normal file
@@ -0,0 +1,39 @@
|
||||
import shajs from 'sha.js'
|
||||
import BigInteger from 'bigi'
|
||||
import schnorr from 'bip-schnorr'
|
||||
|
||||
export function serializeEvent(evt) {
|
||||
return JSON.stringify([
|
||||
0,
|
||||
evt.pubkey,
|
||||
evt.created_at,
|
||||
evt.kind,
|
||||
evt.tags,
|
||||
evt.content
|
||||
])
|
||||
}
|
||||
|
||||
export function getEventID(event) {
|
||||
let hash = shajs('sha256').update(serializeEvent(event)).digest()
|
||||
return hash.toString('hex')
|
||||
}
|
||||
|
||||
export function verifySignature(event) {
|
||||
try {
|
||||
schnorr.verify(
|
||||
Buffer.from(event.pubkey, 'hex'),
|
||||
Buffer.from(getEventID(event), 'hex'),
|
||||
Buffer.from(event.sig, 'hex')
|
||||
)
|
||||
return true
|
||||
} catch (err) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
export function signEvent(event, key) {
|
||||
let eventHash = shajs('sha256').update(serializeEvent(event)).digest()
|
||||
schnorr
|
||||
.sign(new BigInteger(key, 16), eventHash, makeRandom32())
|
||||
.toString('hex')
|
||||
}
|
||||
Reference in New Issue
Block a user