nostr-tools
Tools for developing Nostr clients.
Usage
import {relayPool} from 'nostr-tools'
const pool = relayPool()
pool.setPrivateKey('<hex>')
pool.addRelay('ws://some.relay.com', {read: true, write: true})
pool.addRelay('ws://other.relay.cool', {read: true, write: true})
function onEvent(event, relay) => {
console.log(`got an event from ${relay.url} which is already validated.`, event)
}
pool.sub({cb: onEvent, filter: {author: '<hex>'}})
pool.sub({cb:(event, relay) => {...}, filter: {authors: ['<hex1>', '<hex2>', ..., '<hexn>']}})
const mySubscription = pool.sub({cb: ..., filter: ....})
mySubscription.sub({filter: ....})
mySubscription.sub({cb: ...})
mySubscription.unsub()
const specificChannel = pool.sub({
cb: (event, relay) => {
console.log('got specific event from relay', event, relay)
specificChannel.unsub()
},
filter: {id: '<hex>'}
})
pool.sub({ cb: (event, relay) => { ... }, filter: [{id: '<hex>'}, {'#e': '<hex>'}] })
pool.sub({cb: (event, relay) => {...}, filter: {}})
pool.sub({cb: (event, relay) => {...}, filter: {since: timestamp}})
const ev = await pool.publish(eventObject, (status, url) => {
if (status === 0) {
console.log(`publish request sent to ${url}`)
}
if (status === 1) {
console.log(`event published by ${url}`, ev)
}
})
pool.addRelay('<url>')
For other utils please read the source (for now).