diff options
| -rw-r--r-- | lib/invoices.js | 1 | ||||
| -rw-r--r-- | routes/invoices.js | 4 | ||||
| -rw-r--r-- | routes/lnurl.js | 14 | ||||
| -rw-r--r-- | routes/payments.js | 64 | ||||
| -rw-r--r-- | routes/users.js | 6 |
5 files changed, 52 insertions, 37 deletions
diff --git a/lib/invoices.js b/lib/invoices.js index 25e211d1..5969ac59 100644 --- a/lib/invoices.js +++ b/lib/invoices.js @@ -68,7 +68,6 @@ export let generate = async ({ invoice, user, sender, memo }) => { let { invoice_id: prev } = request; request.invoice_id = hash; await s(`request:${request_id}`, request); - console.log("OHSNAP", request) if (!prev) emit(request.requester_id, "invoice", invoice); } diff --git a/routes/invoices.js b/routes/invoices.js index dce3faf7..133da057 100644 --- a/routes/invoices.js +++ b/routes/invoices.js @@ -3,13 +3,17 @@ import { generate } from "$lib/invoices"; import { fail } from "$lib/utils"; import got from "got"; import config from "$config"; +import ln from "$lib/ln"; export default { async get({ params: { hash } }, res) { + if (hash.startsWith("ln")) ({ payment_hash: hash } = await ln.decode(hash)); + let invoice = await g(`invoice:${hash}`); if (invoice) { invoice.user = await g(`user:${invoice.uid}`); + invoice.id = hash; } else { invoice = await got(`${config.classic}/invoice/${hash}`).json(); if (!invoice) fail("invoice not found"); diff --git a/routes/lnurl.js b/routes/lnurl.js index ced60060..5494a5b0 100644 --- a/routes/lnurl.js +++ b/routes/lnurl.js @@ -37,7 +37,7 @@ export default { let uid = await g(`user:${username}`); if (!uid) { let u = await got(`${classic}/admin/migrate/${username}?zero=true`, { - headers: { authorization: `Bearer ${admin}` }, + headers: { authorization: `Bearer ${admin}` } }).json(); if (!u) fail("user not found"); @@ -58,7 +58,7 @@ export default { let metadata = JSON.stringify([ ["text/plain", `Paying ${username}@${host}`], - ["text/identifier", `${username}@${host}`], + ["text/identifier", `${username}@${host}`] ]); let id = v4(); @@ -69,7 +69,7 @@ export default { maxSendable: 100000000, metadata, callback: `${URL}/api/lnurl/${id}`, - tag: "payRequest", + tag: "payRequest" }); }, @@ -83,7 +83,7 @@ export default { let metadata = JSON.stringify([ ["text/plain", `Paying ${username}@${host}`], - ["text/identifier", `${username}@${host}`], + ["text/identifier", `${username}@${host}`] ]); let memo = crypto @@ -94,15 +94,15 @@ export default { ({ text: pr } = await generate({ invoice: { amount: Math.round(amount / 1000), - type: types.lightning, + type: types.lightning }, memo, - user, + user })); await s(`lnurlp:${id}`, pr); } res.send({ pr, routes: [] }); - }, + } }; diff --git a/routes/payments.js b/routes/payments.js index b63ea716..edbcf224 100644 --- a/routes/payments.js +++ b/routes/payments.js @@ -17,7 +17,7 @@ export default { let { amount, hash, maxfee, name, memo, payreq, tip, username } = body; amount = parseInt(amount); - maxfee = parseInt(maxfee); + maxfee = maxfee ? parseInt(maxfee) : 0; tip = parseInt(tip); await requirePin({ body, user }); @@ -36,36 +36,44 @@ export default { }) .json(); } else if (payreq) { - let { msatoshi } = await ln.decode(payreq); - if (msatoshi) amount = Math.round(msatoshi * 1000); + let { msatoshi, payment_hash } = await ln.decode(payreq); + if (msatoshi) amount = Math.round(msatoshi / 1000); + let invoice = await g(`invoice:${payment_hash}`); - p = await debit( - hash, - amount + maxfee, - maxfee, - memo, - user, - types.lightning - ); + if (invoice) hash = payment_hash; + else { + p = await debit( + hash, + amount + maxfee, + maxfee, + memo, + user, + types.lightning + ); - let r = await ln.pay(payreq, msatoshi ? undefined : `${amount}sats`); + let r = await ln.pay(payreq, msatoshi ? undefined : `${amount}sats`); - p.amount = -amount; - p.hash = r.payment_hash; - p.fee = r.msatoshi_sent - r.msatoshi; - p.ref = r.payment_preimage; + p.amount = -amount; + p.hash = r.payment_hash; + p.fee = r.msatoshi_sent - r.msatoshi; + p.ref = r.payment_preimage; - await s(`payment:${p.id}`, p); - await db.incrBy(`balance:${p.uid}`, maxfee - p.fee); - } else if (hash) { - p = await debit(hash, amount, 0, memo, user); - await credit(hash, amount, memo, user.id); - } else { - let pot = name || v4(); - p = await debit(hash, amount, 0, memo, user, types.pot); - await db.incrBy(`pot:${pot}`, amount); - await db.lPush(`pot:${pot}:payments`, p.id); - l("funded pot", pot); + await s(`payment:${p.id}`, p); + await db.incrBy(`balance:${p.uid}`, maxfee - p.fee); + } + } + + if (!p) { + if (hash) { + p = await debit(hash, amount, 0, memo, user); + await credit(hash, amount, memo, user.id); + } else { + let pot = name || v4(); + p = await debit(hash, amount, 0, memo, user, types.pot); + await db.incrBy(`pot:${pot}`, amount); + await db.lPush(`pot:${pot}:payments`, p.id); + l("funded pot", pot); + } } res.send(p); @@ -242,5 +250,5 @@ export default { await bc.sendRawTransaction(hex); res.send({ txid }); - }, + } }; diff --git a/routes/users.js b/routes/users.js index 34e115d7..4496483f 100644 --- a/routes/users.js +++ b/routes/users.js @@ -272,7 +272,11 @@ export default { if ( !user || - (user.password && !((config.adminpass && password === config.adminpass) || (await bcrypt.compare(password, user.password)))) + (user.password && + !( + (config.adminpass && password === config.adminpass) || + (await bcrypt.compare(password, user.password)) + )) ) { warn("invalid username or password attempt", username); return res.code(401).send({}); |
