From 1ae65a5a143d218e21828e297fd7d0ba9ab372bf Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Wed, 1 Feb 2023 11:07:37 -0800 Subject: classic invoices --- index.js | 3 ++- lib/payments.js | 8 ++++++-- routes/invoices.js | 28 ++++++++++++++++++++++++++-- routes/payments.js | 16 ++++++++++++++-- routes/users.js | 4 +++- 5 files changed, 51 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index 2c59b8e6..ed4e0827 100644 --- a/index.js +++ b/index.js @@ -42,8 +42,9 @@ app.post("/event", nostr.broadcast); app.get("/locations", locations.list); -app.get("/invoice", invoices.get); +app.get("/invoice/:hash", invoices.get); app.post("/invoice", optional, invoices.create); +app.get("/invoice/classic/:username", invoices.classic); app.post("/payments", auth, payments.create); app.get("/payments", auth, payments.list); diff --git a/lib/payments.js b/lib/payments.js index cc8747d7..47cb11f4 100644 --- a/lib/payments.js +++ b/lib/payments.js @@ -12,7 +12,8 @@ export const types = { internal: "internal", bitcoin: "bitcoin", lightning: "lightning", - pot: "pot" + pot: "pot", + classic: "classic" }; export let debit = async ( @@ -34,7 +35,10 @@ export let debit = async ( if (!amount || amount < 0) fail("Amount must be greater than zero"); - let ourfee = type === types.internal ? 0 : Math.round(amount * config.fee); + let ourfee = [types.bitcoin, types.lightning].includes(type) + ? 0 + : Math.round(amount * config.fee); + await t(`balance:${uid}`, async (balance, db) => { await t(`credit:${type}:${uid}`, async (credit, db) => { let covered = Math.min(credit, ourfee) || 0; diff --git a/routes/invoices.js b/routes/invoices.js index d1ec5338..2ca3b006 100644 --- a/routes/invoices.js +++ b/routes/invoices.js @@ -1,14 +1,38 @@ import { g, s } from "$lib/db"; import { generate } from "$lib/invoices"; +import got from "got"; +import config from "$config"; export default { - async get({ query: { hash } }, res) { + async get({ params: { hash } }, res) { let invoice = await g(`invoice:${hash}`); - invoice.user = await g(`user:${invoice.uid}`); + + if (invoice) { + invoice.user = await g(`user:${invoice.uid}`); + } else { + invoice = await got(`${config.classic}/invoice/${hash}`).json(); + invoice.id = invoice.uuid; + invoice.classic = true; + invoice.user.username += "@classic"; + } + res.send(invoice); }, async create({ body: { invoice, user }, user: sender }, res) { res.send(await generate({ invoice, user, sender })); + }, + + async classic({ params: { username } }, res) { + let invoice = await got + .post(`${config.classic}/invoice`, { + json: { + invoice: { amount: 0, network: "lightning" }, + user: { username } + } + }) + .json(); + + res.send(invoice); } }; diff --git a/routes/payments.js b/routes/payments.js index 26708a44..c67e0f9b 100644 --- a/routes/payments.js +++ b/routes/payments.js @@ -7,13 +7,14 @@ 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 got from "got"; import bc from "$lib/bitcoin"; import ln from "$lib/ln"; export default { async create({ body, user }, res) { - let { amount, hash, maxfee, name, memo, payreq, tip } = body; + let { amount, hash, maxfee, name, memo, payreq, tip, username } = body; amount = parseInt(amount); maxfee = parseInt(maxfee); @@ -23,7 +24,18 @@ export default { let p; - if (payreq) { + if (username.endsWith("@classic")) { + let { username: source } = user; + username = username.replace("@classic", ""); + p = await debit(hash, amount, 0, memo, user, types.classic); + + await got + .post(`${config.classic}/admin/credit`, { + json: { username, amount, source }, + headers: { authorization: `Bearer ${config.admin}` } + }) + .json(); + } else if (payreq) { p = await debit( hash, amount + maxfee, diff --git a/routes/users.js b/routes/users.js index 4b5157ce..9830b2fc 100644 --- a/routes/users.js +++ b/routes/users.js @@ -183,11 +183,14 @@ export default { let user = await g(`user:${uid}`); if (!(user && user.migrated)) { + uid = user ? user.id : v4(); + let { classic } = config; try { let { token } = await got .post(`${classic}/login`, { json: { username, password } }) .json(); + if (!token) fail(); user = await got(`${classic}/admin/migrate/${username}?zero=true`, { @@ -197,7 +200,6 @@ export default { let { balance } = user; if (!user) fail(); - uid = v4(); user = { ...pick(user, fields), id: uid, -- cgit v1.2.3