Skip to content

Commit

Permalink
Merge pull request #10 from getAlby/do-not-trust
Browse files Browse the repository at this point in the history
feat: add LNURL verify
  • Loading branch information
im-adithya authored Dec 9, 2024
2 parents c5ccd64 + eaa1ea9 commit 10a2416
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
LOG_LEVEL=debug
LOG_LEVEL=DEBUG
BASE_URL=http://localhost:8080
DATABASE_URL=postgresql://myuser:mypass@localhost:5432/alby_lite
# generate using deno task db:generate:key
Expand Down
42 changes: 38 additions & 4 deletions src/lnurlp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { BASE_URL, DOMAIN } from "./constants.ts";
import { DB } from "./db/db.ts";
import "./nwc/nwcPool.ts";

export function createLnurlApp(db: DB) {
export function createLnurlWellKnownApp(db: DB) {
const hono = new Hono();

hono.get("/:username", async (c) => {
Expand All @@ -22,7 +22,7 @@ export function createLnurlApp(db: DB) {
return c.json({
tag: "payRequest",
commentAllowed: 255,
callback: `${BASE_URL}/.well-known/lnurlp/${username}/callback`,
callback: `${BASE_URL}/lnurlp/${username}/callback`,
minSendable: 1000,
maxSendable: 10000000000,
metadata: `[["text/identifier","${username}@${DOMAIN}"],["text/plain","Sats for ${username}"]]`,
Expand All @@ -32,6 +32,12 @@ export function createLnurlApp(db: DB) {
}
});

return hono;
}

export function createLnurlApp(db: DB) {
const hono = new Hono();

hono.get("/:username/callback", async (c) => {
try {
const username = c.req.param("username");
Expand All @@ -52,17 +58,45 @@ export function createLnurlApp(db: DB) {
});

const transaction = await nwcClient.makeInvoice({
amount: +amount,
amount: Math.floor(+amount / 1000) * 1000,
description: comment,
});

return c.json({
pr: transaction.invoice,
verify: `${BASE_URL}/lnurlp/${username}/verify/${transaction.payment_hash}`,
routes: [],
pr: transaction.invoice,
});
} catch (error) {
return c.json({ status: "ERROR", reason: "" + error });
}
});

hono.get("/:username/verify/:payment_hash", async (c) => {
try {
const username = c.req.param("username");
const paymentHash = c.req.param("payment_hash");
logger.debug("LNURLp verify", { username, paymentHash });

const connectionSecret = await db.findWalletConnectionSecret(username);

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

const transaction = await nwcClient.lookupInvoice({
payment_hash: paymentHash,
});

return c.json({
settled: !!transaction.settled_at,
preimage: transaction.preimage || null,
pr: transaction.invoice,
});
} catch (error) {
return c.json({ status: "ERROR", reason: "" + error });
}
});

return hono;
}
5 changes: 3 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { secureHeaders } from "hono/secure-headers";
//import { sentry } from "npm:@hono/sentry";
import { PORT } from "./constants.ts";
import { DB, runMigration } from "./db/db.ts";
import { createLnurlApp } from "./lnurlp.ts";
import { createLnurlApp, createLnurlWellKnownApp } from "./lnurlp.ts";
import { LOG_LEVEL, logger, loggerMiddleware } from "./logger.ts";
import { NWCPool } from "./nwc/nwcPool.ts";
import { createUsersApp } from "./users.ts";
Expand All @@ -26,7 +26,8 @@ hono.use(secureHeaders());
hono.use("*", sentry({ dsn: SENTRY_DSN }));
}*/

hono.route("/.well-known/lnurlp", createLnurlApp(db));
hono.route("/.well-known/lnurlp", createLnurlWellKnownApp(db));
hono.route("/lnurlp", createLnurlApp(db));
hono.route("/users", createUsersApp(db, nwcPool));

hono.get("/ping", (c) => {
Expand Down

0 comments on commit 10a2416

Please sign in to comment.