diff options
| author | Adam Soltys <[email protected]> | 2025-05-06 13:16:47 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2025-05-06 13:16:53 -0700 |
| commit | 414f8faba5374bcbc06f695db77b304f771cab7a (patch) | |
| tree | 1afd696971424da444195f9de8cfaabc86cb4ac8 /routes | |
| parent | a7417b11f6b943e2ec0ae849a0ec49345fed87f3 (diff) | |
| download | coinos-server-414f8faba5374bcbc06f695db77b304f771cab7a.tar.gz coinos-server-414f8faba5374bcbc06f695db77b304f771cab7a.zip | |
lnurl pay endpoint for latest invoice
Diffstat (limited to 'routes')
| -rw-r--r-- | routes/lnurl.ts | 42 |
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; |
