mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2025-12-08 16:28:49 +00:00
Compare commits
15 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b8f91c37fa | ||
|
|
2da3528362 | ||
|
|
315e9a472c | ||
|
|
a2b1bf0338 | ||
|
|
861a77e2b3 | ||
|
|
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()
|
||||
|
||||
// let's query for an event that exists
|
||||
const event = relay.get(
|
||||
['wss://relay.example.com'],
|
||||
const relays = ['wss://relay.example.com', 'wss://relay.example2.com']
|
||||
|
||||
// let's query for one event that exists
|
||||
const event = pool.get(
|
||||
relays,
|
||||
{
|
||||
ids: ['d7dd5eb3ab747e16f8d0212d53032ea2a7cadef53837e5a6c66d42849fcb9027'],
|
||||
},
|
||||
@@ -78,6 +80,18 @@ if (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 sk = generateSecretKey()
|
||||
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(
|
||||
relays: string[],
|
||||
filter: Filter,
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/* 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 { getHex64, getSubscriptionId } from './fakejson.ts'
|
||||
import { Queue, normalizeURL } from './utils.ts'
|
||||
@@ -12,6 +12,13 @@ export type AbstractRelayConstructorOptions = {
|
||||
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 {
|
||||
public readonly url: string
|
||||
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) {
|
||||
this._connected = false
|
||||
this.connectionPromise = undefined
|
||||
@@ -176,7 +185,7 @@ export class AbstractRelay {
|
||||
switch (data[0]) {
|
||||
case 'EVENT': {
|
||||
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)) {
|
||||
so.onevent(event)
|
||||
}
|
||||
@@ -234,7 +243,7 @@ export class AbstractRelay {
|
||||
}
|
||||
|
||||
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.ws?.send(message)
|
||||
@@ -377,7 +386,15 @@ export class Subscription {
|
||||
if (!this.closed && this.relay.connected) {
|
||||
// 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
|
||||
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.relay.openSubs.delete(this.id)
|
||||
|
||||
4
jsr.json
4
jsr.json
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@nostr/tools",
|
||||
"version": "2.13.0",
|
||||
"version": "2.14.2",
|
||||
"exports": {
|
||||
".": "./index.ts",
|
||||
"./core": "./core.ts",
|
||||
@@ -42,7 +42,9 @@
|
||||
"./nip94": "./nip94.ts",
|
||||
"./nip98": "./nip98.ts",
|
||||
"./nip99": "./nip99.ts",
|
||||
"./nipb7": "./nipb7.ts",
|
||||
"./fakejson": "./fakejson.ts",
|
||||
"./utils": "./utils.ts"
|
||||
"./signer": "./signer.ts"
|
||||
}
|
||||
}
|
||||
|
||||
5
justfile
5
justfile
@@ -12,11 +12,14 @@ test-only file:
|
||||
bun test {{file}}
|
||||
|
||||
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
|
||||
jsr publish --allow-dirty
|
||||
git checkout -- package.json
|
||||
|
||||
# then to npm
|
||||
npm publish
|
||||
|
||||
format:
|
||||
eslint --ext .ts --fix *.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
|
||||
}
|
||||
|
||||
/** @deprecated use isAddressableKind instead */
|
||||
export const isParameterizedReplaceableKind = isAddressableKind
|
||||
|
||||
/** Classification of the event kind. */
|
||||
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 { hexToBytes } from '@noble/hashes/utils'
|
||||
|
||||
const senderPrivateKey = decode(`nsec1p0ht6p3wepe47sjrgesyn4m50m6avk2waqudu9rl324cg2c4ufesyp6rdg`).data as Uint8Array
|
||||
const senderPrivateKey = decode(`nsec1p0ht6p3wepe47sjrgesyn4m50m6avk2waqudu9rl324cg2c4ufesyp6rdg`).data
|
||||
|
||||
const sk1 = hexToBytes('f09ac9b695d0a4c6daa418fe95b977eea20f54d9545592bc36a4f9e14f3eb840')
|
||||
const sk2 = hexToBytes('5393a825e5892d8e18d4a5ea61ced105e8bb2a106f42876be3a40522e0b13747')
|
||||
|
||||
@@ -1,17 +1,14 @@
|
||||
import { test, expect, describe } from 'bun:test'
|
||||
import { generateSecretKey, getPublicKey } from './pure.ts'
|
||||
import { describe, expect, test } from 'bun:test'
|
||||
import {
|
||||
decode,
|
||||
naddrEncode,
|
||||
neventEncode,
|
||||
NostrTypeGuard,
|
||||
nprofileEncode,
|
||||
npubEncode,
|
||||
nsecEncode,
|
||||
neventEncode,
|
||||
type AddressPointer,
|
||||
type ProfilePointer,
|
||||
EventPointer,
|
||||
NostrTypeGuard,
|
||||
nsecEncode
|
||||
} from './nip19.ts'
|
||||
import { generateSecretKey, getPublicKey } from './pure.ts'
|
||||
|
||||
test('encode and decode nsec', () => {
|
||||
let sk = generateSecretKey()
|
||||
@@ -38,7 +35,7 @@ test('encode and decode nprofile', () => {
|
||||
expect(nprofile).toMatch(/nprofile1\w+/)
|
||||
let { type, data } = decode(nprofile)
|
||||
expect(type).toEqual('nprofile')
|
||||
const pointer = data as ProfilePointer
|
||||
const pointer = data
|
||||
expect(pointer.pubkey).toEqual(pk)
|
||||
expect(pointer.relays).toContain(relays[0])
|
||||
expect(pointer.relays).toContain(relays[1])
|
||||
@@ -67,7 +64,7 @@ test('encode and decode naddr', () => {
|
||||
expect(naddr).toMatch(/naddr1\w+/)
|
||||
let { type, data } = decode(naddr)
|
||||
expect(type).toEqual('naddr')
|
||||
const pointer = data as AddressPointer
|
||||
const pointer = data
|
||||
expect(pointer.pubkey).toEqual(pk)
|
||||
expect(pointer.relays).toContain(relays[0])
|
||||
expect(pointer.relays).toContain(relays[1])
|
||||
@@ -86,7 +83,7 @@ test('encode and decode nevent', () => {
|
||||
expect(nevent).toMatch(/nevent1\w+/)
|
||||
let { type, data } = decode(nevent)
|
||||
expect(type).toEqual('nevent')
|
||||
const pointer = data as EventPointer
|
||||
const pointer = data
|
||||
expect(pointer.id).toEqual(pk)
|
||||
expect(pointer.relays).toContain(relays[0])
|
||||
expect(pointer.kind).toEqual(30023)
|
||||
@@ -103,7 +100,7 @@ test('encode and decode nevent with kind 0', () => {
|
||||
expect(nevent).toMatch(/nevent1\w+/)
|
||||
let { type, data } = decode(nevent)
|
||||
expect(type).toEqual('nevent')
|
||||
const pointer = data as EventPointer
|
||||
const pointer = data
|
||||
expect(pointer.id).toEqual(pk)
|
||||
expect(pointer.relays).toContain(relays[0])
|
||||
expect(pointer.kind).toEqual(0)
|
||||
@@ -121,7 +118,7 @@ test('encode and decode naddr with empty "d"', () => {
|
||||
expect(naddr).toMatch(/naddr\w+/)
|
||||
let { type, data } = decode(naddr)
|
||||
expect(type).toEqual('naddr')
|
||||
const pointer = data as AddressPointer
|
||||
const pointer = data
|
||||
expect(pointer.identifier).toEqual('')
|
||||
expect(pointer.relays).toContain(relays[0])
|
||||
expect(pointer.kind).toEqual(3)
|
||||
@@ -133,7 +130,7 @@ test('decode naddr from habla.news', () => {
|
||||
'naddr1qq98yetxv4ex2mnrv4esygrl54h466tz4v0re4pyuavvxqptsejl0vxcmnhfl60z3rth2xkpjspsgqqqw4rsf34vl5',
|
||||
)
|
||||
expect(type).toEqual('naddr')
|
||||
const pointer = data as AddressPointer
|
||||
const pointer = data
|
||||
expect(pointer.pubkey).toEqual('7fa56f5d6962ab1e3cd424e758c3002b8665f7b0d8dcee9fe9e288d7751ac194')
|
||||
expect(pointer.kind).toEqual(30023)
|
||||
expect(pointer.identifier).toEqual('references')
|
||||
@@ -145,7 +142,7 @@ test('decode naddr from go-nostr with different TLV ordering', () => {
|
||||
)
|
||||
|
||||
expect(type).toEqual('naddr')
|
||||
const pointer = data as AddressPointer
|
||||
const pointer = data
|
||||
expect(pointer.pubkey).toEqual('3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d')
|
||||
expect(pointer.relays).toContain('wss://relay.nostr.example.mydomain.example.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):
|
||||
| {
|
||||
type: 'nevent'
|
||||
data: EventPointer
|
||||
}
|
||||
| {
|
||||
type: 'nprofile'
|
||||
data: ProfilePointer
|
||||
}
|
||||
| {
|
||||
type: 'naddr'
|
||||
data: AddressPointer
|
||||
}
|
||||
| {
|
||||
type: 'npub'
|
||||
data: string
|
||||
}
|
||||
| {
|
||||
type: 'nsec'
|
||||
data: Uint8Array
|
||||
}
|
||||
| {
|
||||
type: 'note'
|
||||
data: string
|
||||
} {
|
||||
export type DecodedNevent = {
|
||||
type: 'nevent'
|
||||
data: EventPointer
|
||||
}
|
||||
|
||||
export type DecodedNprofile = {
|
||||
type: 'nprofile'
|
||||
data: ProfilePointer
|
||||
}
|
||||
|
||||
export type DecodedNaddr = {
|
||||
type: 'naddr'
|
||||
data: AddressPointer
|
||||
}
|
||||
|
||||
export type DecodedNsec = {
|
||||
type: 'nsec'
|
||||
data: Uint8Array
|
||||
}
|
||||
|
||||
export type DecodedNpub = {
|
||||
type: 'npub'
|
||||
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 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 type { Event, EventTemplate } from './core.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'
|
||||
|
||||
/**
|
||||
@@ -518,11 +518,11 @@ export async function loadGroupFromCode(pool: AbstractSimplePool, code: string):
|
||||
* @returns A GroupReference object if the code is valid, otherwise null.
|
||||
*/
|
||||
export function parseGroupCode(code: string): null | GroupReference {
|
||||
if (code.startsWith('naddr1')) {
|
||||
if (NostrTypeGuard.isNAddr(code)) {
|
||||
try {
|
||||
let { data } = decode(code)
|
||||
|
||||
let { relays, identifier } = data as AddressPointer
|
||||
let { relays, identifier } = data
|
||||
if (!relays || relays.length === 0) return null
|
||||
|
||||
let host = relays![0]
|
||||
|
||||
3
nip46.ts
3
nip46.ts
@@ -6,6 +6,7 @@ import { NIP05_REGEX } from './nip05.ts'
|
||||
import { SimplePool } from './pool.ts'
|
||||
import { Handlerinformation, NostrConnect } from './kinds.ts'
|
||||
import type { RelayRecord } from './relay.ts'
|
||||
import { Signer } from './signer.ts'
|
||||
|
||||
var _fetch: any
|
||||
|
||||
@@ -82,7 +83,7 @@ export type BunkerSignerParams = {
|
||||
onauth?: (url: string) => void
|
||||
}
|
||||
|
||||
export class BunkerSigner {
|
||||
export class BunkerSigner implements Signer {
|
||||
private params: BunkerSignerParams
|
||||
private pool: AbstractSimplePool
|
||||
private subCloser: SubCloser | undefined
|
||||
|
||||
2
nip47.ts
2
nip47.ts
@@ -32,7 +32,7 @@ export async function makeNwcRequestEvent(
|
||||
invoice,
|
||||
},
|
||||
}
|
||||
const encryptedContent = await encrypt(secretKey, pubkey, JSON.stringify(content))
|
||||
const encryptedContent = encrypt(secretKey, pubkey, JSON.stringify(content))
|
||||
const eventTemplate = {
|
||||
kind: NWCWalletRequest,
|
||||
created_at: Math.round(Date.now() / 1000),
|
||||
|
||||
@@ -1,7 +1,14 @@
|
||||
import { describe, test, expect, mock } from 'bun:test'
|
||||
import { finalizeEvent } 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'
|
||||
|
||||
describe('getZapEndpoint', () => {
|
||||
@@ -317,3 +324,26 @@ describe('makeZapReceipt', () => {
|
||||
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)) {
|
||||
const a = ['a', `${event.kind}:${event.pubkey}:`]
|
||||
zr.tags.push(a)
|
||||
// addressable event
|
||||
// addressable event
|
||||
} else if (isAddressableKind(event.kind)) {
|
||||
let d = event.tags.find(([t, v]) => t === 'd' && v)
|
||||
if (!d) throw new Error('d tag not found or is empty')
|
||||
@@ -142,3 +142,52 @@ export function makeZapReceipt({
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
55
nipb7.test.ts
Normal file
55
nipb7.test.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
import { test, expect } from 'bun:test'
|
||||
import { BlossomClient } from './nipb7.ts'
|
||||
import { sha256 } from '@noble/hashes/sha256'
|
||||
import { bytesToHex } from './utils.ts'
|
||||
import { PlainKeySigner } from './signer.ts'
|
||||
import { generateSecretKey } from './pure.ts'
|
||||
|
||||
test('blossom', async () => {
|
||||
const BLOSSOM_SERVER = 'blossom.primal.net'
|
||||
const TEST_CONTENT = 'hello world'
|
||||
const TEST_BLOB = new Blob([TEST_CONTENT], { type: 'text/plain' })
|
||||
|
||||
const expectedHash = bytesToHex(sha256(new TextEncoder().encode(TEST_CONTENT)))
|
||||
|
||||
const signer = new PlainKeySigner(generateSecretKey())
|
||||
const client = new BlossomClient(BLOSSOM_SERVER, signer)
|
||||
expect(client).toBeDefined()
|
||||
|
||||
// check for non-existent file should throw
|
||||
const invalidHash = expectedHash.slice(0, 62) + 'ba'
|
||||
let hasThrown = false
|
||||
try {
|
||||
await client.check(invalidHash)
|
||||
} catch (err) {
|
||||
hasThrown = true
|
||||
}
|
||||
expect(hasThrown).toBeTrue()
|
||||
|
||||
// upload hello world blob
|
||||
const descriptor = await client.uploadBlob(TEST_BLOB, 'text/plain')
|
||||
expect(descriptor).toBeDefined()
|
||||
expect(descriptor.sha256).toBe(expectedHash)
|
||||
expect(descriptor.size).toBe(TEST_CONTENT.length)
|
||||
expect(descriptor.type).toBe('text/plain')
|
||||
expect(descriptor.url).toContain(expectedHash)
|
||||
expect(descriptor.uploaded).toBeGreaterThan(0)
|
||||
await client.check(expectedHash)
|
||||
|
||||
// download and verify
|
||||
const downloadedBuffer = await client.download(expectedHash)
|
||||
const downloadedContent = new TextDecoder().decode(downloadedBuffer)
|
||||
expect(downloadedContent).toBe(TEST_CONTENT)
|
||||
|
||||
// list blobs should include our uploaded file
|
||||
const blobs = await client.list()
|
||||
|
||||
expect(Array.isArray(blobs)).toBe(true)
|
||||
const ourBlob = blobs.find(blob => blob.sha256 === expectedHash)
|
||||
expect(ourBlob).toBeDefined()
|
||||
expect(ourBlob?.type).toBe('text/plain')
|
||||
expect(ourBlob?.size).toBe(TEST_CONTENT.length)
|
||||
|
||||
// delete
|
||||
await client.delete(expectedHash)
|
||||
})
|
||||
203
nipb7.ts
Normal file
203
nipb7.ts
Normal file
@@ -0,0 +1,203 @@
|
||||
import { sha256 } from '@noble/hashes/sha256'
|
||||
import { EventTemplate } from './core.ts'
|
||||
import { Signer } from './signer.ts'
|
||||
import { bytesToHex } from './utils.ts'
|
||||
|
||||
export type BlobDescriptor = {
|
||||
url: string
|
||||
sha256: string
|
||||
size: number
|
||||
type: string
|
||||
uploaded: number
|
||||
}
|
||||
|
||||
export class BlossomClient {
|
||||
private mediaserver: string
|
||||
private signer: Signer
|
||||
|
||||
constructor(mediaserver: string, signer: Signer) {
|
||||
if (!mediaserver.startsWith('http')) {
|
||||
mediaserver = 'https://' + mediaserver
|
||||
}
|
||||
this.mediaserver = mediaserver.replace(/\/$/, '') + '/'
|
||||
this.signer = signer
|
||||
}
|
||||
|
||||
private async httpCall(
|
||||
method: string,
|
||||
url: string,
|
||||
contentType?: string,
|
||||
addAuthorization?: () => Promise<string>,
|
||||
body?: File | Blob,
|
||||
result?: any,
|
||||
): Promise<any> {
|
||||
const headers: { [_: string]: string } = {}
|
||||
|
||||
if (contentType) {
|
||||
headers['Content-Type'] = contentType
|
||||
}
|
||||
|
||||
if (addAuthorization) {
|
||||
const auth = await addAuthorization()
|
||||
if (auth) {
|
||||
headers['Authorization'] = auth
|
||||
}
|
||||
}
|
||||
|
||||
const response = await fetch(this.mediaserver + url, {
|
||||
method,
|
||||
headers,
|
||||
body,
|
||||
})
|
||||
|
||||
if (response.status >= 300) {
|
||||
const reason = response.headers.get('X-Reason') || response.statusText
|
||||
throw new Error(`${url} returned an error (${response.status}): ${reason}`)
|
||||
}
|
||||
|
||||
if (result !== null && response.headers.get('content-type')?.includes('application/json')) {
|
||||
return await response.json()
|
||||
}
|
||||
|
||||
return response
|
||||
}
|
||||
|
||||
private async authorizationHeader(modify?: (event: EventTemplate) => void): Promise<string> {
|
||||
const now = Math.floor(Date.now() / 1000)
|
||||
const event: EventTemplate = {
|
||||
created_at: now,
|
||||
kind: 24242,
|
||||
content: 'blossom stuff',
|
||||
tags: [['expiration', String(now + 60)]],
|
||||
}
|
||||
|
||||
if (modify) {
|
||||
modify(event)
|
||||
}
|
||||
|
||||
try {
|
||||
const signedEvent = await this.signer.signEvent(event)
|
||||
const eventJson = JSON.stringify(signedEvent)
|
||||
return 'Nostr ' + btoa(eventJson)
|
||||
} catch (error) {
|
||||
return ''
|
||||
}
|
||||
}
|
||||
|
||||
private isValid32ByteHex(hash: string): boolean {
|
||||
return /^[a-f0-9]{64}$/i.test(hash)
|
||||
}
|
||||
|
||||
async check(hash: string): Promise<void> {
|
||||
if (!this.isValid32ByteHex(hash)) {
|
||||
throw new Error(`${hash} is not a valid 32-byte hex string`)
|
||||
}
|
||||
|
||||
try {
|
||||
await this.httpCall('HEAD', hash)
|
||||
} catch (error) {
|
||||
throw new Error(`failed to check for ${hash}: ${error}`)
|
||||
}
|
||||
}
|
||||
|
||||
async uploadBlob(file: File | Blob, contentType?: string): Promise<BlobDescriptor> {
|
||||
const hash = bytesToHex(sha256(new Uint8Array(await file.arrayBuffer())))
|
||||
const actualContentType = contentType || file.type || 'application/octet-stream'
|
||||
|
||||
const bd = await this.httpCall(
|
||||
'PUT',
|
||||
'upload',
|
||||
actualContentType,
|
||||
() =>
|
||||
this.authorizationHeader(evt => {
|
||||
evt.tags.push(['t', 'upload'])
|
||||
evt.tags.push(['x', hash])
|
||||
}),
|
||||
file,
|
||||
{},
|
||||
)
|
||||
|
||||
return bd
|
||||
}
|
||||
|
||||
async uploadFile(file: File): Promise<BlobDescriptor> {
|
||||
return this.uploadBlob(file, file.type)
|
||||
}
|
||||
|
||||
async download(hash: string): Promise<ArrayBuffer> {
|
||||
if (!this.isValid32ByteHex(hash)) {
|
||||
throw new Error(`${hash} is not a valid 32-byte hex string`)
|
||||
}
|
||||
|
||||
const authHeader = await this.authorizationHeader(evt => {
|
||||
evt.tags.push(['t', 'get'])
|
||||
evt.tags.push(['x', hash])
|
||||
})
|
||||
|
||||
const response = await fetch(this.mediaserver + hash, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
Authorization: authHeader,
|
||||
},
|
||||
})
|
||||
|
||||
if (response.status >= 300) {
|
||||
throw new Error(`${hash} is not present in ${this.mediaserver}: ${response.status}`)
|
||||
}
|
||||
|
||||
return await response.arrayBuffer()
|
||||
}
|
||||
|
||||
async downloadAsBlob(hash: string): Promise<Blob> {
|
||||
const arrayBuffer = await this.download(hash)
|
||||
return new Blob([arrayBuffer])
|
||||
}
|
||||
|
||||
async list(): Promise<BlobDescriptor[]> {
|
||||
const pubkey = await this.signer.getPublicKey()
|
||||
|
||||
if (!this.isValid32ByteHex(pubkey)) {
|
||||
throw new Error(`pubkey ${pubkey} is not valid`)
|
||||
}
|
||||
|
||||
try {
|
||||
const bds = await this.httpCall(
|
||||
'GET',
|
||||
`list/${pubkey}`,
|
||||
undefined,
|
||||
() =>
|
||||
this.authorizationHeader(evt => {
|
||||
evt.tags.push(['t', 'list'])
|
||||
}),
|
||||
undefined,
|
||||
[],
|
||||
)
|
||||
return bds
|
||||
} catch (error) {
|
||||
throw new Error(`failed to list blobs: ${error}`)
|
||||
}
|
||||
}
|
||||
|
||||
async delete(hash: string): Promise<void> {
|
||||
if (!this.isValid32ByteHex(hash)) {
|
||||
throw new Error(`${hash} is not a valid 32-byte hex string`)
|
||||
}
|
||||
|
||||
try {
|
||||
await this.httpCall(
|
||||
'DELETE',
|
||||
hash,
|
||||
undefined,
|
||||
() =>
|
||||
this.authorizationHeader(evt => {
|
||||
evt.tags.push(['t', 'delete'])
|
||||
evt.tags.push(['x', hash])
|
||||
}),
|
||||
undefined,
|
||||
null,
|
||||
)
|
||||
} catch (error) {
|
||||
throw new Error(`failed to delete ${hash}: ${error}`)
|
||||
}
|
||||
}
|
||||
}
|
||||
12
package.json
12
package.json
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"type": "module",
|
||||
"name": "nostr-tools",
|
||||
"version": "2.13.0",
|
||||
"version": "2.14.2",
|
||||
"description": "Tools for making a Nostr client.",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
@@ -213,11 +213,21 @@
|
||||
"require": "./lib/cjs/nip99.js",
|
||||
"types": "./lib/types/nip99.d.ts"
|
||||
},
|
||||
"./nipb7": {
|
||||
"import": "./lib/esm/nipb7.js",
|
||||
"require": "./lib/cjs/nipb7.js",
|
||||
"types": "./lib/types/nipb7.d.ts"
|
||||
},
|
||||
"./fakejson": {
|
||||
"import": "./lib/esm/fakejson.js",
|
||||
"require": "./lib/cjs/fakejson.js",
|
||||
"types": "./lib/types/fakejson.d.ts"
|
||||
},
|
||||
"./signer": {
|
||||
"import": "./lib/esm/signer.js",
|
||||
"require": "./lib/cjs/signer.js",
|
||||
"types": "./lib/types/signer.d.ts"
|
||||
},
|
||||
"./utils": {
|
||||
"import": "./lib/esm/utils.js",
|
||||
"require": "./lib/cjs/utils.js",
|
||||
|
||||
12
pool.test.ts
12
pool.test.ts
@@ -119,12 +119,12 @@ test('subscribe many map', async () => {
|
||||
|
||||
const [relayA, relayB, relayC] = relayURLs
|
||||
|
||||
pool.subscribeManyMap(
|
||||
{
|
||||
[relayA]: [{ authors: [pub], kinds: [20001] }],
|
||||
[relayB]: [{ authors: [pub], kinds: [20002] }],
|
||||
[relayC]: [{ kinds: [20003], '#t': ['biloba'] }],
|
||||
},
|
||||
pool.subscribeMap(
|
||||
[
|
||||
{ url: relayA, filter: { authors: [pub], kinds: [20001] } },
|
||||
{ url: relayB, filter: { authors: [pub], kinds: [20002] } },
|
||||
{ url: relayC, filter: { kinds: [20003], '#t': ['biloba'] } },
|
||||
],
|
||||
{
|
||||
onevent(event: Event) {
|
||||
received.push(event)
|
||||
|
||||
7
relay.ts
7
relay.ts
@@ -3,13 +3,6 @@
|
||||
import { verifyEvent } from './pure.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
|
||||
|
||||
try {
|
||||
|
||||
23
signer.ts
Normal file
23
signer.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { EventTemplate, VerifiedEvent } from './core.ts'
|
||||
import { finalizeEvent, getPublicKey } from './pure.ts'
|
||||
|
||||
export interface Signer {
|
||||
getPublicKey(): Promise<string>
|
||||
signEvent(event: EventTemplate): Promise<VerifiedEvent>
|
||||
}
|
||||
|
||||
export class PlainKeySigner implements Signer {
|
||||
private secretKey: Uint8Array
|
||||
|
||||
constructor(secretKey: Uint8Array) {
|
||||
this.secretKey = secretKey
|
||||
}
|
||||
|
||||
async getPublicKey(): Promise<string> {
|
||||
return getPublicKey(this.secretKey)
|
||||
}
|
||||
|
||||
async signEvent(event: EventTemplate): Promise<VerifiedEvent> {
|
||||
return finalizeEvent(event, this.secretKey)
|
||||
}
|
||||
}
|
||||
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 utf8Encoder: TextEncoder = new TextEncoder()
|
||||
|
||||
export { bytesToHex, hexToBytes } from '@noble/hashes/utils'
|
||||
|
||||
export function normalizeURL(url: string): string {
|
||||
try {
|
||||
if (url.indexOf('://') === -1) url = 'wss://' + url
|
||||
|
||||
Reference in New Issue
Block a user