Skip to content

Commit

Permalink
fix: properly follow LNURL spec and add LNURL error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
rolznz committed Oct 8, 2024
1 parent aa342d0 commit eebaea9
Showing 1 changed file with 46 additions and 35 deletions.
81 changes: 46 additions & 35 deletions src/lnurlp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,50 +8,61 @@ import "./nwc/nwcPool.ts";
export function createLnurlApp(db: DB) {
const hono = new Hono();

hono.get("/:username", (c) => {
const username = c.req.param("username");

logger.debug("LNURLp request", { username });

// TODO: zapper support

return c.json({
status: "OK",
tag: "payRequest",
commentAllowed: 255,
callback: `${BASE_URL}/.well-known/lnurlp/${username}/callback`,
minSendable: 1000,
maxSendable: 10000000000,
metadata: `[["text/identifier","${username}@${DOMAIN}"],["text/plain","Sats for ${username}"]]`,
});
hono.get("/:username", async (c) => {
try {
const username = c.req.param("username");

logger.debug("LNURLp request", { username });

// check the user exists
await db.findWalletConnectionSecret(username);

// TODO: zapper support

return c.json({
tag: "payRequest",
commentAllowed: 255,
callback: `${BASE_URL}/.well-known/lnurlp/${username}/callback`,
minSendable: 1000,
maxSendable: 10000000000,
metadata: `[["text/identifier","${username}@${DOMAIN}"],["text/plain","Sats for ${username}"]]`,
});
} catch (error) {
return c.json({ status: "ERROR", reason: "" + error });
}
});

hono.get("/:username/callback", async (c) => {
const username = c.req.param("username");
const amount = c.req.query("amount");
const comment = c.req.query("comment") || "";
logger.debug("LNURLp callback", { username, amount, comment });
try {
const username = c.req.param("username");
const amount = c.req.query("amount");
const comment = c.req.query("comment") || "";
logger.debug("LNURLp callback", { username, amount, comment });

// TODO: store data (e.g. for zaps)
// TODO: store data (e.g. for zaps)

if (!amount) {
return c.text("No amount provided", 404);
}
if (!amount) {
throw new Error("No amount provided");
}

const connectionSecret = await db.findWalletConnectionSecret(username);
const connectionSecret = await db.findWalletConnectionSecret(username);

const nwcClient = new nwc.NWCClient({
nostrWalletConnectUrl: connectionSecret,
});
const nwcClient = new nwc.NWCClient({
nostrWalletConnectUrl: connectionSecret,
});

const transaction = await nwcClient.makeInvoice({
amount: +amount,
description: comment,
});
const transaction = await nwcClient.makeInvoice({
amount: +amount,
description: comment,
});

return c.json({
pr: transaction.invoice,
});
return c.json({
pr: transaction.invoice,
routes: [],
});
} catch (error) {
return c.json({ status: "ERROR", reason: "" + error });
}
});
return hono;
}

0 comments on commit eebaea9

Please sign in to comment.