From 491b98ae72684422ba6ce0045a47c389074bfc62 Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Thu, 27 Jul 2023 11:26:26 -0400 Subject: [PATCH] 1. Increases wait time to download events to 5 minutes 2. Adds multiple filters to do authors and citations at the same time. 3. Increases the list of fixed relays 4. Downloads a list of online relays available --- index.html | 6 +-- js/nostr-broadcast.js | 4 +- js/nostr-utils.js | 122 ++++++++++++++++++++++++++++++++++++++++++ js/relays.js | 14 ++++- 4 files changed, 140 insertions(+), 6 deletions(-) create mode 100644 js/nostr-utils.js diff --git a/index.html b/index.html index e9fb548..b2fbcf1 100644 --- a/index.html +++ b/index.html @@ -108,7 +108,7 @@ id="fetching-progress" name="fetching-progress" min="0" - max="20" + max="300" value="0" style="visibility: hidden" />

@@ -125,7 +125,7 @@ id="broadcasting-progress" name="broadcasting-progress" min="0" - max="20" + max="300" value="0" style="visibility: hidden" />

@@ -191,7 +191,7 @@ - + diff --git a/js/nostr-broadcast.js b/js/nostr-broadcast.js index 831cf2e..e6e080f 100644 --- a/js/nostr-broadcast.js +++ b/js/nostr-broadcast.js @@ -31,8 +31,8 @@ const fetchAndBroadcast = async () => { $('#fetching-progress').val(currValue + 1) }, 1000) // get all events from relays - const filter = { authors: [pubkey] } - const data = await getEvents(filter) + const filters =[{ authors: [pubkey] }, { "#p": [pubkey] }] + const data = await getEvents(filters) // inform user fetching is done $('#fetching-status').html(txt.fetching + checkMark) clearInterval(fetchInterval) diff --git a/js/nostr-utils.js b/js/nostr-utils.js new file mode 100644 index 0000000..9d660cb --- /dev/null +++ b/js/nostr-utils.js @@ -0,0 +1,122 @@ +// from https://github.com/paulmillr/noble-secp256k1/blob/main/index.ts#L803 +function hexToBytes(hex) { + if (typeof hex !== 'string') { + throw new TypeError('hexToBytes: expected string, got ' + typeof hex) + } + if (hex.length % 2) + throw new Error('hexToBytes: received invalid unpadded hex' + hex.length) + const array = new Uint8Array(hex.length / 2) + for (let i = 0; i < array.length; i++) { + const j = i * 2 + const hexByte = hex.slice(j, j + 2) + const byte = Number.parseInt(hexByte, 16) + if (Number.isNaN(byte) || byte < 0) throw new Error('Invalid byte sequence') + array[i] = byte + } + return array + } + + // decode nip19 ('npub') to hex + const npub2hexa = (npub) => { + let { prefix, words } = bech32.bech32.decode(npub, 90) + if (prefix === 'npub') { + let data = new Uint8Array(bech32.bech32.fromWords(words)) + return buffer.Buffer.from(data).toString('hex') + } + } + + // encode hex to nip19 ('npub') + const hexa2npub = (hex) => { + const data = hexToBytes(hex) + const words = bech32.bech32.toWords(data) + const prefix = 'npub' + return bech32.bech32.encode(prefix, words, 90) + } + + // parse inserted pubkey + const parsePubkey = (pubkey) => + pubkey.match('npub1') ? npub2hexa(pubkey) : pubkey + + // download js file + const downloadFile = (data, fileName) => { + const prettyJs = 'const data = ' + JSON.stringify(data, null, 2) + const tempLink = document.createElement('a') + const taBlob = new Blob([prettyJs], { type: 'text/javascript' }) + tempLink.setAttribute('href', URL.createObjectURL(taBlob)) + tempLink.setAttribute('download', fileName) + tempLink.click() + } + + // fetch events from relay, returns a promise + const fetchFromRelay = async (relay, filters, events) => + new Promise((resolve, reject) => { + try { + // prevent hanging forever + setTimeout(() => reject('timeout'), 300_000) + // open websocket + const ws = new WebSocket(relay) + // subscription id + const subsId = 'my-sub' + // subscribe to events filtered by author + ws.onopen = () => { + ws.send(JSON.stringify(['REQ', subsId].concat(filters))) + } + + // Listen for messages + ws.onmessage = (event) => { + const [msgType, subscriptionId, data] = JSON.parse(event.data) + // event messages + if (msgType === 'EVENT' && subscriptionId === subsId) { + const { id } = data + // prevent duplicated events + if (events[id]) return + else events[id] = data + // show how many events were found until this moment + $('#events-found').text(`${Object.keys(events).length} events found`) + } + // end of subscription messages + if (msgType === 'EOSE' && subscriptionId === subsId) resolve() + } + ws.onerror = (err) => reject(err) + } catch (exception) { + reject(exception) + } + }) + + // query relays for events published by this pubkey + const getEvents = async (filters) => { + // events hash + const events = {} + // wait for all relays to finish + await Promise.allSettled( + relays.map((relay) => fetchFromRelay(relay, filters, events)) + ) + // return data as an array of events + return Object.keys(events).map((id) => events[id]) + } + + // send events to a relay, returns a promisse + const sendToRelay = async (relay, data) => + new Promise((resolve, reject) => { + try { + // prevent hanging forever + setTimeout(() => reject('timeout'), 20_000) + const ws = new WebSocket(relay) + // fetch events from relay + ws.onopen = () => { + for (evnt of data) { + ws.send(JSON.stringify(['EVENT', evnt])) + } + ws.close() + resolve(`done for ${relay}`) + } + ws.onerror = (err) => reject(err) + } catch (exception) { + reject(exception) + } + }) + + // broadcast events to list of relays + const broadcastEvents = async (data) => { + await Promise.allSettled(relays.map((relay) => sendToRelay(relay, data))) + } \ No newline at end of file diff --git a/js/relays.js b/js/relays.js index 195e008..4f22f8a 100644 --- a/js/relays.js +++ b/js/relays.js @@ -1,7 +1,8 @@ // list of paid relays from: // https://thebitcoinmanual.com/articles/paid-nostr-relay/ -const relays = [ + +const fixedRelays = [ 'wss://atlas.nostr.land', // paid relay 15000 npub12262qa4uhw7u8gdwlgmntqtv7aye8vdcmvszkqwgs0zchel6mz7s6cgrkj 'wss://bitcoiner.social', // paid relay 1000 npub1dxs2pygtfxsah77yuncsmu3ttqr274qr5g5zva3c7t5s3jtgy2xszsn4st 'wss://brb.io', @@ -43,4 +44,15 @@ const relays = [ 'wss://relay.snort.social', 'wss://relayer.fiatjaf.com', 'wss://rsslay.fiatjaf.com', + 'wss://no.str.cr', + 'wss://nostr.oxtr.dev', + 'wss://nostr.mom', + 'wss://relay.nostr.ch', + 'wss://relay.nostr.band' ] + +var relays = [] + +fetch("https://api.nostr.watch/v1/online") + .then(response => response.json()) + .then(json => relays = fixedRelays.concat(json));