Files
nostr-tools/nip47.ts
T
Kaiandfiatjaf_ 8be6d08a43 fix(nip47): support multiple relays in parseConnectionString
Addresses #494. The NIP-47 spec allows multiple relay parameters in
connection strings, but parseConnectionString only returned the first one.

Changes:
- Add 'relays' field to NWCConnection interface (string array)
- Use searchParams.getAll('relay') to capture all relays
- Keep 'relay' field for backwards compatibility (returns first relay)

This is backwards compatible - existing code using connection.relay
will continue to work, while new code can use connection.relays to
access all specified relays.
2026-03-27 07:02:47 -03:00

46 lines
1.2 KiB
TypeScript

import { type VerifiedEvent, finalizeEvent } from './pure.ts'
import { NWCWalletRequest } from './kinds.ts'
import { encrypt } from './nip04.ts'
interface NWCConnection {
pubkey: string
relay: string
relays: string[]
secret: string
}
export function parseConnectionString(connectionString: string): NWCConnection {
const { host, pathname, searchParams } = new URL(connectionString)
const pubkey = pathname || host
const relays = searchParams.getAll('relay')
const secret = searchParams.get('secret')
if (!pubkey || relays.length === 0 || !secret) {
throw new Error('invalid connection string')
}
return { pubkey, relay: relays[0], relays, secret }
}
export async function makeNwcRequestEvent(
pubkey: string,
secretKey: Uint8Array,
invoice: string,
): Promise<VerifiedEvent> {
const content = {
method: 'pay_invoice',
params: {
invoice,
},
}
const encryptedContent = encrypt(secretKey, pubkey, JSON.stringify(content))
const eventTemplate = {
kind: NWCWalletRequest,
created_at: Math.round(Date.now() / 1000),
content: encryptedContent,
tags: [['p', pubkey]],
}
return finalizeEvent(eventTemplate, secretKey)
}