mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2025-12-09 00:28:51 +00:00
fix dozens of errors so this new release may now actually work.
This commit is contained in:
12
event.js
12
event.js
@@ -13,22 +13,20 @@ export function serializeEvent(evt) {
|
|||||||
])
|
])
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getEventHash(event) {
|
export async function getEventHash(event) {
|
||||||
let eventHash = sha256(Buffer.from(serializeEvent(event)))
|
let eventHash = await sha256(Buffer.from(serializeEvent(event)))
|
||||||
return Buffer.from(eventHash).toString('hex')
|
return Buffer.from(eventHash).toString('hex')
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function verifySignature(event) {
|
export async function verifySignature(event) {
|
||||||
return await secp256k1.schnorr.verify(
|
return await secp256k1.schnorr.verify(
|
||||||
event.signature,
|
event.signature,
|
||||||
getEventHash(event),
|
await getEventHash(event),
|
||||||
event.pubkey
|
event.pubkey
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function signEvent(event, key) {
|
export async function signEvent(event, key) {
|
||||||
let eventHash = getEventHash(event)
|
let eventHash = await getEventHash(event)
|
||||||
return Buffer.from(await secp256k1.schnorr.sign(key, eventHash)).toString(
|
return await secp256k1.schnorr.sign(key, eventHash)
|
||||||
'hex'
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "nostr-tools",
|
"name": "nostr-tools",
|
||||||
"version": "0.1.0",
|
"version": "0.1.1",
|
||||||
"description": "Tools for making a Nostr client.",
|
"description": "Tools for making a Nostr client.",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"repository": {
|
"repository": {
|
||||||
|
|||||||
10
pool.js
10
pool.js
@@ -80,26 +80,26 @@ export function relayPool(globalPrivateKey) {
|
|||||||
if (index !== -1) eventCallbacks.splice(index, 1)
|
if (index !== -1) eventCallbacks.splice(index, 1)
|
||||||
},
|
},
|
||||||
onNotice(cb) {
|
onNotice(cb) {
|
||||||
noticeCallbacks(cb)
|
noticeCallbacks.push(cb)
|
||||||
},
|
},
|
||||||
offNotice(cb) {
|
offNotice(cb) {
|
||||||
let index = noticeCallbacks.indexOf(cb)
|
let index = noticeCallbacks.indexOf(cb)
|
||||||
if (index !== -1) noticeCallbacks.splice(index, 1)
|
if (index !== -1) noticeCallbacks.splice(index, 1)
|
||||||
},
|
},
|
||||||
onAttempt(cb) {
|
onAttempt(cb) {
|
||||||
attemptCallbacks(cb)
|
attemptCallbacks.push(cb)
|
||||||
},
|
},
|
||||||
offAttempt(cb) {
|
offAttempt(cb) {
|
||||||
let index = attemptCallbacks.indexOf(cb)
|
let index = attemptCallbacks.indexOf(cb)
|
||||||
if (index !== -1) attemptCallbacks.splice(index, 1)
|
if (index !== -1) attemptCallbacks.splice(index, 1)
|
||||||
},
|
},
|
||||||
async publish(event) {
|
async publish(event) {
|
||||||
if (!event.signature) {
|
if (!event.sig) {
|
||||||
event.tags = event.tags || []
|
event.tags = event.tags || []
|
||||||
|
|
||||||
if (globalPrivateKey) {
|
if (globalPrivateKey) {
|
||||||
event.id = getEventHash(event)
|
event.id = await getEventHash(event)
|
||||||
event.signature = await signEvent(event, globalPrivateKey)
|
event.sig = await signEvent(event, globalPrivateKey)
|
||||||
} else {
|
} else {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
"can't publish unsigned event. either sign this event beforehand or pass a private key while initializing this relay pool so it can be signed automatically."
|
"can't publish unsigned event. either sign this event beforehand or pass a private key while initializing this relay pool so it can be signed automatically."
|
||||||
|
|||||||
18
relay.js
18
relay.js
@@ -11,12 +11,13 @@ export function normalizeRelayURL(url) {
|
|||||||
|
|
||||||
export function relayConnect(url, onEvent, onNotice) {
|
export function relayConnect(url, onEvent, onNotice) {
|
||||||
url = normalizeRelayURL(url)
|
url = normalizeRelayURL(url)
|
||||||
url = url +=
|
|
||||||
(url.indexOf('?') !== -1 ? '&' : '?') + `session=${Math.random()}`
|
|
||||||
|
|
||||||
const ws = new PersistentWebSocket(url, {
|
const ws = new PersistentWebSocket(
|
||||||
pingTimeout: 30 * 1000
|
url + (url.indexOf('?') !== -1 ? '&' : '?') + `session=${Math.random()}`,
|
||||||
})
|
{
|
||||||
|
pingTimeout: 180 * 1000
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
var isOpen
|
var isOpen
|
||||||
let untilOpen = new Promise(resolve => {
|
let untilOpen = new Promise(resolve => {
|
||||||
@@ -24,16 +25,17 @@ export function relayConnect(url, onEvent, onNotice) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
ws.onopen = () => {
|
ws.onopen = () => {
|
||||||
console.log('connected to ', url)
|
console.log('connected to', url)
|
||||||
isOpen()
|
isOpen()
|
||||||
}
|
}
|
||||||
ws.onerror = err => console.log('error connecting', url, err)
|
ws.onerror = err => console.log('error connecting to relay', url, err)
|
||||||
|
ws.onclose = () => console.log('relay connection closed', url)
|
||||||
|
|
||||||
ws.onmessage = async e => {
|
ws.onmessage = async e => {
|
||||||
let data = JSON.parse(e.data)
|
let data = JSON.parse(e.data)
|
||||||
if (data.length > 1) {
|
if (data.length > 1) {
|
||||||
if (data[0] === 'notice') {
|
if (data[0] === 'notice') {
|
||||||
console.log('message from relay ' + url + ' :' + data[1])
|
console.log('message from relay ' + url + ': ' + data[1])
|
||||||
onNotice(data[1])
|
onNotice(data[1])
|
||||||
} else if (typeof data[0] === 'object') {
|
} else if (typeof data[0] === 'object') {
|
||||||
let context = data[0]
|
let context = data[0]
|
||||||
|
|||||||
4
utils.js
4
utils.js
@@ -1,6 +1,6 @@
|
|||||||
import * as secp256k1 from 'noble-secp256k1'
|
import secp256k1 from 'noble-secp256k1'
|
||||||
|
|
||||||
export const makeRandom32 = () => secp256k1.utils.generateRandomPrivateKey()
|
export const makeRandom32 = () => secp256k1.utils.randomPrivateKey()
|
||||||
export const sha256 = m => secp256k1.utils.sha256(Uint8Array.from(m))
|
export const sha256 = m => secp256k1.utils.sha256(Uint8Array.from(m))
|
||||||
export const getPublicKey = privateKey =>
|
export const getPublicKey = privateKey =>
|
||||||
secp256k1.schnorr.getPublicKey(privateKey)
|
secp256k1.schnorr.getPublicKey(privateKey)
|
||||||
|
|||||||
Reference in New Issue
Block a user