diff options
| author | Adam Soltys <[email protected]> | 2022-09-24 16:38:51 +0000 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2022-09-24 16:38:51 +0000 |
| commit | 5a53862a886c5a384c24e9bdd1966be66c255e93 (patch) | |
| tree | 163fb96b67f77ffbe68b94c8414ec3a12bf13369 | |
| parent | 9abcd851edbf3c262b2e3fd9a5340d3aefc5075d (diff) | |
| download | coinos-server-5a53862a886c5a384c24e9bdd1966be66c255e93.tar.gz coinos-server-5a53862a886c5a384c24e9bdd1966be66c255e93.zip | |
capture less recipient fields
| -rw-r--r-- | routes/lnurl.js | 117 |
1 files changed, 67 insertions, 50 deletions
diff --git a/routes/lnurl.js b/routes/lnurl.js index fc2e5a52..d5c03bf1 100644 --- a/routes/lnurl.js +++ b/routes/lnurl.js @@ -19,7 +19,7 @@ import { createInvoice, decodePaymentRequest, getPayments } from "lightning"; import { computeConversionFee, - conversionFeeReceiver + conversionFeeReceiver, } from "./lightning/conversionFee.js"; import send from "$lib/send"; @@ -36,8 +36,8 @@ app.get("/url", async (req, res, next) => { try { const code = await db.Code.findOne({ where: { - code: req.query.code - } + code: req.query.code, + }, }); if (code) { @@ -56,7 +56,7 @@ app.get("/withdraw", auth, async (req, res, next) => { const result = await lnurlServer.generateNewUrl("withdrawRequest", { minWithdrawable: min * 1000, maxWithdrawable: max * 1000, - defaultDescription: "coinos voucher" + defaultDescription: "coinos voucher", }); withdrawals[result.secret] = req.user; @@ -74,8 +74,8 @@ app.post("/code", async (req, res, next) => { const code = await db.Code.findOrCreate({ where: { code: `lnurl:${encoded.substr(-32)}`, - text: encoded - } + text: encoded, + }, }); res.send(code); @@ -89,7 +89,7 @@ app.post("/withdraw", auth, async (req, res, next) => { const { user } = req; const { amount: value, - params: { callback, k1 } + params: { callback, k1 }, } = req.body; let invoice; @@ -112,7 +112,7 @@ app.post("/withdraw", auth, async (req, res, next) => { currency: user.currency, amount: value, tip: 0, - network: "bitcoin" + network: "bitcoin", }); try { @@ -129,8 +129,8 @@ app.get("/lnurlp/:username", async (req, res, next) => { const { username } = req.params; let user = await db.User.findOne({ where: { - username - } + username, + }, }); if (!user) throw new Error("user not found"); @@ -145,11 +145,16 @@ app.get("/lnurlp/:username", async (req, res, next) => { maxSendable, metadata: JSON.stringify([ ["text/plain", `paying ${user.username}`], - ["text/identifier", `${user.username}@coinos.io`] - ]) + ["text/identifier", `${user.username}@coinos.io`], + ]), }); - recipients[result.secret] = user; + recipients[result.secret] = { + id: user.id, + currency: user.currency, + username, + }; + l("recipient", user.username, result.secret); result = await axios.get(result.url); @@ -165,8 +170,8 @@ app.get("/pay/:username", async (req, res, next) => { const { username } = req.params; let user = await db.User.findOne({ where: { - username - } + username, + }, }); let { amount, minSendable, maxSendable } = req.query; @@ -178,10 +183,18 @@ app.get("/pay/:username", async (req, res, next) => { const result = await lnurlServer.generateNewUrl("payRequest", { minSendable, maxSendable, - metadata: JSON.stringify([["text/plain", `paying ${user.username}`]]) + metadata: JSON.stringify([ + ["text/plain", `paying ${user.username}`], + ["text/identifier", `${user.username}@coinos.io`], + ]), }); - recipients[result.secret] = user; + recipients[result.secret] = { + id: user.id, + currency: user.currency, + username, + }; + l("recipient", user.username, result.secret); res.send(result); } catch (e) { @@ -195,11 +208,12 @@ app.post("/pay", auth, async (req, res, next) => { const { amount, comment, - params: { callback, k1 } + params: { callback, k1 }, } = req.body; - let url = `${callback}${callback.includes("?") ? "&" : "?"}amount=${amount * - 1000}${comment ? "&comment=" + comment : ""}`; + let url = `${callback}${callback.includes("?") ? "&" : "?"}amount=${ + amount * 1000 + }${comment ? "&comment=" + comment : ""}`; try { const parts = callback.split("/"); @@ -211,7 +225,7 @@ app.post("/pay", auth, async (req, res, next) => { { amount, memo: comment, - username: recipients[secret].username + username: recipients[secret].username, }, req.hostname, user @@ -265,7 +279,7 @@ app.get("/decode", async (req, res, next) => { .split("/")[0] .split("@") .slice(-1)[0] - .split(":")[0] + .split(":")[0], }); } @@ -278,7 +292,7 @@ app.get("/decode", async (req, res, next) => { } }); -lnurlServer.on("payRequest:action:processed", async function(event) { +lnurlServer.on("payRequest:action:processed", async function (event) { const { secret, params, result } = event; const { id, invoice } = result; const recipient = recipients[secret]; @@ -292,7 +306,7 @@ lnurlServer.on("payRequest:action:processed", async function(event) { currency: recipient.currency, amount: payreq.satoshis, tip: 0, - network: "lightning" + network: "lightning", }); l("invoice created", i.text, i.amount); @@ -319,8 +333,8 @@ lnurlServer.bindToHook( where: { user_id, asset: config.liquid.btcasset, - pubkey: null - } + pubkey: null, + }, }); if (account.balance < amount) { @@ -338,9 +352,9 @@ lnurlServer.bindToHook( include: [ { model: db.User, - as: "user" - } - ] + as: "user", + }, + ], }); if (keyObj) ({ user } = keyObj); @@ -356,7 +370,7 @@ lnurlServer.bindToHook( user = await register( { username, - password: key + password: key, }, ip ); @@ -381,35 +395,38 @@ lnurlServer.bindToHook( if (config.lna.clightning) { let { msatoshi } = await ln.decodepay(pr); - amount = parseInt(msatoshi/1000); + amount = parseInt(msatoshi / 1000); } else { - ({ num_satoshis: amount } = await decodePaymentRequest({ lnd, request: pr })); - } + ({ num_satoshis: amount } = await decodePaymentRequest({ + lnd, + request: pr, + })); + } let conversionFee = computeConversionFee(amount); - await db.transaction(async transaction => { + await db.transaction(async (transaction) => { let account = await db.Account.findOne({ where: { user_id: user.id, - asset: config.liquid.btcasset + asset: config.liquid.btcasset, }, lock: transaction.LOCK.UPDATE, - transaction + transaction, }); // account that receives conversion fees let receiverAccount = await db.Account.findOne({ where: { - "$user.username$": conversionFeeReceiver + "$user.username$": conversionFeeReceiver, }, include: [ { model: db.User, - as: "user" - } + as: "user", + }, ], lock: transaction.LOCK.UPDATE, - transaction + transaction, }); let conversionFeeDeduction = Math.min( @@ -449,7 +466,7 @@ lnurlServer.bindToHook( currency: receiverAccount.user.currency, confirmed: true, received: true, - network: "COINOS" + network: "COINOS", }, { transaction } ); @@ -466,7 +483,7 @@ lnurlServer.bindToHook( currency: user.currency, confirmed: true, network: "lightning", - fee_payment_id + fee_payment_id, }, { transaction } ); @@ -475,10 +492,10 @@ lnurlServer.bindToHook( try { let { payments } = await ln.getPayments({ lnd, - limit: 5 + limit: 5, }); - let p = payments.find(p => p.request === pr); + let p = payments.find((p) => p.request === pr); if (p) { l("found payment", pr); payment.fee = p.fee; @@ -522,28 +539,28 @@ lnurlServer.bindToHook( } ); -lnurlServer.bindToHook("login", async key => { +lnurlServer.bindToHook("login", async (key) => { try { if (!key) throw new Error("login key not defined"); const exists = await db.Key.findOne({ where: { hex: key }, - include: [{ model: db.User, as: "user" }] + include: [{ model: db.User, as: "user" }], }); let user; if (logins[key] && logins[key] !== "undefined") { const { username } = logins[key]; user = await db.User.findOne({ - where: { username } + where: { username }, }); if (user) { const [k, created] = await db.Key.findOrCreate({ where: { user_id: user.id, - hex: key - } + hex: key, + }, }); if (created) { @@ -554,7 +571,7 @@ lnurlServer.bindToHook("login", async key => { l("user not found"); user = await register({ username: key.substr(0, 8), - password: key + password: key, }); } } else if (exists) ({ user } = exists); |
