From 3b1075e896b441d7ba95cd997a49e7ae618ab118 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Thu, 8 Sep 2022 18:14:28 -0400 Subject: missing import --- routes/lightning/query.js | 1 + 1 file changed, 1 insertion(+) (limited to 'routes') diff --git a/routes/lightning/query.js b/routes/lightning/query.js index c5456a6f..1c217a08 100644 --- a/routes/lightning/query.js +++ b/routes/lightning/query.js @@ -1,3 +1,4 @@ +import db from "$db"; import { emit } from "$lib/sockets"; export default async (req, res) => { -- cgit v1.2.3 From 529cdff2a9df43a156a34072fd71f406cb7186f2 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Thu, 8 Sep 2022 16:45:32 -0700 Subject: missing imports --- lib/buildliquidtx.js | 2 +- routes/liquid/fee.js | 3 +++ routes/liquid/index.js | 4 ++-- routes/liquid/send.js | 12 ++++++++---- routes/payments.js | 4 ++-- 5 files changed, 16 insertions(+), 9 deletions(-) (limited to 'routes') diff --git a/lib/buildliquidtx.js b/lib/buildliquidtx.js index ab2c597e..f2dce6e1 100644 --- a/lib/buildliquidtx.js +++ b/lib/buildliquidtx.js @@ -1,7 +1,7 @@ import store from "./store"; import { prod } from "./utils"; import axios from "axios"; -// import liquid from 'liquidjs-lib'; +import liquid from 'liquidjs-lib'; import coinselect from "coinselect"; import split from "coinselect/split"; const api = prod diff --git a/routes/liquid/fee.js b/routes/liquid/fee.js index 4ec29455..ae6e4393 100644 --- a/routes/liquid/fee.js +++ b/routes/liquid/fee.js @@ -1,7 +1,10 @@ +import config from "$config"; import db from "$db"; import { emit } from "$lib/sockets"; import buildTx from "$lib/buildliquidtx"; import { err, l } from "$lib/logging"; +import { SATS } from "$lib/utils"; +import lq from "$lib/liquid"; export const liquidTx = async ({ address, asset, amount, feeRate, replaceable, user }) => { let tx, fee; diff --git a/routes/liquid/index.js b/routes/liquid/index.js index b75cf23e..c4c50021 100644 --- a/routes/liquid/index.js +++ b/routes/liquid/index.js @@ -1,11 +1,11 @@ import broadcast from "./broadcast"; import generate from "./generate"; -// import fee from "./fee"; +import fee from "./fee"; import send from "./send"; export default { broadcast, generate, -// fee, + fee, send, }; diff --git a/routes/liquid/send.js b/routes/liquid/send.js index 8c94d976..2fa7a22a 100644 --- a/routes/liquid/send.js +++ b/routes/liquid/send.js @@ -5,8 +5,8 @@ import { emit } from "$lib/sockets"; import store from "$lib/store"; import config from "$config"; import { l, warn, err } from "$lib/logging"; -// import { liquidTx } from "$routes/liquid/fee"; -// import { Transaction } from 'liquidjs-lib'; +import { liquidTx } from "$routes/liquid/fee"; +import { Transaction } from 'liquidjs-lib'; import { computeConversionFee, @@ -229,7 +229,7 @@ export const sendLiquid = async ({ asset, amount, user, address, memo, tx, limit let txid = Transaction.fromHex(signed.hex).getId(); for (let i = 0; i < payments.length; i++) { - p = payments[i]; + let p = payments[i]; if (p) { let { account } = p; p.hash = txid; @@ -255,6 +255,7 @@ export const sendLiquid = async ({ asset, amount, user, address, memo, tx, limit return main; } catch (e) { err("problem sending liquid", e.message); + return e; } }; @@ -262,8 +263,11 @@ export default async (req, res) => { let { user } = req; try { - res.send(await sendLiquid({ ...req.body, user })); + let result = await sendLiquid({ ...req.body, user }) + if (result instanceof Error) throw result; + res.send(result); } catch (e) { + console.log(e) err("problem sending liquid", user.username, e.message); return res.code(500).send(e); } diff --git a/routes/payments.js b/routes/payments.js index 1f9a94ec..4ffeb6b3 100644 --- a/routes/payments.js +++ b/routes/payments.js @@ -86,8 +86,8 @@ setTimeout(async () => { app.post("/liquid/broadcast", optionalAuth, lqRoutes.broadcast); app.get("/liquid/generate", auth, lqRoutes.generate); -// app.post("/liquid/fee", auth, lqRoutes.fee); -// app.post("/liquid/send", auth, lqRoutes.send); +app.post("/liquid/fee", auth, lqRoutes.fee); +app.post("/liquid/send", auth, lqRoutes.send); setTimeout(async () => { try { -- cgit v1.2.3 From d2ae5acf545c8b1a4f88f87b8d80b20c9d45245d Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Fri, 9 Sep 2022 10:27:32 -0400 Subject: use actual received amount for lightning payments --- lib/sync.js | 12 ++++++------ routes/lightning/invoice.js | 3 --- routes/lightning/receive.js | 4 ++-- 3 files changed, 8 insertions(+), 11 deletions(-) (limited to 'routes') diff --git a/lib/sync.js b/lib/sync.js index 7b72b20a..536bd582 100644 --- a/lib/sync.js +++ b/lib/sync.js @@ -80,14 +80,14 @@ const sync = async () => { let recent = invoices .filter( i => - i.settled && - differenceInDays(new Date(), new Date(i.settle_date * 1000)) < 2 + i.secret && + differenceInDays(new Date(), new Date(i.created_at)) < 2 ) .map(i => ({ - amount: parseInt(i.amt_paid_sat), - preimage: i.r_preimage.toString("hex"), - pr: i.payment_request.toString("hex"), - createdAt: new Date(i.settle_date * 1000) + amount: parseInt(i.received), + preimage: i.secret, + pr: i.request.toString("hex"), + createdAt: new Date(i.created_at) })); const twoDaysAgo = new Date(new Date().setDate(new Date().getDate() - 2)); diff --git a/routes/lightning/invoice.js b/routes/lightning/invoice.js index 3ef0e109..94aac768 100644 --- a/routes/lightning/invoice.js +++ b/routes/lightning/invoice.js @@ -8,7 +8,6 @@ export default async (req, res) => { if (!tip) tip = 0; let value = amount + tip; - console.log("getting invoice"); try { if (config.lna.clightning) { if (!memo) memo = "coinos"; @@ -20,13 +19,11 @@ export default async (req, res) => { ); res.send({ text: invoice.bolt11 }); } else { - console.log("creating invoice"); const invoice = await createInvoice({ lnd, tokens: value, description: memo }); - console.log("created"); res.send({ text: invoice.request }); } } catch (e) { diff --git a/routes/lightning/receive.js b/routes/lightning/receive.js index 8141e5d3..3ecc4ce9 100644 --- a/routes/lightning/receive.js +++ b/routes/lightning/receive.js @@ -12,7 +12,7 @@ import { l, err, warn } from "$lib/logging"; const handlePayment = async msg => { try { - l("incoming lightning payment", msg.tokens, msg.request.substr(0,20)); + l("incoming lightning payment", msg.received, msg.request.substr(0,20)); if (!msg.secret) return; let account, total, user; @@ -28,7 +28,7 @@ const handlePayment = async msg => { await db.transaction(async transaction => { const { text: hash, currency, memo, rate, tip, user_id } = invoice; - const amount = parseInt(msg.tokens) - tip; + const amount = parseInt(msg.received) - tip; if (amount < 0) throw new Error("amount out of range"); account = await db.Account.findOne({ -- cgit v1.2.3 From efa9cc6aa10751ffbb930333666bfe2720cb798c Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Tue, 13 Sep 2022 11:18:45 -0700 Subject: uncomment liquid code --- routes/liquid/receive.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'routes') diff --git a/routes/liquid/receive.js b/routes/liquid/receive.js index 9c2b490b..480677de 100644 --- a/routes/liquid/receive.js +++ b/routes/liquid/receive.js @@ -11,18 +11,16 @@ import zmq from "zeromq/v5-compat"; import { Op } from "@sequelize/core"; import { fromBase58 } from "bip32"; import bitcoin from "bitcoinjs-lib"; -// import liquidJs from "liquidjs-lib"; +import liquidJs from "liquidjs-lib"; import { l, err, warn } from "$lib/logging"; import lq from "$lib/liquid"; -// const { Block, networks, Transaction } = liquidJs; +const { Block, networks, Transaction } = liquidJs; -/* const network = networks[ config.liquid.network === "mainnet" ? "liquid" : config.liquid.network ]; - */ import { computeConversionFee } from "./conversionFee"; -- cgit v1.2.3 From 48108047dc4ab1557a2ca8f435885ff88d81e858 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Tue, 13 Sep 2022 15:59:07 -0700 Subject: refactor send internal into function --- lib/sendInternal.js | 217 +++++++++++++++++++++++++++++++++++++++++++++++++++ routes/lnurl.js | 15 ++-- routes/send.js | 220 +--------------------------------------------------- 3 files changed, 226 insertions(+), 226 deletions(-) create mode 100644 lib/sendInternal.js (limited to 'routes') diff --git a/lib/sendInternal.js b/lib/sendInternal.js new file mode 100644 index 00000000..80701ca3 --- /dev/null +++ b/lib/sendInternal.js @@ -0,0 +1,217 @@ +import config from "$config"; +import db from "$db"; +import { emit } from "$lib/sockets"; +import store from "$lib/store"; +import { callWebhook } from "$lib/webhooks"; +import axios from "axios"; +import { v4 } from "uuid"; +import { notify } from "$lib/notifications"; +import { sendLiquid } from "$routes/liquid/send"; +import { l, err } from "$lib/logging"; +import { fail } from "$lib/utils"; + +export default async ({ + amount, + address, + payreq, + unconfidential, + asset, + memo, + username, +}, hostname, user) => { + amount = parseInt(amount); + + if (!asset) asset = config.liquid.btcasset; + + if (!amount || amount < 0) fail("Amount must be greater than zero"); + + return db.transaction(async transaction => { + let account; + if (user.account.asset === asset && !user.account.pubkey) { + account = await db.Account.findOne({ + where: { + id: user.account.id, + pubkey: null + }, + lock: transaction.LOCK.UPDATE, + transaction + }); + } else { + account = await db.Account.findOne({ + where: { + user_id: user.id, + asset, + pubkey: null + }, + lock: transaction.LOCK.UPDATE, + transaction + }); + } + + if (!account) throw new Error("Account not found"); + + if (account.balance < amount) { + throw new Error("Insufficient funds"); + } + + let fee = 0; + + await account.decrement({ balance: amount }, { transaction }); + await account.reload({ transaction }); + + let params = { + amount: -amount, + account_id: account.id, + memo, + user_id: user.id, + rate: store.rates[user.currency], + currency: user.currency, + confirmed: true, + hash: `#${v4().substr(0, 6)} ${ + username ? `Payment to ${username}` : "Internal Transfer" + }`, + network: "COINOS" + }; + + if (!username) { + l("creating redeemable payment"); + params.redeemcode = v4(); + params.hash = `${hostname}/redeem/${params.redeemcode}`; + } + + let payment = await db.Payment.create(params, { transaction }); + + payment = payment.get({ plain: true }); + payment.account = account.get({ plain: true }); + + l("sent internal", user.username, -payment.amount); + + emit(user.username, "payment", payment); + emit(user.username, "account", account); + + if (username) { + let recipient = await db.User.findOne( + { + where: { username }, + include: { + model: db.Account, + as: "account" + } + }, + { transaction } + ); + + params = { + where: { user_id: recipient.id }, + order: [["id", "DESC"]] + }; + if (address) params.where.address = address; + else if (payreq) params.where.text = payreq; + let invoice = (address || payreq) && (await db.Invoice.findOne(params)); + + let a2; + let acc = { + user_id: recipient.id, + asset, + pubkey: null + }; + + if (recipient.account.asset === asset && !recipient.account.pubkey) + a2 = recipient.account; + else { + a2 = await db.Account.findOne({ + where: acc, + lock: transaction.LOCK.UPDATE, + transaction + }); + } + + if (a2) { + await a2.increment({ balance: amount }, { transaction }); + await a2.reload({ transaction }); + } else { + let name = asset.substr(0, 6); + let domain; + let ticker = asset.substr(0, 3).toUpperCase(); + let precision = 8; + + if (store.assets[asset]) { + ({ domain, ticker, precision, name } = store.assets[asset]); + } else { + const existing = await db.Account.findOne({ + where: { + asset + }, + order: [["id", "ASC"]], + limit: 1, + lock: transaction.LOCK.UPDATE, + transaction + }); + + if (existing) { + ({ domain, ticker, precision, name } = existing); + } + } + + acc = { ...acc, ...{ domain, ticker, precision, name } }; + acc.balance = amount; + acc.pending = 0; + acc.network = "liquid"; + a2 = await db.Account.create(acc, { transaction }); + } + + let spread = user.username === "coinos" ? 1.02 : 1; + amount = Math.round(amount / spread); + + params = { + amount, + account_id: a2.id, + user_id: recipient.id, + rate: store.rates[recipient.currency] * spread, + currency: recipient.currency, + confirmed: true, + hash: `#${v4().substr(0, 6)} Payment from ${user.username}`, + memo, + network: "COINOS", + received: true + }; + + if (invoice) { + params.invoice_id = invoice.id; + + let c = store.convert[invoice.text]; + if (c) { + l( + "internal payment detected for conversion", + invoice.text, + c.address, + recipient.username + ); + + recipient.account = a2; + + sendLiquid({ + address: c.address, + amount: amount - 100, + user: recipient, + limit: amount + }).catch(console.log); + } + } + + let p2 = await db.Payment.create(params, { transaction }); + + p2 = p2.get({ plain: true }); + p2.account = a2.get({ plain: true }); + emit(recipient.username, "payment", p2); + emit(recipient.username, "account", p2.account); + + l("received internal", recipient.username, amount); + notify(recipient, `Received ${amount} ${a2.ticker} sats`); + callWebhook(invoice, p2); + + } + + return payment; + }); +}; diff --git a/routes/lnurl.js b/routes/lnurl.js index d7d6b172..edebc94d 100644 --- a/routes/lnurl.js +++ b/routes/lnurl.js @@ -15,7 +15,6 @@ import jwt from "jsonwebtoken"; import qs from "query-string"; import bolt11 from "bolt11"; - import { createInvoice, decodePaymentRequest, getPayments } from "lightning"; import { @@ -24,13 +23,14 @@ import { } from "./lightning/conversionFee.js"; import send from "$lib/send"; +import sendInternal from "$lib/sendInternal"; const logins = persist("data/logins.json"); const recipients = persist("data/recipients.json"); const lnurlPayments = persist("data/payments.json"); const withdrawals = persist("data/withdrawals.json"); -console.log("creating lnurl") +console.log("creating lnurl"); export const lnurlServer = lnurl.createServer(config.lnurl); app.get("/url", async (req, res, next) => { @@ -198,19 +198,14 @@ app.post("/pay", auth, async (req, res, next) => { lnurlPayments[secret] = user.id; if (recipients[secret]) { - url = `https://coinos.io/api/send`; - const { data } = await axios.post( - url, + let data = await sendInternal( { amount, memo: comment, username: recipients[secret].username }, - { - headers: { - authorization: req.headers.authorization - } - } + req.hostname, + user ); res.send(data); diff --git a/routes/send.js b/routes/send.js index 5d46f4de..360ad5b2 100644 --- a/routes/send.js +++ b/routes/send.js @@ -1,223 +1,11 @@ -import config from "$config"; -import db from "$db"; -import { emit } from "$lib/sockets"; -import store from "$lib/store"; -import { callWebhook } from "$lib/webhooks"; -import axios from "axios"; -import { v4 } from "uuid"; -import { notify } from "$lib/notifications"; -import { sendLiquid } from "$routes/liquid/send"; -import { l, err } from "$lib/logging"; +import sendInternal from "$lib/sendInternal"; export default async (req, res, next) => { - let { - amount, - address, - payreq, - unconfidential, - asset, - memo, - username - } = req.body; - let { user } = req; - amount = parseInt(amount); - - if (!asset) asset = config.liquid.btcasset; - - if (!amount || amount < 0) - return res.code(500).send("Amount must be greater than zero"); - try { - await db.transaction(async transaction => { - let account; - if (user.account.asset === asset && !user.account.pubkey) { - account = await db.Account.findOne({ - where: { - id: user.account.id, - pubkey: null - }, - lock: transaction.LOCK.UPDATE, - transaction - }); - } else { - account = await db.Account.findOne({ - where: { - user_id: user.id, - asset, - pubkey: null - }, - lock: transaction.LOCK.UPDATE, - transaction - }); - } - - if (!account) throw new Error("Account not found"); - - if (account.balance < amount) { - throw new Error("Insufficient funds"); - } - - let fee = 0; - - await account.decrement({ balance: amount }, { transaction }); - await account.reload({ transaction }); - - let params = { - amount: -amount, - account_id: account.id, - memo, - user_id: user.id, - rate: store.rates[user.currency], - currency: user.currency, - confirmed: true, - hash: `#${v4().substr(0, 6)} ${ - username ? `Payment to ${username}` : "Internal Transfer" - }`, - network: "COINOS" - }; - - if (!username) { - l("creating redeemable payment"); - params.redeemcode = v4(); - params.hash = `${req.hostname}/redeem/${params.redeemcode}`; - } - - let payment = await db.Payment.create(params, { transaction }); - - payment = payment.get({ plain: true }); - payment.account = account.get({ plain: true }); - - l("sent internal", user.username, -payment.amount); - - emit(user.username, "payment", payment); - emit(user.username, "account", account); - - if (username) { - let recipient = await db.User.findOne( - { - where: { username }, - include: { - model: db.Account, - as: "account" - } - }, - { transaction } - ); - - params = { - where: { user_id: recipient.id }, - order: [["id", "DESC"]] - }; - if (address) params.where.address = address; - else if (payreq) params.where.text = payreq; - let invoice = (address || payreq) && (await db.Invoice.findOne(params)); - - let a2; - let acc = { - user_id: recipient.id, - asset, - pubkey: null - }; - - if (recipient.account.asset === asset && !recipient.account.pubkey) - a2 = recipient.account; - else { - a2 = await db.Account.findOne({ - where: acc, - lock: transaction.LOCK.UPDATE, - transaction - }); - } - - if (a2) { - await a2.increment({ balance: amount }, { transaction }); - await a2.reload({ transaction }); - } else { - let name = asset.substr(0, 6); - let domain; - let ticker = asset.substr(0, 3).toUpperCase(); - let precision = 8; - - if (store.assets[asset]) { - ({ domain, ticker, precision, name } = store.assets[asset]); - } else { - const existing = await db.Account.findOne({ - where: { - asset - }, - order: [["id", "ASC"]], - limit: 1, - lock: transaction.LOCK.UPDATE, - transaction - }); - - if (existing) { - ({ domain, ticker, precision, name } = existing); - } - } - - acc = { ...acc, ...{ domain, ticker, precision, name } }; - acc.balance = amount; - acc.pending = 0; - acc.network = "liquid"; - a2 = await db.Account.create(acc, { transaction }); - } - - let spread = user.username === "coinos" ? 1.02 : 1; - amount = Math.round(amount / spread); - - params = { - amount, - account_id: a2.id, - user_id: recipient.id, - rate: store.rates[recipient.currency] * spread, - currency: recipient.currency, - confirmed: true, - hash: `#${v4().substr(0, 6)} Payment from ${user.username}`, - memo, - network: "COINOS", - received: true - }; - - if (invoice) { - params.invoice_id = invoice.id; - - let c = store.convert[invoice.text]; - if (c) { - l( - "internal payment detected for conversion", - invoice.text, - c.address, - recipient.username - ); - - recipient.account = a2; - - sendLiquid({ - address: c.address, - amount: amount - 100, - user: recipient, - limit: amount - }).catch(console.log); - } - } - - let p2 = await db.Payment.create(params, { transaction }); - - p2 = p2.get({ plain: true }); - p2.account = a2.get({ plain: true }); - emit(recipient.username, "payment", p2); - emit(recipient.username, "account", p2.account); - - l("received internal", recipient.username, amount); - notify(recipient, `Received ${amount} ${a2.ticker} sats`); - callWebhook(invoice, p2); - } - res.send(payment); - }); + let payment = await sendInternal(req.body, req.hostname, req.user); + res.send(payment); } catch (e) { - console.log(e); - err("problem sending internal payment", user.username, e.message); + err("problem sending internal payment", e.message); return res.code(500).send(e.message); } }; -- cgit v1.2.3 From 1fe1bac84ca3ecc1159300768843958916f4eaac Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Wed, 14 Sep 2022 12:22:03 -0400 Subject: restart container policy --- package.json | 6 ++---- prod.yml | 2 ++ routes/lnurl.js | 1 - 3 files changed, 4 insertions(+), 5 deletions(-) (limited to 'routes') diff --git a/package.json b/package.json index 06c8a26f..518c2c1a 100644 --- a/package.json +++ b/package.json @@ -3,10 +3,8 @@ "version": "0.0.1", "private": true, "scripts": { - "start": "NODE_OPTIONS=\"--es-module-specifier-resolution=node --experimental-modules --loader ./loader.js --openssl-legacy-provider\" nodemon index.js", - "generate": "sequelize-automate -c config/index.js", - "min": "DEBUG=debug SCOPE=MIN nodemon index.js", - "debug": "DEBUG=debug nodemon index.js" + "start": "NODE_OPTIONS=\"--es-module-specifier-resolution=node --experimental-modules --loader ./loader.js --openssl-legacy-provider\" node index.js", + "dev": "NODE_OPTIONS=\"--es-module-specifier-resolution=node --experimental-modules --loader ./loader.js --openssl-legacy-provider\" nodemon index.js" }, "dependencies": { "@fastify/multipart": "^7.1.2", diff --git a/prod.yml b/prod.yml index c77c129e..dcd0c449 100644 --- a/prod.yml +++ b/prod.yml @@ -3,6 +3,7 @@ services: app: container_name: app image: gcr.io/coinos-326717/github.com/coinos/coinos-server:prod + restart: always ports: - '3118:3118' - '3119:3119' @@ -17,5 +18,6 @@ services: maria: container_name: maria image: mariadb + restart: always environment: MARIADB_ROOT_PASSWORD: pw diff --git a/routes/lnurl.js b/routes/lnurl.js index edebc94d..4335d566 100644 --- a/routes/lnurl.js +++ b/routes/lnurl.js @@ -30,7 +30,6 @@ const recipients = persist("data/recipients.json"); const lnurlPayments = persist("data/payments.json"); const withdrawals = persist("data/withdrawals.json"); -console.log("creating lnurl"); export const lnurlServer = lnurl.createServer(config.lnurl); app.get("/url", async (req, res, next) => { -- cgit v1.2.3