Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: parse nwc url without double slash #220

Merged
merged 2 commits into from
May 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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");
});
});
Loading