mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2025-12-08 16:28:49 +00:00
Compare commits
29 Commits
v0.24.1
...
v1.0.0-alp
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
50c8bb72f9 | ||
|
|
72781e0eab | ||
|
|
bf120c1348 | ||
|
|
3630d377e5 | ||
|
|
53b0091bf4 | ||
|
|
1a7cc5f21f | ||
|
|
1162935f58 | ||
|
|
a49d971f6a | ||
|
|
897919be3b | ||
|
|
39aca167fb | ||
|
|
de8bdd8370 | ||
|
|
46a0a342db | ||
|
|
4fe2a9c91a | ||
|
|
e62b833464 | ||
|
|
100c77d2aa | ||
|
|
12be5a5338 | ||
|
|
b955ba2a09 | ||
|
|
ec805be4ab | ||
|
|
92fb339afb | ||
|
|
f8f125270a | ||
|
|
1b798b2eee | ||
|
|
ae717a1a4a | ||
|
|
b2015c8fe5 | ||
|
|
c5d2e3b037 | ||
|
|
0ef5d1e19c | ||
|
|
7d9d10fdb1 | ||
|
|
a1e1ce131a | ||
|
|
cdb07bb175 | ||
|
|
1f1bcff803 |
@@ -1,5 +1,9 @@
|
||||
{
|
||||
"root": true,
|
||||
|
||||
"parser": "@typescript-eslint/parser",
|
||||
"plugins": ["@typescript-eslint"],
|
||||
|
||||
"parserOptions": {
|
||||
"ecmaVersion": 9,
|
||||
"ecmaFeatures": {
|
||||
@@ -14,9 +18,7 @@
|
||||
"node": true
|
||||
},
|
||||
|
||||
"plugins": [
|
||||
"babel"
|
||||
],
|
||||
"plugins": ["babel"],
|
||||
|
||||
"globals": {
|
||||
"document": false,
|
||||
@@ -33,23 +35,23 @@
|
||||
|
||||
"rules": {
|
||||
"accessor-pairs": 2,
|
||||
"arrow-spacing": [2, { "before": true, "after": true }],
|
||||
"arrow-spacing": [2, {"before": true, "after": true}],
|
||||
"block-spacing": [2, "always"],
|
||||
"brace-style": [2, "1tbs", { "allowSingleLine": true }],
|
||||
"brace-style": [2, "1tbs", {"allowSingleLine": true}],
|
||||
"comma-dangle": 0,
|
||||
"comma-spacing": [2, { "before": false, "after": true }],
|
||||
"comma-spacing": [2, {"before": false, "after": true}],
|
||||
"comma-style": [2, "last"],
|
||||
"constructor-super": 2,
|
||||
"curly": [0, "multi-line"],
|
||||
"dot-location": [2, "property"],
|
||||
"eol-last": 2,
|
||||
"eqeqeq": [2, "allow-null"],
|
||||
"generator-star-spacing": [2, { "before": true, "after": true }],
|
||||
"handle-callback-err": [2, "^(err|error)$" ],
|
||||
"generator-star-spacing": [2, {"before": true, "after": true}],
|
||||
"handle-callback-err": [2, "^(err|error)$"],
|
||||
"indent": 0,
|
||||
"jsx-quotes": [2, "prefer-double"],
|
||||
"key-spacing": [2, { "beforeColon": false, "afterColon": true }],
|
||||
"keyword-spacing": [2, { "before": true, "after": true }],
|
||||
"key-spacing": [2, {"beforeColon": false, "afterColon": true}],
|
||||
"keyword-spacing": [2, {"before": true, "after": true}],
|
||||
"new-cap": 0,
|
||||
"new-parens": 0,
|
||||
"no-array-constructor": 2,
|
||||
@@ -81,12 +83,12 @@
|
||||
"no-irregular-whitespace": 2,
|
||||
"no-iterator": 2,
|
||||
"no-label-var": 2,
|
||||
"no-labels": [2, { "allowLoop": false, "allowSwitch": false }],
|
||||
"no-labels": [2, {"allowLoop": false, "allowSwitch": false}],
|
||||
"no-lone-blocks": 2,
|
||||
"no-mixed-spaces-and-tabs": 2,
|
||||
"no-multi-spaces": 2,
|
||||
"no-multi-str": 2,
|
||||
"no-multiple-empty-lines": [2, { "max": 2 }],
|
||||
"no-multiple-empty-lines": [2, {"max": 2}],
|
||||
"no-native-reassign": 2,
|
||||
"no-negated-in-lhs": 2,
|
||||
"no-new": 0,
|
||||
@@ -115,23 +117,34 @@
|
||||
"no-undef": 2,
|
||||
"no-undef-init": 2,
|
||||
"no-unexpected-multiline": 2,
|
||||
"no-unneeded-ternary": [2, { "defaultAssignment": false }],
|
||||
"no-unneeded-ternary": [2, {"defaultAssignment": false}],
|
||||
"no-unreachable": 2,
|
||||
"no-unused-vars": [2, { "vars": "local", "args": "none", "varsIgnorePattern": "^_"}],
|
||||
"no-unused-vars": [
|
||||
2,
|
||||
{"vars": "local", "args": "none", "varsIgnorePattern": "^_"}
|
||||
],
|
||||
"no-useless-call": 2,
|
||||
"no-useless-constructor": 2,
|
||||
"no-with": 2,
|
||||
"one-var": [0, { "initialized": "never" }],
|
||||
"operator-linebreak": [2, "after", { "overrides": { "?": "before", ":": "before" } }],
|
||||
"one-var": [0, {"initialized": "never"}],
|
||||
"operator-linebreak": [
|
||||
2,
|
||||
"after",
|
||||
{"overrides": {"?": "before", ":": "before"}}
|
||||
],
|
||||
"padded-blocks": [2, "never"],
|
||||
"quotes": [2, "single", { "avoidEscape": true, "allowTemplateLiterals": true }],
|
||||
"quotes": [
|
||||
2,
|
||||
"single",
|
||||
{"avoidEscape": true, "allowTemplateLiterals": true}
|
||||
],
|
||||
"semi": [2, "never"],
|
||||
"semi-spacing": [2, { "before": false, "after": true }],
|
||||
"semi-spacing": [2, {"before": false, "after": true}],
|
||||
"space-before-blocks": [2, "always"],
|
||||
"space-before-function-paren": 0,
|
||||
"space-in-parens": [2, "never"],
|
||||
"space-infix-ops": 2,
|
||||
"space-unary-ops": [2, { "words": true, "nonwords": false }],
|
||||
"space-unary-ops": [2, {"words": true, "nonwords": false}],
|
||||
"spaced-comment": 0,
|
||||
"template-curly-spacing": [2, "never"],
|
||||
"use-isnan": 2,
|
||||
|
||||
18
.github/workflows/npm-publish.yml
vendored
Normal file
18
.github/workflows/npm-publish.yml
vendored
Normal file
@@ -0,0 +1,18 @@
|
||||
name: publish npm package
|
||||
|
||||
on:
|
||||
release:
|
||||
types: [created]
|
||||
|
||||
jobs:
|
||||
publish-npm:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- uses: actions/setup-node@v3
|
||||
with:
|
||||
node-version: 16
|
||||
registry-url: https://registry.npmjs.org/
|
||||
- run: npm publish
|
||||
env:
|
||||
NODE_AUTH_TOKEN: ${{secrets.npm_token}}
|
||||
15
.github/workflows/test.yml
vendored
Normal file
15
.github/workflows/test.yml
vendored
Normal file
@@ -0,0 +1,15 @@
|
||||
name: test every commit
|
||||
on:
|
||||
- push
|
||||
|
||||
jobs:
|
||||
test:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: actions/setup-node@v3
|
||||
with:
|
||||
node-version: 18
|
||||
- run: yarn --ignore-engines
|
||||
- run: node build.cjs
|
||||
- run: yarn test
|
||||
5
.gitignore
vendored
5
.gitignore
vendored
@@ -2,5 +2,8 @@ node_modules
|
||||
dist
|
||||
yarn.lock
|
||||
package-lock.json
|
||||
nostr.js
|
||||
.envrc
|
||||
standalone
|
||||
cjs
|
||||
esm
|
||||
test.html
|
||||
|
||||
215
README.md
215
README.md
@@ -4,90 +4,161 @@ Tools for developing [Nostr](https://github.com/fiatjaf/nostr) clients.
|
||||
|
||||
## Usage
|
||||
|
||||
### Generating a private key and a public key
|
||||
|
||||
```js
|
||||
import {relayPool} from 'nostr-tools'
|
||||
import { generatePrivateKey, getPublicKey } 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
|
||||
let sk = generatePrivateKey() # `sk` is a hex string
|
||||
let pk = getPublicKey(sk) # `pk` is a hex string
|
||||
```
|
||||
|
||||
All functions expect bytearrays as hex strings and output bytearrays as hex strings.
|
||||
### Creating, signing and verifying events
|
||||
|
||||
For other utils please read the source (for now).
|
||||
```js
|
||||
import {
|
||||
validateEvent,
|
||||
verifySignature,
|
||||
signEvent,
|
||||
getEventHash,
|
||||
getPublicKey
|
||||
} from 'nostr-tools'
|
||||
|
||||
let event = {
|
||||
kind: 1,
|
||||
created_at: Math.floor(Date.now() / 1000),
|
||||
tags: [],
|
||||
content: 'hello'
|
||||
}
|
||||
|
||||
event.id = getEventHash(event.id)
|
||||
event.pubkey = getPublicKey(privateKey)
|
||||
event.sig = await signEvent(event, privateKey)
|
||||
|
||||
let ok = validateEvent(event)
|
||||
let veryOk = await verifySignature(event)
|
||||
```
|
||||
|
||||
### Interacting with a relay
|
||||
|
||||
```js
|
||||
import {
|
||||
relayInit,
|
||||
generatePrivateKey,
|
||||
getPublicKey,
|
||||
getEventHash,
|
||||
signEvent
|
||||
} from 'nostr-tools'
|
||||
|
||||
const relay = relayInit('wss://relay.example.com')
|
||||
relay.connect()
|
||||
|
||||
relay.on('connect', () => {
|
||||
console.log(`connected to ${relay.url}`)
|
||||
})
|
||||
relay.on('error', () => {
|
||||
console.log(`failed to connect to ${relay.url}`)
|
||||
})
|
||||
|
||||
// let's query for an event that exists
|
||||
let sub = relay.sub([
|
||||
{
|
||||
ids: ['d7dd5eb3ab747e16f8d0212d53032ea2a7cadef53837e5a6c66d42849fcb9027']
|
||||
}
|
||||
])
|
||||
sub.on('event', event => {
|
||||
console.log('we got the event we wanted:', event)
|
||||
})
|
||||
sub.on('eose', () => {
|
||||
sub.unsub()
|
||||
})
|
||||
|
||||
// let's publish a new event while simultaneously monitoring the relay for it
|
||||
let sk = generatePrivateKey()
|
||||
let pk = getPublicKey(sk)
|
||||
|
||||
let sub = relay.sub([
|
||||
{
|
||||
kinds: [1],
|
||||
authors: [pk]
|
||||
}
|
||||
])
|
||||
|
||||
sub.on('event', event => {
|
||||
console.log('got event:', event)
|
||||
})
|
||||
|
||||
let event = {
|
||||
kind: 1,
|
||||
pubkey: pk,
|
||||
created_at: Math.floor(Date.now() / 1000),
|
||||
tags: [],
|
||||
content: 'hello world'
|
||||
}
|
||||
event.id = getEventHash(event)
|
||||
event.sig = await signEvent(event, sk)
|
||||
|
||||
let pub = relay.publish(event)
|
||||
pub.on('ok', () => {
|
||||
console.log(`{relay.url} has accepted our event`)
|
||||
})
|
||||
pub.on('seen', () => {
|
||||
console.log(`we saw the event on {relay.url}`)
|
||||
})
|
||||
pub.on('failed', reason => {
|
||||
console.log(`failed to publish to {relay.url}: ${reason}`)
|
||||
})
|
||||
|
||||
await relay.close()
|
||||
```
|
||||
|
||||
### Encrypting and decrypting direct messages
|
||||
|
||||
```js
|
||||
import {nip04, getPublicKey, generatePrivateKey} from 'nostr-tools'
|
||||
|
||||
// sender
|
||||
let sk1 = generatePrivateKey()
|
||||
let pk1 = getPublicKey(sk1)
|
||||
|
||||
// receiver
|
||||
let sk2 = generatePrivateKey()
|
||||
let pk2 = getPublicKey(sk2)
|
||||
|
||||
// on the sender side
|
||||
let message = 'hello'
|
||||
let ciphertext = nip04.encrypt(sk1, pk2, 'hello')
|
||||
|
||||
let event = {
|
||||
kind: 4,
|
||||
pubkey: pk1,
|
||||
tags: [['p', pk2]],
|
||||
content: ciphertext,
|
||||
...otherProperties
|
||||
}
|
||||
|
||||
sendEvent(event)
|
||||
|
||||
// on the receiver side
|
||||
|
||||
sub.on('event', (event) => {
|
||||
let sender = event.tags.find(([k, v]) => k === 'p' && && v && v !== '')[1]
|
||||
pk1 === sender
|
||||
let plaintext = nip04.decrypt(sk2, pk1, event.content)
|
||||
})
|
||||
```
|
||||
|
||||
Please consult the tests or [the source code](https://github.com/fiatjaf/nostr-tools) for more information that isn't available here.
|
||||
|
||||
### Using from the browser (if you don't want to use a bundler)
|
||||
|
||||
You can import nostr-tools as an ES module. Just add a script tag like this:
|
||||
|
||||
```html
|
||||
<script type="module">
|
||||
import {generatePrivateKey} from 'https://unpkg.com/nostr-tools/nostr.js'
|
||||
console.log(generatePrivateKey())
|
||||
<script src="https://unpkg.com/nostr-tools/standalone/index.js"></script>
|
||||
<script>
|
||||
window.NostrTools.generatePrivateKey('...') // and so on
|
||||
</script>
|
||||
```
|
||||
|
||||
And import whatever function you would import from `"nostr-tools"` in a bundler.
|
||||
|
||||
## TypeScript
|
||||
|
||||
This module has hand-authored TypeScript declarations. `npm run check-ts` will run a lint-check script to ensure the typings can be loaded and call at least a few standard library functions. It's not at all comprehensive and likely to contain bugs. Issues welcome; tag @rcoder as needed.
|
||||
|
||||
## License
|
||||
|
||||
Public domain.
|
||||
|
||||
38
build.cjs
38
build.cjs
@@ -2,24 +2,36 @@
|
||||
|
||||
const esbuild = require('esbuild')
|
||||
const alias = require('esbuild-plugin-alias')
|
||||
const nodeGlobals = require('@esbuild-plugins/node-globals-polyfill').default
|
||||
|
||||
const buildOptions = {
|
||||
entryPoints: ['index.js'],
|
||||
outfile: 'nostr.js',
|
||||
let common = {
|
||||
entryPoints: ['index.ts'],
|
||||
bundle: true,
|
||||
format: 'esm',
|
||||
plugins: [
|
||||
alias({
|
||||
stream: require.resolve('readable-stream')
|
||||
}),
|
||||
nodeGlobals({buffer: true})
|
||||
})
|
||||
],
|
||||
define: {
|
||||
window: 'self',
|
||||
global: 'self'
|
||||
},
|
||||
loader: {'.js': 'jsx'}
|
||||
sourcemap: 'external'
|
||||
}
|
||||
|
||||
esbuild.build(buildOptions).then(() => console.log('build success.'))
|
||||
esbuild
|
||||
.build({...common, outdir: 'esm/', format: 'esm', packages: 'external'})
|
||||
.then(() => console.log('esm build success.'))
|
||||
|
||||
esbuild
|
||||
.build({...common, outdir: 'cjs/', format: 'cjs', packages: 'external'})
|
||||
.then(() => console.log('cjs build success.'))
|
||||
|
||||
esbuild
|
||||
.build({
|
||||
...common,
|
||||
outdir: 'standalone/',
|
||||
format: 'iife',
|
||||
globalName: 'NostrTools',
|
||||
define: {
|
||||
window: 'self',
|
||||
global: 'self',
|
||||
process: '{"env": {}}'
|
||||
}
|
||||
})
|
||||
.then(() => console.log('standalone build success.'))
|
||||
|
||||
49
event.test.js
Normal file
49
event.test.js
Normal file
@@ -0,0 +1,49 @@
|
||||
/* eslint-env jest */
|
||||
|
||||
const {
|
||||
validateEvent,
|
||||
verifySignature,
|
||||
signEvent,
|
||||
getEventHash,
|
||||
getPublicKey
|
||||
} = require('./cjs')
|
||||
|
||||
const event = {
|
||||
id: 'd7dd5eb3ab747e16f8d0212d53032ea2a7cadef53837e5a6c66d42849fcb9027',
|
||||
kind: 1,
|
||||
pubkey: '22a12a128a3be27cd7fb250cbe796e692896398dc1440ae3fa567812c8107c1c',
|
||||
created_at: 1670869179,
|
||||
content:
|
||||
'NOSTR "WINE-ACCOUNT" WITH HARVEST DATE STAMPED\n\n\n"The older the wine, the greater its reputation"\n\n\n22a12a128a3be27cd7fb250cbe796e692896398dc1440ae3fa567812c8107c1c\n\n\nNWA 2022-12-12\nAA',
|
||||
tags: [['client', 'astral']],
|
||||
sig: 'f110e4fdf67835fb07abc72469933c40bdc7334615610cade9554bf00945a1cebf84f8d079ec325d26fefd76fe51cb589bdbe208ac9cdbd63351ddad24a57559'
|
||||
}
|
||||
|
||||
const unsigned = {
|
||||
created_at: 1671217411,
|
||||
kind: 0,
|
||||
tags: [],
|
||||
content:
|
||||
'{"name":"fiatjaf","about":"buy my merch at fiatjaf store","picture":"https://fiatjaf.com/static/favicon.jpg","nip05":"_@fiatjaf.com"}'
|
||||
}
|
||||
|
||||
const privateKey =
|
||||
'5c6c25b7ef18d8633e97512159954e1aa22809c6b763e94b9f91071836d00217'
|
||||
|
||||
test('validate event', () => {
|
||||
expect(validateEvent(event)).toBeTruthy()
|
||||
})
|
||||
|
||||
test('check signature', async () => {
|
||||
expect(await verifySignature(event)).toBeTruthy()
|
||||
})
|
||||
|
||||
test('sign event', async () => {
|
||||
let sig = await signEvent(unsigned, privateKey)
|
||||
let hash = getEventHash(unsigned)
|
||||
let pubkey = getPublicKey(privateKey)
|
||||
|
||||
let signed = {...unsigned, id: hash, sig, pubkey}
|
||||
|
||||
expect(await verifySignature(signed)).toBeTruthy()
|
||||
})
|
||||
@@ -1,18 +1,29 @@
|
||||
import {Buffer} from 'buffer'
|
||||
import createHash from 'create-hash'
|
||||
// @ts-ignore
|
||||
import * as secp256k1 from '@noble/secp256k1'
|
||||
import {sha256} from '@noble/hashes/sha256'
|
||||
|
||||
export function getBlankEvent() {
|
||||
export type Event = {
|
||||
id?: string
|
||||
sig?: string
|
||||
kind: number
|
||||
tags: string[][]
|
||||
pubkey: string
|
||||
content: string
|
||||
created_at: number
|
||||
}
|
||||
|
||||
export function getBlankEvent(): Event {
|
||||
return {
|
||||
kind: 255,
|
||||
pubkey: null,
|
||||
pubkey: '',
|
||||
content: '',
|
||||
tags: [],
|
||||
created_at: 0
|
||||
}
|
||||
}
|
||||
|
||||
export function serializeEvent(evt) {
|
||||
export function serializeEvent(evt: Event): string {
|
||||
return JSON.stringify([
|
||||
0,
|
||||
evt.pubkey,
|
||||
@@ -23,14 +34,12 @@ export function serializeEvent(evt) {
|
||||
])
|
||||
}
|
||||
|
||||
export function getEventHash(event) {
|
||||
let eventHash = createHash('sha256')
|
||||
.update(Buffer.from(serializeEvent(event)))
|
||||
.digest()
|
||||
export function getEventHash(event: Event): string {
|
||||
let eventHash = sha256(Buffer.from(serializeEvent(event)))
|
||||
return Buffer.from(eventHash).toString('hex')
|
||||
}
|
||||
|
||||
export function validateEvent(event) {
|
||||
export function validateEvent(event: Event): boolean {
|
||||
if (event.id !== getEventHash(event)) return false
|
||||
if (typeof event.content !== 'string') return false
|
||||
if (typeof event.created_at !== 'number') return false
|
||||
@@ -47,12 +56,14 @@ export function validateEvent(event) {
|
||||
return true
|
||||
}
|
||||
|
||||
export function verifySignature(event) {
|
||||
export function verifySignature(
|
||||
event: Event & {id: string; sig: string}
|
||||
): Promise<boolean> {
|
||||
return secp256k1.schnorr.verify(event.sig, event.id, event.pubkey)
|
||||
}
|
||||
|
||||
export async function signEvent(event, key) {
|
||||
export async function signEvent(event: Event, key: string): Promise<string> {
|
||||
return Buffer.from(
|
||||
await secp256k1.schnorr.sign(getEventHash(event), key)
|
||||
await secp256k1.schnorr.sign(event.id || getEventHash(event), key)
|
||||
).toString('hex')
|
||||
}
|
||||
42
filter.test.js
Normal file
42
filter.test.js
Normal file
@@ -0,0 +1,42 @@
|
||||
/* eslint-env jest */
|
||||
|
||||
const {matchFilters} = require('./cjs')
|
||||
|
||||
test('test if filters match', () => {
|
||||
;[
|
||||
{
|
||||
filters: [{ids: ['i']}],
|
||||
good: [{id: 'i'}],
|
||||
bad: [{id: 'j'}]
|
||||
},
|
||||
{
|
||||
filters: [{authors: ['abc']}, {kinds: [1, 3]}],
|
||||
good: [
|
||||
{pubkey: 'xyz', kind: 3},
|
||||
{pubkey: 'abc', kind: 12},
|
||||
{pubkey: 'abc', kind: 1}
|
||||
],
|
||||
bad: [{pubkey: 'hhh', kind: 12}]
|
||||
},
|
||||
{
|
||||
filters: [{'#e': ['yyy'], since: 444}],
|
||||
good: [
|
||||
{
|
||||
tags: [
|
||||
['e', 'uuu'],
|
||||
['e', 'yyy']
|
||||
],
|
||||
created_at: 555
|
||||
}
|
||||
],
|
||||
bad: [{tags: [['e', 'uuu']], created_at: 111}]
|
||||
}
|
||||
].forEach(({filters, good, bad}) => {
|
||||
good.forEach(ev => {
|
||||
expect(matchFilters(filters, ev)).toBeTruthy()
|
||||
})
|
||||
bad.forEach(ev => {
|
||||
expect(matchFilters(filters, ev)).toBeFalsy()
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -1,4 +1,18 @@
|
||||
export function matchFilter(filter, event) {
|
||||
import {Event} from './event'
|
||||
|
||||
export type Filter = {
|
||||
ids?: string[]
|
||||
kinds?: number[]
|
||||
authors?: string[]
|
||||
since?: number
|
||||
until?: number
|
||||
[key: `#${string}`]: string[]
|
||||
}
|
||||
|
||||
export function matchFilter(
|
||||
filter: Filter,
|
||||
event: Event & {id: string}
|
||||
): boolean {
|
||||
if (filter.ids && filter.ids.indexOf(event.id) === -1) return false
|
||||
if (filter.kinds && filter.kinds.indexOf(event.kind) === -1) return false
|
||||
if (filter.authors && filter.authors.indexOf(event.pubkey) === -1)
|
||||
@@ -6,10 +20,12 @@ export function matchFilter(filter, event) {
|
||||
|
||||
for (let f in filter) {
|
||||
if (f[0] === '#') {
|
||||
let tagName = f.slice(1)
|
||||
let values = filter[`#${tagName}`]
|
||||
if (
|
||||
filter[f] &&
|
||||
values &&
|
||||
!event.tags.find(
|
||||
([t, v]) => t === f.slice(1) && filter[f].indexOf(v) !== -1
|
||||
([t, v]) => t === f.slice(1) && values.indexOf(v) !== -1
|
||||
)
|
||||
)
|
||||
return false
|
||||
@@ -22,7 +38,10 @@ export function matchFilter(filter, event) {
|
||||
return true
|
||||
}
|
||||
|
||||
export function matchFilters(filters, event) {
|
||||
export function matchFilters(
|
||||
filters: Filter[],
|
||||
event: Event & {id: string}
|
||||
): boolean {
|
||||
for (let i = 0; i < filters.length; i++) {
|
||||
if (matchFilter(filters[i], event)) return true
|
||||
}
|
||||
107
index.d.ts
vendored
107
index.d.ts
vendored
@@ -1,107 +0,0 @@
|
||||
import { type Buffer } from 'buffer';
|
||||
|
||||
// these should be available from the native @noble/secp256k1 type
|
||||
// declarations, but they somehow aren't so instead: copypasta
|
||||
declare type Hex = Uint8Array | string;
|
||||
declare type PrivKey = Hex | bigint | number;
|
||||
|
||||
declare enum EventKind {
|
||||
Metadata = 0,
|
||||
Text = 1,
|
||||
RelayRec = 2,
|
||||
Contacts = 3,
|
||||
DM = 4,
|
||||
Deleted = 5,
|
||||
}
|
||||
|
||||
// event.js
|
||||
declare type Event = {
|
||||
kind: EventKind,
|
||||
pubkey?: string,
|
||||
content: string,
|
||||
tags: string[],
|
||||
created_at: number,
|
||||
};
|
||||
|
||||
declare function getBlankEvent(): Event;
|
||||
declare function serializeEvent(event: Event): string;
|
||||
declare function getEventHash(event: Event): string;
|
||||
declare function validateEvent(event: Event): boolean;
|
||||
declare function validateSignature(event: Event): boolean;
|
||||
declare function signEvent(event: Event, key: PrivKey): Promise<[Uint8Array, number]>;
|
||||
|
||||
// filter.js
|
||||
declare type Filter = {
|
||||
ids: string[],
|
||||
kinds: EventKind[],
|
||||
authors: string[],
|
||||
since: number,
|
||||
until: number,
|
||||
"#e": string[],
|
||||
"#p": string[],
|
||||
};
|
||||
|
||||
declare function matchFilter(filter: Filter, event: Event): boolean;
|
||||
declare function matchFilters(filters: Filter[], event: Event): boolean;
|
||||
|
||||
// general
|
||||
declare type ClientMessage =
|
||||
["EVENT", Event] |
|
||||
["REQ", string, Filter[]] |
|
||||
["CLOSE", string];
|
||||
|
||||
declare type ServerMessage =
|
||||
["EVENT", string, Event] |
|
||||
["NOTICE", unknown];
|
||||
|
||||
// keys.js
|
||||
declare function generatePrivateKey(): string;
|
||||
declare function getPublicKey(privateKey: Buffer): string;
|
||||
|
||||
// pool.js
|
||||
declare type RelayPolicy = {
|
||||
read: boolean,
|
||||
write: boolean,
|
||||
};
|
||||
|
||||
declare type SubscriptionCallback = (event: Event, relay: string) => void;
|
||||
|
||||
declare type SubscriptionOptions = {
|
||||
cb: SubscriptionCallback,
|
||||
filter: Filter,
|
||||
skipVerification: boolean
|
||||
// TODO: thread through how `beforeSend` actually works before trying to type it
|
||||
// beforeSend(event: Event):
|
||||
};
|
||||
|
||||
declare type Subscription = {
|
||||
unsub(): void,
|
||||
};
|
||||
|
||||
declare type PublishCallback = (status: number) => void;
|
||||
|
||||
// relay.js
|
||||
declare type Relay = {
|
||||
url: string,
|
||||
sub: SubscriptionCallback,
|
||||
publish: (event: Event, cb: PublishCallback) => Promise<Event>,
|
||||
};
|
||||
|
||||
declare type PoolPublishCallback = (status: number, relay: string) => void;
|
||||
|
||||
declare type RelayPool = {
|
||||
setPrivateKey(key: string): void,
|
||||
addRelay(url: string, opts?: RelayPolicy): Relay,
|
||||
sub(opts: SubscriptionOptions, id?: string): Subscription,
|
||||
publish(event: Event, cb: PoolPublishCallback): Promise<Event>,
|
||||
close: () => void,
|
||||
status: number,
|
||||
};
|
||||
|
||||
declare function relayPool(): RelayPool;
|
||||
|
||||
// nip04.js
|
||||
|
||||
// nip05.js
|
||||
|
||||
// nip06.js
|
||||
27
index.js
27
index.js
@@ -1,27 +0,0 @@
|
||||
import {generatePrivateKey, getPublicKey} from './keys.js'
|
||||
import {relayConnect} from './relay.js'
|
||||
import {relayPool} from './pool.js'
|
||||
import {
|
||||
getBlankEvent,
|
||||
signEvent,
|
||||
validateEvent,
|
||||
verifySignature,
|
||||
serializeEvent,
|
||||
getEventHash
|
||||
} from './event.js'
|
||||
import {matchFilter, matchFilters} from './filter.js'
|
||||
|
||||
export {
|
||||
generatePrivateKey,
|
||||
relayConnect,
|
||||
relayPool,
|
||||
signEvent,
|
||||
validateEvent,
|
||||
verifySignature,
|
||||
serializeEvent,
|
||||
getEventHash,
|
||||
getPublicKey,
|
||||
getBlankEvent,
|
||||
matchFilter,
|
||||
matchFilters
|
||||
}
|
||||
@@ -1,42 +0,0 @@
|
||||
import * as process from 'process';
|
||||
import {
|
||||
relayPool,
|
||||
getBlankEvent,
|
||||
validateEvent,
|
||||
RelayPool,
|
||||
Event as NEvent
|
||||
} from './index.js';
|
||||
import { expectType } from 'tsd';
|
||||
|
||||
const pool = relayPool();
|
||||
expectType<RelayPool>(pool);
|
||||
|
||||
const privkey = process.env.NOSTR_PRIVATE_KEY;
|
||||
const pubkey = process.env.NOSTR_PUBLIC_KEY;
|
||||
|
||||
const message = {
|
||||
...getBlankEvent(),
|
||||
kind: 1,
|
||||
content: `just saying hi from pid ${process.pid}`,
|
||||
pubkey,
|
||||
};
|
||||
|
||||
const publishCb = (status: number, url: string) => {
|
||||
console.log({ status, url });
|
||||
};
|
||||
|
||||
pool.setPrivateKey(privkey!);
|
||||
|
||||
const publishF = pool.publish(message, publishCb);
|
||||
expectType<Promise<NEvent>>(publishF);
|
||||
|
||||
publishF.then((event) => {
|
||||
expectType<NEvent>(event);
|
||||
|
||||
console.info({ event });
|
||||
|
||||
if (!validateEvent(event)) {
|
||||
console.error(`event failed to validate!`);
|
||||
process.exit(1);
|
||||
}
|
||||
});
|
||||
8
index.ts
Normal file
8
index.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
export * from './keys'
|
||||
export * from './relay'
|
||||
export * from './event'
|
||||
export * from './filter'
|
||||
|
||||
export * as nip04 from './nip04'
|
||||
export * as nip05 from './nip05'
|
||||
export * as nip06 from './nip06'
|
||||
20
keys.test.js
Normal file
20
keys.test.js
Normal file
@@ -0,0 +1,20 @@
|
||||
/* eslint-env jest */
|
||||
|
||||
const {generatePrivateKey, getPublicKey} = require('./cjs')
|
||||
|
||||
test('test private key generation', () => {
|
||||
expect(generatePrivateKey()).toMatch(/[a-f0-9]{64}/)
|
||||
})
|
||||
|
||||
test('test public key generation', () => {
|
||||
expect(getPublicKey(generatePrivateKey())).toMatch(/[a-f0-9]{64}/)
|
||||
})
|
||||
|
||||
test('test public key from private key deterministic', () => {
|
||||
let sk = generatePrivateKey()
|
||||
let pk = getPublicKey(sk)
|
||||
|
||||
for (let i = 0; i < 5; i++) {
|
||||
expect(getPublicKey(sk)).toEqual(pk)
|
||||
}
|
||||
})
|
||||
@@ -1,10 +1,10 @@
|
||||
import * as secp256k1 from '@noble/secp256k1'
|
||||
import {Buffer} from 'buffer'
|
||||
|
||||
export function generatePrivateKey() {
|
||||
export function generatePrivateKey(): string {
|
||||
return Buffer.from(secp256k1.utils.randomPrivateKey()).toString('hex')
|
||||
}
|
||||
|
||||
export function getPublicKey(privateKey) {
|
||||
export function getPublicKey(privateKey: string): string {
|
||||
return Buffer.from(secp256k1.schnorr.getPublicKey(privateKey)).toString('hex')
|
||||
}
|
||||
14
nip04.test.js
Normal file
14
nip04.test.js
Normal file
@@ -0,0 +1,14 @@
|
||||
/* eslint-env jest */
|
||||
|
||||
const {nip04, getPublicKey, generatePrivateKey} = require('./cjs')
|
||||
|
||||
test('encrypt and decrypt message', () => {
|
||||
let sk1 = generatePrivateKey()
|
||||
let sk2 = generatePrivateKey()
|
||||
let pk1 = getPublicKey(sk1)
|
||||
let pk2 = getPublicKey(sk2)
|
||||
|
||||
expect(nip04.decrypt(sk2, pk1, nip04.encrypt(sk1, pk2, 'hello'))).toEqual(
|
||||
'hello'
|
||||
)
|
||||
})
|
||||
@@ -1,9 +1,10 @@
|
||||
import aes from 'browserify-cipher'
|
||||
import {Buffer} from 'buffer'
|
||||
import {randomBytes} from '@noble/hashes/utils'
|
||||
import * as secp256k1 from '@noble/secp256k1'
|
||||
// @ts-ignore
|
||||
import aes from 'browserify-cipher'
|
||||
|
||||
export function encrypt(privkey, pubkey, text) {
|
||||
export function encrypt(privkey: string, pubkey: string, text: string): string {
|
||||
const key = secp256k1.getSharedSecret(privkey, '02' + pubkey)
|
||||
const normalizedKey = getNormalizedX(key)
|
||||
|
||||
@@ -19,7 +20,11 @@ export function encrypt(privkey, pubkey, text) {
|
||||
return `${encryptedMessage}?iv=${Buffer.from(iv.buffer).toString('base64')}`
|
||||
}
|
||||
|
||||
export function decrypt(privkey, pubkey, ciphertext) {
|
||||
export function decrypt(
|
||||
privkey: string,
|
||||
pubkey: string,
|
||||
ciphertext: string
|
||||
): string {
|
||||
let [cip, iv] = ciphertext.split('?iv=')
|
||||
let key = secp256k1.getSharedSecret(privkey, '02' + pubkey)
|
||||
let normalizedKey = getNormalizedX(key)
|
||||
@@ -35,8 +40,6 @@ export function decrypt(privkey, pubkey, ciphertext) {
|
||||
return decryptedMessage
|
||||
}
|
||||
|
||||
function getNormalizedX(key) {
|
||||
return typeof key === 'string'
|
||||
? key.substr(2, 64)
|
||||
: Buffer.from(key.slice(1, 33)).toString('hex')
|
||||
function getNormalizedX(key: Uint8Array): string {
|
||||
return Buffer.from(key.slice(1, 33)).toString('hex')
|
||||
}
|
||||
28
nip05.js
28
nip05.js
@@ -1,28 +0,0 @@
|
||||
import fetch from 'cross-fetch'
|
||||
|
||||
export async function searchDomain(domain, query = '') {
|
||||
try {
|
||||
let res = await (
|
||||
await fetch(`https://${domain}/.well-known/nostr.json?name=${query}`)
|
||||
).json()
|
||||
|
||||
return res.names
|
||||
} catch (_) {
|
||||
return []
|
||||
}
|
||||
}
|
||||
|
||||
export async function queryName(fullname) {
|
||||
try {
|
||||
let [name, domain] = fullname.split('@')
|
||||
if (!domain) return null
|
||||
|
||||
let res = await (
|
||||
await fetch(`https://${domain}/.well-known/nostr.json?name=${name}`)
|
||||
).json()
|
||||
|
||||
return res.names && res.names[name]
|
||||
} catch (_) {
|
||||
return null
|
||||
}
|
||||
}
|
||||
31
nip05.ts
Normal file
31
nip05.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
var _fetch = fetch
|
||||
|
||||
export function useFetchImplementation(fetchImplementation: any) {
|
||||
_fetch = fetchImplementation
|
||||
}
|
||||
|
||||
export async function searchDomain(
|
||||
domain: string,
|
||||
query = ''
|
||||
): Promise<{[name: string]: string}> {
|
||||
try {
|
||||
let res = await (
|
||||
await _fetch(`https://${domain}/.well-known/nostr.json?name=${query}`)
|
||||
).json()
|
||||
|
||||
return res.names
|
||||
} catch (_) {
|
||||
return {}
|
||||
}
|
||||
}
|
||||
|
||||
export async function queryName(fullname: string): Promise<string> {
|
||||
let [name, domain] = fullname.split('@')
|
||||
if (!domain) throw new Error('invalid identifier, must contain an @')
|
||||
|
||||
let res = await (
|
||||
await _fetch(`https://${domain}/.well-known/nostr.json?name=${name}`)
|
||||
).json()
|
||||
|
||||
return res.names && res.names[name]
|
||||
}
|
||||
26
nip06.js
26
nip06.js
@@ -1,26 +0,0 @@
|
||||
import {wordlist} from 'micro-bip39/wordlists/english.js'
|
||||
import {
|
||||
generateMnemonic,
|
||||
mnemonicToSeedSync,
|
||||
validateMnemonic
|
||||
} from 'micro-bip39'
|
||||
import {HDKey} from 'micro-bip32'
|
||||
|
||||
export function privateKeyFromSeed(seed) {
|
||||
let root = HDKey.fromMasterSeed(Buffer.from(seed, 'hex'))
|
||||
return Buffer.from(root.derive(`m/44'/1237'/0'/0/0`).privateKey).toString(
|
||||
'hex'
|
||||
)
|
||||
}
|
||||
|
||||
export function seedFromWords(mnemonic) {
|
||||
return Buffer.from(mnemonicToSeedSync(mnemonic)).toString('hex')
|
||||
}
|
||||
|
||||
export function generateSeedWords() {
|
||||
return generateMnemonic(wordlist)
|
||||
}
|
||||
|
||||
export function validateWords(words) {
|
||||
return validateMnemonic(words, wordlist)
|
||||
}
|
||||
26
nip06.ts
Normal file
26
nip06.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import {wordlist} from '@scure/bip39/wordlists/english.js'
|
||||
import {
|
||||
generateMnemonic,
|
||||
mnemonicToSeedSync,
|
||||
validateMnemonic
|
||||
} from '@scure/bip39'
|
||||
import {HDKey} from '@scure/bip32'
|
||||
|
||||
export function privateKeyFromSeed(seed: string): string {
|
||||
let root = HDKey.fromMasterSeed(Buffer.from(seed, 'hex'))
|
||||
let privateKey = root.derive(`m/44'/1237'/0'/0/0`).privateKey
|
||||
if (!privateKey) throw new Error('could not derive private key')
|
||||
return Buffer.from(privateKey).toString('hex')
|
||||
}
|
||||
|
||||
export function seedFromWords(mnemonic: string): string {
|
||||
return Buffer.from(mnemonicToSeedSync(mnemonic)).toString('hex')
|
||||
}
|
||||
|
||||
export function generateSeedWords(): string {
|
||||
return generateMnemonic(wordlist)
|
||||
}
|
||||
|
||||
export function validateWords(words: string): boolean {
|
||||
return validateMnemonic(words, wordlist)
|
||||
}
|
||||
40
package.json
40
package.json
@@ -1,49 +1,47 @@
|
||||
{
|
||||
"name": "nostr-tools",
|
||||
"version": "0.24.1",
|
||||
"version": "1.0.0-alpha",
|
||||
"description": "Tools for making a Nostr client.",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/fiatjaf/nostr-tools.git"
|
||||
},
|
||||
"type": "module",
|
||||
"main": "cjs/index.js",
|
||||
"module": "esm/index.js",
|
||||
"dependencies": {
|
||||
"@noble/hashes": "^0.5.7",
|
||||
"@noble/secp256k1": "^1.5.2",
|
||||
"@noble/secp256k1": "^1.7.0",
|
||||
"@scure/bip32": "^1.1.1",
|
||||
"@scure/bip39": "^1.1.0",
|
||||
"browserify-cipher": ">=1",
|
||||
"buffer": ">=5",
|
||||
"create-hash": "^1.2.0",
|
||||
"cross-fetch": "^3.1.4",
|
||||
"micro-bip32": "^0.1.0",
|
||||
"micro-bip39": "^0.1.3",
|
||||
"buffer": "^6.0.3",
|
||||
"websocket-polyfill": "^0.0.3"
|
||||
},
|
||||
"keywords": [
|
||||
"decentralization",
|
||||
"twitter",
|
||||
"p2p",
|
||||
"mastodon",
|
||||
"ssb",
|
||||
"social",
|
||||
"unstoppable",
|
||||
"censorship",
|
||||
"censorship-resistance",
|
||||
"client"
|
||||
"client",
|
||||
"nostr"
|
||||
],
|
||||
"devDependencies": {
|
||||
"@esbuild-plugins/node-globals-polyfill": "^0.1.1",
|
||||
"@types/node": "^18.0.3",
|
||||
"esbuild": "^0.14.38",
|
||||
"@typescript-eslint/eslint-plugin": "^5.46.1",
|
||||
"@typescript-eslint/parser": "^5.46.1",
|
||||
"esbuild": "0.16.9",
|
||||
"esbuild-plugin-alias": "^0.2.1",
|
||||
"eslint": "^8.5.0",
|
||||
"eslint": "^8.30.0",
|
||||
"eslint-plugin-babel": "^5.3.1",
|
||||
"esm-loader-typescript": "^1.0.1",
|
||||
"events": "^3.3.0",
|
||||
"jest": "^29.3.1",
|
||||
"ts-jest": "^29.0.3",
|
||||
"tsd": "^0.22.0",
|
||||
"typescript": "^4.7.4"
|
||||
"typescript": "^4.9.4"
|
||||
},
|
||||
"scripts": {
|
||||
"prepublish": "node build.cjs",
|
||||
"check-ts": "tsd && node --no-warnings --loader=esm-loader-typescript index.test-d.ts"
|
||||
"build": "node build.cjs",
|
||||
"pretest": "node build.cjs",
|
||||
"test": "jest"
|
||||
}
|
||||
}
|
||||
|
||||
206
pool.js
206
pool.js
@@ -1,206 +0,0 @@
|
||||
import {getEventHash, verifySignature, signEvent} from './event.js'
|
||||
import {relayConnect, normalizeRelayURL} from './relay.js'
|
||||
|
||||
export function relayPool() {
|
||||
var globalPrivateKey
|
||||
var globalSigningFunction
|
||||
|
||||
const poolPolicy = {
|
||||
// setting this to a number will cause events to be published to a random
|
||||
// set of relays only, instead of publishing to all relays all the time
|
||||
randomChoice: null,
|
||||
|
||||
// setting this to true will cause .publish() calls to wait until the event has
|
||||
// been published -- or at least attempted to be published -- to all relays
|
||||
wait: false
|
||||
}
|
||||
const relays = {}
|
||||
const noticeCallbacks = []
|
||||
|
||||
function propagateNotice(notice, relayURL) {
|
||||
for (let i = 0; i < noticeCallbacks.length; i++) {
|
||||
let {relay} = relays[relayURL]
|
||||
noticeCallbacks[i](notice, relay)
|
||||
}
|
||||
}
|
||||
|
||||
const activeSubscriptions = {}
|
||||
|
||||
const sub = ({cb, filter, beforeSend}, id) => {
|
||||
if (!id) id = Math.random().toString().slice(2)
|
||||
|
||||
const subControllers = Object.fromEntries(
|
||||
Object.values(relays)
|
||||
.filter(({policy}) => policy.read)
|
||||
.map(({relay}) => [
|
||||
relay.url,
|
||||
relay.sub({cb: event => cb(event, relay.url), filter, beforeSend}, id)
|
||||
])
|
||||
)
|
||||
|
||||
const activeCallback = cb
|
||||
const activeFilters = filter
|
||||
const activeBeforeSend = beforeSend
|
||||
|
||||
const unsub = () => {
|
||||
Object.values(subControllers).forEach(sub => sub.unsub())
|
||||
delete activeSubscriptions[id]
|
||||
}
|
||||
const sub = ({
|
||||
cb = activeCallback,
|
||||
filter = activeFilters,
|
||||
beforeSend = activeBeforeSend
|
||||
}) => {
|
||||
Object.entries(subControllers).map(([relayURL, sub]) => [
|
||||
relayURL,
|
||||
sub.sub({cb: event => cb(event, relayURL), filter, beforeSend}, id)
|
||||
])
|
||||
return activeSubscriptions[id]
|
||||
}
|
||||
const addRelay = relay => {
|
||||
subControllers[relay.url] = relay.sub(
|
||||
{cb: event => cb(event, relay.url), filter, beforeSend},
|
||||
id
|
||||
)
|
||||
return activeSubscriptions[id]
|
||||
}
|
||||
const removeRelay = relayURL => {
|
||||
if (relayURL in subControllers) {
|
||||
subControllers[relayURL].unsub()
|
||||
if (Object.keys(subControllers).length === 0) unsub()
|
||||
}
|
||||
return activeSubscriptions[id]
|
||||
}
|
||||
|
||||
activeSubscriptions[id] = {
|
||||
sub,
|
||||
unsub,
|
||||
addRelay,
|
||||
removeRelay
|
||||
}
|
||||
|
||||
return activeSubscriptions[id]
|
||||
}
|
||||
|
||||
return {
|
||||
sub,
|
||||
relays,
|
||||
setPrivateKey(privateKey) {
|
||||
globalPrivateKey = privateKey
|
||||
},
|
||||
registerSigningFunction(fn) {
|
||||
globalSigningFunction = fn
|
||||
},
|
||||
setPolicy(key, value) {
|
||||
poolPolicy[key] = value
|
||||
},
|
||||
addRelay(url, policy = {read: true, write: true}) {
|
||||
let relayURL = normalizeRelayURL(url)
|
||||
if (relayURL in relays) return
|
||||
|
||||
let relay = relayConnect(url, notice => {
|
||||
propagateNotice(notice, relayURL)
|
||||
})
|
||||
relays[relayURL] = {relay, policy}
|
||||
|
||||
if (policy.read) {
|
||||
Object.values(activeSubscriptions).forEach(subscription =>
|
||||
subscription.addRelay(relay)
|
||||
)
|
||||
}
|
||||
|
||||
return relay
|
||||
},
|
||||
removeRelay(url) {
|
||||
let relayURL = normalizeRelayURL(url)
|
||||
let data = relays[relayURL]
|
||||
if (!data) return
|
||||
|
||||
let {relay} = data
|
||||
Object.values(activeSubscriptions).forEach(subscription =>
|
||||
subscription.removeRelay(relay)
|
||||
)
|
||||
relay.close()
|
||||
delete relays[relayURL]
|
||||
},
|
||||
onNotice(cb) {
|
||||
noticeCallbacks.push(cb)
|
||||
},
|
||||
offNotice(cb) {
|
||||
let index = noticeCallbacks.indexOf(cb)
|
||||
if (index !== -1) noticeCallbacks.splice(index, 1)
|
||||
},
|
||||
async publish(event, statusCallback) {
|
||||
event.id = getEventHash(event)
|
||||
|
||||
if (!event.sig) {
|
||||
event.tags = event.tags || []
|
||||
|
||||
if (globalPrivateKey) {
|
||||
event.sig = await signEvent(event, globalPrivateKey)
|
||||
} else if (globalSigningFunction) {
|
||||
event.sig = await globalSigningFunction(event)
|
||||
if (!event.sig) {
|
||||
// abort here
|
||||
return
|
||||
} else {
|
||||
// check
|
||||
if (!(await verifySignature(event)))
|
||||
throw new Error(
|
||||
'signature provided by custom signing function is invalid.'
|
||||
)
|
||||
}
|
||||
} else {
|
||||
throw new Error(
|
||||
"can't publish unsigned event. either sign this event beforehand, provide a signing function or pass a private key while initializing this relay pool so it can be signed automatically."
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
let writeable = Object.values(relays)
|
||||
.filter(({policy}) => policy.write)
|
||||
.sort(() => Math.random() - 0.5) // random
|
||||
|
||||
let maxTargets = poolPolicy.randomChoice
|
||||
? poolPolicy.randomChoice
|
||||
: writeable.length
|
||||
|
||||
let successes = 0
|
||||
|
||||
if (poolPolicy.wait) {
|
||||
for (let i = 0; i < writeable.length; i++) {
|
||||
let {relay} = writeable[i]
|
||||
|
||||
try {
|
||||
await new Promise(async (resolve, reject) => {
|
||||
try {
|
||||
await relay.publish(event, status => {
|
||||
if (statusCallback) statusCallback(status, relay.url)
|
||||
resolve()
|
||||
})
|
||||
} catch (err) {
|
||||
if (statusCallback) statusCallback(-1, relay.url)
|
||||
}
|
||||
})
|
||||
|
||||
successes++
|
||||
if (successes >= maxTargets) {
|
||||
break
|
||||
}
|
||||
} catch (err) {
|
||||
/***/
|
||||
}
|
||||
}
|
||||
} else {
|
||||
writeable.forEach(async ({relay}) => {
|
||||
let callback = statusCallback
|
||||
? status => statusCallback(status, relay.url)
|
||||
: null
|
||||
relay.publish(event, callback)
|
||||
})
|
||||
}
|
||||
|
||||
return event
|
||||
}
|
||||
}
|
||||
}
|
||||
195
relay.js
195
relay.js
@@ -1,195 +0,0 @@
|
||||
/* global WebSocket */
|
||||
|
||||
import 'websocket-polyfill'
|
||||
|
||||
import {verifySignature, validateEvent} from './event.js'
|
||||
import {matchFilters} from './filter.js'
|
||||
|
||||
export function normalizeRelayURL(url) {
|
||||
let [host, ...qs] = url.trim().split('?')
|
||||
if (host.slice(0, 4) === 'http') host = 'ws' + host.slice(4)
|
||||
if (host.slice(0, 2) !== 'ws') host = 'wss://' + host
|
||||
if (host.length && host[host.length - 1] === '/') host = host.slice(0, -1)
|
||||
return [host, ...qs].join('?')
|
||||
}
|
||||
|
||||
export function relayConnect(url, onNotice = () => {}, onError = () => {}) {
|
||||
url = normalizeRelayURL(url)
|
||||
|
||||
var ws, resolveOpen, untilOpen, wasClosed
|
||||
var openSubs = {}
|
||||
var isSetToSkipVerification = {}
|
||||
let attemptNumber = 1
|
||||
let nextAttemptSeconds = 1
|
||||
|
||||
function resetOpenState() {
|
||||
untilOpen = new Promise(resolve => {
|
||||
resolveOpen = resolve
|
||||
})
|
||||
}
|
||||
|
||||
var channels = {}
|
||||
|
||||
function connect() {
|
||||
ws = new WebSocket(url)
|
||||
|
||||
ws.onopen = () => {
|
||||
console.log('connected to', url)
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
ws.onerror = err => {
|
||||
console.log('error connecting to relay', url)
|
||||
onError(err)
|
||||
}
|
||||
ws.onclose = () => {
|
||||
resetOpenState()
|
||||
attemptNumber++
|
||||
nextAttemptSeconds += attemptNumber ** 3
|
||||
if (nextAttemptSeconds > 14400) {
|
||||
nextAttemptSeconds = 14400 // 4 hours
|
||||
}
|
||||
console.log(
|
||||
`relay ${url} connection closed. reconnecting in ${nextAttemptSeconds} seconds.`
|
||||
)
|
||||
setTimeout(async () => {
|
||||
try {
|
||||
connect()
|
||||
} catch (err) {}
|
||||
}, nextAttemptSeconds * 1000)
|
||||
|
||||
wasClosed = true
|
||||
}
|
||||
|
||||
ws.onmessage = async e => {
|
||||
var data
|
||||
try {
|
||||
data = JSON.parse(e.data)
|
||||
} catch (err) {
|
||||
data = e.data
|
||||
}
|
||||
|
||||
if (data.length > 1) {
|
||||
if (data[0] === 'NOTICE') {
|
||||
if (data.length < 2) return
|
||||
|
||||
console.log('message from relay ' + url + ': ' + data[1])
|
||||
onNotice(data[1])
|
||||
return
|
||||
}
|
||||
|
||||
if (data[0] === 'EVENT') {
|
||||
if (data.length < 3) return
|
||||
|
||||
let channel = data[1]
|
||||
let event = data[2]
|
||||
|
||||
if (
|
||||
validateEvent(event) &&
|
||||
(isSetToSkipVerification[channel] || verifySignature(event)) &&
|
||||
channels[channel] &&
|
||||
matchFilters(openSubs[channel], event)
|
||||
) {
|
||||
channels[channel](event)
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
resetOpenState()
|
||||
|
||||
try {
|
||||
connect()
|
||||
} catch (err) {}
|
||||
|
||||
async function trySend(params) {
|
||||
let msg = JSON.stringify(params)
|
||||
|
||||
await untilOpen
|
||||
ws.send(msg)
|
||||
}
|
||||
|
||||
const sub = (
|
||||
{cb, filter, beforeSend, skipVerification},
|
||||
channel = Math.random().toString().slice(2)
|
||||
) => {
|
||||
var filters = []
|
||||
if (Array.isArray(filter)) {
|
||||
filters = filter
|
||||
} else {
|
||||
filters.push(filter)
|
||||
}
|
||||
|
||||
if (beforeSend) {
|
||||
const beforeSendResult = beforeSend({filter, relay: url, channel})
|
||||
filters = beforeSendResult.filter
|
||||
}
|
||||
|
||||
trySend(['REQ', channel, ...filters])
|
||||
channels[channel] = cb
|
||||
openSubs[channel] = filters
|
||||
isSetToSkipVerification[channel] = skipVerification
|
||||
|
||||
const activeCallback = cb
|
||||
const activeFilters = filters
|
||||
const activeBeforeSend = beforeSend
|
||||
|
||||
return {
|
||||
sub: ({
|
||||
cb = activeCallback,
|
||||
filter = activeFilters,
|
||||
beforeSend = activeBeforeSend
|
||||
}) => sub({cb, filter, beforeSend, skipVerification}, channel),
|
||||
unsub: () => {
|
||||
delete openSubs[channel]
|
||||
delete channels[channel]
|
||||
delete isSetToSkipVerification[channel]
|
||||
trySend(['CLOSE', channel])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
url,
|
||||
sub,
|
||||
async publish(event, statusCallback) {
|
||||
try {
|
||||
await trySend(['EVENT', event])
|
||||
if (statusCallback) {
|
||||
statusCallback(0)
|
||||
let {unsub} = sub(
|
||||
{
|
||||
cb: () => {
|
||||
statusCallback(1)
|
||||
unsub()
|
||||
clearTimeout(willUnsub)
|
||||
},
|
||||
filter: {ids: [event.id]}
|
||||
},
|
||||
`monitor-${event.id.slice(0, 5)}`
|
||||
)
|
||||
let willUnsub = setTimeout(unsub, 5000)
|
||||
}
|
||||
} catch (err) {
|
||||
if (statusCallback) statusCallback(-1)
|
||||
}
|
||||
},
|
||||
close() {
|
||||
ws.close()
|
||||
},
|
||||
get status() {
|
||||
return ws.readyState
|
||||
}
|
||||
}
|
||||
}
|
||||
117
relay.test.js
Normal file
117
relay.test.js
Normal file
@@ -0,0 +1,117 @@
|
||||
/* eslint-env jest */
|
||||
|
||||
const {
|
||||
relayInit,
|
||||
generatePrivateKey,
|
||||
getPublicKey,
|
||||
getEventHash,
|
||||
signEvent
|
||||
} = require('./cjs')
|
||||
|
||||
describe('relay interaction', () => {
|
||||
let relay = relayInit('wss://nostr-pub.wellorder.net/')
|
||||
|
||||
beforeAll(() => {
|
||||
relay.connect()
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
await relay.close()
|
||||
})
|
||||
|
||||
test('connectivity', () => {
|
||||
return expect(
|
||||
new Promise(resolve => {
|
||||
relay.on('connect', () => {
|
||||
resolve(true)
|
||||
})
|
||||
relay.on('error', () => {
|
||||
resolve(false)
|
||||
})
|
||||
})
|
||||
).resolves.toBe(true)
|
||||
})
|
||||
|
||||
test('querying', () => {
|
||||
var resolve1
|
||||
var resolve2
|
||||
|
||||
let sub = relay.sub([
|
||||
{
|
||||
ids: [
|
||||
'd7dd5eb3ab747e16f8d0212d53032ea2a7cadef53837e5a6c66d42849fcb9027'
|
||||
]
|
||||
}
|
||||
])
|
||||
sub.on('event', event => {
|
||||
expect(event).toHaveProperty(
|
||||
'id',
|
||||
'd7dd5eb3ab747e16f8d0212d53032ea2a7cadef53837e5a6c66d42849fcb9027'
|
||||
)
|
||||
resolve1(true)
|
||||
})
|
||||
sub.on('eose', () => {
|
||||
resolve2(true)
|
||||
})
|
||||
|
||||
return expect(
|
||||
Promise.all([
|
||||
new Promise(resolve => {
|
||||
resolve1 = resolve
|
||||
}),
|
||||
new Promise(resolve => {
|
||||
resolve2 = resolve
|
||||
})
|
||||
])
|
||||
).resolves.toEqual([true, true])
|
||||
})
|
||||
|
||||
test('listening (twice) and publishing', async () => {
|
||||
let sk = generatePrivateKey()
|
||||
let pk = getPublicKey(sk)
|
||||
var resolve1
|
||||
var resolve2
|
||||
|
||||
let sub = relay.sub([
|
||||
{
|
||||
kinds: [27572],
|
||||
authors: [pk]
|
||||
}
|
||||
])
|
||||
|
||||
sub.on('event', event => {
|
||||
expect(event).toHaveProperty('pubkey', pk)
|
||||
expect(event).toHaveProperty('kind', 27572)
|
||||
expect(event).toHaveProperty('content', 'nostr-tools test suite')
|
||||
resolve1(true)
|
||||
})
|
||||
sub.on('event', event => {
|
||||
expect(event).toHaveProperty('pubkey', pk)
|
||||
expect(event).toHaveProperty('kind', 27572)
|
||||
expect(event).toHaveProperty('content', 'nostr-tools test suite')
|
||||
resolve2(true)
|
||||
})
|
||||
|
||||
let event = {
|
||||
kind: 27572,
|
||||
pubkey: pk,
|
||||
created_at: Math.floor(Date.now() / 1000),
|
||||
tags: [],
|
||||
content: 'nostr-tools test suite'
|
||||
}
|
||||
event.id = getEventHash(event)
|
||||
event.sig = await signEvent(event, sk)
|
||||
|
||||
relay.publish(event)
|
||||
return expect(
|
||||
Promise.all([
|
||||
new Promise(resolve => {
|
||||
resolve1 = resolve
|
||||
}),
|
||||
new Promise(resolve => {
|
||||
resolve2 = resolve
|
||||
})
|
||||
])
|
||||
).resolves.toEqual([true, true])
|
||||
})
|
||||
})
|
||||
311
relay.ts
Normal file
311
relay.ts
Normal file
@@ -0,0 +1,311 @@
|
||||
/* global WebSocket */
|
||||
|
||||
import 'websocket-polyfill'
|
||||
|
||||
import {Event, verifySignature, validateEvent} from './event'
|
||||
import {Filter, matchFilters} from './filter'
|
||||
|
||||
export type Relay = {
|
||||
url: string
|
||||
status: number
|
||||
connect: () => void
|
||||
close: () => void
|
||||
sub: (filters: Filter[], opts: SubscriptionOptions) => Sub
|
||||
publish: (event: Event) => Pub
|
||||
on: (type: 'connect' | 'disconnect' | 'notice', cb: any) => void
|
||||
off: (type: 'connect' | 'disconnect' | 'notice', cb: any) => void
|
||||
}
|
||||
export type Pub = {
|
||||
on: (type: 'ok' | 'seen' | 'failed', cb: any) => void
|
||||
off: (type: 'ok' | 'seen' | 'failed', cb: any) => void
|
||||
}
|
||||
export type Sub = {
|
||||
sub: (filters: Filter[], opts: SubscriptionOptions) => Sub
|
||||
unsub: () => void
|
||||
on: (type: 'event' | 'eose', cb: any) => void
|
||||
off: (type: 'event' | 'eose', cb: any) => void
|
||||
}
|
||||
|
||||
type SubscriptionOptions = {
|
||||
skipVerification?: boolean
|
||||
id?: string
|
||||
}
|
||||
|
||||
export function relayInit(url: string): Relay {
|
||||
var ws: WebSocket
|
||||
var resolveOpen: () => void
|
||||
var resolveClose: () => void
|
||||
var untilOpen: Promise<void>
|
||||
var wasClosed: boolean
|
||||
var closed: boolean
|
||||
var openSubs: {[id: string]: {filters: Filter[]} & SubscriptionOptions} = {}
|
||||
var listeners: {
|
||||
connect: Array<() => void>
|
||||
disconnect: Array<() => void>
|
||||
error: Array<() => void>
|
||||
notice: Array<(msg: string) => void>
|
||||
} = {
|
||||
connect: [],
|
||||
disconnect: [],
|
||||
error: [],
|
||||
notice: []
|
||||
}
|
||||
var subListeners: {
|
||||
[subid: string]: {
|
||||
event: Array<(event: Event) => void>
|
||||
eose: Array<() => void>
|
||||
}
|
||||
} = {}
|
||||
var pubListeners: {
|
||||
[eventid: string]: {
|
||||
ok: Array<() => void>
|
||||
seen: Array<() => void>
|
||||
failed: Array<(reason: string) => void>
|
||||
}
|
||||
} = {}
|
||||
let attemptNumber = 1
|
||||
let nextAttemptSeconds = 1
|
||||
let isConnected = false
|
||||
|
||||
function resetOpenState() {
|
||||
untilOpen = new Promise(resolve => {
|
||||
resolveOpen = resolve
|
||||
})
|
||||
}
|
||||
|
||||
function connectRelay() {
|
||||
ws = new WebSocket(url)
|
||||
|
||||
ws.onopen = () => {
|
||||
listeners.connect.forEach(cb => cb())
|
||||
resolveOpen()
|
||||
isConnected = true
|
||||
|
||||
// restablish old subscriptions
|
||||
if (wasClosed) {
|
||||
wasClosed = false
|
||||
for (let id in openSubs) {
|
||||
let {filters} = openSubs[id]
|
||||
sub(filters, openSubs[id])
|
||||
}
|
||||
}
|
||||
}
|
||||
ws.onerror = () => {
|
||||
isConnected = false
|
||||
listeners.error.forEach(cb => cb())
|
||||
}
|
||||
ws.onclose = async () => {
|
||||
isConnected = false
|
||||
listeners.disconnect.forEach(cb => cb())
|
||||
|
||||
if (closed) {
|
||||
// we've closed this because we wanted, so end everything
|
||||
resolveClose()
|
||||
return
|
||||
}
|
||||
|
||||
// otherwise keep trying to reconnect
|
||||
resetOpenState()
|
||||
attemptNumber++
|
||||
nextAttemptSeconds += attemptNumber ** 3
|
||||
if (nextAttemptSeconds > 14400) {
|
||||
nextAttemptSeconds = 14400 // 4 hours
|
||||
}
|
||||
console.log(
|
||||
`relay ${url} connection closed. reconnecting in ${nextAttemptSeconds} seconds.`
|
||||
)
|
||||
setTimeout(async () => {
|
||||
try {
|
||||
connectRelay()
|
||||
} catch (err) {}
|
||||
}, nextAttemptSeconds * 1000)
|
||||
|
||||
wasClosed = true
|
||||
}
|
||||
|
||||
ws.onmessage = async e => {
|
||||
var data
|
||||
try {
|
||||
data = JSON.parse(e.data)
|
||||
} catch (err) {
|
||||
data = e.data
|
||||
}
|
||||
|
||||
if (data.length >= 1) {
|
||||
switch (data[0]) {
|
||||
case 'EVENT':
|
||||
if (data.length !== 3) return // ignore empty or malformed EVENT
|
||||
|
||||
let id = data[1]
|
||||
let event = data[2]
|
||||
if (
|
||||
validateEvent(event) &&
|
||||
openSubs[id] &&
|
||||
(openSubs[id].skipVerification || verifySignature(event)) &&
|
||||
matchFilters(openSubs[id].filters, event)
|
||||
) {
|
||||
openSubs[id]
|
||||
subListeners[id]?.event.forEach(cb => cb(event))
|
||||
}
|
||||
return
|
||||
case 'EOSE': {
|
||||
if (data.length !== 2) return // ignore empty or malformed EOSE
|
||||
let id = data[1]
|
||||
subListeners[id]?.eose.forEach(cb => cb())
|
||||
return
|
||||
}
|
||||
case 'OK': {
|
||||
if (data.length < 3) return // ignore empty or malformed OK
|
||||
let id: string = data[1]
|
||||
let ok: boolean = data[2]
|
||||
let reason: string = data[3] || ''
|
||||
if (ok) pubListeners[id]?.ok.forEach(cb => cb())
|
||||
else pubListeners[id]?.failed.forEach(cb => cb(reason))
|
||||
return
|
||||
}
|
||||
case 'NOTICE':
|
||||
if (data.length !== 2) return // ignore empty or malformed NOTICE
|
||||
let notice = data[1]
|
||||
listeners.notice.forEach(cb => cb(notice))
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
resetOpenState()
|
||||
|
||||
async function connect(): Promise<void> {
|
||||
if (ws?.readyState && ws.readyState === 1) return // ws already open
|
||||
try {
|
||||
connectRelay()
|
||||
} catch (err) {}
|
||||
}
|
||||
|
||||
async function trySend(params: [string, ...any]) {
|
||||
let msg = JSON.stringify(params)
|
||||
|
||||
await untilOpen
|
||||
ws.send(msg)
|
||||
}
|
||||
|
||||
const sub = (
|
||||
filters: Filter[],
|
||||
{
|
||||
skipVerification = false,
|
||||
id = Math.random().toString().slice(2)
|
||||
}: SubscriptionOptions = {}
|
||||
): Sub => {
|
||||
let subid = id
|
||||
|
||||
openSubs[subid] = {
|
||||
id: subid,
|
||||
filters,
|
||||
skipVerification
|
||||
}
|
||||
trySend(['REQ', subid, ...filters])
|
||||
|
||||
return {
|
||||
sub: (newFilters, newOpts = {}) =>
|
||||
sub(newFilters || filters, {
|
||||
skipVerification: newOpts.skipVerification || skipVerification,
|
||||
id: subid
|
||||
}),
|
||||
unsub: () => {
|
||||
delete openSubs[subid]
|
||||
delete subListeners[subid]
|
||||
trySend(['CLOSE', subid])
|
||||
},
|
||||
on: (type: 'event' | 'eose', cb: any): void => {
|
||||
subListeners[subid] = subListeners[subid] || {
|
||||
event: [],
|
||||
eose: []
|
||||
}
|
||||
subListeners[subid][type].push(cb)
|
||||
},
|
||||
off: (type: 'event' | 'eose', cb: any): void => {
|
||||
let idx = subListeners[subid][type].indexOf(cb)
|
||||
if (idx >= 0) subListeners[subid][type].splice(idx, 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
url,
|
||||
sub,
|
||||
on: (type: 'connect' | 'disconnect' | 'notice', cb: any): void => {
|
||||
listeners[type].push(cb)
|
||||
if (type === 'connect' && isConnected) {
|
||||
cb()
|
||||
}
|
||||
},
|
||||
off: (type: 'connect' | 'disconnect' | 'notice', cb: any): void => {
|
||||
let index = listeners[type].indexOf(cb)
|
||||
if (index !== -1) listeners[type].splice(index, 1)
|
||||
},
|
||||
publish(event: Event): Pub {
|
||||
if (!event.id) throw new Error(`event ${event} has no id`)
|
||||
let id = event.id
|
||||
|
||||
var sent = false
|
||||
var mustMonitor = false
|
||||
|
||||
trySend(['EVENT', event])
|
||||
.then(() => {
|
||||
sent = true
|
||||
if (mustMonitor) {
|
||||
startMonitoring()
|
||||
mustMonitor = false
|
||||
}
|
||||
})
|
||||
.catch(() => {})
|
||||
|
||||
const startMonitoring = () => {
|
||||
let monitor = sub([{ids: [id]}], {
|
||||
id: `monitor-${id.slice(0, 5)}`
|
||||
})
|
||||
let willUnsub = setTimeout(() => {
|
||||
pubListeners[id].failed.forEach(cb =>
|
||||
cb('event not seen after 5 seconds')
|
||||
)
|
||||
monitor.unsub()
|
||||
}, 5000)
|
||||
monitor.on('event', () => {
|
||||
clearTimeout(willUnsub)
|
||||
pubListeners[id].seen.forEach(cb => cb())
|
||||
})
|
||||
}
|
||||
|
||||
return {
|
||||
on: (type: 'ok' | 'seen' | 'failed', cb: any) => {
|
||||
pubListeners[id] = pubListeners[id] || {
|
||||
ok: [],
|
||||
seen: [],
|
||||
failed: []
|
||||
}
|
||||
pubListeners[id][type].push(cb)
|
||||
|
||||
if (type === 'seen') {
|
||||
if (sent) startMonitoring()
|
||||
else mustMonitor = true
|
||||
}
|
||||
},
|
||||
off: (type: 'ok' | 'seen' | 'failed', cb: any) => {
|
||||
let idx = pubListeners[id][type].indexOf(cb)
|
||||
if (idx >= 0) pubListeners[id][type].splice(idx, 1)
|
||||
}
|
||||
}
|
||||
},
|
||||
connect,
|
||||
close(): Promise<void> {
|
||||
closed = true // prevent ws from trying to reconnect
|
||||
ws.close()
|
||||
return new Promise(resolve => {
|
||||
resolveClose = resolve
|
||||
})
|
||||
},
|
||||
get status() {
|
||||
return ws.readyState
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,25 +1,15 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"module": "es2020",
|
||||
"target": "es2020",
|
||||
"lib": ["dom", "es2020"],
|
||||
"esModuleInterop": true,
|
||||
"moduleResolution": "node",
|
||||
"allowSyntheticDefaultImports": true,
|
||||
"module": "esnext",
|
||||
"target": "esnext",
|
||||
"lib": ["dom", "dom.iterable", "esnext"],
|
||||
"declaration": true,
|
||||
"strict": true,
|
||||
"noImplicitAny": true,
|
||||
"noImplicitThis": true,
|
||||
"strictNullChecks": true,
|
||||
"strictFunctionTypes": true,
|
||||
"baseUrl": "./",
|
||||
"typeRoots": ["."],
|
||||
"types": ["node"],
|
||||
"noEmit": true,
|
||||
"forceConsistentCasingInFileNames": true
|
||||
},
|
||||
"files": [
|
||||
"index.d.ts",
|
||||
"t/nostr-tools-tests.ts"
|
||||
]
|
||||
"moduleResolution": "node",
|
||||
"skipLibCheck": true,
|
||||
"esModuleInterop": true,
|
||||
"emitDeclarationOnly": true,
|
||||
"outDir": "dist",
|
||||
"rootDir": "."
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user