From 692315c0d4259a274322ca579a7b6118d033bf7a Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Tue, 24 Jan 2023 12:28:49 -0800 Subject: lnurl --- lib/invoices.js | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 lib/invoices.js (limited to 'lib/invoices.js') diff --git a/lib/invoices.js b/lib/invoices.js new file mode 100644 index 00000000..7a45b1c0 --- /dev/null +++ b/lib/invoices.js @@ -0,0 +1,73 @@ +import store from "$lib/store"; +import { l } from "$lib/logging"; +import { emit } from "$lib/sockets"; +import { db, g, s } from "$lib/db"; +import { bip21, fail } from "$lib/utils"; +import { types } from "$lib/payments"; +import { v4 } from "uuid"; + +import bc from "$lib/bitcoin"; +import ln from "$lib/ln"; + +export let invoice = async ({ invoice, user, sender }) => { + let { currency, tip, amount, rate, request_id, type } = invoice; + + if (amount < 0) fail("amount out of range"); + if (tip > 5 * amount || tip < 0) fail("tip amount out of range"); + tip = tip || 0; + + if (!user) user = sender; + let uid = await g(`user:${user.username}`); + user = await g(`user:${uid}`); + + if (!user) fail("user not provided"); + + if (!currency) currency = user.currency; + if (!rate) rate = store.rates[currency]; + if (amount < 0) fail("invalid amount"); + + let hash, text; + if (type === types.lightning) { + let amt = amount ? `${amount + tip}sat` : "any"; + let r = await ln.invoice(amt, new Date(), "", 3600); + + hash = r.payment_hash; + text = r.bolt11; + } else if (type === types.bitcoin) { + hash = await bc.getNewAddress(); + text = bip21(hash, invoice); + } else if (type === types.internal) { + hash = v4(); + } else { + fail("unrecognized type"); + } + + invoice = { + ...invoice, + amount, + hash, + text, + currency, + rate, + tip, + uid, + received: 0, + created: Date.now() + }; + + l("creating invoice", user.username, amount, tip, currency, invoice.hash); + + await s(`invoice:${hash}`, invoice); + await db.lPush(`${uid}:invoices`, hash); + + if (request_id) { + let request = await g(`request:${request_id}`); + let { invoice_id: prev } = request; + request.invoice_id = invoice.id; + await s(`request:${request_id}`, request); + + if (!prev) emit(request.requester.username, "invoice", invoice); + } + + return invoice; +}; -- cgit v1.2.3 From adf35d736de2cd870e363abc28a0989158ea0b5b Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Tue, 24 Jan 2023 14:46:46 -0800 Subject: move lnurl routes --- lib/invoices.js | 2 +- routes/invoices.js | 4 ++-- routes/lnurl.js | 4 ++-- routes/payments.js | 59 ------------------------------------------------------ 4 files changed, 5 insertions(+), 64 deletions(-) (limited to 'lib/invoices.js') diff --git a/lib/invoices.js b/lib/invoices.js index 7a45b1c0..f17223aa 100644 --- a/lib/invoices.js +++ b/lib/invoices.js @@ -9,7 +9,7 @@ import { v4 } from "uuid"; import bc from "$lib/bitcoin"; import ln from "$lib/ln"; -export let invoice = async ({ invoice, user, sender }) => { +export let generate = async ({ invoice, user, sender }) => { let { currency, tip, amount, rate, request_id, type } = invoice; if (amount < 0) fail("amount out of range"); diff --git a/routes/invoices.js b/routes/invoices.js index a4155d94..d1ec5338 100644 --- a/routes/invoices.js +++ b/routes/invoices.js @@ -1,5 +1,5 @@ import { g, s } from "$lib/db"; -import { invoice } from "$lib/invoices"; +import { generate } from "$lib/invoices"; export default { async get({ query: { hash } }, res) { @@ -9,6 +9,6 @@ export default { }, async create({ body: { invoice, user }, user: sender }, res) { - res.send(await invoice({ invoice, user, sender })); + res.send(await generate({ invoice, user, sender })); } }; diff --git a/routes/lnurl.js b/routes/lnurl.js index cb1b75c5..63488033 100644 --- a/routes/lnurl.js +++ b/routes/lnurl.js @@ -3,7 +3,7 @@ import { l } from "$lib/logging"; import { fail } from "$lib/utils"; import { v4 } from "uuid"; import got from "got"; -import { invoice } from "$lib/invoices"; +import { generate } from "$lib/invoices"; import { bech32 } from "bech32"; export default { @@ -50,7 +50,7 @@ export default { let uid = await g(`lnurl:${id}`); let user = await g(`user:${uid}`); - ({ text: pr } = await invoice({ + ({ text: pr } = await generate({ invoice: { amount, type: types.lightning diff --git a/routes/payments.js b/routes/payments.js index b80d54f6..a060d90a 100644 --- a/routes/payments.js +++ b/routes/payments.js @@ -7,9 +7,6 @@ import { l, err } from "$lib/logging"; import { fail, btc, sats } from "$lib/utils"; import { requirePin } from "$lib/auth"; import { debit, credit, confirm, types } from "$lib/payments"; -import { bech32 } from "bech32"; -import { invoice } from "$lib/invoices"; -import got from "got"; import bc from "$lib/bitcoin"; import ln from "$lib/ln"; @@ -220,60 +217,4 @@ export default { res.send({ txid }); }, - async encode({ query: { address } }, res) { - let [name, domain] = address.split("@"); - let url = `https://${domain}/.well-known/lnurlp/${name}`; - let r = await got(url).json(); - if (r.tag !== "payRequest") fail("not an ln address"); - let enc = bech32.encode("lnurl", bech32.toWords(Buffer.from(url)), 20000); - res.send(enc); - }, - - async decode({ query: { text } }, res) { - let url = Buffer.from( - bech32.fromWords(bech32.decode(text, 20000).words) - ).toString(); - - res.send(await got(url).json()); - }, - - async lnurlp({ params: { username } }, res) { - let uid = await g(`user:${username}`); - if (!uid) fail("user not found"); - - let id = v4(); - await s(`lnurl:${id}`, uid); - let { URL } = process.env; - let host = URL.split("/").at(-1); - - res.send({ - metadata: [ - ["text/plain", `Paying ${username}@${host}`], - ["text/identifier", `${username}@${host}`] - ], - callback: `${host}/lnurl/${id}`, - tag: "payRequest" - }); - }, - - async lnurl({ params: { id }, query: { amount } }, res) { - let pr = await g(`lnurlp:${id}`); - - if (!pr) { - let uid = await g(`lnurl:${id}`); - let user = await g(`user:${uid}`); - - ({ text: pr } = await invoice({ - invoice: { - amount, - type: types.lightning - }, - user - })); - - await s(`lnurlp:${id}`, pr); - } - - res.send({ pr }); - } }; -- cgit v1.2.3 From 283ffc9d8442ecbbc85c0c40167cc03f20f64a40 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Sat, 28 Jan 2023 00:54:08 -0800 Subject: parse ints --- lib/invoices.js | 4 +++- lib/upload.js | 1 + 2 files changed, 4 insertions(+), 1 deletion(-) (limited to 'lib/invoices.js') diff --git a/lib/invoices.js b/lib/invoices.js index f17223aa..814e87d9 100644 --- a/lib/invoices.js +++ b/lib/invoices.js @@ -12,9 +12,11 @@ import ln from "$lib/ln"; export let generate = async ({ invoice, user, sender }) => { let { currency, tip, amount, rate, request_id, type } = invoice; + amount = parseInt(amount); + tip = parseInt(tip || 0); + if (amount < 0) fail("amount out of range"); if (tip > 5 * amount || tip < 0) fail("tip amount out of range"); - tip = tip || 0; if (!user) user = sender; let uid = await g(`user:${user.username}`); diff --git a/lib/upload.js b/lib/upload.js index 5f5aebed..2f8180a0 100644 --- a/lib/upload.js +++ b/lib/upload.js @@ -12,6 +12,7 @@ export default async (req, res) => { params: { type }, user } = req; + console.log("TYPE", type) if (!["banner", "profile"].includes(type)) throw new Error("invalid upload"); let data = await req.file(); -- cgit v1.2.3 From fe0455ffe2d93eda5792ec1ef38348ef24aba2aa Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Wed, 1 Feb 2023 19:58:13 +0000 Subject: preserve user id --- lib/invoices.js | 2 +- routes/invoices.js | 1 + routes/users.js | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) (limited to 'lib/invoices.js') diff --git a/lib/invoices.js b/lib/invoices.js index 814e87d9..4a09f44b 100644 --- a/lib/invoices.js +++ b/lib/invoices.js @@ -12,7 +12,7 @@ import ln from "$lib/ln"; export let generate = async ({ invoice, user, sender }) => { let { currency, tip, amount, rate, request_id, type } = invoice; - amount = parseInt(amount); + amount = parseInt(amount || 0); tip = parseInt(tip || 0); if (amount < 0) fail("amount out of range"); diff --git a/routes/invoices.js b/routes/invoices.js index 2ca3b006..d856ef6b 100644 --- a/routes/invoices.js +++ b/routes/invoices.js @@ -13,6 +13,7 @@ export default { invoice = await got(`${config.classic}/invoice/${hash}`).json(); invoice.id = invoice.uuid; invoice.classic = true; + invoice.user.id = invoice.user.uuid; invoice.user.username += "@classic"; } diff --git a/routes/users.js b/routes/users.js index 0692ed23..e3264937 100644 --- a/routes/users.js +++ b/routes/users.js @@ -235,7 +235,7 @@ export default { headers: { authorization: `Bearer ${config.admin}` } }).json(); - u = { id: v4(), about: u.address, ...pick(u, fields) }; + u = { id: u.uuid, about: u.address, ...pick(u, fields) }; delete u.address; await s(`user:${p.with.username}`, u.id); -- cgit v1.2.3