Compare commits

..

1 Commits

Author SHA1 Message Date
fiatjaf
6d4916e6f7 eslint and minor fixes. 2021-12-29 14:35:28 -03:00
7 changed files with 42 additions and 32 deletions

View File

@@ -1,4 +1,5 @@
{ {
"root": true,
"parserOptions": { "parserOptions": {
"ecmaVersion": 9, "ecmaVersion": 9,
"ecmaFeatures": { "ecmaFeatures": {

View File

@@ -34,13 +34,13 @@ export function verifySignature(event) {
if (event.id !== getEventHash(event)) return false if (event.id !== getEventHash(event)) return false
return verifySchnorr( return verifySchnorr(
Buffer.from(event.id, 'hex'), Buffer.from(event.id, 'hex'),
Buffer.from(event.pubkey, 'hex') Buffer.from(event.pubkey, 'hex'),
Buffer.from(event.sig, 'hex'), Buffer.from(event.sig, 'hex')
) )
} }
export function signEvent(event, key) { export function signEvent(event, key) {
let eventHash = Buffer.from(getEventHash(event), 'hex') let eventHash = Buffer.from(getEventHash(event), 'hex')
let key = Buffer.from(key, 'hex') let keyB = Buffer.from(key, 'hex')
return Buffer.from(signSchnorr(eventHash, key)).toString('hex') return Buffer.from(signSchnorr(eventHash, keyB)).toString('hex')
} }

View File

@@ -11,6 +11,7 @@ import {
import {matchFilter, matchFilters} from './filter' import {matchFilter, matchFilters} from './filter'
export { export {
generatePrivateKey,
relayConnect, relayConnect,
relayPool, relayPool,
signEvent, signEvent,

View File

@@ -1,4 +1,3 @@
import createHmac from 'create-hmac'
import {wordlist} from 'micro-bip39/wordlists/english' import {wordlist} from 'micro-bip39/wordlists/english'
import { import {
generateMnemonic, generateMnemonic,

View File

@@ -1,6 +1,6 @@
{ {
"name": "nostr-tools", "name": "nostr-tools",
"version": "0.12.1", "version": "0.12.2",
"description": "Tools for making a Nostr client.", "description": "Tools for making a Nostr client.",
"repository": { "repository": {
"type": "git", "type": "git",
@@ -12,7 +12,6 @@
"browserify-cipher": ">=1", "browserify-cipher": ">=1",
"buffer": ">=5", "buffer": ">=5",
"create-hash": "^1.2.0", "create-hash": "^1.2.0",
"create-hmac": ">=1",
"dns-packet": "^5.2.4", "dns-packet": "^5.2.4",
"micro-bip39": "^0.1.3", "micro-bip39": "^0.1.3",
"randombytes": ">=2", "randombytes": ">=2",
@@ -30,5 +29,9 @@
"censorship", "censorship",
"censorship-resistance", "censorship-resistance",
"client" "client"
] ],
"devDependencies": {
"eslint": "^8.5.0",
"eslint-plugin-babel": "^5.3.1"
}
} }

50
pool.js
View File

@@ -3,7 +3,6 @@ import {relayConnect, normalizeRelayURL} from './relay'
export function relayPool(globalPrivateKey) { export function relayPool(globalPrivateKey) {
const relays = {} const relays = {}
const globalSub = []
const noticeCallbacks = [] const noticeCallbacks = []
function propagateNotice(notice, relayURL) { function propagateNotice(notice, relayURL) {
@@ -28,29 +27,34 @@ export function relayPool(globalPrivateKey) {
const activeCallback = cb const activeCallback = cb
const activeFilters = filter const activeFilters = filter
activeSubscriptions[id] = { const unsub = () => {
sub: ({cb = activeCallback, filter = activeFilters}) => { Object.values(subControllers).forEach(sub => sub.unsub())
Object.entries(subControllers).map(([relayURL, sub]) => [ delete activeSubscriptions[id]
relayURL, }
sub.sub({cb, filter}, id) const sub = ({cb = activeCallback, filter = activeFilters}) => {
]) Object.entries(subControllers).map(([relayURL, sub]) => [
return activeSubscriptions[id] relayURL,
}, sub.sub({cb, filter}, id)
addRelay: relay => { ])
subControllers[relay.url] = relay.sub({cb, filter}, id) return activeSubscriptions[id]
return activeSubscriptions[id] }
}, const addRelay = relay => {
removeRelay: relayURL => { subControllers[relay.url] = relay.sub({cb, filter}, id)
if (relayURL in subControllers) { return activeSubscriptions[id]
subControllers[relayURL].unsub() }
if (Object.keys(subControllers).length === 0) unsub() const removeRelay = relayURL => {
} if (relayURL in subControllers) {
return activeSubscriptions[id] subControllers[relayURL].unsub()
}, if (Object.keys(subControllers).length === 0) unsub()
unsub: () => {
Object.values(subControllers).forEach(sub => sub.unsub())
delete activeSubscriptions[id]
} }
return activeSubscriptions[id]
}
activeSubscriptions[id] = {
sub,
unsub,
addRelay,
removeRelay
} }
return activeSubscriptions[id] return activeSubscriptions[id]

View File

@@ -1,3 +1,5 @@
/* global WebSocket */
import 'websocket-polyfill' import 'websocket-polyfill'
import {verifySignature} from './event' import {verifySignature} from './event'
@@ -148,7 +150,7 @@ export function relayConnect(url, onNotice) {
try { try {
await trySend(['EVENT', event]) await trySend(['EVENT', event])
statusCallback(0) statusCallback(0)
let {unsub} = relay.sub( let {unsub} = sub(
{ {
cb: () => { cb: () => {
statusCallback(1) statusCallback(1)