diff options
| author | Adam Soltys <[email protected]> | 2024-12-24 00:47:08 -0800 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2024-12-24 00:47:12 -0800 |
| commit | 4a9d5e1f40be289970f3dec2006852b160ae8e41 (patch) | |
| tree | f6feb393c91e79048a7ad6ca4c69381d1a1e29f6 /routes/lnurl.ts | |
| parent | 47236bc1ac522aa14e280e1a9664b3bd90e761db (diff) | |
| download | coinos-server-4a9d5e1f40be289970f3dec2006852b160ae8e41.tar.gz coinos-server-4a9d5e1f40be289970f3dec2006852b160ae8e41.zip | |
lnurlw
Diffstat (limited to 'routes/lnurl.ts')
| -rw-r--r-- | routes/lnurl.ts | 51 |
1 files changed, 47 insertions, 4 deletions
diff --git a/routes/lnurl.ts b/routes/lnurl.ts index 11b81273..8aeeac6f 100644 --- a/routes/lnurl.ts +++ b/routes/lnurl.ts @@ -1,9 +1,11 @@ -import { g, s } from "$lib/db"; +import { db, g, s } from "$lib/db"; import { generate } from "$lib/invoices"; +import ln from "$lib/ln"; import { err, warn } from "$lib/logging"; import { serverPubkey } from "$lib/nostr"; +import { credit } from "$lib/payments"; import { types } from "$lib/payments"; -import { bail, fail, getUser } from "$lib/utils"; +import { bail, fail, getInvoice, getUser } from "$lib/utils"; import { bech32 } from "bech32"; import got from "got"; import { v4 } from "uuid"; @@ -137,8 +139,7 @@ export default { const { params: { id }, } = req; - const inv = await g(`invoice:${id}`); - + const inv = await getInvoice(id); if (!inv) return res.send({ status: "ERROR", reason: "Not found" }); const { hash, received, amount, preimage } = inv; @@ -146,4 +147,46 @@ export default { res.send({ pr: hash, status: "OK", settled, preimage: preimage || null }); }, + + async withdraw(req, res) { + try { + const { pr, k1: id } = req.query; + const invoice = await getInvoice(pr); + const { amount_msat } = await ln.decode(pr); + const amount = Math.ceil(amount_msat / 1000); + await db.debit(`fund:${id}`, "", amount, 0, 0, 0); + let p; + if (invoice) { + const { hash } = invoice; + p = await credit({ hash, amount, memo: id, ref: id, type: types.fund }); + } else { + await ln.xpay({ + invstring: pr.replace(/\s/g, "").toLowerCase(), + maxfee: 0, + retry_for: 10, + }); + const rates = await g("rates"); + p = { + id: v4(), + amount: -amount, + hash: pr, + confirmed: true, + rate: rates.USD, + currency: "USD", + type: types.fund, + ref: id, + created: Date.now(), + }; + await s(`payment:${p.id}`, p); + await s(`payment:${pr}`, p); + } + + await db.lPush(`fund:${id}:payments`, p.id); + res.send({ status: "OK" }); + } catch (e) { + console.log(e); + warn("lnurlw failed", e.message); + res.send({ status: "ERROR", reason: "Withdrawal failed" }); + } + }, }; |
