fix dozens of errors so this new release may now actually work.

This commit is contained in:
fiatjaf 2021-01-09 22:00:35 -03:00
parent 44edef63f9
commit 65e6f22a00
5 changed files with 23 additions and 23 deletions

View File

@ -13,22 +13,20 @@ export function serializeEvent(evt) {
])
}
export function getEventHash(event) {
let eventHash = sha256(Buffer.from(serializeEvent(event)))
export async function getEventHash(event) {
let eventHash = await sha256(Buffer.from(serializeEvent(event)))
return Buffer.from(eventHash).toString('hex')
}
export async function verifySignature(event) {
return await secp256k1.schnorr.verify(
event.signature,
getEventHash(event),
await getEventHash(event),
event.pubkey
)
}
export async function signEvent(event, key) {
let eventHash = getEventHash(event)
return Buffer.from(await secp256k1.schnorr.sign(key, eventHash)).toString(
'hex'
)
let eventHash = await getEventHash(event)
return await secp256k1.schnorr.sign(key, eventHash)
}

View File

@ -1,6 +1,6 @@
{
"name": "nostr-tools",
"version": "0.1.0",
"version": "0.1.1",
"description": "Tools for making a Nostr client.",
"main": "index.js",
"repository": {

10
pool.js
View File

@ -80,26 +80,26 @@ export function relayPool(globalPrivateKey) {
if (index !== -1) eventCallbacks.splice(index, 1)
},
onNotice(cb) {
noticeCallbacks(cb)
noticeCallbacks.push(cb)
},
offNotice(cb) {
let index = noticeCallbacks.indexOf(cb)
if (index !== -1) noticeCallbacks.splice(index, 1)
},
onAttempt(cb) {
attemptCallbacks(cb)
attemptCallbacks.push(cb)
},
offAttempt(cb) {
let index = attemptCallbacks.indexOf(cb)
if (index !== -1) attemptCallbacks.splice(index, 1)
},
async publish(event) {
if (!event.signature) {
if (!event.sig) {
event.tags = event.tags || []
if (globalPrivateKey) {
event.id = getEventHash(event)
event.signature = await signEvent(event, globalPrivateKey)
event.id = await getEventHash(event)
event.sig = await signEvent(event, globalPrivateKey)
} else {
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."

View File

@ -11,12 +11,13 @@ export function normalizeRelayURL(url) {
export function relayConnect(url, onEvent, onNotice) {
url = normalizeRelayURL(url)
url = url +=
(url.indexOf('?') !== -1 ? '&' : '?') + `session=${Math.random()}`
const ws = new PersistentWebSocket(url, {
pingTimeout: 30 * 1000
})
const ws = new PersistentWebSocket(
url + (url.indexOf('?') !== -1 ? '&' : '?') + `session=${Math.random()}`,
{
pingTimeout: 180 * 1000
}
)
var isOpen
let untilOpen = new Promise(resolve => {
@ -24,16 +25,17 @@ export function relayConnect(url, onEvent, onNotice) {
})
ws.onopen = () => {
console.log('connected to ', url)
console.log('connected to', url)
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 => {
let data = JSON.parse(e.data)
if (data.length > 1) {
if (data[0] === 'notice') {
console.log('message from relay ' + url + ' :' + data[1])
console.log('message from relay ' + url + ': ' + data[1])
onNotice(data[1])
} else if (typeof data[0] === 'object') {
let context = data[0]

View File

@ -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 getPublicKey = privateKey =>
secp256k1.schnorr.getPublicKey(privateKey)