diff --git a/package.json b/package.json index fa30fa9..67fe225 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@getalby/sdk", - "version": "3.5.0", + "version": "3.5.1", "description": "The SDK to integrate with Nostr Wallet Connect and the Alby API", "repository": "https://github.com/getAlby/js-sdk.git", "bugs": "https://github.com/getAlby/js-sdk/issues", diff --git a/src/NWCClient.ts b/src/NWCClient.ts index a9c6a11..dea38bf 100644 --- a/src/NWCClient.ts +++ b/src/NWCClient.ts @@ -200,9 +200,13 @@ export class NWCClient { options: NWCOptions; static parseWalletConnectUrl(walletConnectUrl: string): NWCOptions { + // makes it possible to parse with URL in the different environments (browser/node/...) + // parses both new and legacy protocols, with or without "//" walletConnectUrl = walletConnectUrl .replace("nostrwalletconnect://", "http://") - .replace("nostr+walletconnect://", "http://"); // makes it possible to parse with URL in the different environments (browser/node/...) + .replace("nostr+walletconnect://", "http://") + .replace("nostrwalletconnect:", "http://") + .replace("nostr+walletconnect:", "http://"); const url = new URL(walletConnectUrl); const relayUrl = url.searchParams.get("relay"); if (!relayUrl) { diff --git a/src/webln/NWCClient.test.ts b/src/webln/NWCClient.test.ts new file mode 100644 index 0000000..55a6961 --- /dev/null +++ b/src/webln/NWCClient.test.ts @@ -0,0 +1,43 @@ +import "websocket-polyfill"; +import { NWCClient } from "../NWCClient"; + +// this has no funds on it, I think ;-) +const exampleNwcUrl = + "nostr+walletconnect://69effe7b49a6dd5cf525bd0905917a5005ffe480b58eeb8e861418cf3ae760d9?relay=wss://relay.getalby.com/v1&secret=e839faf78693765b3833027fefa5a305c78f6965d0a5d2e47a3fcb25aa7cc45b"; + +describe("parseWalletConnectUrl", () => { + test("standard protocol", () => { + const parsed = NWCClient.parseWalletConnectUrl(exampleNwcUrl); + expect(parsed.walletPubkey).toBe( + "69effe7b49a6dd5cf525bd0905917a5005ffe480b58eeb8e861418cf3ae760d9", + ); + expect(parsed.secret).toBe( + "e839faf78693765b3833027fefa5a305c78f6965d0a5d2e47a3fcb25aa7cc45b", + ); + expect(parsed.relayUrl).toBe("wss://relay.getalby.com/v1"); + }); + test("protocol without double slash", () => { + const parsed = NWCClient.parseWalletConnectUrl( + exampleNwcUrl.replace("nostr+walletconnect://", "nostr+walletconnect:"), + ); + expect(parsed.walletPubkey).toBe( + "69effe7b49a6dd5cf525bd0905917a5005ffe480b58eeb8e861418cf3ae760d9", + ); + expect(parsed.secret).toBe( + "e839faf78693765b3833027fefa5a305c78f6965d0a5d2e47a3fcb25aa7cc45b", + ); + expect(parsed.relayUrl).toBe("wss://relay.getalby.com/v1"); + }); + test("legacy protocol without double slash", () => { + const parsed = NWCClient.parseWalletConnectUrl( + exampleNwcUrl.replace("nostr+walletconnect://", "nostrwalletconnect:"), + ); + expect(parsed.walletPubkey).toBe( + "69effe7b49a6dd5cf525bd0905917a5005ffe480b58eeb8e861418cf3ae760d9", + ); + expect(parsed.secret).toBe( + "e839faf78693765b3833027fefa5a305c78f6965d0a5d2e47a3fcb25aa7cc45b", + ); + expect(parsed.relayUrl).toBe("wss://relay.getalby.com/v1"); + }); +});