Skip to content
Closed
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
3 changes: 2 additions & 1 deletion examples/l402.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ if (!nostrWalletConnectUrl) {
const nwc = new NostrWebLNProvider({
nostrWalletConnectUrl,
});
await nwc.enable();
nwc.on("sendPayment", (response) => {
console.info(`payment response:`, response);
});
Expand All @@ -22,4 +23,4 @@ fetchWithL402(url, {}, { wallet: nwc })
.then((data) => {
console.info(data);
nwc.close();
});
});
12 changes: 9 additions & 3 deletions src/l402/l402.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ const memoryStorage = new MemoryStorage();
const HEADER_KEY = "L402";

interface Wallet {
sendPayment(paymentRequest: string): Promise<{ preimage: string }>;
sendPayment?(paymentRequest: string): Promise<{ preimage: string }>;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to keep type safety we can do:

type Wallet = 
  | { sendPayment(paymentRequest: string): Promise<{ preimage: string }> }
  | { payInvoice(paymentRequest: string): Promise<{ preimage: string }> };

payInvoice?(args: { invoice: string }): Promise<{ preimage: string }>;
}

export const fetchWithL402 = async (
Expand All @@ -25,6 +26,9 @@ export const fetchWithL402 = async (
if (!wallet) {
throw new Error("wallet is missing");
}
if (!wallet.sendPayment && !wallet.payInvoice) {
throw new Error("wallet must have a sendPayment or payInvoice function");
}
Comment on lines +29 to +31
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Harden method validation to avoid runtime bind crashes.

Line 29 currently validates truthiness, not callability. A non-function truthy value would pass and fail at Line 60 when calling .bind(...).

Proposed guard fix
-  if (!wallet.sendPayment && !wallet.payInvoice) {
+  if (
+    typeof wallet.sendPayment !== "function" &&
+    typeof wallet.payInvoice !== "function"
+  ) {
     throw new Error("wallet must have a sendPayment or payInvoice function");
   }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/l402/l402.ts` around lines 29 - 31, The current validation checks only
truthiness for wallet.sendPayment and wallet.payInvoice, which allows
non-function values to pass and later cause a crash when calling .bind; update
the check in the l402 module so you verify callability (e.g., typeof
wallet.sendPayment === 'function' or typeof wallet.payInvoice === 'function')
and throw an Error if neither is a function; then ensure the subsequent usage
that calls .bind on the chosen method (the sendPayment/payInvoice reference used
later) only happens after selecting a verified function.

const store = options.store || memoryStorage;
if (!fetchArgs) {
fetchArgs = {};
Expand All @@ -51,9 +55,11 @@ export const fetchWithL402 = async (

const details = parseL402(header);
const token = details.token || details.macaroon;
const inv = details.invoice;
const invoice = details.invoice;

const invResp = await wallet.sendPayment(inv);
const invResp = wallet.sendPayment
? await wallet.sendPayment(invoice)
: await wallet.payInvoice!({ invoice });

store.setItem(
url,
Expand Down
Loading