diff options
| author | Adam Soltys <[email protected]> | 2023-02-06 16:48:13 -0800 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2023-02-06 16:48:13 -0800 |
| commit | 9c667055c6367e1b2e239686e72f52995679a47e (patch) | |
| tree | 0b1d4aee51ec2d4e92d08a9d1404ba4e2ecd462a /routes/lightning | |
| parent | 15294844dbc8e73b4d1e92008a7eebc6b86b9ec5 (diff) | |
| parent | 6f5602d112b68adb7452abf3047f20c32e7725b6 (diff) | |
| download | coinos-server-9c667055c6367e1b2e239686e72f52995679a47e.tar.gz coinos-server-9c667055c6367e1b2e239686e72f52995679a47e.zip | |
Merge branch 'master' into test
Diffstat (limited to 'routes/lightning')
| -rw-r--r-- | routes/lightning/channel.js | 29 | ||||
| -rw-r--r-- | routes/lightning/channelRequest.js | 24 | ||||
| -rw-r--r-- | routes/lightning/conversionFee.js | 12 | ||||
| -rw-r--r-- | routes/lightning/index.js | 13 | ||||
| -rw-r--r-- | routes/lightning/invoice.js | 25 | ||||
| -rw-r--r-- | routes/lightning/parse.js | 19 | ||||
| -rw-r--r-- | routes/lightning/query.js | 20 | ||||
| -rw-r--r-- | routes/lightning/receive.js | 162 | ||||
| -rw-r--r-- | routes/lightning/send.js | 17 |
9 files changed, 0 insertions, 321 deletions
diff --git a/routes/lightning/channel.js b/routes/lightning/channel.js deleted file mode 100644 index d5d5b7b2..00000000 --- a/routes/lightning/channel.js +++ /dev/null @@ -1,29 +0,0 @@ -import axios from "axios"; -import ln from "$lib/ln"; - -export default async (req, res) => { - const { params } = req.body; - const [public_key, socket] = params.uri.split("@"); - - l("connecting to peer", req.user.username, public_key, socket); - let result; - - const { callback, k1 } = params; - let remoteid; - try { - remoteid = (await ln.getinfo()).id; - } catch (e) { - err("problem getting lightning node info", e.message); - return res.code(500).send(e.message); - } - - let url = `${callback}?k1=${k1}&remoteid=${remoteid}&private=0`; - - try { - let response = await axios.get(url); - res.send(response.data); - } catch (e) { - err("problem sending channel request", e.message); - return res.code(500).send({ status: "ERROR", reason: e.message }); - } -}; diff --git a/routes/lightning/channelRequest.js b/routes/lightning/channelRequest.js deleted file mode 100644 index 74e4cbba..00000000 --- a/routes/lightning/channelRequest.js +++ /dev/null @@ -1,24 +0,0 @@ -import { lnurlServer } from "$routes/lnurl"; -import { err } from "$lib/logging"; - -export const channelRequest = async (req, res) => { - let { localAmt, pushAmt } = req.body; - - pushAmt = 0; - - try { - if (localAmt < 20000) - throw new Error("amount must be greater than 20000 SAT"); - if (localAmt > 1000000) throw new Error("amount must be <= 1000000"); - - const result = await lnurlServer.generateNewUrl("channelRequest", { - localAmt, - pushAmt - }); - - res.send(result); - } catch (e) { - err("problem generating channel request", e.message); - res.code(500).send(e.message); - } -}; diff --git a/routes/lightning/conversionFee.js b/routes/lightning/conversionFee.js deleted file mode 100644 index f6cf434c..00000000 --- a/routes/lightning/conversionFee.js +++ /dev/null @@ -1,12 +0,0 @@ -// Code for the conversion fee -// This is in a separate file so that the same function can be used by ./send and ./receive - -const conversionFeeMultiplier = 0.001; -export const conversionFeeReceiver = "coinosfees"; // account that receives the fees - -/** - * Returns the fee you have to pay for withdrawing 'amount' lightning BTC. - */ -export function computeConversionFee(amount) { - return Math.floor(amount * conversionFeeMultiplier); -} diff --git a/routes/lightning/index.js b/routes/lightning/index.js deleted file mode 100644 index b4611ef5..00000000 --- a/routes/lightning/index.js +++ /dev/null @@ -1,13 +0,0 @@ -import channel from "./channel"; -import invoice from "./invoice"; -import query from "./query"; -import send from "./send"; -import parse from "./parse"; - -export default { - channel, - invoice, - query, - send, - parse -}; diff --git a/routes/lightning/invoice.js b/routes/lightning/invoice.js deleted file mode 100644 index ae974120..00000000 --- a/routes/lightning/invoice.js +++ /dev/null @@ -1,25 +0,0 @@ -import config from "$config"; -import { err } from "$lib/logging"; -import ln from "$lib/ln"; -import { v4 } from "uuid"; - -export default async (req, res) => { - let { amount, memo, tip } = req.body; - if (!tip) tip = 0; - let value = amount + tip; - - try { - if (!memo) memo = "coinos"; - const invoice = await ln.invoice( - value ? `${value}sat` : "any", - v4(), - memo, - 360 - ); - res.send({ text: invoice.bolt11 }); - } catch (e) { - console.log(e); - err("problem creating invoice", e.message); - res.code(500).send(e.message); - } -}; diff --git a/routes/lightning/parse.js b/routes/lightning/parse.js deleted file mode 100644 index 1b77019a..00000000 --- a/routes/lightning/parse.js +++ /dev/null @@ -1,19 +0,0 @@ -import ln from "$lib/ln"; -import store from "$lib/store"; - -export default async (req, res) => { - let { payreq } = req.body; - let hour = 1000 * 60 * 60; - let { last } = store.nodes; - let { nodes } = store; - - if (!last || last > Date.now() - hour) ({ nodes } = await ln.listnodes()); - store.nodes = nodes; - - let twoWeeksAgo = new Date(new Date().setDate(new Date().getDate() - 14)); - let { msatoshi, payee } = await ln.decodepay(payreq); - let node = nodes.find((n) => n.nodeid === payee); - let alias = node ? node.alias : payee.substr(0, 12); - - res.send({ alias, amount: Math.round(msatoshi / 1000) }); -}; diff --git a/routes/lightning/query.js b/routes/lightning/query.js deleted file mode 100644 index 1c217a08..00000000 --- a/routes/lightning/query.js +++ /dev/null @@ -1,20 +0,0 @@ -import db from "$db"; -import { emit } from "$lib/sockets"; - -export default async (req, res) => { - const { payreq } = req.body; - - const invoice = await db.Invoice.findOne({ - include: { - attributes: ["username"], - model: db.User, - as: "user" - }, - where: { - text: payreq - } - }); - - if (invoice) emit(req.user.username, "to", invoice.user); - res.send({}); -}; diff --git a/routes/lightning/receive.js b/routes/lightning/receive.js deleted file mode 100644 index ef973acc..00000000 --- a/routes/lightning/receive.js +++ /dev/null @@ -1,162 +0,0 @@ -import db from "$db"; -import store from "$lib/store"; -import { emit } from "$lib/sockets"; -import config from "$config"; -import { notify } from "$lib/notifications"; -import { callWebhook } from "$lib/webhooks"; -import { computeConversionFee } from "./conversionFee"; -import { sendLiquid } from "$routes/liquid/send"; -import ln from "$lib/ln"; -import { l, err, warn } from "$lib/logging"; - -const handlePayment = async msg => { - try { - l("incoming lightning payment", msg.received); - - if (!msg.secret) return; - let account, total, user; - - const invoice = await db.Invoice.findOne({ - where: { - text: msg.request - } - }); - - if (!invoice) - return warn("received lightning with no invoice", msg.request); - - await db.transaction(async transaction => { - const { text: hash, currency, memo, rate, tip, user_id } = invoice; - const amount = parseInt(msg.received) - tip; - if (amount < 0) throw new Error("amount out of range"); - - account = await db.Account.findOne({ - where: { - user_id, - asset: config.liquid.btcasset, - pubkey: null - }, - include: { - model: db.User, - as: "user" - }, - lock: transaction.LOCK.UPDATE, - transaction - }); - - ({ user } = account); - - let preimage = msg.secret; - - let payment = await db.Payment.create( - { - account_id: account.id, - user_id, - hash, - memo, - amount, - currency, - preimage, - rate, - received: true, - confirmed: true, - network: "lightning", - tip, - invoice_id: invoice.id - }, - { transaction } - ); - - total = amount + tip; - - invoice.status = "paid"; - await invoice.save({ transaction }); - - await invoice.increment({ received: total }, { transaction }); - await account.increment({ balance: total }, { transaction }); - // get the # of fee credits you would need to pay off this amount of bitcoin - await account.increment( - { lightning_credits: computeConversionFee(total) }, - { transaction } - ); - await account.reload({ transaction }); - await invoice.reload({ transaction }); - await payment.save({ transaction }); - store.payments.push(msg.request); - - payment = payment.get({ plain: true }); - payment.account = account.get({ plain: true }); - payment.invoice = invoice.get({ plain: true }); - - callWebhook(invoice, payment); - - emit(user.username, "payment", payment); - emit(user.username, "account", payment.account); - notify(user, `Received ${total} SAT`); - - l( - "lightning payment received", - user.username, - payment.amount, - payment.tip - ); - }); - - let c = store.convert[msg.request]; - if (msg.request && c) { - l( - "lightning detected for conversion request", - msg.request, - c.address, - user.username - ); - - user.account = account; - - try { - sendLiquid({ - address: c.address, - amount: total - 100, - user, - limit: total - }); - } catch (e) { - err("problem sending liquid payment", e.message, e.stack); - } - } - } catch (e) { - console.log(e); - err("problem receiving lightning payment", e.message); - } -}; - -const poll = async ln => { - try { - const wait = async i => { - const { - bolt11: request, - pay_index, - status, - msatoshi_received, - payment_preimage: secret - } = await ln.waitanyinvoice(i); - - let settled = status === "paid"; - let received = parseInt(msatoshi_received / 1000); - - await handlePayment({ - request, - received, - secret - }); - await wait(pay_index); - }; - - const { invoices } = await ln.listinvoices(); - await wait(Math.max(...invoices.map(i => i.pay_index).filter(n => n))); - } catch (e) { - err("problem waiting for c-lightning invoice", e.message); - } -}; - -poll(ln); diff --git a/routes/lightning/send.js b/routes/lightning/send.js deleted file mode 100644 index b41d5ed0..00000000 --- a/routes/lightning/send.js +++ /dev/null @@ -1,17 +0,0 @@ -import { err } from "$lib/logging"; -import send from "$lib/send"; -import { requirePin } from "$lib/utils"; - -export default async (req, res) => { - let { amount, route, memo, payreq } = req.body; - let { user } = req; - - try { - await requirePin(req); - res.send(await send(amount, memo, payreq, user)); - } catch (e) { - console.log(e); - err("problem sending lightning payment", user.username, payreq, e.message); - res.code(500).send(e.message); - } -}; |
