Skip to content

Commit

Permalink
Merge pull request #220 from getAlby/fix/parse-nwc-url-no-double-slash
Browse files Browse the repository at this point in the history
fix: parse nwc url without double slash
  • Loading branch information
rolznz authored May 12, 2024
2 parents 8251d3d + e3264ab commit add5ecf
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
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 add5ecf

Please sign in to comment.