mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2025-12-09 16:48:50 +00:00
Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9132b722f3 | ||
|
|
ae2f97655b | ||
|
|
5b78a829c7 | ||
|
|
de26ee98c5 | ||
|
|
1437bbdb0f | ||
|
|
57354b9fb4 | ||
|
|
924075b803 | ||
|
|
666a02027e | ||
|
|
eff9ea9579 | ||
|
|
ca174e6cd8 |
20
README.md
20
README.md
@@ -67,9 +67,11 @@ import { SimplePool } from 'nostr-tools/pool'
|
|||||||
|
|
||||||
const pool = new SimplePool()
|
const pool = new SimplePool()
|
||||||
|
|
||||||
// let's query for an event that exists
|
const relays = ['wss://relay.example.com', 'wss://relay.example2.com']
|
||||||
const event = relay.get(
|
|
||||||
['wss://relay.example.com'],
|
// let's query for one event that exists
|
||||||
|
const event = pool.get(
|
||||||
|
relays,
|
||||||
{
|
{
|
||||||
ids: ['d7dd5eb3ab747e16f8d0212d53032ea2a7cadef53837e5a6c66d42849fcb9027'],
|
ids: ['d7dd5eb3ab747e16f8d0212d53032ea2a7cadef53837e5a6c66d42849fcb9027'],
|
||||||
},
|
},
|
||||||
@@ -78,6 +80,18 @@ if (event) {
|
|||||||
console.log('it exists indeed on this relay:', event)
|
console.log('it exists indeed on this relay:', event)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// let's query for more than one event that exists
|
||||||
|
const events = pool.querySync(
|
||||||
|
relays,
|
||||||
|
{
|
||||||
|
kinds: [1],
|
||||||
|
limit: 10
|
||||||
|
},
|
||||||
|
)
|
||||||
|
if (events) {
|
||||||
|
console.log('it exists indeed on this relay:', events)
|
||||||
|
}
|
||||||
|
|
||||||
// let's publish a new event while simultaneously monitoring the relay for it
|
// let's publish a new event while simultaneously monitoring the relay for it
|
||||||
let sk = generateSecretKey()
|
let sk = generateSecretKey()
|
||||||
let pk = getPublicKey(sk)
|
let pk = getPublicKey(sk)
|
||||||
|
|||||||
115
abstract-pool.ts
115
abstract-pool.ts
@@ -180,121 +180,6 @@ export class AbstractSimplePool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @deprecated Use subscribeMap instead.
|
|
||||||
*/
|
|
||||||
subscribeManyMap(requests: { [relay: string]: Filter[] }, params: SubscribeManyParams): SubCloser {
|
|
||||||
if (this.trackRelays) {
|
|
||||||
params.receivedEvent = (relay: AbstractRelay, id: string) => {
|
|
||||||
let set = this.seenOn.get(id)
|
|
||||||
if (!set) {
|
|
||||||
set = new Set()
|
|
||||||
this.seenOn.set(id, set)
|
|
||||||
}
|
|
||||||
set.add(relay)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const _knownIds = new Set<string>()
|
|
||||||
const subs: Subscription[] = []
|
|
||||||
const relaysLength = Object.keys(requests).length
|
|
||||||
|
|
||||||
// batch all EOSEs into a single
|
|
||||||
const eosesReceived: boolean[] = []
|
|
||||||
let handleEose = (i: number) => {
|
|
||||||
if (eosesReceived[i]) return // do not act twice for the same relay
|
|
||||||
eosesReceived[i] = true
|
|
||||||
if (eosesReceived.filter(a => a).length === relaysLength) {
|
|
||||||
params.oneose?.()
|
|
||||||
handleEose = () => {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// batch all closes into a single
|
|
||||||
const closesReceived: string[] = []
|
|
||||||
let handleClose = (i: number, reason: string) => {
|
|
||||||
if (closesReceived[i]) return // do not act twice for the same relay
|
|
||||||
handleEose(i)
|
|
||||||
closesReceived[i] = reason
|
|
||||||
if (closesReceived.filter(a => a).length === relaysLength) {
|
|
||||||
params.onclose?.(closesReceived)
|
|
||||||
handleClose = () => {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const localAlreadyHaveEventHandler = (id: string) => {
|
|
||||||
if (params.alreadyHaveEvent?.(id)) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
const have = _knownIds.has(id)
|
|
||||||
_knownIds.add(id)
|
|
||||||
return have
|
|
||||||
}
|
|
||||||
|
|
||||||
// open a subscription in all given relays
|
|
||||||
const allOpened = Promise.all(
|
|
||||||
Object.entries(requests).map(async (req, i, arr) => {
|
|
||||||
if (arr.indexOf(req) !== i) {
|
|
||||||
// duplicate
|
|
||||||
handleClose(i, 'duplicate url')
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
let [url, filters] = req
|
|
||||||
url = normalizeURL(url)
|
|
||||||
|
|
||||||
let relay: AbstractRelay
|
|
||||||
try {
|
|
||||||
relay = await this.ensureRelay(url, {
|
|
||||||
connectionTimeout: params.maxWait ? Math.max(params.maxWait * 0.8, params.maxWait - 1000) : undefined,
|
|
||||||
})
|
|
||||||
} catch (err) {
|
|
||||||
handleClose(i, (err as any)?.message || String(err))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
let subscription = relay.subscribe(filters, {
|
|
||||||
...params,
|
|
||||||
oneose: () => handleEose(i),
|
|
||||||
onclose: reason => {
|
|
||||||
if (reason.startsWith('auth-required:') && params.doauth) {
|
|
||||||
relay
|
|
||||||
.auth(params.doauth)
|
|
||||||
.then(() => {
|
|
||||||
relay.subscribe(filters, {
|
|
||||||
...params,
|
|
||||||
oneose: () => handleEose(i),
|
|
||||||
onclose: reason => {
|
|
||||||
handleClose(i, reason) // the second time we won't try to auth anymore
|
|
||||||
},
|
|
||||||
alreadyHaveEvent: localAlreadyHaveEventHandler,
|
|
||||||
eoseTimeout: params.maxWait,
|
|
||||||
})
|
|
||||||
})
|
|
||||||
.catch(err => {
|
|
||||||
handleClose(i, `auth was required and attempted, but failed with: ${err}`)
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
handleClose(i, reason)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
alreadyHaveEvent: localAlreadyHaveEventHandler,
|
|
||||||
eoseTimeout: params.maxWait,
|
|
||||||
})
|
|
||||||
|
|
||||||
subs.push(subscription)
|
|
||||||
}),
|
|
||||||
)
|
|
||||||
|
|
||||||
return {
|
|
||||||
async close() {
|
|
||||||
await allOpened
|
|
||||||
subs.forEach(sub => {
|
|
||||||
sub.close()
|
|
||||||
})
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
subscribeEose(
|
subscribeEose(
|
||||||
relays: string[],
|
relays: string[],
|
||||||
filter: Filter,
|
filter: Filter,
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
/* global WebSocket */
|
/* global WebSocket */
|
||||||
|
|
||||||
import type { Event, EventTemplate, VerifiedEvent, Nostr } from './core.ts'
|
import type { Event, EventTemplate, VerifiedEvent, Nostr, NostrEvent } from './core.ts'
|
||||||
import { matchFilters, type Filter } from './filter.ts'
|
import { matchFilters, type Filter } from './filter.ts'
|
||||||
import { getHex64, getSubscriptionId } from './fakejson.ts'
|
import { getHex64, getSubscriptionId } from './fakejson.ts'
|
||||||
import { Queue, normalizeURL } from './utils.ts'
|
import { Queue, normalizeURL } from './utils.ts'
|
||||||
@@ -12,6 +12,13 @@ export type AbstractRelayConstructorOptions = {
|
|||||||
websocketImplementation?: typeof WebSocket
|
websocketImplementation?: typeof WebSocket
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export class SendingOnClosedConnection extends Error {
|
||||||
|
constructor(message: string, relay: string) {
|
||||||
|
super(`Tried to send message '${message} on a closed connection to ${relay}.`)
|
||||||
|
this.name = 'SendingOnClosedConnection'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export class AbstractRelay {
|
export class AbstractRelay {
|
||||||
public readonly url: string
|
public readonly url: string
|
||||||
private _connected: boolean = false
|
private _connected: boolean = false
|
||||||
@@ -112,7 +119,9 @@ export class AbstractRelay {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
this.ws.onclose = async () => {
|
this.ws.onclose = ev => {
|
||||||
|
clearTimeout(this.connectionTimeoutHandle)
|
||||||
|
reject((ev as any).message || 'websocket closed')
|
||||||
if (this._connected) {
|
if (this._connected) {
|
||||||
this._connected = false
|
this._connected = false
|
||||||
this.connectionPromise = undefined
|
this.connectionPromise = undefined
|
||||||
@@ -176,7 +185,7 @@ export class AbstractRelay {
|
|||||||
switch (data[0]) {
|
switch (data[0]) {
|
||||||
case 'EVENT': {
|
case 'EVENT': {
|
||||||
const so = this.openSubs.get(data[1] as string) as Subscription
|
const so = this.openSubs.get(data[1] as string) as Subscription
|
||||||
const event = data[2] as Event
|
const event = data[2] as NostrEvent
|
||||||
if (this.verifyEvent(event) && matchFilters(so.filters, event)) {
|
if (this.verifyEvent(event) && matchFilters(so.filters, event)) {
|
||||||
so.onevent(event)
|
so.onevent(event)
|
||||||
}
|
}
|
||||||
@@ -234,7 +243,7 @@ export class AbstractRelay {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public async send(message: string) {
|
public async send(message: string) {
|
||||||
if (!this.connectionPromise) throw new Error('sending on closed connection')
|
if (!this.connectionPromise) throw new SendingOnClosedConnection(message, this.url)
|
||||||
|
|
||||||
this.connectionPromise.then(() => {
|
this.connectionPromise.then(() => {
|
||||||
this.ws?.send(message)
|
this.ws?.send(message)
|
||||||
@@ -377,7 +386,15 @@ export class Subscription {
|
|||||||
if (!this.closed && this.relay.connected) {
|
if (!this.closed && this.relay.connected) {
|
||||||
// if the connection was closed by the user calling .close() we will send a CLOSE message
|
// if the connection was closed by the user calling .close() we will send a CLOSE message
|
||||||
// otherwise this._open will be already set to false so we will skip this
|
// otherwise this._open will be already set to false so we will skip this
|
||||||
this.relay.send('["CLOSE",' + JSON.stringify(this.id) + ']')
|
try {
|
||||||
|
this.relay.send('["CLOSE",' + JSON.stringify(this.id) + ']')
|
||||||
|
} catch (err) {
|
||||||
|
if (err instanceof SendingOnClosedConnection) {
|
||||||
|
/* doesn't matter, it's ok */
|
||||||
|
} else {
|
||||||
|
throw err
|
||||||
|
}
|
||||||
|
}
|
||||||
this.closed = true
|
this.closed = true
|
||||||
}
|
}
|
||||||
this.relay.openSubs.delete(this.id)
|
this.relay.openSubs.delete(this.id)
|
||||||
|
|||||||
2
jsr.json
2
jsr.json
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@nostr/tools",
|
"name": "@nostr/tools",
|
||||||
"version": "2.13.0",
|
"version": "2.13.3",
|
||||||
"exports": {
|
"exports": {
|
||||||
".": "./index.ts",
|
".": "./index.ts",
|
||||||
"./core": "./core.ts",
|
"./core": "./core.ts",
|
||||||
|
|||||||
5
justfile
5
justfile
@@ -12,11 +12,14 @@ test-only file:
|
|||||||
bun test {{file}}
|
bun test {{file}}
|
||||||
|
|
||||||
publish: build
|
publish: build
|
||||||
npm publish
|
# publish to jsr first because it is more strict and will catch some errors
|
||||||
perl -i -0pe "s/},\n \"optionalDependencies\": {\n/,/" package.json
|
perl -i -0pe "s/},\n \"optionalDependencies\": {\n/,/" package.json
|
||||||
jsr publish --allow-dirty
|
jsr publish --allow-dirty
|
||||||
git checkout -- package.json
|
git checkout -- package.json
|
||||||
|
|
||||||
|
# then to npm
|
||||||
|
npm publish
|
||||||
|
|
||||||
format:
|
format:
|
||||||
eslint --ext .ts --fix *.ts
|
eslint --ext .ts --fix *.ts
|
||||||
prettier --write *.ts
|
prettier --write *.ts
|
||||||
|
|||||||
3
kinds.ts
3
kinds.ts
@@ -20,9 +20,6 @@ export function isAddressableKind(kind: number): boolean {
|
|||||||
return 30000 <= kind && kind < 40000
|
return 30000 <= kind && kind < 40000
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @deprecated use isAddressableKind instead */
|
|
||||||
export const isParameterizedReplaceableKind = isAddressableKind
|
|
||||||
|
|
||||||
/** Classification of the event kind. */
|
/** Classification of the event kind. */
|
||||||
export type KindClassification = 'regular' | 'replaceable' | 'ephemeral' | 'parameterized' | 'unknown'
|
export type KindClassification = 'regular' | 'replaceable' | 'ephemeral' | 'parameterized' | 'unknown'
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import { decode } from './nip19.ts'
|
|||||||
import { wrapEvent, wrapManyEvents, unwrapEvent } from './nip17.ts'
|
import { wrapEvent, wrapManyEvents, unwrapEvent } from './nip17.ts'
|
||||||
import { hexToBytes } from '@noble/hashes/utils'
|
import { hexToBytes } from '@noble/hashes/utils'
|
||||||
|
|
||||||
const senderPrivateKey = decode(`nsec1p0ht6p3wepe47sjrgesyn4m50m6avk2waqudu9rl324cg2c4ufesyp6rdg`).data as Uint8Array
|
const senderPrivateKey = decode(`nsec1p0ht6p3wepe47sjrgesyn4m50m6avk2waqudu9rl324cg2c4ufesyp6rdg`).data
|
||||||
|
|
||||||
const sk1 = hexToBytes('f09ac9b695d0a4c6daa418fe95b977eea20f54d9545592bc36a4f9e14f3eb840')
|
const sk1 = hexToBytes('f09ac9b695d0a4c6daa418fe95b977eea20f54d9545592bc36a4f9e14f3eb840')
|
||||||
const sk2 = hexToBytes('5393a825e5892d8e18d4a5ea61ced105e8bb2a106f42876be3a40522e0b13747')
|
const sk2 = hexToBytes('5393a825e5892d8e18d4a5ea61ced105e8bb2a106f42876be3a40522e0b13747')
|
||||||
|
|||||||
@@ -1,17 +1,14 @@
|
|||||||
import { test, expect, describe } from 'bun:test'
|
import { describe, expect, test } from 'bun:test'
|
||||||
import { generateSecretKey, getPublicKey } from './pure.ts'
|
|
||||||
import {
|
import {
|
||||||
decode,
|
decode,
|
||||||
naddrEncode,
|
naddrEncode,
|
||||||
|
neventEncode,
|
||||||
|
NostrTypeGuard,
|
||||||
nprofileEncode,
|
nprofileEncode,
|
||||||
npubEncode,
|
npubEncode,
|
||||||
nsecEncode,
|
nsecEncode
|
||||||
neventEncode,
|
|
||||||
type AddressPointer,
|
|
||||||
type ProfilePointer,
|
|
||||||
EventPointer,
|
|
||||||
NostrTypeGuard,
|
|
||||||
} from './nip19.ts'
|
} from './nip19.ts'
|
||||||
|
import { generateSecretKey, getPublicKey } from './pure.ts'
|
||||||
|
|
||||||
test('encode and decode nsec', () => {
|
test('encode and decode nsec', () => {
|
||||||
let sk = generateSecretKey()
|
let sk = generateSecretKey()
|
||||||
@@ -38,7 +35,7 @@ test('encode and decode nprofile', () => {
|
|||||||
expect(nprofile).toMatch(/nprofile1\w+/)
|
expect(nprofile).toMatch(/nprofile1\w+/)
|
||||||
let { type, data } = decode(nprofile)
|
let { type, data } = decode(nprofile)
|
||||||
expect(type).toEqual('nprofile')
|
expect(type).toEqual('nprofile')
|
||||||
const pointer = data as ProfilePointer
|
const pointer = data
|
||||||
expect(pointer.pubkey).toEqual(pk)
|
expect(pointer.pubkey).toEqual(pk)
|
||||||
expect(pointer.relays).toContain(relays[0])
|
expect(pointer.relays).toContain(relays[0])
|
||||||
expect(pointer.relays).toContain(relays[1])
|
expect(pointer.relays).toContain(relays[1])
|
||||||
@@ -67,7 +64,7 @@ test('encode and decode naddr', () => {
|
|||||||
expect(naddr).toMatch(/naddr1\w+/)
|
expect(naddr).toMatch(/naddr1\w+/)
|
||||||
let { type, data } = decode(naddr)
|
let { type, data } = decode(naddr)
|
||||||
expect(type).toEqual('naddr')
|
expect(type).toEqual('naddr')
|
||||||
const pointer = data as AddressPointer
|
const pointer = data
|
||||||
expect(pointer.pubkey).toEqual(pk)
|
expect(pointer.pubkey).toEqual(pk)
|
||||||
expect(pointer.relays).toContain(relays[0])
|
expect(pointer.relays).toContain(relays[0])
|
||||||
expect(pointer.relays).toContain(relays[1])
|
expect(pointer.relays).toContain(relays[1])
|
||||||
@@ -86,7 +83,7 @@ test('encode and decode nevent', () => {
|
|||||||
expect(nevent).toMatch(/nevent1\w+/)
|
expect(nevent).toMatch(/nevent1\w+/)
|
||||||
let { type, data } = decode(nevent)
|
let { type, data } = decode(nevent)
|
||||||
expect(type).toEqual('nevent')
|
expect(type).toEqual('nevent')
|
||||||
const pointer = data as EventPointer
|
const pointer = data
|
||||||
expect(pointer.id).toEqual(pk)
|
expect(pointer.id).toEqual(pk)
|
||||||
expect(pointer.relays).toContain(relays[0])
|
expect(pointer.relays).toContain(relays[0])
|
||||||
expect(pointer.kind).toEqual(30023)
|
expect(pointer.kind).toEqual(30023)
|
||||||
@@ -103,7 +100,7 @@ test('encode and decode nevent with kind 0', () => {
|
|||||||
expect(nevent).toMatch(/nevent1\w+/)
|
expect(nevent).toMatch(/nevent1\w+/)
|
||||||
let { type, data } = decode(nevent)
|
let { type, data } = decode(nevent)
|
||||||
expect(type).toEqual('nevent')
|
expect(type).toEqual('nevent')
|
||||||
const pointer = data as EventPointer
|
const pointer = data
|
||||||
expect(pointer.id).toEqual(pk)
|
expect(pointer.id).toEqual(pk)
|
||||||
expect(pointer.relays).toContain(relays[0])
|
expect(pointer.relays).toContain(relays[0])
|
||||||
expect(pointer.kind).toEqual(0)
|
expect(pointer.kind).toEqual(0)
|
||||||
@@ -121,7 +118,7 @@ test('encode and decode naddr with empty "d"', () => {
|
|||||||
expect(naddr).toMatch(/naddr\w+/)
|
expect(naddr).toMatch(/naddr\w+/)
|
||||||
let { type, data } = decode(naddr)
|
let { type, data } = decode(naddr)
|
||||||
expect(type).toEqual('naddr')
|
expect(type).toEqual('naddr')
|
||||||
const pointer = data as AddressPointer
|
const pointer = data
|
||||||
expect(pointer.identifier).toEqual('')
|
expect(pointer.identifier).toEqual('')
|
||||||
expect(pointer.relays).toContain(relays[0])
|
expect(pointer.relays).toContain(relays[0])
|
||||||
expect(pointer.kind).toEqual(3)
|
expect(pointer.kind).toEqual(3)
|
||||||
@@ -133,7 +130,7 @@ test('decode naddr from habla.news', () => {
|
|||||||
'naddr1qq98yetxv4ex2mnrv4esygrl54h466tz4v0re4pyuavvxqptsejl0vxcmnhfl60z3rth2xkpjspsgqqqw4rsf34vl5',
|
'naddr1qq98yetxv4ex2mnrv4esygrl54h466tz4v0re4pyuavvxqptsejl0vxcmnhfl60z3rth2xkpjspsgqqqw4rsf34vl5',
|
||||||
)
|
)
|
||||||
expect(type).toEqual('naddr')
|
expect(type).toEqual('naddr')
|
||||||
const pointer = data as AddressPointer
|
const pointer = data
|
||||||
expect(pointer.pubkey).toEqual('7fa56f5d6962ab1e3cd424e758c3002b8665f7b0d8dcee9fe9e288d7751ac194')
|
expect(pointer.pubkey).toEqual('7fa56f5d6962ab1e3cd424e758c3002b8665f7b0d8dcee9fe9e288d7751ac194')
|
||||||
expect(pointer.kind).toEqual(30023)
|
expect(pointer.kind).toEqual(30023)
|
||||||
expect(pointer.identifier).toEqual('references')
|
expect(pointer.identifier).toEqual('references')
|
||||||
@@ -145,7 +142,7 @@ test('decode naddr from go-nostr with different TLV ordering', () => {
|
|||||||
)
|
)
|
||||||
|
|
||||||
expect(type).toEqual('naddr')
|
expect(type).toEqual('naddr')
|
||||||
const pointer = data as AddressPointer
|
const pointer = data
|
||||||
expect(pointer.pubkey).toEqual('3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d')
|
expect(pointer.pubkey).toEqual('3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d')
|
||||||
expect(pointer.relays).toContain('wss://relay.nostr.example.mydomain.example.com')
|
expect(pointer.relays).toContain('wss://relay.nostr.example.mydomain.example.com')
|
||||||
expect(pointer.relays).toContain('wss://nostr.banana.com')
|
expect(pointer.relays).toContain('wss://nostr.banana.com')
|
||||||
|
|||||||
65
nip19.ts
65
nip19.ts
@@ -70,31 +70,46 @@ export function decodeNostrURI(nip19code: string): ReturnType<typeof decode> | {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function decode(code: string):
|
export type DecodedNevent = {
|
||||||
| {
|
type: 'nevent'
|
||||||
type: 'nevent'
|
data: EventPointer
|
||||||
data: EventPointer
|
}
|
||||||
}
|
|
||||||
| {
|
export type DecodedNprofile = {
|
||||||
type: 'nprofile'
|
type: 'nprofile'
|
||||||
data: ProfilePointer
|
data: ProfilePointer
|
||||||
}
|
}
|
||||||
| {
|
|
||||||
type: 'naddr'
|
export type DecodedNaddr = {
|
||||||
data: AddressPointer
|
type: 'naddr'
|
||||||
}
|
data: AddressPointer
|
||||||
| {
|
}
|
||||||
type: 'npub'
|
|
||||||
data: string
|
export type DecodedNsec = {
|
||||||
}
|
type: 'nsec'
|
||||||
| {
|
data: Uint8Array
|
||||||
type: 'nsec'
|
}
|
||||||
data: Uint8Array
|
|
||||||
}
|
export type DecodedNpub = {
|
||||||
| {
|
type: 'npub'
|
||||||
type: 'note'
|
data: string
|
||||||
data: string
|
}
|
||||||
} {
|
|
||||||
|
export type DecodedNote = {
|
||||||
|
type: 'note'
|
||||||
|
data: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export type DecodedResult = DecodedNevent | DecodedNprofile | DecodedNaddr | DecodedNpub | DecodedNsec | DecodedNote
|
||||||
|
|
||||||
|
export function decode(nip19: NEvent): DecodedNevent
|
||||||
|
export function decode(nip19: NProfile): DecodedNprofile
|
||||||
|
export function decode(nip19: NAddr): DecodedNaddr
|
||||||
|
export function decode(nip19: NSec): DecodedNsec
|
||||||
|
export function decode(nip19: NPub): DecodedNpub
|
||||||
|
export function decode(nip19: Note): DecodedNote
|
||||||
|
export function decode(code: string): DecodedResult
|
||||||
|
export function decode(code: string): DecodedResult {
|
||||||
let { prefix, words } = bech32.decode(code, Bech32MaxSize)
|
let { prefix, words } = bech32.decode(code, Bech32MaxSize)
|
||||||
let data = new Uint8Array(bech32.fromWords(words))
|
let data = new Uint8Array(bech32.fromWords(words))
|
||||||
|
|
||||||
|
|||||||
6
nip29.ts
6
nip29.ts
@@ -2,7 +2,7 @@ import { AbstractSimplePool } from './abstract-pool.ts'
|
|||||||
import { Subscription } from './abstract-relay.ts'
|
import { Subscription } from './abstract-relay.ts'
|
||||||
import type { Event, EventTemplate } from './core.ts'
|
import type { Event, EventTemplate } from './core.ts'
|
||||||
import { fetchRelayInformation, RelayInformation } from './nip11.ts'
|
import { fetchRelayInformation, RelayInformation } from './nip11.ts'
|
||||||
import { AddressPointer, decode } from './nip19.ts'
|
import { AddressPointer, decode, NostrTypeGuard } from './nip19.ts'
|
||||||
import { normalizeURL } from './utils.ts'
|
import { normalizeURL } from './utils.ts'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -518,11 +518,11 @@ export async function loadGroupFromCode(pool: AbstractSimplePool, code: string):
|
|||||||
* @returns A GroupReference object if the code is valid, otherwise null.
|
* @returns A GroupReference object if the code is valid, otherwise null.
|
||||||
*/
|
*/
|
||||||
export function parseGroupCode(code: string): null | GroupReference {
|
export function parseGroupCode(code: string): null | GroupReference {
|
||||||
if (code.startsWith('naddr1')) {
|
if (NostrTypeGuard.isNAddr(code)) {
|
||||||
try {
|
try {
|
||||||
let { data } = decode(code)
|
let { data } = decode(code)
|
||||||
|
|
||||||
let { relays, identifier } = data as AddressPointer
|
let { relays, identifier } = data
|
||||||
if (!relays || relays.length === 0) return null
|
if (!relays || relays.length === 0) return null
|
||||||
|
|
||||||
let host = relays![0]
|
let host = relays![0]
|
||||||
|
|||||||
2
nip47.ts
2
nip47.ts
@@ -32,7 +32,7 @@ export async function makeNwcRequestEvent(
|
|||||||
invoice,
|
invoice,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
const encryptedContent = await encrypt(secretKey, pubkey, JSON.stringify(content))
|
const encryptedContent = encrypt(secretKey, pubkey, JSON.stringify(content))
|
||||||
const eventTemplate = {
|
const eventTemplate = {
|
||||||
kind: NWCWalletRequest,
|
kind: NWCWalletRequest,
|
||||||
created_at: Math.round(Date.now() / 1000),
|
created_at: Math.round(Date.now() / 1000),
|
||||||
|
|||||||
@@ -1,7 +1,14 @@
|
|||||||
import { describe, test, expect, mock } from 'bun:test'
|
import { describe, test, expect, mock } from 'bun:test'
|
||||||
import { finalizeEvent } from './pure.ts'
|
import { finalizeEvent } from './pure.ts'
|
||||||
import { getPublicKey, generateSecretKey } from './pure.ts'
|
import { getPublicKey, generateSecretKey } from './pure.ts'
|
||||||
import { getZapEndpoint, makeZapReceipt, makeZapRequest, useFetchImplementation, validateZapRequest } from './nip57.ts'
|
import {
|
||||||
|
getSatoshisAmountFromBolt11,
|
||||||
|
getZapEndpoint,
|
||||||
|
makeZapReceipt,
|
||||||
|
makeZapRequest,
|
||||||
|
useFetchImplementation,
|
||||||
|
validateZapRequest,
|
||||||
|
} from './nip57.ts'
|
||||||
import { buildEvent } from './test-helpers.ts'
|
import { buildEvent } from './test-helpers.ts'
|
||||||
|
|
||||||
describe('getZapEndpoint', () => {
|
describe('getZapEndpoint', () => {
|
||||||
@@ -317,3 +324,26 @@ describe('makeZapReceipt', () => {
|
|||||||
expect(JSON.stringify(result.tags)).not.toContain('preimage')
|
expect(JSON.stringify(result.tags)).not.toContain('preimage')
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('parses the amount from bolt11 invoices', () => {
|
||||||
|
expect(
|
||||||
|
getSatoshisAmountFromBolt11(
|
||||||
|
'lnbc4u1p5zcarnpp5djng98r73nxu66nxp6gndjkw24q7rdzgp7p80lt0gk4z3h3krkssdq9tfpygcqzzsxqzjcsp58hz3v5qefdm70g5fnm2cn6q9thzpu6m4f5wjqurhur5xzmf9vl3s9qxpqysgq9v6qv86xaruzeak9jjyz54fygrkn526z7xhm0llh8wl44gcgh0rznhjqdswd4cjurzdgh0pgzrfj4sd7f3mf89jd6kadse008ex7kxgqqa5xrk',
|
||||||
|
),
|
||||||
|
).toEqual(400)
|
||||||
|
expect(
|
||||||
|
getSatoshisAmountFromBolt11(
|
||||||
|
'lnbc8400u1p5zcaz5pp5ltvyhtg4ed7sd8jurj28ugmavezkmqsadpe3t9npufpcrd0uet0scqzyssp5l3hz4ayt5ee0p83ma4a96l2rruhx33eyycewldu2ffa5pk2qx7jq9q7sqqqqqqqqqqqqqqqqqqqsqqqqqysgqdq8w3jhxaqmqz9gxqyjw5qrzjqwryaup9lh50kkranzgcdnn2fgvx390wgj5jd07rwr3vxeje0glclll8qkt3np4rqyqqqqlgqqqqqeqqjqhuhjk5u9r850ncxngne7cfp9s08s2nm6c2rkz7jhl8gjmlx0fga5tlncgeuh4avlsrkq6ljyyhgq8rrxprga03esqhd0gf5455x6tdcqahhw9q',
|
||||||
|
),
|
||||||
|
).toEqual(840000)
|
||||||
|
expect(
|
||||||
|
getSatoshisAmountFromBolt11(
|
||||||
|
'lnbc210n1p5zcuaxpp52nn778cfk46md4ld0hdj2juuzvfrsrdaf4ek2k0yeensae07x2cqdq9tfpygcqzzsxqzjcsp5768c4k79jtnq92pgppan8rjnujcpcqhnqwqwk3lm5dfr7e0k2a7s9qxpqysgqt8lnh9l7ple27t73x7gty570ltas2s33uahc7egke5tdmhxr3ezn590wf2utxyt7d3afnk2lxc2u0enc6n53ck4mxwpmzpxa7ws05aqp0c5x3r',
|
||||||
|
),
|
||||||
|
).toEqual(21)
|
||||||
|
expect(
|
||||||
|
getSatoshisAmountFromBolt11(
|
||||||
|
'lnbc899640n1p5zcuavpp5w72fqrf09286lq33vw364qryrq5nw60z4dhdx56f8w05xkx4massdq9tfpygcqzzsxqzjcsp5qrqn4kpvem5jwpl63kj5pfdlqxg2plaffz0prz7vaqjy29uc66us9qxpqysgqlhzzqmn2jxd2476404krm8nvrarymwq7nj2zecl92xug54ek0mfntdxvxwslf756m8kq0r7jtpantm52fmewc72r5lfmd85505jnemgqw5j0pc',
|
||||||
|
),
|
||||||
|
).toEqual(89964)
|
||||||
|
})
|
||||||
|
|||||||
51
nip57.ts
51
nip57.ts
@@ -77,7 +77,7 @@ export function makeZapRequest({
|
|||||||
if (isReplaceableKind(event.kind)) {
|
if (isReplaceableKind(event.kind)) {
|
||||||
const a = ['a', `${event.kind}:${event.pubkey}:`]
|
const a = ['a', `${event.kind}:${event.pubkey}:`]
|
||||||
zr.tags.push(a)
|
zr.tags.push(a)
|
||||||
// addressable event
|
// addressable event
|
||||||
} else if (isAddressableKind(event.kind)) {
|
} else if (isAddressableKind(event.kind)) {
|
||||||
let d = event.tags.find(([t, v]) => t === 'd' && v)
|
let d = event.tags.find(([t, v]) => t === 'd' && v)
|
||||||
if (!d) throw new Error('d tag not found or is empty')
|
if (!d) throw new Error('d tag not found or is empty')
|
||||||
@@ -142,3 +142,52 @@ export function makeZapReceipt({
|
|||||||
|
|
||||||
return zap
|
return zap
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function getSatoshisAmountFromBolt11(bolt11: string): number {
|
||||||
|
if (bolt11.length < 50) {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
bolt11 = bolt11.substring(0, 50)
|
||||||
|
const idx = bolt11.lastIndexOf('1')
|
||||||
|
if (idx === -1) {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
const hrp = bolt11.substring(0, idx)
|
||||||
|
if (!hrp.startsWith('lnbc')) {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
const amount = hrp.substring(4) // equivalent to strings.CutPrefix
|
||||||
|
|
||||||
|
if (amount.length < 1) {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// if last character is a digit, then the amount can just be interpreted as BTC
|
||||||
|
const char = amount[amount.length - 1]
|
||||||
|
const digit = char.charCodeAt(0) - '0'.charCodeAt(0)
|
||||||
|
const isDigit = digit >= 0 && digit <= 9
|
||||||
|
|
||||||
|
let cutPoint = amount.length - 1
|
||||||
|
if (isDigit) {
|
||||||
|
cutPoint++
|
||||||
|
}
|
||||||
|
|
||||||
|
if (cutPoint < 1) {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
const num = parseInt(amount.substring(0, cutPoint))
|
||||||
|
|
||||||
|
switch (char) {
|
||||||
|
case 'm':
|
||||||
|
return num * 100000
|
||||||
|
case 'u':
|
||||||
|
return num * 100
|
||||||
|
case 'n':
|
||||||
|
return num / 10
|
||||||
|
case 'p':
|
||||||
|
return num / 10000
|
||||||
|
default:
|
||||||
|
return num * 100000000
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"name": "nostr-tools",
|
"name": "nostr-tools",
|
||||||
"version": "2.13.0",
|
"version": "2.13.3",
|
||||||
"description": "Tools for making a Nostr client.",
|
"description": "Tools for making a Nostr client.",
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
|
|||||||
12
pool.test.ts
12
pool.test.ts
@@ -119,12 +119,12 @@ test('subscribe many map', async () => {
|
|||||||
|
|
||||||
const [relayA, relayB, relayC] = relayURLs
|
const [relayA, relayB, relayC] = relayURLs
|
||||||
|
|
||||||
pool.subscribeManyMap(
|
pool.subscribeMap(
|
||||||
{
|
[
|
||||||
[relayA]: [{ authors: [pub], kinds: [20001] }],
|
{ url: relayA, filter: { authors: [pub], kinds: [20001] } },
|
||||||
[relayB]: [{ authors: [pub], kinds: [20002] }],
|
{ url: relayB, filter: { authors: [pub], kinds: [20002] } },
|
||||||
[relayC]: [{ kinds: [20003], '#t': ['biloba'] }],
|
{ url: relayC, filter: { kinds: [20003], '#t': ['biloba'] } },
|
||||||
},
|
],
|
||||||
{
|
{
|
||||||
onevent(event: Event) {
|
onevent(event: Event) {
|
||||||
received.push(event)
|
received.push(event)
|
||||||
|
|||||||
7
relay.ts
7
relay.ts
@@ -3,13 +3,6 @@
|
|||||||
import { verifyEvent } from './pure.ts'
|
import { verifyEvent } from './pure.ts'
|
||||||
import { AbstractRelay } from './abstract-relay.ts'
|
import { AbstractRelay } from './abstract-relay.ts'
|
||||||
|
|
||||||
/**
|
|
||||||
* @deprecated use Relay.connect() instead.
|
|
||||||
*/
|
|
||||||
export function relayConnect(url: string): Promise<Relay> {
|
|
||||||
return Relay.connect(url)
|
|
||||||
}
|
|
||||||
|
|
||||||
var _WebSocket: typeof WebSocket
|
var _WebSocket: typeof WebSocket
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|||||||
2
utils.ts
2
utils.ts
@@ -3,6 +3,8 @@ import type { Event } from './core.ts'
|
|||||||
export const utf8Decoder: TextDecoder = new TextDecoder('utf-8')
|
export const utf8Decoder: TextDecoder = new TextDecoder('utf-8')
|
||||||
export const utf8Encoder: TextEncoder = new TextEncoder()
|
export const utf8Encoder: TextEncoder = new TextEncoder()
|
||||||
|
|
||||||
|
export { bytesToHex, hexToBytes } from '@noble/hashes/utils'
|
||||||
|
|
||||||
export function normalizeURL(url: string): string {
|
export function normalizeURL(url: string): string {
|
||||||
try {
|
try {
|
||||||
if (url.indexOf('://') === -1) url = 'wss://' + url
|
if (url.indexOf('://') === -1) url = 'wss://' + url
|
||||||
|
|||||||
Reference in New Issue
Block a user