Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add LNURL verify #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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 .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 {
Copy link
Collaborator

Choose a reason for hiding this comment

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

You should not need : Context. In Typescript, it is inferred from the argument type. Are you sure your environment is setup correctly?

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,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why was this added? you cannot have sub-millisats

});

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,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why || null? shouldn't it be omitted from the response body if it is undefined?

Copy link

Choose a reason for hiding this comment

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

not sure what the spec says, but having the key in the response but a blank value makes sense to me.

});
} 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
Loading