Skip to content

Commit

Permalink
fix: parse nwc url without double slash
Browse files Browse the repository at this point in the history
  • Loading branch information
rolznz committed May 12, 2024
1 parent 8251d3d commit c6db2a9
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/NWCClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
43 changes: 43 additions & 0 deletions src/webln/NWCClient.test.ts
Original file line number Diff line number Diff line change
@@ -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");
});
});

0 comments on commit c6db2a9

Please sign in to comment.