Skip to content

Commit

Permalink
feat: add LNURL verify
Browse files Browse the repository at this point in the history
  • Loading branch information
im-adithya committed Nov 23, 2024
1 parent c5ccd64 commit ccf1a10
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 15 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
48 changes: 41 additions & 7 deletions src/lnurlp.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Hono } from "hono";
import { Context, Hono } from "hono";
import { nwc } from "npm:@getalby/sdk";
import { logger } from "../src/logger.ts";
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) => {
hono.get("/:username", async (c: Context) => {
try {
const username = c.req.param("username");

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,7 +32,13 @@ export function createLnurlApp(db: DB) {
}
});

hono.get("/:username/callback", async (c) => {
return hono;
}

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

hono.get("/:username/callback", async (c: Context) => {
try {
const username = c.req.param("username");
const amount = c.req.query("amount");
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: Context) => {
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;
}
11 changes: 6 additions & 5 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Hono } from "hono";
import { Context, Hono } from "hono";
import { serveStatic } from "hono/deno";
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,16 +26,17 @@ 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) => {
hono.get("/ping", (c: Context) => {
return c.body("OK");
});

hono.use("/favicon.ico", serveStatic({ path: "./favicon.ico" }));

hono.get("/robots.txt", (c) => {
hono.get("/robots.txt", (c: Context) => {
return c.body("User-agent: *\nDisallow: /", 200);
});

Expand Down
4 changes: 2 additions & 2 deletions src/users.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Hono } from "hono";
import { Context, Hono } from "hono";
import { DOMAIN } from "./constants.ts";
import { DB } from "./db/db.ts";
import { logger } from "./logger.ts";
Expand All @@ -7,7 +7,7 @@ import { NWCPool } from "./nwc/nwcPool.ts";
export function createUsersApp(db: DB, nwcPool: NWCPool) {
const hono = new Hono();

hono.post("/", async (c) => {
hono.post("/", async (c: Context) => {
logger.debug("create user", {});

const createUserRequest: { connectionSecret: string; username?: string } =
Expand Down

0 comments on commit ccf1a10

Please sign in to comment.