Files
nostr-tools/nip47.ts
Kai 1f2c967b04 feat(nip47): support multiple relays in parseConnectionString
NIP-47 connection strings can specify multiple relay params, but
parseConnectionString only returned the first one.

Adds a 'relays' field (string[]) to NWCConnection while keeping the
existing 'relay' field (deprecated) for backward compatibility.

Fixes #494
2026-04-25 13:41:06 -03:00

47 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
/** @deprecated Use `relays` instead. This returns only the first relay. */
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)
}