mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2025-12-09 16:48:50 +00:00
Compare commits
41 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c362212778 | ||
|
|
a8938a3a0f | ||
|
|
a21329da3f | ||
|
|
63f4a49a69 | ||
|
|
27749d91b8 | ||
|
|
9530849f0a | ||
|
|
b8aa75b6e1 | ||
|
|
344762820c | ||
|
|
f43d23d344 | ||
|
|
bf55ad6b5a | ||
|
|
04a46b815c | ||
|
|
165ff44dff | ||
|
|
7bfd23af3c | ||
|
|
3d93ec8446 | ||
|
|
0f841138cd | ||
|
|
336948b1d1 | ||
|
|
d46794c681 | ||
|
|
93cef5d886 | ||
|
|
2324f9548e | ||
|
|
f9748d9cc3 | ||
|
|
3a22dd3da6 | ||
|
|
d13039dc11 | ||
|
|
95b03902cc | ||
|
|
ab5ea8de36 | ||
|
|
a330b97590 | ||
|
|
24406b5679 | ||
|
|
6dbcc87d93 | ||
|
|
0ddcfdce68 | ||
|
|
87bf349ce8 | ||
|
|
54dfc7b972 | ||
|
|
32793146a4 | ||
|
|
c42cd925ce | ||
|
|
43ccb72476 | ||
|
|
b2b7999517 | ||
|
|
a568afc295 | ||
|
|
9bcaed6e60 | ||
|
|
5a9cbbb557 | ||
|
|
e9acc59809 | ||
|
|
18fe9637b9 | ||
|
|
ff3bf4a51c | ||
|
|
7ff97b5488 |
6
.github/workflows/test.yml
vendored
6
.github/workflows/test.yml
vendored
@@ -1,7 +1,9 @@
|
|||||||
name: test every commit
|
name: test every commit
|
||||||
on:
|
on:
|
||||||
- push
|
push:
|
||||||
- pull_request
|
branches:
|
||||||
|
- master
|
||||||
|
pull_request:
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
test:
|
test:
|
||||||
|
|||||||
53
README.md
53
README.md
@@ -4,6 +4,13 @@ Tools for developing [Nostr](https://github.com/fiatjaf/nostr) clients.
|
|||||||
|
|
||||||
Only depends on _@scure_ and _@noble_ packages.
|
Only depends on _@scure_ and _@noble_ packages.
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
```bash
|
||||||
|
npm install nostr-tools # or yarn add nostr-tools
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
### Generating a private key and a public key
|
### Generating a private key and a public key
|
||||||
@@ -53,8 +60,6 @@ import {
|
|||||||
} from 'nostr-tools'
|
} from 'nostr-tools'
|
||||||
|
|
||||||
const relay = relayInit('wss://relay.example.com')
|
const relay = relayInit('wss://relay.example.com')
|
||||||
await relay.connect()
|
|
||||||
|
|
||||||
relay.on('connect', () => {
|
relay.on('connect', () => {
|
||||||
console.log(`connected to ${relay.url}`)
|
console.log(`connected to ${relay.url}`)
|
||||||
})
|
})
|
||||||
@@ -62,6 +67,8 @@ relay.on('error', () => {
|
|||||||
console.log(`failed to connect to ${relay.url}`)
|
console.log(`failed to connect to ${relay.url}`)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
await relay.connect()
|
||||||
|
|
||||||
// let's query for an event that exists
|
// let's query for an event that exists
|
||||||
let sub = relay.sub([
|
let sub = relay.sub([
|
||||||
{
|
{
|
||||||
@@ -104,14 +111,16 @@ let pub = relay.publish(event)
|
|||||||
pub.on('ok', () => {
|
pub.on('ok', () => {
|
||||||
console.log(`${relay.url} has accepted our event`)
|
console.log(`${relay.url} has accepted our event`)
|
||||||
})
|
})
|
||||||
pub.on('seen', () => {
|
|
||||||
console.log(`we saw the event on ${relay.url}`)
|
|
||||||
})
|
|
||||||
pub.on('failed', reason => {
|
pub.on('failed', reason => {
|
||||||
console.log(`failed to publish to ${relay.url}: ${reason}`)
|
console.log(`failed to publish to ${relay.url}: ${reason}`)
|
||||||
})
|
})
|
||||||
|
|
||||||
await relay.close()
|
let events = await relay.list([{kinds: [0, 1]}])
|
||||||
|
let event = await relay.get({
|
||||||
|
ids: ['44e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245']
|
||||||
|
})
|
||||||
|
|
||||||
|
relay.close()
|
||||||
```
|
```
|
||||||
|
|
||||||
To use this on Node.js you first must install `websocket-polyfill` and import it:
|
To use this on Node.js you first must install `websocket-polyfill` and import it:
|
||||||
@@ -123,36 +132,39 @@ import 'websocket-polyfill'
|
|||||||
### Interacting with multiple relays
|
### Interacting with multiple relays
|
||||||
|
|
||||||
```js
|
```js
|
||||||
import {pool} from 'nostr-tools'
|
import {SimplePool} from 'nostr-tools'
|
||||||
|
|
||||||
const pool = new SimplePool()
|
const pool = new SimplePool()
|
||||||
|
|
||||||
let relays = ['wss://relay.example.com', 'wss://relay.example2.com']
|
let relays = ['wss://relay.example.com', 'wss://relay.example2.com']
|
||||||
|
|
||||||
relays.forEach(async url => {
|
let relay = await pool.ensureRelay('wss://relay.example3.com')
|
||||||
let relay = pool.ensureRelay(url)
|
|
||||||
await relay.connect()
|
|
||||||
})
|
|
||||||
|
|
||||||
let relay = pool.ensureRelay('wss://relay.example3.com')
|
let sub = pool.sub([...relays, relay], [{
|
||||||
|
|
||||||
let subs = pool.sub([...relays, relay], {
|
|
||||||
authors: ['32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245']
|
authors: ['32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245']
|
||||||
})
|
}])
|
||||||
|
|
||||||
subs.forEach(sub =>
|
|
||||||
sub.on('event', event => {
|
sub.on('event', event => {
|
||||||
// this will only be called once the first time the event is received
|
// this will only be called once the first time the event is received
|
||||||
// ...
|
// ...
|
||||||
})
|
})
|
||||||
)
|
|
||||||
|
|
||||||
let pubs = pool.publish(newEvent)
|
let pubs = pool.publish(relays, newEvent)
|
||||||
pubs.forEach(pub =>
|
pubs.forEach(pub =>
|
||||||
pub.on('ok', () => {
|
pub.on('ok', () => {
|
||||||
// ...
|
// ...
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
|
|
||||||
|
let events = await pool.list(relays, [{kinds: [0, 1]}])
|
||||||
|
let event = await pool.get(relays, {
|
||||||
|
ids: ['44e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245']
|
||||||
|
})
|
||||||
|
|
||||||
|
let relaysForEvent = pool.seenOn(
|
||||||
|
'44e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245'
|
||||||
|
)
|
||||||
|
// relaysForEvent will be an array of URLs from relays a given event was seen on
|
||||||
```
|
```
|
||||||
|
|
||||||
### Querying profile data from a NIP-05 address
|
### Querying profile data from a NIP-05 address
|
||||||
@@ -283,6 +295,11 @@ Please consult the tests or [the source code](https://github.com/fiatjaf/nostr-t
|
|||||||
</script>
|
</script>
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Plumbing
|
||||||
|
|
||||||
|
1. Install [`just`](https://just.systems/)
|
||||||
|
2. `just -l`
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
Public domain.
|
Public domain.
|
||||||
|
|||||||
10
build.js
10
build.js
@@ -1,5 +1,6 @@
|
|||||||
#!/usr/bin/env node
|
#!/usr/bin/env node
|
||||||
|
|
||||||
|
const fs = require('fs')
|
||||||
const esbuild = require('esbuild')
|
const esbuild = require('esbuild')
|
||||||
|
|
||||||
let common = {
|
let common = {
|
||||||
@@ -11,11 +12,16 @@ let common = {
|
|||||||
esbuild
|
esbuild
|
||||||
.build({
|
.build({
|
||||||
...common,
|
...common,
|
||||||
outfile: 'lib/nostr.esm.js',
|
outfile: 'lib/esm/nostr.mjs',
|
||||||
format: 'esm',
|
format: 'esm',
|
||||||
packages: 'external'
|
packages: 'external'
|
||||||
})
|
})
|
||||||
.then(() => console.log('esm build success.'))
|
.then(() => {
|
||||||
|
const packageJson = JSON.stringify({ type: 'module' })
|
||||||
|
fs.writeFileSync(`${__dirname}/lib/esm/package.json`, packageJson, 'utf8')
|
||||||
|
|
||||||
|
console.log('esm build success.')
|
||||||
|
})
|
||||||
|
|
||||||
esbuild
|
esbuild
|
||||||
.build({
|
.build({
|
||||||
|
|||||||
43
event.ts
43
event.ts
@@ -2,6 +2,7 @@ import * as secp256k1 from '@noble/secp256k1'
|
|||||||
import {sha256} from '@noble/hashes/sha256'
|
import {sha256} from '@noble/hashes/sha256'
|
||||||
|
|
||||||
import {utf8Encoder} from './utils'
|
import {utf8Encoder} from './utils'
|
||||||
|
import {getPublicKey} from './keys'
|
||||||
|
|
||||||
/* eslint-disable no-unused-vars */
|
/* eslint-disable no-unused-vars */
|
||||||
export enum Kind {
|
export enum Kind {
|
||||||
@@ -16,30 +17,49 @@ export enum Kind {
|
|||||||
ChannelMetadata = 41,
|
ChannelMetadata = 41,
|
||||||
ChannelMessage = 42,
|
ChannelMessage = 42,
|
||||||
ChannelHideMessage = 43,
|
ChannelHideMessage = 43,
|
||||||
ChannelMuteUser = 44
|
ChannelMuteUser = 44,
|
||||||
|
Report = 1984,
|
||||||
|
ZapRequest = 9734,
|
||||||
|
Zap = 9735,
|
||||||
|
RelayList = 10002,
|
||||||
|
ClientAuth = 22242,
|
||||||
|
Article = 30023
|
||||||
}
|
}
|
||||||
|
|
||||||
export type Event = {
|
export type EventTemplate = {
|
||||||
id?: string
|
|
||||||
sig?: string
|
|
||||||
kind: Kind
|
kind: Kind
|
||||||
tags: string[][]
|
tags: string[][]
|
||||||
pubkey: string
|
|
||||||
content: string
|
content: string
|
||||||
created_at: number
|
created_at: number
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getBlankEvent(): Event {
|
export type UnsignedEvent = EventTemplate & {
|
||||||
|
pubkey: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export type Event = UnsignedEvent & {
|
||||||
|
id: string
|
||||||
|
sig: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getBlankEvent(): EventTemplate {
|
||||||
return {
|
return {
|
||||||
kind: 255,
|
kind: 255,
|
||||||
pubkey: '',
|
|
||||||
content: '',
|
content: '',
|
||||||
tags: [],
|
tags: [],
|
||||||
created_at: 0
|
created_at: 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function serializeEvent(evt: Event): string {
|
export function finishEvent(t: EventTemplate, privateKey: string): Event {
|
||||||
|
let event = t as Event
|
||||||
|
event.pubkey = getPublicKey(privateKey)
|
||||||
|
event.id = getEventHash(event)
|
||||||
|
event.sig = signEvent(event, privateKey)
|
||||||
|
return event
|
||||||
|
}
|
||||||
|
|
||||||
|
export function serializeEvent(evt: UnsignedEvent): string {
|
||||||
if (!validateEvent(evt))
|
if (!validateEvent(evt))
|
||||||
throw new Error("can't serialize event with wrong or missing properties")
|
throw new Error("can't serialize event with wrong or missing properties")
|
||||||
|
|
||||||
@@ -53,12 +73,13 @@ export function serializeEvent(evt: Event): string {
|
|||||||
])
|
])
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getEventHash(event: Event): string {
|
export function getEventHash(event: UnsignedEvent): string {
|
||||||
let eventHash = sha256(utf8Encoder.encode(serializeEvent(event)))
|
let eventHash = sha256(utf8Encoder.encode(serializeEvent(event)))
|
||||||
return secp256k1.utils.bytesToHex(eventHash)
|
return secp256k1.utils.bytesToHex(eventHash)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function validateEvent(event: Event): boolean {
|
export function validateEvent(event: UnsignedEvent): boolean {
|
||||||
|
if (typeof event !== 'object') return false
|
||||||
if (typeof event.content !== 'string') return false
|
if (typeof event.content !== 'string') return false
|
||||||
if (typeof event.created_at !== 'number') return false
|
if (typeof event.created_at !== 'number') return false
|
||||||
if (typeof event.pubkey !== 'string') return false
|
if (typeof event.pubkey !== 'string') return false
|
||||||
@@ -84,7 +105,7 @@ export function verifySignature(event: Event & {sig: string}): boolean {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function signEvent(event: Event, key: string): string {
|
export function signEvent(event: UnsignedEvent, key: string): string {
|
||||||
return secp256k1.utils.bytesToHex(
|
return secp256k1.utils.bytesToHex(
|
||||||
secp256k1.schnorr.signSync(getEventHash(event), key)
|
secp256k1.schnorr.signSync(getEventHash(event), key)
|
||||||
)
|
)
|
||||||
|
|||||||
1
index.ts
1
index.ts
@@ -9,6 +9,7 @@ export * as nip05 from './nip05'
|
|||||||
export * as nip06 from './nip06'
|
export * as nip06 from './nip06'
|
||||||
export * as nip19 from './nip19'
|
export * as nip19 from './nip19'
|
||||||
export * as nip26 from './nip26'
|
export * as nip26 from './nip26'
|
||||||
|
export * as nip57 from './nip57'
|
||||||
|
|
||||||
export * as fj from './fakejson'
|
export * as fj from './fakejson'
|
||||||
export * as utils from './utils'
|
export * as utils from './utils'
|
||||||
|
|||||||
3
justfile
3
justfile
@@ -11,3 +11,6 @@ test: build
|
|||||||
|
|
||||||
testOnly file: build
|
testOnly file: build
|
||||||
jest {{file}}
|
jest {{file}}
|
||||||
|
|
||||||
|
publish: build
|
||||||
|
npm publish
|
||||||
|
|||||||
@@ -34,3 +34,37 @@ test('encode and decode nprofile', () => {
|
|||||||
expect(data.relays).toContain(relays[0])
|
expect(data.relays).toContain(relays[0])
|
||||||
expect(data.relays).toContain(relays[1])
|
expect(data.relays).toContain(relays[1])
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('encode and decode naddr', () => {
|
||||||
|
let pk = getPublicKey(generatePrivateKey())
|
||||||
|
let relays = [
|
||||||
|
'wss://relay.nostr.example.mydomain.example.com',
|
||||||
|
'wss://nostr.banana.com'
|
||||||
|
]
|
||||||
|
let naddr = nip19.naddrEncode({
|
||||||
|
pubkey: pk,
|
||||||
|
relays,
|
||||||
|
kind: 30023,
|
||||||
|
identifier: 'banana'
|
||||||
|
})
|
||||||
|
expect(naddr).toMatch(/naddr1\w+/)
|
||||||
|
let {type, data} = nip19.decode(naddr)
|
||||||
|
expect(type).toEqual('naddr')
|
||||||
|
expect(data.pubkey).toEqual(pk)
|
||||||
|
expect(data.relays).toContain(relays[0])
|
||||||
|
expect(data.relays).toContain(relays[1])
|
||||||
|
expect(data.kind).toEqual(30023)
|
||||||
|
expect(data.identifier).toEqual('banana')
|
||||||
|
})
|
||||||
|
|
||||||
|
test('encode and decode naddr from habla.news', () => {
|
||||||
|
let {type, data} = nip19.decode(
|
||||||
|
'naddr1qq98yetxv4ex2mnrv4esygrl54h466tz4v0re4pyuavvxqptsejl0vxcmnhfl60z3rth2xkpjspsgqqqw4rsf34vl5'
|
||||||
|
)
|
||||||
|
expect(type).toEqual('naddr')
|
||||||
|
expect(data.pubkey).toEqual(
|
||||||
|
'7fa56f5d6962ab1e3cd424e758c3002b8665f7b0d8dcee9fe9e288d7751ac194'
|
||||||
|
)
|
||||||
|
expect(data.kind).toEqual(30023)
|
||||||
|
expect(data.identifier).toEqual('references')
|
||||||
|
})
|
||||||
|
|||||||
59
nip19.ts
59
nip19.ts
@@ -15,14 +15,22 @@ export type EventPointer = {
|
|||||||
relays?: string[]
|
relays?: string[]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type AddressPointer = {
|
||||||
|
identifier: string
|
||||||
|
pubkey: string
|
||||||
|
kind: number
|
||||||
|
relays?: string[]
|
||||||
|
}
|
||||||
|
|
||||||
export function decode(nip19: string): {
|
export function decode(nip19: string): {
|
||||||
type: string
|
type: string
|
||||||
data: ProfilePointer | EventPointer | string
|
data: ProfilePointer | EventPointer | AddressPointer | string
|
||||||
} {
|
} {
|
||||||
let {prefix, words} = bech32.decode(nip19, Bech32MaxSize)
|
let {prefix, words} = bech32.decode(nip19, Bech32MaxSize)
|
||||||
let data = new Uint8Array(bech32.fromWords(words))
|
let data = new Uint8Array(bech32.fromWords(words))
|
||||||
|
|
||||||
if (prefix === 'nprofile') {
|
switch (prefix) {
|
||||||
|
case 'nprofile': {
|
||||||
let tlv = parseTLV(data)
|
let tlv = parseTLV(data)
|
||||||
if (!tlv[0]?.[0]) throw new Error('missing TLV 0 for nprofile')
|
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')
|
if (tlv[0][0].length !== 32) throw new Error('TLV 0 should be 32 bytes')
|
||||||
@@ -31,12 +39,11 @@ export function decode(nip19: string): {
|
|||||||
type: 'nprofile',
|
type: 'nprofile',
|
||||||
data: {
|
data: {
|
||||||
pubkey: secp256k1.utils.bytesToHex(tlv[0][0]),
|
pubkey: secp256k1.utils.bytesToHex(tlv[0][0]),
|
||||||
relays: tlv[1].map(d => utf8Decoder.decode(d))
|
relays: tlv[1] ? tlv[1].map(d => utf8Decoder.decode(d)) : []
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
case 'nevent': {
|
||||||
if (prefix === 'nevent') {
|
|
||||||
let tlv = parseTLV(data)
|
let tlv = parseTLV(data)
|
||||||
if (!tlv[0]?.[0]) throw new Error('missing TLV 0 for nevent')
|
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')
|
if (tlv[0][0].length !== 32) throw new Error('TLV 0 should be 32 bytes')
|
||||||
@@ -45,17 +52,39 @@ export function decode(nip19: string): {
|
|||||||
type: 'nevent',
|
type: 'nevent',
|
||||||
data: {
|
data: {
|
||||||
id: secp256k1.utils.bytesToHex(tlv[0][0]),
|
id: secp256k1.utils.bytesToHex(tlv[0][0]),
|
||||||
relays: tlv[1].map(d => utf8Decoder.decode(d))
|
relays: tlv[1] ? tlv[1].map(d => utf8Decoder.decode(d)) : []
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (prefix === 'nsec' || prefix === 'npub' || prefix === 'note') {
|
case 'naddr': {
|
||||||
|
let tlv = parseTLV(data)
|
||||||
|
if (!tlv[0]?.[0]) throw new Error('missing TLV 0 for naddr')
|
||||||
|
if (!tlv[2]?.[0]) throw new Error('missing TLV 2 for naddr')
|
||||||
|
if (tlv[2][0].length !== 32) throw new Error('TLV 2 should be 32 bytes')
|
||||||
|
if (!tlv[3]?.[0]) throw new Error('missing TLV 3 for naddr')
|
||||||
|
if (tlv[3][0].length !== 4) throw new Error('TLV 3 should be 4 bytes')
|
||||||
|
|
||||||
|
return {
|
||||||
|
type: 'naddr',
|
||||||
|
data: {
|
||||||
|
identifier: utf8Decoder.decode(tlv[0][0]),
|
||||||
|
pubkey: secp256k1.utils.bytesToHex(tlv[2][0]),
|
||||||
|
kind: parseInt(secp256k1.utils.bytesToHex(tlv[3][0]), 16),
|
||||||
|
relays: tlv[1] ? tlv[1].map(d => utf8Decoder.decode(d)) : []
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
case 'nsec':
|
||||||
|
case 'npub':
|
||||||
|
case 'note':
|
||||||
return {type: prefix, data: secp256k1.utils.bytesToHex(data)}
|
return {type: prefix, data: secp256k1.utils.bytesToHex(data)}
|
||||||
}
|
|
||||||
|
|
||||||
|
default:
|
||||||
throw new Error(`unknown prefix ${prefix}`)
|
throw new Error(`unknown prefix ${prefix}`)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type TLV = {[t: number]: Uint8Array[]}
|
type TLV = {[t: number]: Uint8Array[]}
|
||||||
|
|
||||||
@@ -110,6 +139,20 @@ export function neventEncode(event: EventPointer): string {
|
|||||||
return bech32.encode('nevent', words, Bech32MaxSize)
|
return bech32.encode('nevent', words, Bech32MaxSize)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function naddrEncode(addr: AddressPointer): string {
|
||||||
|
let kind = new ArrayBuffer(4)
|
||||||
|
new DataView(kind).setUint32(0, addr.kind, false)
|
||||||
|
|
||||||
|
let data = encodeTLV({
|
||||||
|
0: [utf8Encoder.encode(addr.identifier)],
|
||||||
|
1: (addr.relays || []).map(url => utf8Encoder.encode(url)),
|
||||||
|
2: [secp256k1.utils.hexToBytes(addr.pubkey)],
|
||||||
|
3: [new Uint8Array(kind)]
|
||||||
|
})
|
||||||
|
let words = bech32.toWords(data)
|
||||||
|
return bech32.encode('naddr', words, Bech32MaxSize)
|
||||||
|
}
|
||||||
|
|
||||||
function encodeTLV(tlv: TLV): Uint8Array {
|
function encodeTLV(tlv: TLV): Uint8Array {
|
||||||
let entries: Uint8Array[] = []
|
let entries: Uint8Array[] = []
|
||||||
|
|
||||||
|
|||||||
138
nip57.ts
Normal file
138
nip57.ts
Normal file
@@ -0,0 +1,138 @@
|
|||||||
|
import {bech32} from '@scure/base'
|
||||||
|
|
||||||
|
import {Event, EventTemplate, validateEvent, verifySignature} from './event'
|
||||||
|
import {utf8Decoder} from './utils'
|
||||||
|
|
||||||
|
var _fetch: any
|
||||||
|
|
||||||
|
try {
|
||||||
|
_fetch = fetch
|
||||||
|
} catch {}
|
||||||
|
|
||||||
|
export function useFetchImplementation(fetchImplementation: any) {
|
||||||
|
_fetch = fetchImplementation
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getZapEndpoint(metadata: Event): Promise<null | string> {
|
||||||
|
try {
|
||||||
|
let lnurl: string = ''
|
||||||
|
let {lud06, lud16} = JSON.parse(metadata.content)
|
||||||
|
if (lud06) {
|
||||||
|
let {words} = bech32.decode(lud06, 1000)
|
||||||
|
let data = bech32.fromWords(words)
|
||||||
|
lnurl = utf8Decoder.decode(data)
|
||||||
|
} else if (lud16) {
|
||||||
|
let [name, domain] = lud16.split('@')
|
||||||
|
lnurl = `https://${domain}/.well-known/lnurlp/${name}`
|
||||||
|
} else {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
let res = await _fetch(lnurl)
|
||||||
|
let body = await res.json()
|
||||||
|
|
||||||
|
if (body.allowsNostr && body.nostrPubkey) {
|
||||||
|
return body.callback
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
/*-*/
|
||||||
|
}
|
||||||
|
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
export function makeZapRequest({
|
||||||
|
profile,
|
||||||
|
event,
|
||||||
|
amount,
|
||||||
|
relays,
|
||||||
|
comment = ''
|
||||||
|
}: {
|
||||||
|
profile: string
|
||||||
|
event: string | null
|
||||||
|
amount: number
|
||||||
|
comment: string
|
||||||
|
relays: string[]
|
||||||
|
}): EventTemplate {
|
||||||
|
if (!amount) throw new Error('amount not given')
|
||||||
|
if (!profile) throw new Error('profile not given')
|
||||||
|
|
||||||
|
let zr = {
|
||||||
|
kind: 9734,
|
||||||
|
created_at: Math.round(Date.now() / 1000),
|
||||||
|
content: comment,
|
||||||
|
tags: [
|
||||||
|
['p', profile],
|
||||||
|
['amount', amount.toString()],
|
||||||
|
['relays', ...relays]
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
if (event) {
|
||||||
|
zr.tags.push(['e', event])
|
||||||
|
}
|
||||||
|
|
||||||
|
return zr
|
||||||
|
}
|
||||||
|
|
||||||
|
export function validateZapRequest(zapRequestString: string): string | null {
|
||||||
|
let zapRequest: Event
|
||||||
|
|
||||||
|
try {
|
||||||
|
zapRequest = JSON.parse(zapRequestString)
|
||||||
|
} catch (err) {
|
||||||
|
return 'Invalid zap request JSON.'
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!validateEvent(zapRequest))
|
||||||
|
return 'Zap request is not a valid Nostr event.'
|
||||||
|
if (!verifySignature(zapRequest)) return 'Invalid signature on zap request.'
|
||||||
|
|
||||||
|
let p = zapRequest.tags.find(([t, v]) => t === 'p' && v)
|
||||||
|
if (!p) return "Zap request doesn't have a 'p' tag."
|
||||||
|
if (!p[1].match(/^[a-f0-9]{64}$/))
|
||||||
|
return "Zap request 'p' tag is not valid hex."
|
||||||
|
|
||||||
|
let e = zapRequest.tags.find(([t, v]) => t === 'e' && v)
|
||||||
|
if (e && !e[1].match(/^[a-f0-9]{64}$/))
|
||||||
|
return "Zap request 'e' tag is not valid hex."
|
||||||
|
|
||||||
|
let relays = zapRequest.tags.find(([t, v]) => t === 'relays' && v)
|
||||||
|
if (!relays) return "Zap request doesn't have a 'relays' tag."
|
||||||
|
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
export function makeZapReceipt({
|
||||||
|
zapRequest,
|
||||||
|
preimage,
|
||||||
|
bolt11,
|
||||||
|
paidAt
|
||||||
|
}: {
|
||||||
|
zapRequest: string
|
||||||
|
preimage: string | null
|
||||||
|
bolt11: string
|
||||||
|
paidAt: Date
|
||||||
|
}): EventTemplate {
|
||||||
|
let zr: Event = JSON.parse(zapRequest)
|
||||||
|
let tagsFromZapRequest = zr.tags.filter(
|
||||||
|
([t]) => t === 'e' || t === 'p' || t === 'a'
|
||||||
|
)
|
||||||
|
|
||||||
|
let zap = {
|
||||||
|
kind: 9735,
|
||||||
|
created_at: Math.round(paidAt.getTime() / 1000),
|
||||||
|
content: '',
|
||||||
|
tags: [
|
||||||
|
...tagsFromZapRequest,
|
||||||
|
['bolt11', bolt11],
|
||||||
|
['description', zapRequest]
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
if (preimage) {
|
||||||
|
zap.tags.push(['preimage', preimage])
|
||||||
|
}
|
||||||
|
|
||||||
|
return zap
|
||||||
|
}
|
||||||
35
package.json
35
package.json
@@ -1,19 +1,24 @@
|
|||||||
{
|
{
|
||||||
"name": "nostr-tools",
|
"name": "nostr-tools",
|
||||||
"version": "1.2.4",
|
"version": "1.7.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",
|
"main": "lib/nostr.cjs.js",
|
||||||
"module": "lib/nostr.esm.js",
|
"module": "lib/esm/nostr.mjs",
|
||||||
|
"exports": {
|
||||||
|
"import": "./lib/esm/nostr.mjs",
|
||||||
|
"require": "./lib/nostr.cjs.js"
|
||||||
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@noble/hashes": "^0.5.7",
|
"@noble/hashes": "1.0.0",
|
||||||
"@noble/secp256k1": "^1.7.0",
|
"@noble/secp256k1": "^1.7.1",
|
||||||
"@scure/base": "^1.1.1",
|
"@scure/base": "^1.1.1",
|
||||||
"@scure/bip32": "^1.1.1",
|
"@scure/bip32": "^1.1.5",
|
||||||
"@scure/bip39": "^1.1.0"
|
"@scure/bip39": "^1.1.1",
|
||||||
|
"prettier": "^2.8.4"
|
||||||
},
|
},
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"decentralization",
|
"decentralization",
|
||||||
@@ -23,20 +28,20 @@
|
|||||||
"nostr"
|
"nostr"
|
||||||
],
|
],
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/node": "^18.0.3",
|
"@types/node": "^18.13.0",
|
||||||
"@typescript-eslint/eslint-plugin": "^5.46.1",
|
"@typescript-eslint/eslint-plugin": "^5.51.0",
|
||||||
"@typescript-eslint/parser": "^5.46.1",
|
"@typescript-eslint/parser": "^5.51.0",
|
||||||
"esbuild": "0.16.9",
|
"esbuild": "0.16.9",
|
||||||
"esbuild-plugin-alias": "^0.2.1",
|
"esbuild-plugin-alias": "^0.2.1",
|
||||||
"eslint": "^8.30.0",
|
"eslint": "^8.33.0",
|
||||||
"eslint-plugin-babel": "^5.3.1",
|
"eslint-plugin-babel": "^5.3.1",
|
||||||
"esm-loader-typescript": "^1.0.1",
|
"esm-loader-typescript": "^1.0.3",
|
||||||
"events": "^3.3.0",
|
"events": "^3.3.0",
|
||||||
"jest": "^29.3.1",
|
"jest": "^29.4.2",
|
||||||
"node-fetch": "2",
|
"node-fetch": "^2.6.9",
|
||||||
"ts-jest": "^29.0.3",
|
"ts-jest": "^29.0.5",
|
||||||
"tsd": "^0.22.0",
|
"tsd": "^0.22.0",
|
||||||
"typescript": "^4.9.4",
|
"typescript": "^4.9.5",
|
||||||
"websocket-polyfill": "^0.0.3"
|
"websocket-polyfill": "^0.0.3"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
92
pool.test.js
92
pool.test.js
@@ -19,50 +19,28 @@ let relays = [
|
|||||||
'wss://nostr.zebedee.cloud/'
|
'wss://nostr.zebedee.cloud/'
|
||||||
]
|
]
|
||||||
|
|
||||||
beforeAll(async () => {
|
afterAll(() => {
|
||||||
Promise.all(
|
pool.close([
|
||||||
relays.map(relay => {
|
...relays,
|
||||||
try {
|
'wss://nostr-relay.untethr.me',
|
||||||
let r = pool.ensureRelay(relay)
|
'wss://offchain.pub',
|
||||||
return r.connect()
|
'wss://eden.nostr.land'
|
||||||
} catch (err) {
|
])
|
||||||
/***/
|
|
||||||
}
|
|
||||||
})
|
|
||||||
)
|
|
||||||
})
|
|
||||||
|
|
||||||
afterAll(async () => {
|
|
||||||
relays.forEach(relay => {
|
|
||||||
try {
|
|
||||||
let r = pool.ensureRelay(relay)
|
|
||||||
r.close()
|
|
||||||
} catch (err) {
|
|
||||||
/***/
|
|
||||||
}
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
|
||||||
test('removing duplicates when querying', async () => {
|
test('removing duplicates when querying', async () => {
|
||||||
let priv = generatePrivateKey()
|
let priv = generatePrivateKey()
|
||||||
let pub = getPublicKey(priv)
|
let pub = getPublicKey(priv)
|
||||||
|
|
||||||
let subs = pool.sub(relays, [
|
let sub = pool.sub(relays, [{authors: [pub]}])
|
||||||
{
|
|
||||||
authors: [pub]
|
|
||||||
}
|
|
||||||
])
|
|
||||||
|
|
||||||
let received = []
|
let received = []
|
||||||
|
|
||||||
subs.forEach(sub =>
|
|
||||||
sub.on('event', event => {
|
sub.on('event', event => {
|
||||||
// this should be called only once even though we're listening
|
// this should be called only once even though we're listening
|
||||||
// to multiple relays because the events will be catched and
|
// to multiple relays because the events will be catched and
|
||||||
// deduplicated efficiently (without even being parsed)
|
// deduplicated efficiently (without even being parsed)
|
||||||
received.push(event)
|
received.push(event)
|
||||||
})
|
})
|
||||||
)
|
|
||||||
|
|
||||||
let event = {
|
let event = {
|
||||||
pubkey: pub,
|
pubkey: pub,
|
||||||
@@ -81,25 +59,22 @@ test('removing duplicates when querying', async () => {
|
|||||||
expect(received).toHaveLength(1)
|
expect(received).toHaveLength(1)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('removing duplicates correctly when double querying', async () => {
|
test('same with double querying', async () => {
|
||||||
let priv = generatePrivateKey()
|
let priv = generatePrivateKey()
|
||||||
let pub = getPublicKey(priv)
|
let pub = getPublicKey(priv)
|
||||||
|
|
||||||
let subs1 = pool.sub(relays, [{authors: [pub]}])
|
let sub1 = pool.sub(relays, [{authors: [pub]}])
|
||||||
let subs2 = pool.sub(relays, [{authors: [pub]}])
|
let sub2 = pool.sub(relays, [{authors: [pub]}])
|
||||||
|
|
||||||
let received = []
|
let received = []
|
||||||
|
|
||||||
subs1.forEach(sub =>
|
sub1.on('event', event => {
|
||||||
sub.on('event', event => {
|
|
||||||
received.push(event)
|
received.push(event)
|
||||||
})
|
})
|
||||||
)
|
|
||||||
subs2.forEach(sub =>
|
sub2.on('event', event => {
|
||||||
sub.on('event', event => {
|
|
||||||
received.push(event)
|
received.push(event)
|
||||||
})
|
})
|
||||||
)
|
|
||||||
|
|
||||||
let event = {
|
let event = {
|
||||||
pubkey: pub,
|
pubkey: pub,
|
||||||
@@ -117,3 +92,42 @@ test('removing duplicates correctly when double querying', async () => {
|
|||||||
|
|
||||||
expect(received).toHaveLength(2)
|
expect(received).toHaveLength(2)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('get()', async () => {
|
||||||
|
let event = await pool.get(relays, {
|
||||||
|
ids: ['d7dd5eb3ab747e16f8d0212d53032ea2a7cadef53837e5a6c66d42849fcb9027']
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(event).toHaveProperty(
|
||||||
|
'id',
|
||||||
|
'd7dd5eb3ab747e16f8d0212d53032ea2a7cadef53837e5a6c66d42849fcb9027'
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('list()', async () => {
|
||||||
|
let events = await pool.list(
|
||||||
|
[...relays, 'wss://offchain.pub', 'wss://eden.nostr.land'],
|
||||||
|
[
|
||||||
|
{
|
||||||
|
authors: [
|
||||||
|
'3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d'
|
||||||
|
],
|
||||||
|
kinds: [1],
|
||||||
|
limit: 2
|
||||||
|
}
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
// the actual received number will be greater than 2, but there will be no duplicates
|
||||||
|
expect(events.length).toEqual(
|
||||||
|
events
|
||||||
|
.map(evt => evt.id)
|
||||||
|
.reduce((acc, n) => (acc.indexOf(n) !== -1 ? acc : [...acc, n]), [])
|
||||||
|
.length
|
||||||
|
)
|
||||||
|
|
||||||
|
let relaysForAllEvents = events
|
||||||
|
.map(event => pool.seenOn(event.id))
|
||||||
|
.reduce((acc, n) => acc.concat(n), [])
|
||||||
|
expect(relaysForAllEvents.length).toBeGreaterThanOrEqual(events.length)
|
||||||
|
})
|
||||||
|
|||||||
175
pool.ts
175
pool.ts
@@ -6,13 +6,25 @@ import {SubscriptionOptions, Sub, Pub} from './relay'
|
|||||||
|
|
||||||
export class SimplePool {
|
export class SimplePool {
|
||||||
private _conn: {[url: string]: Relay}
|
private _conn: {[url: string]: Relay}
|
||||||
|
private _seenOn: {[id: string]: Set<string>} = {} // a map of all events we've seen in each relay
|
||||||
|
|
||||||
constructor(defaultRelays: string[] = []) {
|
private eoseSubTimeout: number
|
||||||
|
private getTimeout: number
|
||||||
|
|
||||||
|
constructor(options: {eoseSubTimeout?: number; getTimeout?: number} = {}) {
|
||||||
this._conn = {}
|
this._conn = {}
|
||||||
defaultRelays.forEach(this.ensureRelay)
|
this.eoseSubTimeout = options.eoseSubTimeout || 3400
|
||||||
|
this.getTimeout = options.getTimeout || 3400
|
||||||
}
|
}
|
||||||
|
|
||||||
ensureRelay(url: string): Relay {
|
close(relays: string[]): void {
|
||||||
|
relays.forEach(url => {
|
||||||
|
let relay = this._conn[normalizeURL(url)]
|
||||||
|
if (relay) relay.close()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
async ensureRelay(url: string): Promise<Relay> {
|
||||||
const nm = normalizeURL(url)
|
const nm = normalizeURL(url)
|
||||||
const existing = this._conn[nm]
|
const existing = this._conn[nm]
|
||||||
if (existing) return existing
|
if (existing) return existing
|
||||||
@@ -20,41 +32,150 @@ export class SimplePool {
|
|||||||
const relay = relayInit(nm)
|
const relay = relayInit(nm)
|
||||||
this._conn[nm] = relay
|
this._conn[nm] = relay
|
||||||
|
|
||||||
|
await relay.connect()
|
||||||
|
|
||||||
return relay
|
return relay
|
||||||
}
|
}
|
||||||
|
|
||||||
sub(relays: string[], filters: Filter[], opts?: SubscriptionOptions): Sub[] {
|
sub(relays: string[], filters: Filter[], opts?: SubscriptionOptions): Sub {
|
||||||
let _knownIds: Set<string> = new Set()
|
let _knownIds: Set<string> = new Set()
|
||||||
let modifiedOpts = opts || {}
|
let modifiedOpts = opts || {}
|
||||||
modifiedOpts.alreadyHaveEvent = id => _knownIds.has(id)
|
modifiedOpts.alreadyHaveEvent = (id, url) => {
|
||||||
|
let set = this._seenOn[id] || new Set()
|
||||||
|
set.add(url)
|
||||||
|
this._seenOn[id] = set
|
||||||
|
return _knownIds.has(id)
|
||||||
|
}
|
||||||
|
|
||||||
return relays.map(relay => {
|
let subs: Sub[] = []
|
||||||
let r = this._conn[relay]
|
let eventListeners: Set<(event: Event) => void> = new Set()
|
||||||
if (!r) return badSub()
|
let eoseListeners: Set<() => void> = new Set()
|
||||||
|
let eosesMissing = relays.length
|
||||||
|
|
||||||
|
let eoseSent = false
|
||||||
|
let eoseTimeout = setTimeout(() => {
|
||||||
|
eoseSent = true
|
||||||
|
for (let cb of eoseListeners.values()) cb()
|
||||||
|
}, this.eoseSubTimeout)
|
||||||
|
|
||||||
|
relays.forEach(async relay => {
|
||||||
|
let r
|
||||||
|
try {
|
||||||
|
r = await this.ensureRelay(relay)
|
||||||
|
} catch (err) {
|
||||||
|
handleEose()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (!r) return
|
||||||
let s = r.sub(filters, modifiedOpts)
|
let s = r.sub(filters, modifiedOpts)
|
||||||
s.on('event', (event: Event) => _knownIds.add(event.id as string))
|
s.on('event', (event: Event) => {
|
||||||
return s
|
_knownIds.add(event.id as string)
|
||||||
|
for (let cb of eventListeners.values()) cb(event)
|
||||||
})
|
})
|
||||||
}
|
s.on('eose', () => {
|
||||||
|
if (eoseSent) return
|
||||||
publish(relays: string[], event: Event): Pub[] {
|
handleEose()
|
||||||
return relays.map(relay => {
|
|
||||||
let r = this._conn[relay]
|
|
||||||
if (!r) return badPub(relay)
|
|
||||||
let s = r.publish(event)
|
|
||||||
return s
|
|
||||||
})
|
})
|
||||||
}
|
subs.push(s)
|
||||||
}
|
|
||||||
|
|
||||||
function badSub(): Sub {
|
function handleEose() {
|
||||||
return {
|
eosesMissing--
|
||||||
on() {},
|
if (eosesMissing === 0) {
|
||||||
off() {},
|
clearTimeout(eoseTimeout)
|
||||||
sub(): Sub {
|
for (let cb of eoseListeners.values()) cb()
|
||||||
return badSub()
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
let greaterSub: Sub = {
|
||||||
|
sub(filters, opts) {
|
||||||
|
subs.forEach(sub => sub.sub(filters, opts))
|
||||||
|
return greaterSub
|
||||||
},
|
},
|
||||||
unsub() {}
|
unsub() {
|
||||||
|
subs.forEach(sub => sub.unsub())
|
||||||
|
},
|
||||||
|
on(type, cb) {
|
||||||
|
switch (type) {
|
||||||
|
case 'event':
|
||||||
|
eventListeners.add(cb)
|
||||||
|
break
|
||||||
|
case 'eose':
|
||||||
|
eoseListeners.add(cb)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
},
|
||||||
|
off(type, cb) {
|
||||||
|
if (type === 'event') {
|
||||||
|
eventListeners.delete(cb)
|
||||||
|
} else if (type === 'eose') eoseListeners.delete(cb)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return greaterSub
|
||||||
|
}
|
||||||
|
|
||||||
|
get(
|
||||||
|
relays: string[],
|
||||||
|
filter: Filter,
|
||||||
|
opts?: SubscriptionOptions
|
||||||
|
): Promise<Event | null> {
|
||||||
|
return new Promise(resolve => {
|
||||||
|
let sub = this.sub(relays, [filter], opts)
|
||||||
|
let timeout = setTimeout(() => {
|
||||||
|
sub.unsub()
|
||||||
|
resolve(null)
|
||||||
|
}, this.getTimeout)
|
||||||
|
sub.on('event', (event: Event) => {
|
||||||
|
resolve(event)
|
||||||
|
clearTimeout(timeout)
|
||||||
|
sub.unsub()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
list(
|
||||||
|
relays: string[],
|
||||||
|
filters: Filter[],
|
||||||
|
opts?: SubscriptionOptions
|
||||||
|
): Promise<Event[]> {
|
||||||
|
return new Promise(resolve => {
|
||||||
|
let events: Event[] = []
|
||||||
|
let sub = this.sub(relays, filters, opts)
|
||||||
|
|
||||||
|
sub.on('event', (event: Event) => {
|
||||||
|
events.push(event)
|
||||||
|
})
|
||||||
|
|
||||||
|
// we can rely on an eose being emitted here because pool.sub() will fake one
|
||||||
|
sub.on('eose', () => {
|
||||||
|
sub.unsub()
|
||||||
|
resolve(events)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
publish(relays: string[], event: Event): Pub {
|
||||||
|
let pubs = relays.map(relay => {
|
||||||
|
let r = this._conn[normalizeURL(relay)]
|
||||||
|
if (!r) return badPub(relay)
|
||||||
|
return r.publish(event)
|
||||||
|
})
|
||||||
|
|
||||||
|
return {
|
||||||
|
on(type, cb) {
|
||||||
|
pubs.forEach((pub, i) => {
|
||||||
|
pub.on(type, () => cb(relays[i]))
|
||||||
|
})
|
||||||
|
},
|
||||||
|
off() {
|
||||||
|
// do nothing here, FIXME
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
seenOn(id: string): string[] {
|
||||||
|
return Array.from(this._seenOn[id]?.values?.() || [])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -15,8 +15,8 @@ beforeAll(() => {
|
|||||||
relay.connect()
|
relay.connect()
|
||||||
})
|
})
|
||||||
|
|
||||||
afterAll(async () => {
|
afterAll(() => {
|
||||||
await relay.close()
|
relay.close()
|
||||||
})
|
})
|
||||||
|
|
||||||
test('connectivity', () => {
|
test('connectivity', () => {
|
||||||
@@ -32,7 +32,7 @@ test('connectivity', () => {
|
|||||||
).resolves.toBe(true)
|
).resolves.toBe(true)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('querying', () => {
|
test('querying', async () => {
|
||||||
var resolve1
|
var resolve1
|
||||||
var resolve2
|
var resolve2
|
||||||
|
|
||||||
@@ -52,8 +52,7 @@ test('querying', () => {
|
|||||||
resolve2(true)
|
resolve2(true)
|
||||||
})
|
})
|
||||||
|
|
||||||
return expect(
|
let [t1, t2] = await Promise.all([
|
||||||
Promise.all([
|
|
||||||
new Promise(resolve => {
|
new Promise(resolve => {
|
||||||
resolve1 = resolve
|
resolve1 = resolve
|
||||||
}),
|
}),
|
||||||
@@ -61,7 +60,34 @@ test('querying', () => {
|
|||||||
resolve2 = resolve
|
resolve2 = resolve
|
||||||
})
|
})
|
||||||
])
|
])
|
||||||
).resolves.toEqual([true, true])
|
|
||||||
|
expect(t1).toEqual(true)
|
||||||
|
expect(t2).toEqual(true)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('get()', async () => {
|
||||||
|
let event = await relay.get({
|
||||||
|
ids: ['d7dd5eb3ab747e16f8d0212d53032ea2a7cadef53837e5a6c66d42849fcb9027']
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(event).toHaveProperty(
|
||||||
|
'id',
|
||||||
|
'd7dd5eb3ab747e16f8d0212d53032ea2a7cadef53837e5a6c66d42849fcb9027'
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('list()', async () => {
|
||||||
|
let events = await relay.list([
|
||||||
|
{
|
||||||
|
authors: [
|
||||||
|
'3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d'
|
||||||
|
],
|
||||||
|
kinds: [1],
|
||||||
|
limit: 2
|
||||||
|
}
|
||||||
|
])
|
||||||
|
|
||||||
|
expect(events.length).toEqual(2)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('listening (twice) and publishing', async () => {
|
test('listening (twice) and publishing', async () => {
|
||||||
|
|||||||
132
relay.ts
132
relay.ts
@@ -10,15 +10,17 @@ export type Relay = {
|
|||||||
url: string
|
url: string
|
||||||
status: number
|
status: number
|
||||||
connect: () => Promise<void>
|
connect: () => Promise<void>
|
||||||
close: () => Promise<void>
|
close: () => void
|
||||||
sub: (filters: Filter[], opts?: SubscriptionOptions) => Sub
|
sub: (filters: Filter[], opts?: SubscriptionOptions) => Sub
|
||||||
|
list: (filters: Filter[], opts?: SubscriptionOptions) => Promise<Event[]>
|
||||||
|
get: (filter: Filter, opts?: SubscriptionOptions) => Promise<Event | null>
|
||||||
publish: (event: Event) => Pub
|
publish: (event: Event) => Pub
|
||||||
on: (type: RelayEvent, cb: any) => void
|
on: (type: RelayEvent, cb: any) => void
|
||||||
off: (type: RelayEvent, cb: any) => void
|
off: (type: RelayEvent, cb: any) => void
|
||||||
}
|
}
|
||||||
export type Pub = {
|
export type Pub = {
|
||||||
on: (type: 'ok' | 'seen' | 'failed', cb: any) => void
|
on: (type: 'ok' | 'failed', cb: any) => void
|
||||||
off: (type: 'ok' | 'seen' | 'failed', cb: any) => void
|
off: (type: 'ok' | 'failed', cb: any) => void
|
||||||
}
|
}
|
||||||
export type Sub = {
|
export type Sub = {
|
||||||
sub: (filters: Filter[], opts: SubscriptionOptions) => Sub
|
sub: (filters: Filter[], opts: SubscriptionOptions) => Sub
|
||||||
@@ -28,18 +30,13 @@ export type Sub = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export type SubscriptionOptions = {
|
export type SubscriptionOptions = {
|
||||||
skipVerification?: boolean
|
|
||||||
alreadyHaveEvent?: null | ((id: string) => boolean)
|
|
||||||
id?: string
|
id?: string
|
||||||
|
skipVerification?: boolean
|
||||||
|
alreadyHaveEvent?: null | ((id: string, relay: string) => boolean)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function relayInit(url: string): Relay {
|
export function relayInit(url: string): Relay {
|
||||||
var ws: WebSocket
|
var ws: WebSocket
|
||||||
var resolveClose: () => void
|
|
||||||
var setOpen: (value: PromiseLike<void> | void) => void
|
|
||||||
var untilOpen = new Promise<void>(resolve => {
|
|
||||||
setOpen = resolve
|
|
||||||
})
|
|
||||||
var openSubs: {[id: string]: {filters: Filter[]} & SubscriptionOptions} = {}
|
var openSubs: {[id: string]: {filters: Filter[]} & SubscriptionOptions} = {}
|
||||||
var listeners: {
|
var listeners: {
|
||||||
connect: Array<() => void>
|
connect: Array<() => void>
|
||||||
@@ -72,7 +69,6 @@ export function relayInit(url: string): Relay {
|
|||||||
|
|
||||||
ws.onopen = () => {
|
ws.onopen = () => {
|
||||||
listeners.connect.forEach(cb => cb())
|
listeners.connect.forEach(cb => cb())
|
||||||
setOpen()
|
|
||||||
resolve()
|
resolve()
|
||||||
}
|
}
|
||||||
ws.onerror = () => {
|
ws.onerror = () => {
|
||||||
@@ -81,7 +77,6 @@ export function relayInit(url: string): Relay {
|
|||||||
}
|
}
|
||||||
ws.onclose = async () => {
|
ws.onclose = async () => {
|
||||||
listeners.disconnect.forEach(cb => cb())
|
listeners.disconnect.forEach(cb => cb())
|
||||||
resolveClose && resolveClose()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let incomingMessageQueue: string[] = []
|
let incomingMessageQueue: string[] = []
|
||||||
@@ -106,8 +101,12 @@ export function relayInit(url: string): Relay {
|
|||||||
|
|
||||||
let subid = getSubscriptionId(json)
|
let subid = getSubscriptionId(json)
|
||||||
if (subid) {
|
if (subid) {
|
||||||
let {alreadyHaveEvent} = openSubs[subid]
|
let so = openSubs[subid]
|
||||||
if (alreadyHaveEvent && alreadyHaveEvent(getHex64(json, 'id'))) {
|
if (
|
||||||
|
so &&
|
||||||
|
so.alreadyHaveEvent &&
|
||||||
|
so.alreadyHaveEvent(getHex64(json, 'id'), url)
|
||||||
|
) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -134,15 +133,22 @@ export function relayInit(url: string): Relay {
|
|||||||
return
|
return
|
||||||
case 'EOSE': {
|
case 'EOSE': {
|
||||||
let id = data[1]
|
let id = data[1]
|
||||||
;(subListeners[id]?.eose || []).forEach(cb => cb())
|
if (id in subListeners) {
|
||||||
|
subListeners[id].eose.forEach(cb => cb())
|
||||||
|
subListeners[id].eose = [] // 'eose' only happens once per sub, so stop listeners here
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
case 'OK': {
|
case 'OK': {
|
||||||
let id: string = data[1]
|
let id: string = data[1]
|
||||||
let ok: boolean = data[2]
|
let ok: boolean = data[2]
|
||||||
let reason: string = data[3] || ''
|
let reason: string = data[3] || ''
|
||||||
if (ok) pubListeners[id]?.ok.forEach(cb => cb())
|
if (id in pubListeners) {
|
||||||
else pubListeners[id]?.failed.forEach(cb => cb(reason))
|
if (ok) pubListeners[id].ok.forEach(cb => cb())
|
||||||
|
else pubListeners[id].failed.forEach(cb => cb(reason))
|
||||||
|
pubListeners[id].ok = [] // 'ok' only happens once per pub, so stop listeners here
|
||||||
|
pubListeners[id].failed = []
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
case 'NOTICE':
|
case 'NOTICE':
|
||||||
@@ -157,15 +163,23 @@ export function relayInit(url: string): Relay {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function connected() {
|
||||||
|
return ws?.readyState === 1
|
||||||
|
}
|
||||||
|
|
||||||
async function connect(): Promise<void> {
|
async function connect(): Promise<void> {
|
||||||
if (ws?.readyState && ws.readyState === 1) return // ws already open
|
if (connected()) return // ws already open
|
||||||
await connectRelay()
|
await connectRelay()
|
||||||
}
|
}
|
||||||
|
|
||||||
async function trySend(params: [string, ...any]) {
|
async function trySend(params: [string, ...any]) {
|
||||||
let msg = JSON.stringify(params)
|
let msg = JSON.stringify(params)
|
||||||
|
if (!connected()) {
|
||||||
await untilOpen
|
await new Promise(resolve => setTimeout(resolve, 1000))
|
||||||
|
if (!connected()) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
ws.send(msg)
|
ws.send(msg)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
@@ -231,54 +245,51 @@ export function relayInit(url: string): Relay {
|
|||||||
let index = listeners[type].indexOf(cb)
|
let index = listeners[type].indexOf(cb)
|
||||||
if (index !== -1) listeners[type].splice(index, 1)
|
if (index !== -1) listeners[type].splice(index, 1)
|
||||||
},
|
},
|
||||||
|
list: (filters: Filter[], opts?: SubscriptionOptions): Promise<Event[]> =>
|
||||||
|
new Promise(resolve => {
|
||||||
|
let s = sub(filters, opts)
|
||||||
|
let events: Event[] = []
|
||||||
|
let timeout = setTimeout(() => {
|
||||||
|
s.unsub()
|
||||||
|
resolve(events)
|
||||||
|
}, 1500)
|
||||||
|
s.on('eose', () => {
|
||||||
|
s.unsub()
|
||||||
|
clearTimeout(timeout)
|
||||||
|
resolve(events)
|
||||||
|
})
|
||||||
|
s.on('event', (event: Event) => {
|
||||||
|
events.push(event)
|
||||||
|
})
|
||||||
|
}),
|
||||||
|
get: (filter: Filter, opts?: SubscriptionOptions): Promise<Event | null> =>
|
||||||
|
new Promise(resolve => {
|
||||||
|
let s = sub([filter], opts)
|
||||||
|
let timeout = setTimeout(() => {
|
||||||
|
s.unsub()
|
||||||
|
resolve(null)
|
||||||
|
}, 1500)
|
||||||
|
s.on('event', (event: Event) => {
|
||||||
|
s.unsub()
|
||||||
|
clearTimeout(timeout)
|
||||||
|
resolve(event)
|
||||||
|
})
|
||||||
|
}),
|
||||||
publish(event: Event): Pub {
|
publish(event: Event): Pub {
|
||||||
if (!event.id) throw new Error(`event ${event} has no id`)
|
if (!event.id) throw new Error(`event ${event} has no id`)
|
||||||
let id = event.id
|
let id = event.id
|
||||||
|
|
||||||
var sent = false
|
|
||||||
var mustMonitor = false
|
|
||||||
|
|
||||||
trySend(['EVENT', event])
|
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 {
|
return {
|
||||||
on: (type: 'ok' | 'seen' | 'failed', cb: any) => {
|
on: (type: 'ok' | 'failed', cb: any) => {
|
||||||
pubListeners[id] = pubListeners[id] || {
|
pubListeners[id] = pubListeners[id] || {
|
||||||
ok: [],
|
ok: [],
|
||||||
seen: [],
|
|
||||||
failed: []
|
failed: []
|
||||||
}
|
}
|
||||||
pubListeners[id][type].push(cb)
|
pubListeners[id][type].push(cb)
|
||||||
|
|
||||||
if (type === 'seen') {
|
|
||||||
if (sent) startMonitoring()
|
|
||||||
else mustMonitor = true
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
off: (type: 'ok' | 'seen' | 'failed', cb: any) => {
|
off: (type: 'ok' | 'failed', cb: any) => {
|
||||||
let listeners = pubListeners[id]
|
let listeners = pubListeners[id]
|
||||||
if (!listeners) return
|
if (!listeners) return
|
||||||
let idx = listeners[type].indexOf(cb)
|
let idx = listeners[type].indexOf(cb)
|
||||||
@@ -287,11 +298,12 @@ export function relayInit(url: string): Relay {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
connect,
|
connect,
|
||||||
close(): Promise<void> {
|
close(): void {
|
||||||
ws.close()
|
listeners = {connect: [], disconnect: [], error: [], notice: []}
|
||||||
return new Promise<void>(resolve => {
|
subListeners = {}
|
||||||
resolveClose = resolve
|
pubListeners = {}
|
||||||
})
|
|
||||||
|
ws?.close()
|
||||||
},
|
},
|
||||||
get status() {
|
get status() {
|
||||||
return ws?.readyState ?? 3
|
return ws?.readyState ?? 3
|
||||||
|
|||||||
Reference in New Issue
Block a user