Tools for developing Nostr clients.
Go to file
fiatjaf 8edcea2ed3 fix buffer import and use rollup for transpiling the package. 2021-12-11 08:01:03 -03:00
.eslintrc.json update secp256k1 library, add nip04.js 2021-12-10 21:41:05 -03:00
.gitignore remove yarn.lock 2021-02-10 00:13:14 -03:00
.prettierrc.yaml update secp256k1 library, add nip04.js 2021-12-10 21:41:05 -03:00
README.md add channel reusing and cancellation to README. 2021-05-22 21:24:19 -03:00
event.js fix buffer import and use rollup for transpiling the package. 2021-12-11 08:01:03 -03:00
index.js fix buffer import and use rollup for transpiling the package. 2021-12-11 08:01:03 -03:00
nip04.js fix buffer import and use rollup for transpiling the package. 2021-12-11 08:01:03 -03:00
nip05.js fix buffer import and use rollup for transpiling the package. 2021-12-11 08:01:03 -03:00
package.json fix buffer import and use rollup for transpiling the package. 2021-12-11 08:01:03 -03:00
pool.js fix some important bugs so it is actually usable. 2021-05-22 22:14:27 -03:00
relay.js fix some important bugs so it is actually usable. 2021-05-22 22:14:27 -03:00
rollup.config.js fix buffer import and use rollup for transpiling the package. 2021-12-11 08:01:03 -03:00
utils.js update secp256k1 library, add nip04.js 2021-12-10 21:41:05 -03:00

README.md

nostr-tools

Tools for developing Nostr clients.

Usage

import {relayPool} from 'nostr-tools'

const pool = relayPool()

pool.setPrivateKey('<hex>') // optional

pool.addRelay('ws://some.relay.com', {read: true, write: true})
pool.addRelay('ws://other.relay.cool', {read: true, write: true})

// example callback function for a subscription
function onEvent(event, relay) => {
  console.log(`got an event from ${relay.url} which is already validated.`, event)
}

// subscribing to a single user
// author is the user's public key
pool.sub({cb: onEvent, filter: {author: '<hex>'}})

//  or bulk follow
pool.sub({cb:(event, relay) => {...}, filter: {authors: ['<hex1>', '<hex2>', ..., '<hexn>']}})

// reuse a subscription channel
const mySubscription = pool.sub({cb: ..., filter: ....})
mySubscription.sub({filter: ....})
mySubscription.sub({cb: ...})
mySubscription.unsub()

// get specific event
const specificChannel = pool.sub({
  cb: (event, relay) => {
    console.log('got specific event from relay', event, relay)
    specificChannel.unsub()
  },
  filter: {id: '<hex>'}
})

// or get a specific event plus all the events that reference it in the 'e' tag
pool.sub({ cb: (event, relay) => { ... }, filter: [{id: '<hex>'}, {'#e': '<hex>'}] })

// get all events
pool.sub({cb: (event, relay) => {...}, filter: {}})

// get recent events
pool.sub({cb: (event, relay) => {...}, filter: {since: timestamp}})

// publishing events(inside an async function):
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)
  }
})
// it will be signed automatically with the key supplied above
// or pass an already signed event to bypass this

// subscribing to a new relay
pool.addRelay('<url>')
// will automatically subscribe to the all the events called with .sub above

For other utils please read the source (for now).