mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2025-12-09 16:48:50 +00:00
Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ab5ea8de36 | ||
|
|
a330b97590 | ||
|
|
24406b5679 | ||
|
|
6dbcc87d93 | ||
|
|
0ddcfdce68 | ||
|
|
87bf349ce8 | ||
|
|
54dfc7b972 | ||
|
|
32793146a4 |
@@ -104,9 +104,6 @@ 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}`)
|
||||||
})
|
})
|
||||||
|
|||||||
23
event.ts
23
event.ts
@@ -16,23 +16,31 @@ 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 Event = EventTemplate & {
|
||||||
|
pubkey: string
|
||||||
|
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
|
||||||
@@ -59,6 +67,7 @@ export function getEventHash(event: Event): string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function validateEvent(event: Event): boolean {
|
export function validateEvent(event: Event): 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
|
||||||
|
|||||||
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'
|
||||||
|
|||||||
140
magic.ts
Normal file
140
magic.ts
Normal file
@@ -0,0 +1,140 @@
|
|||||||
|
import {Relay, relayInit} from './relay'
|
||||||
|
import {Event} from './event'
|
||||||
|
import {normalizeURL} from './utils'
|
||||||
|
|
||||||
|
export default function (
|
||||||
|
writeableRelays: string[],
|
||||||
|
fallbackRelays: string[],
|
||||||
|
safeRelays: string[]
|
||||||
|
) {
|
||||||
|
return new MagicPool(fallbackRelays, writeableRelays, safeRelays)
|
||||||
|
}
|
||||||
|
|
||||||
|
class MagicPool {
|
||||||
|
private _conn: {[url: string]: Relay}
|
||||||
|
private _fallback: {[url: string]: Relay}
|
||||||
|
private _write: {[url: string]: Relay}
|
||||||
|
private _safe: {[url: string]: Relay}
|
||||||
|
|
||||||
|
private _profileRelays: {[pubkey: string]: RelayTableScore}
|
||||||
|
private _tempCache: {[id: string]: Event}
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
fallbackRelays: string[],
|
||||||
|
writeableRelays: string[],
|
||||||
|
safeRelays: string[] = [
|
||||||
|
'wss://eden.nostr.land',
|
||||||
|
'wss://nostr.milou.lol',
|
||||||
|
'wss://relay.minds.com/nostr/v1/ws'
|
||||||
|
]
|
||||||
|
) {
|
||||||
|
this._conn = {}
|
||||||
|
this._write = {}
|
||||||
|
this._fallback = {}
|
||||||
|
this._profileRelays = {}
|
||||||
|
this._tempCache = {}
|
||||||
|
|
||||||
|
const hasEventId = (id: string): boolean => id in this._tempCache
|
||||||
|
const init = (url: string) => {
|
||||||
|
this._conn[normalizeURL(url)] = relayInit(normalizeURL(url), hasEventId)
|
||||||
|
}
|
||||||
|
|
||||||
|
fallbackRelays.forEach(init)
|
||||||
|
writeableRelays.forEach(init)
|
||||||
|
safeRelays.forEach(init)
|
||||||
|
|
||||||
|
this._write = Object.fromEntries(
|
||||||
|
writeableRelays.map(url => [
|
||||||
|
normalizeURL(url),
|
||||||
|
this._conn[normalizeURL(url)]
|
||||||
|
])
|
||||||
|
)
|
||||||
|
this._fallback = Object.fromEntries(
|
||||||
|
fallbackRelays.map(url => [
|
||||||
|
normalizeURL(url),
|
||||||
|
this._conn[normalizeURL(url)]
|
||||||
|
])
|
||||||
|
)
|
||||||
|
this._safe = Object.fromEntries(
|
||||||
|
safeRelays.map(url => [normalizeURL(url), this._conn[normalizeURL(url)]])
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
publish(event: Event) {
|
||||||
|
return Promise.all(
|
||||||
|
Object.entries(this._write).map(
|
||||||
|
([url, relay]) =>
|
||||||
|
new Promise(async resolve => {
|
||||||
|
await relay.connect()
|
||||||
|
let pub = relay.publish(event)
|
||||||
|
let to = setTimeout(() => {
|
||||||
|
let end = setTimeout(() => {
|
||||||
|
resolve({url, success: false, reason: 'timeout'})
|
||||||
|
}, 2500)
|
||||||
|
pub.on('seen', () => {
|
||||||
|
clearTimeout(end)
|
||||||
|
resolve({url, success: true, reason: 'seen'})
|
||||||
|
})
|
||||||
|
}, 2500)
|
||||||
|
pub.on('ok', () => {
|
||||||
|
clearTimeout(to)
|
||||||
|
resolve({url, success: true, reason: 'ok'})
|
||||||
|
})
|
||||||
|
pub.on('failed', (reason: string) => {
|
||||||
|
clearTimeout(to)
|
||||||
|
resolve({url, success: false, reason})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
profile(
|
||||||
|
pubkey: string,
|
||||||
|
onUpdate: (events: Event[]) => void
|
||||||
|
): {
|
||||||
|
page(n: number): void
|
||||||
|
} {
|
||||||
|
var relays = new Set()
|
||||||
|
let rts = this._profileRelays[pubkey]
|
||||||
|
if (rts) {
|
||||||
|
relays = rts.get(3)
|
||||||
|
}
|
||||||
|
|
||||||
|
let fallback = Object.values(this._fallback)
|
||||||
|
for (let i = 0; i < fallback.length; i++) {
|
||||||
|
if (relays.size < 3) {
|
||||||
|
relays.add(fallback[Math.floor(Math.random() * fallback.length)])
|
||||||
|
} else break
|
||||||
|
}
|
||||||
|
|
||||||
|
// start subscription
|
||||||
|
for (let r in relays) {
|
||||||
|
r.
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
page(n: number) {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class RelayTableScore {
|
||||||
|
seen: string[] = []
|
||||||
|
hinted: string[] = []
|
||||||
|
explicit: string[] = []
|
||||||
|
|
||||||
|
get(n: number): Set<string> {
|
||||||
|
let relays = new Set<string>()
|
||||||
|
for (let i = 0; i < n; i++) {
|
||||||
|
for (let j = 0; j < 3; j++) {
|
||||||
|
let v = [this.seen, this.explicit, this.hinted][j][i]
|
||||||
|
if (v) {
|
||||||
|
relays.add(v)
|
||||||
|
if (relays.size >= n) return relays
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return relays
|
||||||
|
}
|
||||||
|
}
|
||||||
107
nip57.ts
Normal file
107
nip57.ts
Normal file
@@ -0,0 +1,107 @@
|
|||||||
|
import {bech32} from '@scure/base'
|
||||||
|
|
||||||
|
import {Event, EventTemplate} 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: string
|
||||||
|
comment: string
|
||||||
|
relays: string[]
|
||||||
|
}): EventTemplate {
|
||||||
|
let zr = {
|
||||||
|
kind: 9734,
|
||||||
|
created_at: Math.round(Date.now() / 1000),
|
||||||
|
content: comment,
|
||||||
|
tags: [
|
||||||
|
['p', profile],
|
||||||
|
['amount', amount],
|
||||||
|
['relays', ...relays]
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
if (event) {
|
||||||
|
zr.tags.push(['e', event])
|
||||||
|
}
|
||||||
|
|
||||||
|
return zr
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "nostr-tools",
|
"name": "nostr-tools",
|
||||||
"version": "1.4.0",
|
"version": "1.4.2",
|
||||||
"description": "Tools for making a Nostr client.",
|
"description": "Tools for making a Nostr client.",
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
|
|||||||
64
relay.ts
64
relay.ts
@@ -19,8 +19,8 @@ export type Relay = {
|
|||||||
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
|
||||||
@@ -38,10 +38,6 @@ export type SubscriptionOptions = {
|
|||||||
export function relayInit(url: string): Relay {
|
export function relayInit(url: string): Relay {
|
||||||
var ws: WebSocket
|
var ws: WebSocket
|
||||||
var resolveClose: () => void
|
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>
|
||||||
@@ -74,7 +70,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 = () => {
|
||||||
@@ -140,15 +135,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':
|
||||||
@@ -171,7 +173,6 @@ export function relayInit(url: string): Relay {
|
|||||||
async function trySend(params: [string, ...any]) {
|
async function trySend(params: [string, ...any]) {
|
||||||
let msg = JSON.stringify(params)
|
let msg = JSON.stringify(params)
|
||||||
|
|
||||||
await untilOpen
|
|
||||||
try {
|
try {
|
||||||
ws.send(msg)
|
ws.send(msg)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
@@ -271,50 +272,17 @@ export function relayInit(url: string): Relay {
|
|||||||
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)
|
||||||
@@ -324,6 +292,10 @@ export function relayInit(url: string): Relay {
|
|||||||
},
|
},
|
||||||
connect,
|
connect,
|
||||||
close(): Promise<void> {
|
close(): Promise<void> {
|
||||||
|
listeners = {connect: [], disconnect: [], error: [], notice: []}
|
||||||
|
subListeners = {}
|
||||||
|
pubListeners = {}
|
||||||
|
|
||||||
if (ws.readyState > 1) return Promise.resolve()
|
if (ws.readyState > 1) return Promise.resolve()
|
||||||
ws.close()
|
ws.close()
|
||||||
return new Promise<void>(resolve => {
|
return new Promise<void>(resolve => {
|
||||||
|
|||||||
Reference in New Issue
Block a user