aboutsummaryrefslogtreecommitdiffstats
path: root/routes
diff options
context:
space:
mode:
Diffstat (limited to 'routes')
-rw-r--r--routes/lnurl.ts42
1 files changed, 41 insertions, 1 deletions
diff --git a/routes/lnurl.ts b/routes/lnurl.ts
index 9f247267..e5b3e60c 100644
--- a/routes/lnurl.ts
+++ b/routes/lnurl.ts
@@ -20,7 +20,9 @@ export default {
query: { address },
} = req;
const [name, domain] = address.split("@");
- const url = `https://${domain}/.well-known/lnurlp/${name.toLowerCase().replace(/\s/g,"")}`;
+ const url = `https://${domain}/.well-known/lnurlp/${name
+ .toLowerCase()
+ .replace(/\s/g, "")}`;
try {
const r = await got(url).json();
@@ -149,6 +151,44 @@ export default {
res.send({ pr: hash, status: "OK", settled, preimage: preimage || null });
},
+ async pay(req, res) {
+ const { username } = req.params;
+ try {
+ const user = await getUser(username);
+ const iid = await db.lIndex(`${user.id}:invoices`, 0);
+ const invoice = await getInvoice(iid);
+ const fiveMinutes = 1000 * 60 * 5;
+ const paid = invoice.received >= invoice.amount;
+ const expired = Date.now() - invoice.created > fiveMinutes;
+ if (paid || expired) fail("No active invoice");
+
+ const { id: uid } = user;
+
+ const metadata = JSON.stringify([
+ ["text/plain", `Paying ${username}@${host}`],
+ ["text/identifier", `${username}@${host}`],
+ ]);
+
+ const id = v4();
+ await s(`lnurl:${id}`, uid);
+
+ res.send({
+ allowsNostr: true,
+ minSendable: invoice.amount * 1000,
+ maxSendable: invoice.amount * 1000,
+ metadata,
+ nostrPubkey: serverPubkey,
+ commentAllowed: 512,
+ callback: `${URL}/api/lnurl/${id}`,
+ tag: "payRequest",
+ });
+ } catch (e) {
+ if (!e.message.includes("found"))
+ warn("problem generating lnurlp request", username, e.message);
+ bail(res, e.message);
+ }
+ },
+
// async withdraw(req, res) {
// try {
// const { pr, k1: id } = req.query;