mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2025-12-09 16:48:50 +00:00
Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
84e4fb1f92 |
@@ -1,9 +1,5 @@
|
|||||||
{
|
{
|
||||||
"root": true,
|
"root": true,
|
||||||
|
|
||||||
"parser": "@typescript-eslint/parser",
|
|
||||||
"plugins": ["@typescript-eslint"],
|
|
||||||
|
|
||||||
"parserOptions": {
|
"parserOptions": {
|
||||||
"ecmaVersion": 9,
|
"ecmaVersion": 9,
|
||||||
"ecmaFeatures": {
|
"ecmaFeatures": {
|
||||||
@@ -18,13 +14,14 @@
|
|||||||
"node": true
|
"node": true
|
||||||
},
|
},
|
||||||
|
|
||||||
"plugins": ["babel"],
|
"plugins": [
|
||||||
|
"babel"
|
||||||
|
],
|
||||||
|
|
||||||
"globals": {
|
"globals": {
|
||||||
"document": false,
|
"document": false,
|
||||||
"navigator": false,
|
"navigator": false,
|
||||||
"window": false,
|
"window": false,
|
||||||
"crypto": false,
|
|
||||||
"location": false,
|
"location": false,
|
||||||
"URL": false,
|
"URL": false,
|
||||||
"URLSearchParams": false,
|
"URLSearchParams": false,
|
||||||
@@ -120,25 +117,14 @@
|
|||||||
"no-unexpected-multiline": 2,
|
"no-unexpected-multiline": 2,
|
||||||
"no-unneeded-ternary": [2, { "defaultAssignment": false }],
|
"no-unneeded-ternary": [2, { "defaultAssignment": false }],
|
||||||
"no-unreachable": 2,
|
"no-unreachable": 2,
|
||||||
"no-unused-vars": [
|
"no-unused-vars": [2, { "vars": "local", "args": "none", "varsIgnorePattern": "^_"}],
|
||||||
2,
|
|
||||||
{"vars": "local", "args": "none", "varsIgnorePattern": "^_"}
|
|
||||||
],
|
|
||||||
"no-useless-call": 2,
|
"no-useless-call": 2,
|
||||||
"no-useless-constructor": 2,
|
"no-useless-constructor": 2,
|
||||||
"no-with": 2,
|
"no-with": 2,
|
||||||
"one-var": [0, { "initialized": "never" }],
|
"one-var": [0, { "initialized": "never" }],
|
||||||
"operator-linebreak": [
|
"operator-linebreak": [2, "after", { "overrides": { "?": "before", ":": "before" } }],
|
||||||
2,
|
|
||||||
"after",
|
|
||||||
{"overrides": {"?": "before", ":": "before"}}
|
|
||||||
],
|
|
||||||
"padded-blocks": [2, "never"],
|
"padded-blocks": [2, "never"],
|
||||||
"quotes": [
|
"quotes": [2, "single", { "avoidEscape": true, "allowTemplateLiterals": true }],
|
||||||
2,
|
|
||||||
"single",
|
|
||||||
{"avoidEscape": true, "allowTemplateLiterals": true}
|
|
||||||
],
|
|
||||||
"semi": [2, "never"],
|
"semi": [2, "never"],
|
||||||
"semi-spacing": [2, { "before": false, "after": true }],
|
"semi-spacing": [2, { "before": false, "after": true }],
|
||||||
"space-before-blocks": [2, "always"],
|
"space-before-blocks": [2, "always"],
|
||||||
|
|||||||
21
.github/workflows/npm-publish.yml
vendored
21
.github/workflows/npm-publish.yml
vendored
@@ -1,21 +0,0 @@
|
|||||||
name: publish npm package
|
|
||||||
|
|
||||||
on:
|
|
||||||
push:
|
|
||||||
tags: [v*]
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
publish-npm:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v3
|
|
||||||
- uses: actions/setup-node@v3
|
|
||||||
with:
|
|
||||||
node-version: 18
|
|
||||||
- run: yarn --ignore-engines
|
|
||||||
- run: node build.js
|
|
||||||
- run: yarn test
|
|
||||||
- uses: JS-DevTools/npm-publish@v1
|
|
||||||
with:
|
|
||||||
token: ${{ secrets.NPM_TOKEN }}
|
|
||||||
greater-version-only: true
|
|
||||||
17
.github/workflows/test.yml
vendored
17
.github/workflows/test.yml
vendored
@@ -1,17 +0,0 @@
|
|||||||
name: test every commit
|
|
||||||
on:
|
|
||||||
push:
|
|
||||||
branches:
|
|
||||||
- master
|
|
||||||
|
|
||||||
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.js
|
|
||||||
- run: yarn test
|
|
||||||
3
.gitignore
vendored
3
.gitignore
vendored
@@ -2,6 +2,3 @@ node_modules
|
|||||||
dist
|
dist
|
||||||
yarn.lock
|
yarn.lock
|
||||||
package-lock.json
|
package-lock.json
|
||||||
.envrc
|
|
||||||
lib
|
|
||||||
test.html
|
|
||||||
|
|||||||
275
README.md
275
README.md
@@ -2,252 +2,71 @@
|
|||||||
|
|
||||||
Tools for developing [Nostr](https://github.com/fiatjaf/nostr) clients.
|
Tools for developing [Nostr](https://github.com/fiatjaf/nostr) clients.
|
||||||
|
|
||||||
Only depends on _@scure_ and _@noble_ packages.
|
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
### Generating a private key and a public key
|
|
||||||
|
|
||||||
```js
|
```js
|
||||||
import {generatePrivateKey, getPublicKey} from 'nostr-tools'
|
import {relayPool} from 'nostr-tools'
|
||||||
|
|
||||||
let sk = generatePrivateKey() // `sk` is a hex string
|
const pool = relayPool()
|
||||||
let pk = getPublicKey(sk) // `pk` is a hex string
|
|
||||||
```
|
|
||||||
|
|
||||||
### Creating, signing and verifying events
|
pool.setPrivateKey('<hex>') // optional
|
||||||
|
|
||||||
```js
|
pool.addRelay('ws://some.relay.com', {read: true, write: true})
|
||||||
import {
|
pool.addRelay('ws://other.relay.cool', {read: true, write: true})
|
||||||
validateEvent,
|
|
||||||
verifySignature,
|
|
||||||
signEvent,
|
|
||||||
getEventHash,
|
|
||||||
getPublicKey
|
|
||||||
} from 'nostr-tools'
|
|
||||||
|
|
||||||
let event = {
|
// example callback function for a subscription
|
||||||
kind: 1,
|
function onEvent(event, relay) {
|
||||||
created_at: Math.floor(Date.now() / 1000),
|
console.log(`got an event from ${relay.url} which is already validated.`, event)
|
||||||
tags: [],
|
|
||||||
content: 'hello'
|
|
||||||
}
|
}
|
||||||
|
|
||||||
event.id = getEventHash(event)
|
// subscribing to a single user
|
||||||
event.pubkey = getPublicKey(privateKey)
|
// author is the user's public key
|
||||||
event.sig = signEvent(event, privateKey)
|
pool.sub({cb: onEvent, filter: {author: '<hex>'}})
|
||||||
|
|
||||||
let ok = validateEvent(event)
|
// or bulk follow
|
||||||
let veryOk = verifySignature(event)
|
pool.sub({cb:(event, relay) => {...}, filter: {authors: ['<hex1>', '<hex2>', ..., '<hexn>']}})
|
||||||
```
|
|
||||||
|
|
||||||
### Interacting with a relay
|
// reuse a subscription channel
|
||||||
|
const mySubscription = pool.sub({cb: ..., filter: ....})
|
||||||
|
mySubscription.sub({filter: ....})
|
||||||
|
mySubscription.sub({cb: ...})
|
||||||
|
mySubscription.unsub()
|
||||||
|
|
||||||
```js
|
// get specific event
|
||||||
import {
|
const specificChannel = pool.sub({
|
||||||
relayInit,
|
cb: (event, relay) => {
|
||||||
generatePrivateKey,
|
console.log('got specific event from relay', event, relay)
|
||||||
getPublicKey,
|
specificChannel.unsub()
|
||||||
getEventHash,
|
},
|
||||||
signEvent
|
filter: {id: '<hex>'}
|
||||||
} from 'nostr-tools'
|
|
||||||
|
|
||||||
const relay = relayInit('wss://relay.example.com')
|
|
||||||
await 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
|
// or get a specific event plus all the events that reference it in the 'e' tag
|
||||||
let sub = relay.sub([
|
pool.sub({ cb: (event, relay) => { ... }, filter: [{id: '<hex>'}, {'#e': '<hex>'}] })
|
||||||
{
|
|
||||||
ids: ['d7dd5eb3ab747e16f8d0212d53032ea2a7cadef53837e5a6c66d42849fcb9027']
|
// 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) {
|
||||||
sub.on('event', event => {
|
console.log(`event published by ${url}`, ev)
|
||||||
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)
|
|
||||||
})
|
})
|
||||||
|
// it will be signed automatically with the key supplied above
|
||||||
|
// or pass an already signed event to bypass this
|
||||||
|
|
||||||
let event = {
|
// subscribing to a new relay
|
||||||
kind: 1,
|
pool.addRelay('<url>')
|
||||||
pubkey: pk,
|
// will automatically subscribe to the all the events called with .sub above
|
||||||
created_at: Math.floor(Date.now() / 1000),
|
|
||||||
tags: [],
|
|
||||||
content: 'hello world'
|
|
||||||
}
|
|
||||||
event.id = getEventHash(event)
|
|
||||||
event.sig = 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()
|
|
||||||
```
|
```
|
||||||
|
|
||||||
To use this on Node.js you first must install `websocket-polyfill` and import it:
|
All functions expect bytearrays as hex strings and output bytearrays as hex strings.
|
||||||
|
|
||||||
```js
|
For other utils please read the source (for now).
|
||||||
import 'websocket-polyfill'
|
|
||||||
```
|
|
||||||
|
|
||||||
### Querying profile data from a NIP-05 address
|
|
||||||
|
|
||||||
```js
|
|
||||||
import {nip05} from 'nostr-tools'
|
|
||||||
|
|
||||||
let profile = await nip05.queryProfile('jb55.com')
|
|
||||||
console.log(profile.pubkey)
|
|
||||||
// prints: 32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245
|
|
||||||
console.log(profile.relays)
|
|
||||||
// prints: [wss://relay.damus.io]
|
|
||||||
```
|
|
||||||
|
|
||||||
To use this on Node.js you first must install `node-fetch@2` and call something like this:
|
|
||||||
|
|
||||||
```js
|
|
||||||
nip05.useFetchImplementation(require('node-fetch'))
|
|
||||||
```
|
|
||||||
|
|
||||||
### Encoding and decoding NIP-19 codes
|
|
||||||
|
|
||||||
```js
|
|
||||||
import {nip19, generatePrivateKey, getPublicKey} from 'nostr-tools'
|
|
||||||
|
|
||||||
let sk = generatePrivateKey()
|
|
||||||
let nsec = nip19.nsecEncode(sk)
|
|
||||||
let {type, data} = nip19.decode(nsec)
|
|
||||||
assert(type === 'nsec')
|
|
||||||
assert(data === sk)
|
|
||||||
|
|
||||||
let pk = getPublicKey(generatePrivateKey())
|
|
||||||
let npub = nip19.npubEncode(pk)
|
|
||||||
let {type, data} = nip19.decode(npub)
|
|
||||||
assert(type === 'npub')
|
|
||||||
assert(data === pk)
|
|
||||||
|
|
||||||
let pk = getPublicKey(generatePrivateKey())
|
|
||||||
let relays = [
|
|
||||||
'wss://relay.nostr.example.mydomain.example.com',
|
|
||||||
'wss://nostr.banana.com'
|
|
||||||
]
|
|
||||||
let nprofile = nip19.nprofileEncode({pubkey: pk, relays})
|
|
||||||
let {type, data} = nip19.decode(nprofile)
|
|
||||||
assert(type === 'nprofile')
|
|
||||||
assert(data.pubkey === pk)
|
|
||||||
assert(data.relays.length === 2)
|
|
||||||
```
|
|
||||||
|
|
||||||
### 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 = await 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 = await nip04.decrypt(sk2, pk1, event.content)
|
|
||||||
})
|
|
||||||
```
|
|
||||||
|
|
||||||
### Performing and checking for delegation
|
|
||||||
|
|
||||||
```js
|
|
||||||
import {nip26, getPublicKey, generatePrivateKey} from 'nostr-tools'
|
|
||||||
|
|
||||||
// delegator
|
|
||||||
let sk1 = generatePrivateKey()
|
|
||||||
let pk1 = getPublicKey(sk1)
|
|
||||||
|
|
||||||
// delegatee
|
|
||||||
let sk2 = generatePrivateKey()
|
|
||||||
let pk2 = getPublicKey(sk2)
|
|
||||||
|
|
||||||
// generate delegation
|
|
||||||
let delegation = nip26.createDelegation(sk1, {
|
|
||||||
pubkey: pk2,
|
|
||||||
kind: 1,
|
|
||||||
since: Math.round(Date.now() / 1000),
|
|
||||||
until: Math.round(Date.now() / 1000) + 60 * 60 * 24 * 30 /* 30 days */
|
|
||||||
})
|
|
||||||
|
|
||||||
// the delegatee uses the delegation when building an event
|
|
||||||
let event = {
|
|
||||||
pubkey: pk2,
|
|
||||||
kind: 1,
|
|
||||||
created_at: Math.round(Date.now() / 1000),
|
|
||||||
content: 'hello from a delegated key',
|
|
||||||
tags: [['delegation', delegation.from, delegation.cond, delegation.sig]]
|
|
||||||
}
|
|
||||||
|
|
||||||
// finally any receiver of this event can check for the presence of a valid delegation tag
|
|
||||||
let delegator = nip26.getDelegator(event)
|
|
||||||
assert(delegator === pk1) // will be null if there is no delegation tag or if it is invalid
|
|
||||||
```
|
|
||||||
|
|
||||||
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)
|
|
||||||
|
|
||||||
```html
|
|
||||||
<script src="https://unpkg.com/nostr-tools/lib/nostr.bundle.js"></script>
|
|
||||||
<script>
|
|
||||||
window.NostrTools.generatePrivateKey('...') // and so on
|
|
||||||
</script>
|
|
||||||
```
|
|
||||||
|
|
||||||
## License
|
|
||||||
|
|
||||||
Public domain.
|
|
||||||
|
|||||||
41
build.js
41
build.js
@@ -1,41 +0,0 @@
|
|||||||
#!/usr/bin/env node
|
|
||||||
|
|
||||||
const esbuild = require('esbuild')
|
|
||||||
|
|
||||||
let common = {
|
|
||||||
entryPoints: ['index.ts'],
|
|
||||||
bundle: true,
|
|
||||||
sourcemap: 'external'
|
|
||||||
}
|
|
||||||
|
|
||||||
esbuild
|
|
||||||
.build({
|
|
||||||
...common,
|
|
||||||
outfile: 'lib/nostr.esm.js',
|
|
||||||
format: 'esm',
|
|
||||||
packages: 'external'
|
|
||||||
})
|
|
||||||
.then(() => console.log('esm build success.'))
|
|
||||||
|
|
||||||
esbuild
|
|
||||||
.build({
|
|
||||||
...common,
|
|
||||||
outfile: 'lib/nostr.cjs.js',
|
|
||||||
format: 'cjs',
|
|
||||||
packages: 'external'
|
|
||||||
})
|
|
||||||
.then(() => console.log('cjs build success.'))
|
|
||||||
|
|
||||||
esbuild
|
|
||||||
.build({
|
|
||||||
...common,
|
|
||||||
outfile: 'lib/nostr.bundle.js',
|
|
||||||
format: 'iife',
|
|
||||||
globalName: 'NostrTools',
|
|
||||||
define: {
|
|
||||||
window: 'self',
|
|
||||||
global: 'self',
|
|
||||||
process: '{"env": {}}'
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.then(() => console.log('standalone build success.'))
|
|
||||||
58
event.js
Normal file
58
event.js
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
import {Buffer} from 'buffer'
|
||||||
|
import createHash from 'create-hash'
|
||||||
|
import * as secp256k1 from '@noble/secp256k1'
|
||||||
|
|
||||||
|
export function getBlankEvent() {
|
||||||
|
return {
|
||||||
|
kind: 255,
|
||||||
|
pubkey: null,
|
||||||
|
content: '',
|
||||||
|
tags: [],
|
||||||
|
created_at: 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function serializeEvent(evt) {
|
||||||
|
return JSON.stringify([
|
||||||
|
0,
|
||||||
|
evt.pubkey,
|
||||||
|
evt.created_at,
|
||||||
|
evt.kind,
|
||||||
|
evt.tags,
|
||||||
|
evt.content
|
||||||
|
])
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getEventHash(event) {
|
||||||
|
let eventHash = createHash('sha256')
|
||||||
|
.update(Buffer.from(serializeEvent(event)))
|
||||||
|
.digest()
|
||||||
|
return Buffer.from(eventHash).toString('hex')
|
||||||
|
}
|
||||||
|
|
||||||
|
export function validateEvent(event) {
|
||||||
|
if (event.id !== getEventHash(event)) return false
|
||||||
|
if (typeof event.content !== 'string') return false
|
||||||
|
if (typeof event.created_at !== 'number') return false
|
||||||
|
|
||||||
|
if (!Array.isArray(event.tags)) return false
|
||||||
|
for (let i = 0; i < event.tags.length; i++) {
|
||||||
|
let tag = event.tags[i]
|
||||||
|
if (!Array.isArray(tag)) return false
|
||||||
|
for (let j = 0; j < tag.length; j++) {
|
||||||
|
if (typeof tag[j] === 'object') return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
export function verifySignature(event) {
|
||||||
|
return secp256k1.schnorr.verify(event.sig, event.id, event.pubkey)
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function signEvent(event, key) {
|
||||||
|
return Buffer.from(secp256k1.schnorr.sign(getEventHash(event), key)).toString(
|
||||||
|
'hex'
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -1,49 +0,0 @@
|
|||||||
/* eslint-env jest */
|
|
||||||
|
|
||||||
const {
|
|
||||||
validateEvent,
|
|
||||||
verifySignature,
|
|
||||||
signEvent,
|
|
||||||
getEventHash,
|
|
||||||
getPublicKey
|
|
||||||
} = require('./lib/nostr.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()
|
|
||||||
})
|
|
||||||
86
event.ts
86
event.ts
@@ -1,86 +0,0 @@
|
|||||||
import * as secp256k1 from '@noble/secp256k1'
|
|
||||||
import {sha256} from '@noble/hashes/sha256'
|
|
||||||
|
|
||||||
import {utf8Encoder} from './utils'
|
|
||||||
|
|
||||||
|
|
||||||
/* eslint-disable no-unused-vars */
|
|
||||||
export enum Kind {
|
|
||||||
Metadata = 0,
|
|
||||||
Text = 1,
|
|
||||||
RecommendRelay = 2,
|
|
||||||
Contacts = 3,
|
|
||||||
EncryptedDirectMessage = 4,
|
|
||||||
EventDeletion = 5,
|
|
||||||
Reaction = 7,
|
|
||||||
ChannelCreation = 40,
|
|
||||||
ChannelMetadata = 41,
|
|
||||||
ChannelMessage = 42,
|
|
||||||
ChannelHideMessage = 43,
|
|
||||||
ChannelMuteUser = 44,
|
|
||||||
}
|
|
||||||
|
|
||||||
export type Event = {
|
|
||||||
id?: string
|
|
||||||
sig?: string
|
|
||||||
kind: Kind
|
|
||||||
tags: string[][]
|
|
||||||
pubkey: string
|
|
||||||
content: string
|
|
||||||
created_at: number
|
|
||||||
}
|
|
||||||
|
|
||||||
export function getBlankEvent(): Event {
|
|
||||||
return {
|
|
||||||
kind: 255,
|
|
||||||
pubkey: '',
|
|
||||||
content: '',
|
|
||||||
tags: [],
|
|
||||||
created_at: 0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export function serializeEvent(evt: Event): string {
|
|
||||||
return JSON.stringify([
|
|
||||||
0,
|
|
||||||
evt.pubkey,
|
|
||||||
evt.created_at,
|
|
||||||
evt.kind,
|
|
||||||
evt.tags,
|
|
||||||
evt.content
|
|
||||||
])
|
|
||||||
}
|
|
||||||
|
|
||||||
export function getEventHash(event: Event): string {
|
|
||||||
let eventHash = sha256(utf8Encoder.encode(serializeEvent(event)))
|
|
||||||
return secp256k1.utils.bytesToHex(eventHash)
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
if (!Array.isArray(event.tags)) return false
|
|
||||||
for (let i = 0; i < event.tags.length; i++) {
|
|
||||||
let tag = event.tags[i]
|
|
||||||
if (!Array.isArray(tag)) return false
|
|
||||||
for (let j = 0; j < tag.length; j++) {
|
|
||||||
if (typeof tag[j] === 'object') return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
export function verifySignature(
|
|
||||||
event: Event & {id: string; sig: string}
|
|
||||||
): boolean {
|
|
||||||
return secp256k1.schnorr.verifySync(event.sig, event.id, event.pubkey)
|
|
||||||
}
|
|
||||||
|
|
||||||
export function signEvent(event: Event, key: string): string {
|
|
||||||
return secp256k1.utils.bytesToHex(
|
|
||||||
secp256k1.schnorr.signSync(getEventHash(event), key)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
@@ -1,19 +1,4 @@
|
|||||||
import {Event} from './event'
|
export function matchFilter(filter, event) {
|
||||||
|
|
||||||
export type Filter = {
|
|
||||||
ids?: string[]
|
|
||||||
kinds?: number[]
|
|
||||||
authors?: string[]
|
|
||||||
since?: number
|
|
||||||
until?: number
|
|
||||||
limit?: 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.ids && filter.ids.indexOf(event.id) === -1) return false
|
||||||
if (filter.kinds && filter.kinds.indexOf(event.kind) === -1) return false
|
if (filter.kinds && filter.kinds.indexOf(event.kind) === -1) return false
|
||||||
if (filter.authors && filter.authors.indexOf(event.pubkey) === -1)
|
if (filter.authors && filter.authors.indexOf(event.pubkey) === -1)
|
||||||
@@ -21,12 +6,10 @@ export function matchFilter(
|
|||||||
|
|
||||||
for (let f in filter) {
|
for (let f in filter) {
|
||||||
if (f[0] === '#') {
|
if (f[0] === '#') {
|
||||||
let tagName = f.slice(1)
|
|
||||||
let values = filter[`#${tagName}`]
|
|
||||||
if (
|
if (
|
||||||
values &&
|
filter[f] &&
|
||||||
!event.tags.find(
|
!event.tags.find(
|
||||||
([t, v]) => t === f.slice(1) && values.indexOf(v) !== -1
|
([t, v]) => t === f.slice(1) && filter[f].indexOf(v) !== -1
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
return false
|
return false
|
||||||
@@ -39,10 +22,7 @@ export function matchFilter(
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
export function matchFilters(
|
export function matchFilters(filters, event) {
|
||||||
filters: Filter[],
|
|
||||||
event: Event & {id: string}
|
|
||||||
): boolean {
|
|
||||||
for (let i = 0; i < filters.length; i++) {
|
for (let i = 0; i < filters.length; i++) {
|
||||||
if (matchFilter(filters[i], event)) return true
|
if (matchFilter(filters[i], event)) return true
|
||||||
}
|
}
|
||||||
@@ -1,42 +0,0 @@
|
|||||||
/* eslint-env jest */
|
|
||||||
|
|
||||||
const {matchFilters} = require('./lib/nostr.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()
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
27
index.js
Normal file
27
index.js
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
import {generatePrivateKey, getPublicKey} from './keys'
|
||||||
|
import {relayConnect} from './relay'
|
||||||
|
import {relayPool} from './pool'
|
||||||
|
import {
|
||||||
|
getBlankEvent,
|
||||||
|
signEvent,
|
||||||
|
validateEvent,
|
||||||
|
verifySignature,
|
||||||
|
serializeEvent,
|
||||||
|
getEventHash
|
||||||
|
} from './event'
|
||||||
|
import {matchFilter, matchFilters} from './filter'
|
||||||
|
|
||||||
|
export {
|
||||||
|
generatePrivateKey,
|
||||||
|
relayConnect,
|
||||||
|
relayPool,
|
||||||
|
signEvent,
|
||||||
|
validateEvent,
|
||||||
|
verifySignature,
|
||||||
|
serializeEvent,
|
||||||
|
getEventHash,
|
||||||
|
getPublicKey,
|
||||||
|
getBlankEvent,
|
||||||
|
matchFilter,
|
||||||
|
matchFilters
|
||||||
|
}
|
||||||
19
index.ts
19
index.ts
@@ -1,19 +0,0 @@
|
|||||||
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'
|
|
||||||
export * as nip19 from './nip19'
|
|
||||||
export * as nip26 from './nip26'
|
|
||||||
|
|
||||||
// monkey patch secp256k1
|
|
||||||
import * as secp256k1 from '@noble/secp256k1'
|
|
||||||
import {hmac} from '@noble/hashes/hmac'
|
|
||||||
import {sha256} from '@noble/hashes/sha256'
|
|
||||||
secp256k1.utils.hmacSha256Sync = (key, ...msgs) =>
|
|
||||||
hmac(sha256, key, secp256k1.utils.concatBytes(...msgs))
|
|
||||||
secp256k1.utils.sha256Sync = (...msgs) =>
|
|
||||||
sha256(secp256k1.utils.concatBytes(...msgs))
|
|
||||||
9
keys.js
Normal file
9
keys.js
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
import * as secp256k1 from '@noble/secp256k1'
|
||||||
|
|
||||||
|
export function generatePrivateKey() {
|
||||||
|
return Buffer.from(secp256k1.utils.randomPrivateKey()).toString('hex')
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getPublicKey(privateKey) {
|
||||||
|
return Buffer.from(secp256k1.schnorr.getPublicKey(privateKey)).toString('hex')
|
||||||
|
}
|
||||||
20
keys.test.js
20
keys.test.js
@@ -1,20 +0,0 @@
|
|||||||
/* eslint-env jest */
|
|
||||||
|
|
||||||
const {generatePrivateKey, getPublicKey} = require('./lib/nostr.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)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
9
keys.ts
9
keys.ts
@@ -1,9 +0,0 @@
|
|||||||
import * as secp256k1 from '@noble/secp256k1'
|
|
||||||
|
|
||||||
export function generatePrivateKey(): string {
|
|
||||||
return secp256k1.utils.bytesToHex(secp256k1.utils.randomPrivateKey())
|
|
||||||
}
|
|
||||||
|
|
||||||
export function getPublicKey(privateKey: string): string {
|
|
||||||
return secp256k1.utils.bytesToHex(secp256k1.schnorr.getPublicKey(privateKey))
|
|
||||||
}
|
|
||||||
42
nip04.js
Normal file
42
nip04.js
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
import aes from 'browserify-cipher'
|
||||||
|
import {Buffer} from 'buffer'
|
||||||
|
import {randomBytes} from '@noble/hashes/utils'
|
||||||
|
import * as secp256k1 from '@noble/secp256k1'
|
||||||
|
|
||||||
|
export function encrypt(privkey, pubkey, text) {
|
||||||
|
const key = secp256k1.getSharedSecret(privkey, '02' + pubkey)
|
||||||
|
const normalizedKey = getNormalizedX(key)
|
||||||
|
|
||||||
|
let iv = Uint8Array.from(randomBytes(16))
|
||||||
|
var cipher = aes.createCipheriv(
|
||||||
|
'aes-256-cbc',
|
||||||
|
Buffer.from(normalizedKey, 'hex'),
|
||||||
|
iv
|
||||||
|
)
|
||||||
|
let encryptedMessage = cipher.update(text, 'utf8', 'base64')
|
||||||
|
encryptedMessage += cipher.final('base64')
|
||||||
|
|
||||||
|
return `${encryptedMessage}?iv=${Buffer.from(iv.buffer).toString('base64')}`
|
||||||
|
}
|
||||||
|
|
||||||
|
export function decrypt(privkey, pubkey, ciphertext) {
|
||||||
|
let [cip, iv] = ciphertext.split('?iv=')
|
||||||
|
let key = secp256k1.getSharedSecret(privkey, '02' + pubkey)
|
||||||
|
let normalizedKey = getNormalizedX(key)
|
||||||
|
|
||||||
|
var decipher = aes.createDecipheriv(
|
||||||
|
'aes-256-cbc',
|
||||||
|
Buffer.from(normalizedKey, 'hex'),
|
||||||
|
Buffer.from(iv, 'base64')
|
||||||
|
)
|
||||||
|
let decryptedMessage = decipher.update(cip, 'base64')
|
||||||
|
decryptedMessage += decipher.final('utf8')
|
||||||
|
|
||||||
|
return decryptedMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
function getNormalizedX(key) {
|
||||||
|
return typeof key === 'string'
|
||||||
|
? key.substr(2, 64)
|
||||||
|
: Buffer.from(key.slice(1, 33)).toString('hex')
|
||||||
|
}
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
/* eslint-env jest */
|
|
||||||
|
|
||||||
globalThis.crypto = require('crypto')
|
|
||||||
const {nip04, getPublicKey, generatePrivateKey} = require('./lib/nostr.cjs')
|
|
||||||
|
|
||||||
test('encrypt and decrypt message', async () => {
|
|
||||||
let sk1 = generatePrivateKey()
|
|
||||||
let sk2 = generatePrivateKey()
|
|
||||||
let pk1 = getPublicKey(sk1)
|
|
||||||
let pk2 = getPublicKey(sk2)
|
|
||||||
|
|
||||||
expect(
|
|
||||||
await nip04.decrypt(sk2, pk1, await nip04.encrypt(sk1, pk2, 'hello'))
|
|
||||||
).toEqual('hello')
|
|
||||||
})
|
|
||||||
66
nip04.ts
66
nip04.ts
@@ -1,66 +0,0 @@
|
|||||||
import {randomBytes} from '@noble/hashes/utils'
|
|
||||||
import * as secp256k1 from '@noble/secp256k1'
|
|
||||||
import {base64} from '@scure/base'
|
|
||||||
|
|
||||||
import {utf8Decoder, utf8Encoder} from './utils'
|
|
||||||
|
|
||||||
export async function encrypt(
|
|
||||||
privkey: string,
|
|
||||||
pubkey: string,
|
|
||||||
text: string
|
|
||||||
): Promise<string> {
|
|
||||||
const key = secp256k1.getSharedSecret(privkey, '02' + pubkey)
|
|
||||||
const normalizedKey = getNormalizedX(key)
|
|
||||||
|
|
||||||
let iv = Uint8Array.from(randomBytes(16))
|
|
||||||
let plaintext = utf8Encoder.encode(text)
|
|
||||||
let cryptoKey = await crypto.subtle.importKey(
|
|
||||||
'raw',
|
|
||||||
normalizedKey,
|
|
||||||
{name: 'AES-CBC'},
|
|
||||||
false,
|
|
||||||
['encrypt']
|
|
||||||
)
|
|
||||||
let ciphertext = await crypto.subtle.encrypt(
|
|
||||||
{name: 'AES-CBC', iv},
|
|
||||||
cryptoKey,
|
|
||||||
plaintext
|
|
||||||
)
|
|
||||||
let ctb64 = base64.encode(new Uint8Array(ciphertext))
|
|
||||||
let ivb64 = base64.encode(new Uint8Array(iv.buffer))
|
|
||||||
|
|
||||||
return `${ctb64}?iv=${ivb64}`
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function decrypt(
|
|
||||||
privkey: string,
|
|
||||||
pubkey: string,
|
|
||||||
data: string
|
|
||||||
): Promise<string> {
|
|
||||||
let [ctb64, ivb64] = data.split('?iv=')
|
|
||||||
let key = secp256k1.getSharedSecret(privkey, '02' + pubkey)
|
|
||||||
let normalizedKey = getNormalizedX(key)
|
|
||||||
|
|
||||||
let cryptoKey = await crypto.subtle.importKey(
|
|
||||||
'raw',
|
|
||||||
normalizedKey,
|
|
||||||
{name: 'AES-CBC'},
|
|
||||||
false,
|
|
||||||
['decrypt']
|
|
||||||
)
|
|
||||||
let ciphertext = base64.decode(ctb64)
|
|
||||||
let iv = base64.decode(ivb64)
|
|
||||||
|
|
||||||
let plaintext = await crypto.subtle.decrypt(
|
|
||||||
{name: 'AES-CBC', iv},
|
|
||||||
cryptoKey,
|
|
||||||
ciphertext
|
|
||||||
)
|
|
||||||
|
|
||||||
let text = utf8Decoder.decode(plaintext)
|
|
||||||
return text
|
|
||||||
}
|
|
||||||
|
|
||||||
function getNormalizedX(key: Uint8Array): Uint8Array {
|
|
||||||
return key.slice(1, 33)
|
|
||||||
}
|
|
||||||
32
nip05.js
Normal file
32
nip05.js
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
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) {
|
||||||
|
domain = name
|
||||||
|
name = '_'
|
||||||
|
}
|
||||||
|
|
||||||
|
let res = await (
|
||||||
|
await fetch(`https://${domain}/.well-known/nostr.json?name=${name}`)
|
||||||
|
).json()
|
||||||
|
|
||||||
|
return res.names && res.names[name]
|
||||||
|
} catch (_) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,20 +0,0 @@
|
|||||||
/* eslint-env jest */
|
|
||||||
|
|
||||||
const fetch = require('node-fetch')
|
|
||||||
const {nip05} = require('./lib/nostr.cjs')
|
|
||||||
|
|
||||||
test('fetch nip05 profiles', async () => {
|
|
||||||
nip05.useFetchImplementation(fetch)
|
|
||||||
|
|
||||||
let p1 = await nip05.queryProfile('jb55.com')
|
|
||||||
expect(p1.pubkey).toEqual(
|
|
||||||
'32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245'
|
|
||||||
)
|
|
||||||
expect(p1.relays).toEqual(['wss://relay.damus.io'])
|
|
||||||
|
|
||||||
let p2 = await nip05.queryProfile('jb55@jb55.com')
|
|
||||||
expect(p2.pubkey).toEqual(
|
|
||||||
'32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245'
|
|
||||||
)
|
|
||||||
expect(p2.relays).toEqual(['wss://relay.damus.io'])
|
|
||||||
})
|
|
||||||
54
nip05.ts
54
nip05.ts
@@ -1,54 +0,0 @@
|
|||||||
import {ProfilePointer} from './nip19'
|
|
||||||
|
|
||||||
var _fetch: any
|
|
||||||
|
|
||||||
try {
|
|
||||||
_fetch = fetch
|
|
||||||
} catch {}
|
|
||||||
|
|
||||||
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 queryProfile(
|
|
||||||
fullname: string
|
|
||||||
): Promise<ProfilePointer | null> {
|
|
||||||
let [name, domain] = fullname.split('@')
|
|
||||||
|
|
||||||
if (!domain) {
|
|
||||||
// if there is no @, it is because it is just a domain, so assume the name is "_"
|
|
||||||
domain = name
|
|
||||||
name = '_'
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!name.match(/^[a-z0-9-_]+$/)) return null
|
|
||||||
|
|
||||||
let res = await (
|
|
||||||
await _fetch(`https://${domain}/.well-known/nostr.json?name=${name}`)
|
|
||||||
).json()
|
|
||||||
|
|
||||||
if (!res?.names?.[name]) return null
|
|
||||||
|
|
||||||
let pubkey = res.names[name] as string
|
|
||||||
let relays = (res.relays?.[pubkey] || []) as string[] // nip35
|
|
||||||
|
|
||||||
return {
|
|
||||||
pubkey,
|
|
||||||
relays
|
|
||||||
}
|
|
||||||
}
|
|
||||||
26
nip06.js
Normal file
26
nip06.js
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
import {wordlist} from 'micro-bip39/wordlists/english'
|
||||||
|
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)
|
||||||
|
}
|
||||||
23
nip06.ts
23
nip06.ts
@@ -1,23 +0,0 @@
|
|||||||
import * as secp256k1 from '@noble/secp256k1'
|
|
||||||
import {wordlist} from '@scure/bip39/wordlists/english.js'
|
|
||||||
import {
|
|
||||||
generateMnemonic,
|
|
||||||
mnemonicToSeedSync,
|
|
||||||
validateMnemonic
|
|
||||||
} from '@scure/bip39'
|
|
||||||
import {HDKey} from '@scure/bip32'
|
|
||||||
|
|
||||||
export function privateKeyFromSeedWords(mnemonic: string): string {
|
|
||||||
let root = HDKey.fromMasterSeed(mnemonicToSeedSync(mnemonic))
|
|
||||||
let privateKey = root.derive(`m/44'/1237'/0'/0/0`).privateKey
|
|
||||||
if (!privateKey) throw new Error('could not derive private key')
|
|
||||||
return secp256k1.utils.bytesToHex(privateKey)
|
|
||||||
}
|
|
||||||
|
|
||||||
export function generateSeedWords(): string {
|
|
||||||
return generateMnemonic(wordlist)
|
|
||||||
}
|
|
||||||
|
|
||||||
export function validateWords(words: string): boolean {
|
|
||||||
return validateMnemonic(words, wordlist)
|
|
||||||
}
|
|
||||||
@@ -1,36 +0,0 @@
|
|||||||
/* eslint-env jest */
|
|
||||||
|
|
||||||
const {nip19, generatePrivateKey, getPublicKey} = require('./lib/nostr.cjs')
|
|
||||||
|
|
||||||
test('encode and decode nsec', () => {
|
|
||||||
let sk = generatePrivateKey()
|
|
||||||
let nsec = nip19.nsecEncode(sk)
|
|
||||||
expect(nsec).toMatch(/nsec1\w+/)
|
|
||||||
let {type, data} = nip19.decode(nsec)
|
|
||||||
expect(type).toEqual('nsec')
|
|
||||||
expect(data).toEqual(sk)
|
|
||||||
})
|
|
||||||
|
|
||||||
test('encode and decode npub', () => {
|
|
||||||
let pk = getPublicKey(generatePrivateKey())
|
|
||||||
let npub = nip19.npubEncode(pk)
|
|
||||||
expect(npub).toMatch(/npub1\w+/)
|
|
||||||
let {type, data} = nip19.decode(npub)
|
|
||||||
expect(type).toEqual('npub')
|
|
||||||
expect(data).toEqual(pk)
|
|
||||||
})
|
|
||||||
|
|
||||||
test('encode and decode nprofile', () => {
|
|
||||||
let pk = getPublicKey(generatePrivateKey())
|
|
||||||
let relays = [
|
|
||||||
'wss://relay.nostr.example.mydomain.example.com',
|
|
||||||
'wss://nostr.banana.com'
|
|
||||||
]
|
|
||||||
let nprofile = nip19.nprofileEncode({pubkey: pk, relays})
|
|
||||||
expect(nprofile).toMatch(/nprofile1\w+/)
|
|
||||||
let {type, data} = nip19.decode(nprofile)
|
|
||||||
expect(type).toEqual('nprofile')
|
|
||||||
expect(data.pubkey).toEqual(pk)
|
|
||||||
expect(data.relays).toContain(relays[0])
|
|
||||||
expect(data.relays).toContain(relays[1])
|
|
||||||
})
|
|
||||||
125
nip19.ts
125
nip19.ts
@@ -1,125 +0,0 @@
|
|||||||
import * as secp256k1 from '@noble/secp256k1'
|
|
||||||
import {bech32} from '@scure/base'
|
|
||||||
|
|
||||||
import {utf8Decoder, utf8Encoder} from './utils'
|
|
||||||
|
|
||||||
export type ProfilePointer = {
|
|
||||||
pubkey: string // hex
|
|
||||||
relays?: string[]
|
|
||||||
}
|
|
||||||
|
|
||||||
export type EventPointer = {
|
|
||||||
id: string // hex
|
|
||||||
relays?: string[]
|
|
||||||
}
|
|
||||||
|
|
||||||
export function decode(nip19: string): {
|
|
||||||
type: string
|
|
||||||
data: ProfilePointer | EventPointer | string
|
|
||||||
} {
|
|
||||||
let {prefix, words} = bech32.decode(nip19, 1000)
|
|
||||||
let data = new Uint8Array(bech32.fromWords(words))
|
|
||||||
|
|
||||||
if (prefix === 'nprofile') {
|
|
||||||
let tlv = parseTLV(data)
|
|
||||||
if (!tlv[0]?.[0]) throw new Error('missing TLV 0 for nprofile')
|
|
||||||
if (tlv[0][0].length !== 32) throw new Error('TLV 0 should be 32 bytes')
|
|
||||||
|
|
||||||
return {
|
|
||||||
type: 'nprofile',
|
|
||||||
data: {
|
|
||||||
pubkey: secp256k1.utils.bytesToHex(tlv[0][0]),
|
|
||||||
relays: tlv[1].map(d => utf8Decoder.decode(d))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (prefix === 'nevent') {
|
|
||||||
let tlv = parseTLV(data)
|
|
||||||
if (!tlv[0]?.[0]) throw new Error('missing TLV 0 for nevent')
|
|
||||||
if (tlv[0][0].length !== 32) throw new Error('TLV 0 should be 32 bytes')
|
|
||||||
|
|
||||||
return {
|
|
||||||
type: 'nevent',
|
|
||||||
data: {
|
|
||||||
id: secp256k1.utils.bytesToHex(tlv[0][0]),
|
|
||||||
relays: tlv[1].map(d => utf8Decoder.decode(d))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (prefix === 'nsec' || prefix === 'npub' || prefix === 'note') {
|
|
||||||
return {type: prefix, data: secp256k1.utils.bytesToHex(data)}
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new Error(`unknown prefix ${prefix}`)
|
|
||||||
}
|
|
||||||
|
|
||||||
type TLV = {[t: number]: Uint8Array[]}
|
|
||||||
|
|
||||||
function parseTLV(data: Uint8Array): TLV {
|
|
||||||
let result: TLV = {}
|
|
||||||
let rest = data
|
|
||||||
while (rest.length > 0) {
|
|
||||||
let t = rest[0]
|
|
||||||
let l = rest[1]
|
|
||||||
let v = rest.slice(2, 2 + l)
|
|
||||||
rest = rest.slice(2 + l)
|
|
||||||
if (v.length < l) continue
|
|
||||||
result[t] = result[t] || []
|
|
||||||
result[t].push(v)
|
|
||||||
}
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
export function nsecEncode(hex: string): string {
|
|
||||||
return encodeBytes('nsec', hex)
|
|
||||||
}
|
|
||||||
|
|
||||||
export function npubEncode(hex: string): string {
|
|
||||||
return encodeBytes('npub', hex)
|
|
||||||
}
|
|
||||||
|
|
||||||
export function noteEncode(hex: string): string {
|
|
||||||
return encodeBytes('note', hex)
|
|
||||||
}
|
|
||||||
|
|
||||||
function encodeBytes(prefix: string, hex: string): string {
|
|
||||||
let data = secp256k1.utils.hexToBytes(hex)
|
|
||||||
let words = bech32.toWords(data)
|
|
||||||
return bech32.encode(prefix, words, 1000)
|
|
||||||
}
|
|
||||||
|
|
||||||
export function nprofileEncode(profile: ProfilePointer): string {
|
|
||||||
let data = encodeTLV({
|
|
||||||
0: [secp256k1.utils.hexToBytes(profile.pubkey)],
|
|
||||||
1: (profile.relays || []).map(url => utf8Encoder.encode(url))
|
|
||||||
})
|
|
||||||
let words = bech32.toWords(data)
|
|
||||||
return bech32.encode('nprofile', words, 1000)
|
|
||||||
}
|
|
||||||
|
|
||||||
export function neventEncode(event: EventPointer): string {
|
|
||||||
let data = encodeTLV({
|
|
||||||
0: [secp256k1.utils.hexToBytes(event.id)],
|
|
||||||
1: (event.relays || []).map(url => utf8Encoder.encode(url))
|
|
||||||
})
|
|
||||||
let words = bech32.toWords(data)
|
|
||||||
return bech32.encode('nevent', words, 1000)
|
|
||||||
}
|
|
||||||
|
|
||||||
function encodeTLV(tlv: TLV): Uint8Array {
|
|
||||||
let entries: Uint8Array[] = []
|
|
||||||
|
|
||||||
Object.entries(tlv).forEach(([t, vs]) => {
|
|
||||||
vs.forEach(v => {
|
|
||||||
let entry = new Uint8Array(v.length + 2)
|
|
||||||
entry.set([parseInt(t)], 0)
|
|
||||||
entry.set([v.length], 1)
|
|
||||||
entry.set(v, 2)
|
|
||||||
entries.push(entry)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
return secp256k1.utils.concatBytes(...entries)
|
|
||||||
}
|
|
||||||
105
nip26.test.js
105
nip26.test.js
@@ -1,105 +0,0 @@
|
|||||||
/* eslint-env jest */
|
|
||||||
|
|
||||||
const {nip26, getPublicKey, generatePrivateKey} = require('./lib/nostr.cjs')
|
|
||||||
|
|
||||||
test('parse good delegation from NIP', async () => {
|
|
||||||
expect(
|
|
||||||
nip26.getDelegator({
|
|
||||||
id: 'a080fd288b60ac2225ff2e2d815291bd730911e583e177302cc949a15dc2b2dc',
|
|
||||||
pubkey:
|
|
||||||
'62903b1ff41559daf9ee98ef1ae67cc52f301bb5ce26d14baba3052f649c3f49',
|
|
||||||
created_at: 1660896109,
|
|
||||||
kind: 1,
|
|
||||||
tags: [
|
|
||||||
[
|
|
||||||
'delegation',
|
|
||||||
'86f0689bd48dcd19c67a19d994f938ee34f251d8c39976290955ff585f2db42e',
|
|
||||||
'kind=1&created_at>1640995200',
|
|
||||||
'c33c88ba78ec3c760e49db591ac5f7b129e3887c8af7729795e85a0588007e5ac89b46549232d8f918eefd73e726cb450135314bfda419c030d0b6affe401ec1'
|
|
||||||
]
|
|
||||||
],
|
|
||||||
content: 'Hello world',
|
|
||||||
sig: 'cd4a3cd20dc61dcbc98324de561a07fd23b3d9702115920c0814b5fb822cc5b7c5bcdaf3fa326d24ed50c5b9c8214d66c75bae34e3a84c25e4d122afccb66eb6'
|
|
||||||
})
|
|
||||||
).toEqual('86f0689bd48dcd19c67a19d994f938ee34f251d8c39976290955ff585f2db42e')
|
|
||||||
})
|
|
||||||
|
|
||||||
test('parse bad delegations', async () => {
|
|
||||||
expect(
|
|
||||||
nip26.getDelegator({
|
|
||||||
id: 'a080fd288b60ac2225ff2e2d815291bd730911e583e177302cc949a15dc2b2dc',
|
|
||||||
pubkey:
|
|
||||||
'62903b1ff41559daf9ee98ef1ae67cc52f301bb5ce26d14baba3052f649c3f49',
|
|
||||||
created_at: 1660896109,
|
|
||||||
kind: 1,
|
|
||||||
tags: [
|
|
||||||
[
|
|
||||||
'delegation',
|
|
||||||
'86f0689bd48dcd19c67a19d994f938ee34f251d8c39976290955ff585f2db42f',
|
|
||||||
'kind=1&created_at>1640995200',
|
|
||||||
'c33c88ba78ec3c760e49db591ac5f7b129e3887c8af7729795e85a0588007e5ac89b46549232d8f918eefd73e726cb450135314bfda419c030d0b6affe401ec1'
|
|
||||||
]
|
|
||||||
],
|
|
||||||
content: 'Hello world',
|
|
||||||
sig: 'cd4a3cd20dc61dcbc98324de561a07fd23b3d9702115920c0814b5fb822cc5b7c5bcdaf3fa326d24ed50c5b9c8214d66c75bae34e3a84c25e4d122afccb66eb6'
|
|
||||||
})
|
|
||||||
).toEqual(null)
|
|
||||||
|
|
||||||
expect(
|
|
||||||
nip26.getDelegator({
|
|
||||||
id: 'a080fd288b60ac2225ff2e2d815291bd730911e583e177302cc949a15dc2b2dc',
|
|
||||||
pubkey:
|
|
||||||
'62903b1ff41559daf9ee98ef1ae67cc52f301bb5ce26d14baba3052f649c3f49',
|
|
||||||
created_at: 1660896109,
|
|
||||||
kind: 1,
|
|
||||||
tags: [
|
|
||||||
[
|
|
||||||
'delegation',
|
|
||||||
'86f0689bd48dcd19c67a19d994f938ee34f251d8c39976290955ff585f2db42e',
|
|
||||||
'kind=1&created_at>1740995200',
|
|
||||||
'c33c88ba78ec3c760e49db591ac5f7b129e3887c8af7729795e85a0588007e5ac89b46549232d8f918eefd73e726cb450135314bfda419c030d0b6affe401ec1'
|
|
||||||
]
|
|
||||||
],
|
|
||||||
content: 'Hello world',
|
|
||||||
sig: 'cd4a3cd20dc61dcbc98324de561a07fd23b3d9702115920c0814b5fb822cc5b7c5bcdaf3fa326d24ed50c5b9c8214d66c75bae34e3a84c25e4d122afccb66eb6'
|
|
||||||
})
|
|
||||||
).toEqual(null)
|
|
||||||
|
|
||||||
expect(
|
|
||||||
nip26.getDelegator({
|
|
||||||
id: 'a080fd288b60ac2225ff2e2d815291bd730911e583e177302cc949a15dc2b2dc',
|
|
||||||
pubkey:
|
|
||||||
'62903b1ff41559daf9ee98ef1ae67c152f301bb5ce26d14baba3052f649c3f49',
|
|
||||||
created_at: 1660896109,
|
|
||||||
kind: 1,
|
|
||||||
tags: [
|
|
||||||
[
|
|
||||||
'delegation',
|
|
||||||
'86f0689bd48dcd19c67a19d994f938ee34f251d8c39976290955ff585f2db42e',
|
|
||||||
'kind=1&created_at>1640995200',
|
|
||||||
'c33c88ba78ec3c760e49db591ac5f7b129e3887c8af7729795e85a0588007e5ac89b46549232d8f918eefd73e726cb450135314bfda419c030d0b6affe401ec1'
|
|
||||||
]
|
|
||||||
],
|
|
||||||
content: 'Hello world',
|
|
||||||
sig: 'cd4a3cd20dc61dcbc98324de561a07fd23b3d9702115920c0814b5fb822cc5b7c5bcdaf3fa326d24ed50c5b9c8214d66c75bae34e3a84c25e4d122afccb66eb6'
|
|
||||||
})
|
|
||||||
).toEqual(null)
|
|
||||||
})
|
|
||||||
|
|
||||||
test('create and verify delegation', async () => {
|
|
||||||
let sk1 = generatePrivateKey()
|
|
||||||
let pk1 = getPublicKey(sk1)
|
|
||||||
let sk2 = generatePrivateKey()
|
|
||||||
let pk2 = getPublicKey(sk2)
|
|
||||||
let delegation = nip26.createDelegation(sk1, {pubkey: pk2, kind: 1})
|
|
||||||
expect(delegation).toHaveProperty('from', pk1)
|
|
||||||
expect(delegation).toHaveProperty('to', pk2)
|
|
||||||
expect(delegation).toHaveProperty('cond', 'kind=1')
|
|
||||||
|
|
||||||
let event = {
|
|
||||||
kind: 1,
|
|
||||||
tags: [['delegation', delegation.from, delegation.cond, delegation.sig]],
|
|
||||||
pubkey: pk2
|
|
||||||
}
|
|
||||||
expect(nip26.getDelegator(event)).toEqual(pk1)
|
|
||||||
})
|
|
||||||
90
nip26.ts
90
nip26.ts
@@ -1,90 +0,0 @@
|
|||||||
import * as secp256k1 from '@noble/secp256k1'
|
|
||||||
import {sha256} from '@noble/hashes/sha256'
|
|
||||||
|
|
||||||
import {Event} from './event'
|
|
||||||
import {utf8Encoder} from './utils'
|
|
||||||
import {getPublicKey} from './keys'
|
|
||||||
|
|
||||||
export type Parameters = {
|
|
||||||
pubkey: string // the key to whom the delegation will be given
|
|
||||||
kind: number | undefined
|
|
||||||
until: number | undefined // delegation will only be valid until this date
|
|
||||||
since: number | undefined // delegation will be valid from this date on
|
|
||||||
}
|
|
||||||
|
|
||||||
export type Delegation = {
|
|
||||||
from: string // the pubkey who signed the delegation
|
|
||||||
to: string // the pubkey that is allowed to use the delegation
|
|
||||||
cond: string // the string of conditions as they should be included in the event tag
|
|
||||||
sig: string
|
|
||||||
}
|
|
||||||
|
|
||||||
export function createDelegation(
|
|
||||||
privateKey: string,
|
|
||||||
parameters: Parameters
|
|
||||||
): Delegation {
|
|
||||||
let conditions = []
|
|
||||||
if ((parameters.kind || -1) >= 0) conditions.push(`kind=${parameters.kind}`)
|
|
||||||
if (parameters.until) conditions.push(`created_at<${parameters.until}`)
|
|
||||||
if (parameters.since) conditions.push(`created_at>${parameters.since}`)
|
|
||||||
let cond = conditions.join('&')
|
|
||||||
|
|
||||||
if (cond === '')
|
|
||||||
throw new Error('refusing to create a delegation without any conditions')
|
|
||||||
|
|
||||||
let sighash = sha256(
|
|
||||||
utf8Encoder.encode(`nostr:delegation:${parameters.pubkey}:${cond}`)
|
|
||||||
)
|
|
||||||
|
|
||||||
let sig = secp256k1.utils.bytesToHex(
|
|
||||||
secp256k1.schnorr.signSync(sighash, privateKey)
|
|
||||||
)
|
|
||||||
|
|
||||||
return {
|
|
||||||
from: getPublicKey(privateKey),
|
|
||||||
to: parameters.pubkey,
|
|
||||||
cond,
|
|
||||||
sig
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export function getDelegator(event: Event): string | null {
|
|
||||||
// find delegation tag
|
|
||||||
let tag = event.tags.find(tag => tag[0] === 'delegation' && tag.length >= 4)
|
|
||||||
if (!tag) return null
|
|
||||||
|
|
||||||
let pubkey = tag[1]
|
|
||||||
let cond = tag[2]
|
|
||||||
let sig = tag[3]
|
|
||||||
|
|
||||||
// check conditions
|
|
||||||
let conditions = cond.split('&')
|
|
||||||
for (let i = 0; i < conditions.length; i++) {
|
|
||||||
let [key, operator, value] = conditions[i].split(/\b/)
|
|
||||||
|
|
||||||
// the supported conditions are just 'kind' and 'created_at' for now
|
|
||||||
if (key === 'kind' && operator === '=' && event.kind === parseInt(value))
|
|
||||||
continue
|
|
||||||
else if (
|
|
||||||
key === 'created_at' &&
|
|
||||||
operator === '<' &&
|
|
||||||
event.created_at < parseInt(value)
|
|
||||||
)
|
|
||||||
continue
|
|
||||||
else if (
|
|
||||||
key === 'created_at' &&
|
|
||||||
operator === '>' &&
|
|
||||||
event.created_at > parseInt(value)
|
|
||||||
)
|
|
||||||
continue
|
|
||||||
else return null // invalid condition
|
|
||||||
}
|
|
||||||
|
|
||||||
// check signature
|
|
||||||
let sighash = sha256(
|
|
||||||
utf8Encoder.encode(`nostr:delegation:${event.pubkey}:${cond}`)
|
|
||||||
)
|
|
||||||
if (!secp256k1.schnorr.verifySync(sig, sighash, pubkey)) return null
|
|
||||||
|
|
||||||
return pubkey
|
|
||||||
}
|
|
||||||
47
package.json
47
package.json
@@ -1,47 +1,36 @@
|
|||||||
{
|
{
|
||||||
"name": "nostr-tools",
|
"name": "nostr-tools",
|
||||||
"version": "1.1.0",
|
"version": "0.22.0",
|
||||||
"description": "Tools for making a Nostr client.",
|
"description": "Tools for making a Nostr client.",
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://github.com/fiatjaf/nostr-tools.git"
|
"url": "https://github.com/fiatjaf/nostr-tools.git"
|
||||||
},
|
},
|
||||||
"main": "lib/nostr.cjs.js",
|
|
||||||
"module": "lib/nostr.esm.js",
|
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@noble/hashes": "^0.5.7",
|
"@noble/hashes": "^0.5.7",
|
||||||
"@noble/secp256k1": "^1.7.0",
|
"@noble/secp256k1": "^1.5.2",
|
||||||
"@scure/base": "^1.1.1",
|
"browserify-cipher": ">=1",
|
||||||
"@scure/bip32": "^1.1.1",
|
"buffer": ">=5",
|
||||||
"@scure/bip39": "^1.1.0"
|
"create-hash": "^1.2.0",
|
||||||
|
"cross-fetch": "^3.1.4",
|
||||||
|
"micro-bip32": "^0.1.0",
|
||||||
|
"micro-bip39": "^0.1.3",
|
||||||
|
"websocket-polyfill": "^0.0.3"
|
||||||
},
|
},
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"decentralization",
|
"decentralization",
|
||||||
|
"twitter",
|
||||||
|
"p2p",
|
||||||
|
"mastodon",
|
||||||
|
"ssb",
|
||||||
"social",
|
"social",
|
||||||
|
"unstoppable",
|
||||||
|
"censorship",
|
||||||
"censorship-resistance",
|
"censorship-resistance",
|
||||||
"client",
|
"client"
|
||||||
"nostr"
|
|
||||||
],
|
],
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/node": "^18.0.3",
|
"eslint": "^8.5.0",
|
||||||
"@typescript-eslint/eslint-plugin": "^5.46.1",
|
"eslint-plugin-babel": "^5.3.1"
|
||||||
"@typescript-eslint/parser": "^5.46.1",
|
|
||||||
"esbuild": "0.16.9",
|
|
||||||
"esbuild-plugin-alias": "^0.2.1",
|
|
||||||
"eslint": "^8.30.0",
|
|
||||||
"eslint-plugin-babel": "^5.3.1",
|
|
||||||
"esm-loader-typescript": "^1.0.1",
|
|
||||||
"events": "^3.3.0",
|
|
||||||
"jest": "^29.3.1",
|
|
||||||
"node-fetch": "2",
|
|
||||||
"ts-jest": "^29.0.3",
|
|
||||||
"tsd": "^0.22.0",
|
|
||||||
"typescript": "^4.9.4",
|
|
||||||
"websocket-polyfill": "^0.0.3"
|
|
||||||
},
|
|
||||||
"scripts": {
|
|
||||||
"build": "node build.js",
|
|
||||||
"pretest": "node build.js",
|
|
||||||
"test": "jest"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
196
pool.js
Normal file
196
pool.js
Normal file
@@ -0,0 +1,196 @@
|
|||||||
|
import {getEventHash, verifySignature, signEvent} from './event'
|
||||||
|
import {relayConnect, normalizeRelayURL} from './relay'
|
||||||
|
|
||||||
|
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}, id = Math.random().toString().slice(2)) => {
|
||||||
|
const subControllers = Object.fromEntries(
|
||||||
|
Object.values(relays)
|
||||||
|
.filter(({policy}) => policy.read)
|
||||||
|
.map(({relay}) => [
|
||||||
|
relay.url,
|
||||||
|
relay.sub({filter, cb: event => cb(event, relay.url)}, id)
|
||||||
|
])
|
||||||
|
)
|
||||||
|
|
||||||
|
const activeCallback = cb
|
||||||
|
const activeFilters = filter
|
||||||
|
|
||||||
|
const unsub = () => {
|
||||||
|
Object.values(subControllers).forEach(sub => sub.unsub())
|
||||||
|
delete activeSubscriptions[id]
|
||||||
|
}
|
||||||
|
const sub = ({cb = activeCallback, filter = activeFilters}) => {
|
||||||
|
Object.entries(subControllers).map(([relayURL, sub]) => [
|
||||||
|
relayURL,
|
||||||
|
sub.sub({cb, filter}, id)
|
||||||
|
])
|
||||||
|
return activeSubscriptions[id]
|
||||||
|
}
|
||||||
|
const addRelay = relay => {
|
||||||
|
subControllers[relay.url] = relay.sub({cb, filter}, 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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
180
relay.js
Normal file
180
relay.js
Normal file
@@ -0,0 +1,180 @@
|
|||||||
|
/* global WebSocket */
|
||||||
|
|
||||||
|
import 'websocket-polyfill'
|
||||||
|
|
||||||
|
import {verifySignature, validateEvent} from './event'
|
||||||
|
import {matchFilters} from './filter'
|
||||||
|
|
||||||
|
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 = {}
|
||||||
|
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) &&
|
||||||
|
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}, channel = Math.random().toString().slice(2)) => {
|
||||||
|
var filters = []
|
||||||
|
if (Array.isArray(filter)) {
|
||||||
|
filters = filter
|
||||||
|
} else {
|
||||||
|
filters.push(filter)
|
||||||
|
}
|
||||||
|
|
||||||
|
trySend(['REQ', channel, ...filters])
|
||||||
|
channels[channel] = cb
|
||||||
|
openSubs[channel] = filters
|
||||||
|
|
||||||
|
const activeCallback = cb
|
||||||
|
const activeFilters = filters
|
||||||
|
|
||||||
|
return {
|
||||||
|
sub: ({cb = activeCallback, filter = activeFilters}) =>
|
||||||
|
sub({cb, filter}, channel),
|
||||||
|
unsub: () => {
|
||||||
|
delete openSubs[channel]
|
||||||
|
delete channels[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: {id: 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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
114
relay.test.js
114
relay.test.js
@@ -1,114 +0,0 @@
|
|||||||
/* eslint-env jest */
|
|
||||||
|
|
||||||
require('websocket-polyfill')
|
|
||||||
const {
|
|
||||||
relayInit,
|
|
||||||
generatePrivateKey,
|
|
||||||
getPublicKey,
|
|
||||||
getEventHash,
|
|
||||||
signEvent
|
|
||||||
} = require('./lib/nostr.cjs')
|
|
||||||
|
|
||||||
let relay = relayInit('wss://nostr-dev.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])
|
|
||||||
})
|
|
||||||
271
relay.ts
271
relay.ts
@@ -1,271 +0,0 @@
|
|||||||
/* global WebSocket */
|
|
||||||
|
|
||||||
import {Event, verifySignature, validateEvent} from './event'
|
|
||||||
import {Filter, matchFilters} from './filter'
|
|
||||||
|
|
||||||
type RelayEvent = 'connect' | 'disconnect' | 'error' | 'notice'
|
|
||||||
|
|
||||||
export type Relay = {
|
|
||||||
url: string
|
|
||||||
status: number
|
|
||||||
connect: () => Promise<void>
|
|
||||||
close: () => Promise<void>
|
|
||||||
sub: (filters: Filter[], opts?: SubscriptionOptions) => Sub
|
|
||||||
publish: (event: Event) => Pub
|
|
||||||
on: (type: RelayEvent, cb: any) => void
|
|
||||||
off: (type: RelayEvent, 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 resolveClose: () => void
|
|
||||||
var untilOpen: Promise<void>
|
|
||||||
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>
|
|
||||||
}
|
|
||||||
} = {}
|
|
||||||
|
|
||||||
async function connectRelay(): Promise<void> {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
ws = new WebSocket(url)
|
|
||||||
|
|
||||||
ws.onopen = () => {
|
|
||||||
listeners.connect.forEach(cb => cb())
|
|
||||||
resolve()
|
|
||||||
}
|
|
||||||
ws.onerror = () => {
|
|
||||||
listeners.error.forEach(cb => cb())
|
|
||||||
reject()
|
|
||||||
}
|
|
||||||
ws.onclose = async () => {
|
|
||||||
listeners.disconnect.forEach(cb => cb())
|
|
||||||
resolveClose && resolveClose()
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
async function connect(): Promise<void> {
|
|
||||||
if (ws?.readyState && ws.readyState === 1) return // ws already open
|
|
||||||
await connectRelay()
|
|
||||||
}
|
|
||||||
|
|
||||||
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 listeners = subListeners[subid]
|
|
||||||
let idx = listeners[type].indexOf(cb)
|
|
||||||
if (idx >= 0) listeners[type].splice(idx, 1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
url,
|
|
||||||
sub,
|
|
||||||
on: (
|
|
||||||
type: RelayEvent,
|
|
||||||
cb: any
|
|
||||||
): void => {
|
|
||||||
listeners[type].push(cb)
|
|
||||||
if (type === 'connect' && ws?.readyState === 1) {
|
|
||||||
cb()
|
|
||||||
}
|
|
||||||
},
|
|
||||||
off: (
|
|
||||||
type: RelayEvent,
|
|
||||||
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 listeners = pubListeners[id]
|
|
||||||
if (!listeners) return
|
|
||||||
let idx = listeners[type].indexOf(cb)
|
|
||||||
if (idx >= 0) listeners[type].splice(idx, 1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
connect,
|
|
||||||
close(): Promise<void> {
|
|
||||||
ws.close()
|
|
||||||
return new Promise<void>(resolve => {
|
|
||||||
resolveClose = resolve
|
|
||||||
})
|
|
||||||
},
|
|
||||||
get status() {
|
|
||||||
return ws?.readyState ?? 3
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
{
|
|
||||||
"compilerOptions": {
|
|
||||||
"module": "esnext",
|
|
||||||
"target": "esnext",
|
|
||||||
"lib": ["dom", "dom.iterable", "esnext"],
|
|
||||||
"declaration": true,
|
|
||||||
"strict": true,
|
|
||||||
"moduleResolution": "node",
|
|
||||||
"skipLibCheck": true,
|
|
||||||
"esModuleInterop": true,
|
|
||||||
"emitDeclarationOnly": true,
|
|
||||||
"outDir": "dist",
|
|
||||||
"rootDir": "."
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user