Compare commits

..

1 Commits

Author SHA1 Message Date
fiatjaf
7447524028 add nip06. 2021-12-11 19:28:00 -03:00
4 changed files with 18 additions and 32 deletions

View File

@@ -1,5 +1,4 @@
import createHmac from 'create-hmac'
import randomBytes from 'randombytes'
import * as bip39 from 'bip39'
export function privateKeyFromSeed(seed) {

View File

@@ -1,6 +1,6 @@
{
"name": "nostr-tools",
"version": "0.9.1",
"version": "0.7.0",
"description": "Tools for making a Nostr client.",
"repository": {
"type": "git",

12
pool.js
View File

@@ -21,7 +21,7 @@ export function relayPool(globalPrivateKey) {
.filter(({policy}) => policy.read)
.map(({relay}) => [
relay.url,
relay.sub({filter, cb: event => cb(event, relay.url)}, id)
relay.sub({filter, cb: event => cb(event, relay.url)})
])
)
@@ -29,23 +29,19 @@ export function relayPool(globalPrivateKey) {
const activeFilters = filter
activeSubscriptions[id] = {
sub: ({cb = activeCallback, filter = activeFilters}) => {
sub: ({cb = activeCallback, filter = activeFilters}) =>
Object.entries(subControllers).map(([relayURL, sub]) => [
relayURL,
sub.sub({cb, filter}, id)
])
return activeSubscriptions[id]
},
]),
addRelay: relay => {
subControllers[relay.url] = relay.sub({cb, filter}, id)
return activeSubscriptions[id]
subControllers[relay.url] = relay.sub({cb, filter})
},
removeRelay: relayURL => {
if (relayURL in subControllers) {
subControllers[relayURL].unsub()
if (Object.keys(subControllers).length === 0) unsub()
}
return activeSubscriptions[id]
},
unsub: () => {
Object.values(subControllers).forEach(sub => sub.unsub())

View File

@@ -13,7 +13,7 @@ export function normalizeRelayURL(url) {
export function relayConnect(url, onNotice) {
url = normalizeRelayURL(url)
var ws, resolveOpen, untilOpen, wasClosed
var ws, resolveOpen, untilOpen
var openSubs = {}
let attemptNumber = 1
let nextAttemptSeconds = 1
@@ -34,13 +34,10 @@ export function relayConnect(url, onNotice) {
resolveOpen()
// restablish old subscriptions
if (wasClosed) {
wasClosed = false
for (let channel in openSubs) {
let filters = openSubs[channel]
let cb = channels[channel]
sub({cb, filter: filters}, channel)
}
for (let channel in openSubs) {
let filters = openSubs[channel]
let cb = channels[channel]
sub({cb, filter: filters}, channel)
}
}
ws.onerror = () => {
@@ -49,10 +46,7 @@ export function relayConnect(url, onNotice) {
ws.onclose = () => {
resetOpenState()
attemptNumber++
nextAttemptSeconds += attemptNumber ** 3
if (nextAttemptSeconds > 14400) {
nextAttemptSeconds = 14400 // 4 hours
}
nextAttemptSeconds += attemptNumber
console.log(
`relay ${url} connection closed. reconnecting in ${nextAttemptSeconds} seconds.`
)
@@ -61,8 +55,6 @@ export function relayConnect(url, onNotice) {
connect()
} catch (err) {}
}, nextAttemptSeconds * 1000)
wasClosed = true
}
ws.onmessage = async e => {
@@ -92,6 +84,8 @@ export function relayConnect(url, onNotice) {
if (channels[channel]) {
channels[channel](event)
}
} else {
console.warn('got event with invalid signature from ' + url, event)
}
return
}
@@ -145,15 +139,12 @@ export function relayConnect(url, onNotice) {
try {
await trySend(['EVENT', event])
statusCallback(0)
let {unsub} = relay.sub(
{
cb: () => {
statusCallback(1)
},
filter: {id: event.id}
let {unsub} = relay.sub({
cb: () => {
statusCallback(1)
},
`monitor-${event.id.slice(0, 5)}`
)
filter: {id: event.id}
})
setTimeout(unsub, 5000)
} catch (err) {
statusCallback(-1)