diff options
| -rw-r--r-- | lib/send.js | 36 | ||||
| -rw-r--r-- | package.json | 2 | ||||
| -rw-r--r-- | routes/bitcoin/fee.js | 7 | ||||
| -rw-r--r-- | routes/bitcoin/send.js | 5 | ||||
| -rw-r--r-- | routes/lightning/send.js | 4 | ||||
| -rw-r--r-- | routes/liquid/fee.js | 21 | ||||
| -rw-r--r-- | routes/liquid/index.js | 4 | ||||
| -rw-r--r-- | routes/liquid/send.js | 10 | ||||
| -rw-r--r-- | routes/lnurl.js | 1 | ||||
| -rw-r--r-- | routes/payments.js | 3 |
10 files changed, 53 insertions, 40 deletions
diff --git a/lib/send.js b/lib/send.js index a4280401..26709b61 100644 --- a/lib/send.js +++ b/lib/send.js @@ -1,3 +1,5 @@ +import config from "$config"; +import db from "$db"; import { emit } from "$lib/sockets"; import store from "./store"; import bolt11 from "bolt11"; @@ -6,9 +8,11 @@ import { computeConversionFee, conversionFeeReceiver } from "../routes/lightning/conversionFee"; -import ln from "$lib/ln"; +import lnd from "$lib/lnd"; +import { pay } from "lightning"; +import { l, warn, err } from "$lib/logging"; -export const send = async (amount, memo, hash, user) => { +export default async (amount, memo, hash, user) => { l("attempting lightning payment", user.username, amount, hash); let payreq = bolt11.decode(hash); @@ -25,7 +29,7 @@ export const send = async (amount, memo, hash, user) => { if (!amount || payreq.satoshis > amount) amount = payreq.satoshis; - if (seen.includes(hash)) { + if (store.seen.includes(hash)) { warn("attempted to pay a paid invoice", user.username); throw new Error("Invoice has been paid, can't pay again"); } @@ -82,21 +86,12 @@ export const send = async (amount, memo, hash, user) => { await account.reload({ transaction }); l("debited account", account.id); + let tokens; + if (!payreq.satoshis) tokens = amount; - if (config.lna.clightning) { - let m = await ln.pay(hash); - let sent; - ({ - msatoshi: total, - msatoshi_sent: sent, - payment_preimage: preimage - } = m); - - fee = parseInt((sent - total) / 1000); - total = parseInt(total / 1000); - } else { - let m = await ln.pay({ - tokens: amount, + let m = await pay({ + lnd, + tokens, request: hash, max_paths: 10, max_fee: { fixed: Math.min(10000, account.balance) } @@ -106,11 +101,10 @@ export const send = async (amount, memo, hash, user) => { if (m.payment_error) throw new Error(m.payment_error); ({ fee, secret: preimage, tokens: total } = m); - } preimage = preimage.toString("hex"); - if (seen.includes(preimage)) { + if (store.seen.includes(preimage)) { warn("duplicate payment detected", preimage); throw new Error("Duplicate payment detected"); } @@ -220,8 +214,8 @@ export const send = async (amount, memo, hash, user) => { emit(user.username, "account", payment.account); emit(user.username, "payment", payment); - seen.push(preimage); - seen.push(hash); + store.seen.push(preimage); + store.seen.push(hash); l("sent lightning", user.username, total - fee, fee); }); diff --git a/package.json b/package.json index 06c8a26f..870ceada 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "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", + "start": "DEBUG=lnurl* 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" diff --git a/routes/bitcoin/fee.js b/routes/bitcoin/fee.js index af622ca5..fb67c08e 100644 --- a/routes/bitcoin/fee.js +++ b/routes/bitcoin/fee.js @@ -1,5 +1,10 @@ +import config from "$config"; +import db from "$db"; +import bc from "$lib/bitcoin"; +import { err } from "$lib/logging"; import { emit } from "$lib/sockets"; import buildTx from "$lib/buildtx"; +import { SATS } from "$lib/utils"; export default async (req, res) => { let { user } = req; @@ -57,7 +62,7 @@ export default async (req, res) => { await bc.walletPassphrase(config.bitcoin.walletpass, 300); let signed = await bc.signRawTransactionWithWallet(tx.hex); - decoded = await bc.decodeRawTransaction(signed.hex); + let decoded = await bc.decodeRawTransaction(signed.hex); feeRate = Math.round((tx.fee * SATS * 1000) / decoded.vsize); res.send({ feeRate, tx }); diff --git a/routes/bitcoin/send.js b/routes/bitcoin/send.js index 51d702fd..bde3ce00 100644 --- a/routes/bitcoin/send.js +++ b/routes/bitcoin/send.js @@ -1,6 +1,11 @@ +import bc from "$lib/bitcoin"; +import { emit } from "$lib/sockets"; +import db from "$db"; import { toSats } from "$lib/utils"; import store from "$lib/store"; import config from "$config"; +import { l, warn, err } from "$lib/logging"; + const btc = config.liquid.btcasset; import { computeConversionFee, diff --git a/routes/lightning/send.js b/routes/lightning/send.js index c5de7ece..ba88aaf6 100644 --- a/routes/lightning/send.js +++ b/routes/lightning/send.js @@ -1,3 +1,6 @@ +import { err } from "$lib/logging"; +import send from "$lib/send"; + export default async (req, res) => { let { amount, route, memo, payreq } = req.body; let { user } = req; @@ -5,6 +8,7 @@ export default async (req, res) => { try { res.send(await send(amount, memo, payreq, user)); } catch (e) { + console.log(e) err( "problem sending lightning payment", user.username, diff --git a/routes/liquid/fee.js b/routes/liquid/fee.js index 34ee2553..4ec29455 100644 --- a/routes/liquid/fee.js +++ b/routes/liquid/fee.js @@ -1,9 +1,10 @@ +import db from "$db"; import { emit } from "$lib/sockets"; import buildTx from "$lib/buildliquidtx"; +import { err, l } from "$lib/logging"; -liquidTx = async ({ address, asset, amount, feeRate, replaceable, user }) => { +export const liquidTx = async ({ address, asset, amount, feeRate, replaceable, user }) => { let tx, fee; - let node = lq; if (user.account.pubkey) { let psbt = await buildTx({ @@ -27,7 +28,7 @@ liquidTx = async ({ address, asset, amount, feeRate, replaceable, user }) => { }); if (invoice) { - let { ismine } = await node.getAddressInfo(address); + let { ismine } = await lq.getAddressInfo(address); if (ismine) { emit(user.username, "to", invoice.user); return; @@ -35,7 +36,7 @@ liquidTx = async ({ address, asset, amount, feeRate, replaceable, user }) => { } if (config.liquid.walletpass) - await node.walletPassphrase(config.liquid.walletpass, 300); + await lq.walletPassphrase(config.liquid.walletpass, 300); amount = parseInt(amount); @@ -46,9 +47,9 @@ liquidTx = async ({ address, asset, amount, feeRate, replaceable, user }) => { let value = (amount / SATS).toFixed(8); if (feeRate) params.feeRate = (feeRate / SATS).toFixed(8); - let info = await node.getAddressInfo(address); + let info = await lq.getAddressInfo(address); - tx = await node.createRawTransaction( + tx = await lq.createRawTransaction( [], [ { @@ -62,12 +63,12 @@ liquidTx = async ({ address, asset, amount, feeRate, replaceable, user }) => { l("funding tx for fee estimate", tx); - tx = await node.fundRawTransaction(tx, params); + tx = await lq.fundRawTransaction(tx, params); - let blinded = await node.blindRawTransaction(tx.hex); - let signed = await node.signRawTransactionWithWallet(blinded); + let blinded = await lq.blindRawTransaction(tx.hex); + let signed = await lq.signRawTransactionWithWallet(blinded); - decoded = await node.decodeRawTransaction(signed.hex); + let decoded = await lq.decodeRawTransaction(signed.hex); feeRate = Math.round((tx.fee * SATS * 1000) / decoded.vsize); l("estimated", asset, feeRate); diff --git a/routes/liquid/index.js b/routes/liquid/index.js index 6da18363..b75cf23e 100644 --- a/routes/liquid/index.js +++ b/routes/liquid/index.js @@ -2,12 +2,10 @@ import broadcast from "./broadcast"; import generate from "./generate"; // import fee from "./fee"; import send from "./send"; -// import taxi from "./taxi"; export default { broadcast, generate, - // fee, +// fee, send, - // taxi }; diff --git a/routes/liquid/send.js b/routes/liquid/send.js index 5297de1c..8c94d976 100644 --- a/routes/liquid/send.js +++ b/routes/liquid/send.js @@ -1,15 +1,21 @@ +import db from "$db"; +import lq from "$lib/liquid"; import { toSats } from "$lib/utils"; import { emit } from "$lib/sockets"; import store from "$lib/store"; import config from "$config"; -const btc = config.liquid.btcasset; -const lcad = config.liquid.cadasset; +import { l, warn, err } from "$lib/logging"; +// import { liquidTx } from "$routes/liquid/fee"; // import { Transaction } from 'liquidjs-lib'; + import { computeConversionFee, conversionFeeReceiver } from "./conversionFee.js"; +const btc = config.liquid.btcasset; +const lcad = config.liquid.cadasset; + export const sendLiquid = async ({ asset, amount, user, address, memo, tx, limit }) => { try { l("sending liquid", amount, address); diff --git a/routes/lnurl.js b/routes/lnurl.js index 2749262d..a0d2a6d0 100644 --- a/routes/lnurl.js +++ b/routes/lnurl.js @@ -27,6 +27,7 @@ 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) => { diff --git a/routes/payments.js b/routes/payments.js index a9313520..1f9a94ec 100644 --- a/routes/payments.js +++ b/routes/payments.js @@ -87,8 +87,7 @@ 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("/taxi", auth, lqRoutes.taxi); +// app.post("/liquid/send", auth, lqRoutes.send); setTimeout(async () => { try { |
