Skip to content

Commit add5ecf

Browse files
authored
Merge pull request #220 from getAlby/fix/parse-nwc-url-no-double-slash
fix: parse nwc url without double slash
2 parents 8251d3d + e3264ab commit add5ecf

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@getalby/sdk",
3-
"version": "3.5.0",
3+
"version": "3.5.1",
44
"description": "The SDK to integrate with Nostr Wallet Connect and the Alby API",
55
"repository": "https://github.com/getAlby/js-sdk.git",
66
"bugs": "https://github.com/getAlby/js-sdk/issues",

src/NWCClient.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,13 @@ export class NWCClient {
200200
options: NWCOptions;
201201

202202
static parseWalletConnectUrl(walletConnectUrl: string): NWCOptions {
203+
// makes it possible to parse with URL in the different environments (browser/node/...)
204+
// parses both new and legacy protocols, with or without "//"
203205
walletConnectUrl = walletConnectUrl
204206
.replace("nostrwalletconnect://", "http://")
205-
.replace("nostr+walletconnect://", "http://"); // makes it possible to parse with URL in the different environments (browser/node/...)
207+
.replace("nostr+walletconnect://", "http://")
208+
.replace("nostrwalletconnect:", "http://")
209+
.replace("nostr+walletconnect:", "http://");
206210
const url = new URL(walletConnectUrl);
207211
const relayUrl = url.searchParams.get("relay");
208212
if (!relayUrl) {

src/webln/NWCClient.test.ts

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import "websocket-polyfill";
2+
import { NWCClient } from "../NWCClient";
3+
4+
// this has no funds on it, I think ;-)
5+
const exampleNwcUrl =
6+
"nostr+walletconnect://69effe7b49a6dd5cf525bd0905917a5005ffe480b58eeb8e861418cf3ae760d9?relay=wss://relay.getalby.com/v1&secret=e839faf78693765b3833027fefa5a305c78f6965d0a5d2e47a3fcb25aa7cc45b";
7+
8+
describe("parseWalletConnectUrl", () => {
9+
test("standard protocol", () => {
10+
const parsed = NWCClient.parseWalletConnectUrl(exampleNwcUrl);
11+
expect(parsed.walletPubkey).toBe(
12+
"69effe7b49a6dd5cf525bd0905917a5005ffe480b58eeb8e861418cf3ae760d9",
13+
);
14+
expect(parsed.secret).toBe(
15+
"e839faf78693765b3833027fefa5a305c78f6965d0a5d2e47a3fcb25aa7cc45b",
16+
);
17+
expect(parsed.relayUrl).toBe("wss://relay.getalby.com/v1");
18+
});
19+
test("protocol without double slash", () => {
20+
const parsed = NWCClient.parseWalletConnectUrl(
21+
exampleNwcUrl.replace("nostr+walletconnect://", "nostr+walletconnect:"),
22+
);
23+
expect(parsed.walletPubkey).toBe(
24+
"69effe7b49a6dd5cf525bd0905917a5005ffe480b58eeb8e861418cf3ae760d9",
25+
);
26+
expect(parsed.secret).toBe(
27+
"e839faf78693765b3833027fefa5a305c78f6965d0a5d2e47a3fcb25aa7cc45b",
28+
);
29+
expect(parsed.relayUrl).toBe("wss://relay.getalby.com/v1");
30+
});
31+
test("legacy protocol without double slash", () => {
32+
const parsed = NWCClient.parseWalletConnectUrl(
33+
exampleNwcUrl.replace("nostr+walletconnect://", "nostrwalletconnect:"),
34+
);
35+
expect(parsed.walletPubkey).toBe(
36+
"69effe7b49a6dd5cf525bd0905917a5005ffe480b58eeb8e861418cf3ae760d9",
37+
);
38+
expect(parsed.secret).toBe(
39+
"e839faf78693765b3833027fefa5a305c78f6965d0a5d2e47a3fcb25aa7cc45b",
40+
);
41+
expect(parsed.relayUrl).toBe("wss://relay.getalby.com/v1");
42+
});
43+
});

0 commit comments

Comments
 (0)