nostr-tools
Advanced tools
Comparing version 0.1.0 to 0.1.2
16
event.js
@@ -11,3 +11,3 @@ import * as secp256k1 from 'noble-secp256k1' | ||
evt.kind, | ||
evt.tags, | ||
evt.tags || [], | ||
evt.content | ||
@@ -17,4 +17,4 @@ ]) | ||
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') | ||
@@ -25,4 +25,4 @@ } | ||
return await secp256k1.schnorr.verify( | ||
event.signature, | ||
getEventHash(event), | ||
event.sig, | ||
await getEventHash(event), | ||
event.pubkey | ||
@@ -33,6 +33,4 @@ ) | ||
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(eventHash, key) | ||
} |
{ | ||
"name": "nostr-tools", | ||
"version": "0.1.0", | ||
"version": "0.1.2", | ||
"description": "Tools for making a Nostr client.", | ||
@@ -15,5 +15,2 @@ "main": "index.js", | ||
}, | ||
"scripts": { | ||
"prepublish": "rollup -c rollup.config.js" | ||
}, | ||
"keywords": [ | ||
@@ -20,0 +17,0 @@ "decentralization", |
16
pool.js
@@ -11,6 +11,6 @@ import {getEventHash, signEvent} from './event' | ||
function propagateEvent(context, event, relayURL) { | ||
function propagateEvent(event, context, relayURL) { | ||
for (let i = 0; i < eventCallbacks.length; i++) { | ||
let {relay} = relays[relayURL] | ||
eventCallbacks[i](context, event, relay) | ||
eventCallbacks[i](event, context, relay) | ||
} | ||
@@ -37,3 +37,3 @@ } | ||
} else if (policyFilter.read && policy.read) { | ||
await fn(relays) | ||
await fn(relay) | ||
} | ||
@@ -85,3 +85,3 @@ } | ||
onNotice(cb) { | ||
noticeCallbacks(cb) | ||
noticeCallbacks.push(cb) | ||
}, | ||
@@ -93,3 +93,3 @@ offNotice(cb) { | ||
onAttempt(cb) { | ||
attemptCallbacks(cb) | ||
attemptCallbacks.push(cb) | ||
}, | ||
@@ -101,8 +101,8 @@ offAttempt(cb) { | ||
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 { | ||
@@ -109,0 +109,0 @@ throw new Error( |
@@ -17,3 +17,3 @@ # nostr-tools | ||
pool.onEvent((context, event, relay) => { | ||
pool.onEvent((event, context, relay) => { | ||
console.log(`got a relay with context ${context} from ${relay.url} which is already validated.`, event) | ||
@@ -20,0 +20,0 @@ }) |
24
relay.js
@@ -14,8 +14,9 @@ import PersistentWebSocket from 'pws' | ||
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 | ||
} | ||
) | ||
@@ -28,6 +29,7 @@ var isOpen | ||
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) | ||
@@ -38,10 +40,10 @@ ws.onmessage = async e => { | ||
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] | ||
let event = data[1] | ||
let event = data[0] | ||
let context = data[1] | ||
if (await verifySignature(event)) { | ||
onEvent(context, event) | ||
onEvent(event, context) | ||
} else { | ||
@@ -48,0 +50,0 @@ console.warn( |
@@ -1,6 +0,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) |
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
0
0
9390
7
258