Compare commits

..

3 Commits

Author SHA1 Message Date
fiatjaf
ce214ebbab small tweaks on relayConnect. 2021-12-30 15:02:05 -03:00
fiatjaf
800beb37f1 cut out the first byte of pubkeys. 2021-12-29 15:15:53 -03:00
fiatjaf
6d4916e6f7 eslint and minor fixes. 2021-12-29 14:35:28 -03:00
8 changed files with 63 additions and 48 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

@@ -13,7 +13,7 @@ export function generatePrivateKey() {
} }
export function getPublicKey(privateKey) { export function getPublicKey(privateKey) {
return Buffer.from( return Buffer.from(pointFromScalar(Buffer.from(privateKey, 'hex'), true))
pointFromScalar(Buffer.from(privateKey, 'hex'), true) .toString('hex')
).toString('hex') .slice(2)
} }

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.4",
"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'
@@ -11,7 +13,7 @@ export function normalizeRelayURL(url) {
return [host, ...qs].join('?') return [host, ...qs].join('?')
} }
export function relayConnect(url, onNotice) { export function relayConnect(url, onNotice = () => {}, onError = () => {}) {
url = normalizeRelayURL(url) url = normalizeRelayURL(url)
var ws, resolveOpen, untilOpen, wasClosed var ws, resolveOpen, untilOpen, wasClosed
@@ -44,8 +46,9 @@ export function relayConnect(url, onNotice) {
} }
} }
} }
ws.onerror = () => { ws.onerror = (err) => {
console.log('error connecting to relay', url) console.log('error connecting to relay', url)
onError(err)
} }
ws.onclose = () => { ws.onclose = () => {
resetOpenState() resetOpenState()
@@ -144,22 +147,26 @@ export function relayConnect(url, onNotice) {
return { return {
url, url,
sub, sub,
async publish(event, statusCallback = status => {}) { async publish(event, statusCallback) {
try { try {
await trySend(['EVENT', event]) await trySend(['EVENT', event])
statusCallback(0) if (statusCallback) {
let {unsub} = relay.sub( statusCallback(0)
{ let {unsub} = sub(
cb: () => { {
statusCallback(1) cb: () => {
statusCallback(1)
unsub()
clearTimeout(willUnsub)
},
filter: {id: event.id}
}, },
filter: {id: event.id} `monitor-${event.id.slice(0, 5)}`
}, )
`monitor-${event.id.slice(0, 5)}` let willUnsub = setTimeout(unsub, 5000)
) }
setTimeout(unsub, 5000)
} catch (err) { } catch (err) {
statusCallback(-1) if (statusCallback) statusCallback(-1)
} }
}, },
close() { close() {