Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"editor.formatOnSave": true,
"editor.defaultFormatter": "esbenp.prettier-vscode",
"prettier.trailingComma": "all"
"prettier.trailingComma": "all"
}
19 changes: 19 additions & 0 deletions src/nwc/NWCClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,25 @@ describe("parseWalletConnectUrl", () => {
]);
});
});
describe("parseWalletConnectUrl validation", () => {
test("throws when no relay is provided", () => {
expect(() =>
NWCClient.parseWalletConnectUrl(
"nostr+walletconnect://abc123",
),
).toThrow();
});

test("throws when secret is required but missing", () => {
expect(() =>
new NWCClient({
nostrWalletConnectUrl:
"nostr+walletconnect://abc123?relay=wss://relay.example.com",
}),
).toThrow();
});
});


describe("NWCClient", () => {
test("standard protocol", () => {
Expand Down
38 changes: 37 additions & 1 deletion src/nwc/NWCClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,37 @@ export class NWCClient {
options: NWCOptions;
private _encryptionType: Nip47EncryptionType | undefined;




static validateWalletConnectUrl(
options: NWCOptions,
requireSecret = false,
) {
if (!options.walletPubkey) {
throw new Error("Invalid WalletConnect URL: missing wallet pubkey");
}

if (!options.relayUrls || options.relayUrls.length === 0) {
throw new Error("Invalid WalletConnect URL: no relay URLs provided");
}

for (const relay of options.relayUrls) {
try {
new URL(relay);
} catch {
throw new Error(`Invalid relay URL: ${relay}`);
}
}

if (requireSecret && !options.secret) {
throw new Error(
"Invalid WalletConnect URL: missing secret parameter",
);
}
}


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 "//"
Expand All @@ -107,13 +138,18 @@ export class NWCClient {
if (lud16) {
options.lud16 = lud16;
}
NWCClient.validateWalletConnectUrl(options);
return options;
}

constructor(options?: NewNWCClientOptions) {
if (options && options.nostrWalletConnectUrl) {
const parsed = NWCClient.parseWalletConnectUrl(
options.nostrWalletConnectUrl,
);
NWCClient.validateWalletConnectUrl(parsed, true);
options = {
...NWCClient.parseWalletConnectUrl(options.nostrWalletConnectUrl),
...parsed,
...options,
};
}
Expand Down