From 1f2c967b042e2972fe29cc4ad42ef5928e2ff33e Mon Sep 17 00:00:00 2001 From: Kai Date: Sat, 25 Apr 2026 12:04:59 +0200 Subject: [PATCH] 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 --- nip47.test.ts | 17 +++++++++++++++-- nip47.ts | 1 + 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/nip47.test.ts b/nip47.test.ts index a457f38..33128fd 100644 --- a/nip47.test.ts +++ b/nip47.test.ts @@ -8,20 +8,33 @@ describe('parseConnectionString', () => { test('returns pubkey, relay, and secret if connection string has double slash', () => { const connectionString = 'nostr+walletconnect://b889ff5b1513b641e2a139f661a661364979c5beee91842f8f0ef42ab558e9d4?relay=wss%3A%2F%2Frelay.damus.io&secret=71a8c14c1407c113601079c4302dab36460f0ccd0ad506f1f2dc73b5100e4f3c' - const { pubkey, relay, secret } = parseConnectionString(connectionString) + const { pubkey, relay, relays, secret } = parseConnectionString(connectionString) expect(pubkey).toBe('b889ff5b1513b641e2a139f661a661364979c5beee91842f8f0ef42ab558e9d4') expect(relay).toBe('wss://relay.damus.io') + expect(relays).toEqual(['wss://relay.damus.io']) expect(secret).toBe('71a8c14c1407c113601079c4302dab36460f0ccd0ad506f1f2dc73b5100e4f3c') }) test('returns pubkey, relay, and secret if connection string is valid', () => { const connectionString = 'nostr+walletconnect:b889ff5b1513b641e2a139f661a661364979c5beee91842f8f0ef42ab558e9d4?relay=wss%3A%2F%2Frelay.damus.io&secret=71a8c14c1407c113601079c4302dab36460f0ccd0ad506f1f2dc73b5100e4f3c' - const { pubkey, relay, secret } = parseConnectionString(connectionString) + const { pubkey, relay, relays, secret } = parseConnectionString(connectionString) expect(pubkey).toBe('b889ff5b1513b641e2a139f661a661364979c5beee91842f8f0ef42ab558e9d4') expect(relay).toBe('wss://relay.damus.io') + expect(relays).toEqual(['wss://relay.damus.io']) + expect(secret).toBe('71a8c14c1407c113601079c4302dab36460f0ccd0ad506f1f2dc73b5100e4f3c') + }) + + test('returns multiple relays when connection string has multiple relay params', () => { + const connectionString = + 'nostr+walletconnect://b889ff5b1513b641e2a139f661a661364979c5beee91842f8f0ef42ab558e9d4?relay=wss%3A%2F%2Frelay.damus.io&relay=wss%3A%2F%2Fnos.lol&secret=71a8c14c1407c113601079c4302dab36460f0ccd0ad506f1f2dc73b5100e4f3c' + const { pubkey, relay, relays, secret } = parseConnectionString(connectionString) + + expect(pubkey).toBe('b889ff5b1513b641e2a139f661a661364979c5beee91842f8f0ef42ab558e9d4') + expect(relay).toBe('wss://relay.damus.io') + expect(relays).toEqual(['wss://relay.damus.io', 'wss://nos.lol']) expect(secret).toBe('71a8c14c1407c113601079c4302dab36460f0ccd0ad506f1f2dc73b5100e4f3c') }) diff --git a/nip47.ts b/nip47.ts index 7579ae3..98db7ce 100644 --- a/nip47.ts +++ b/nip47.ts @@ -4,6 +4,7 @@ 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