aboutsummaryrefslogtreecommitdiffstats
path: root/routes/lightning
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2022-09-05 22:58:23 -0700
committerAdam Soltys <[email protected]>2022-09-05 23:01:46 -0700
commit412df5f844de11b3fc71bb7ffaf9c8e1d9f19d65 (patch)
tree209296b3a93bc89b025e8ad0dcf846c23bb82746 /routes/lightning
parent8a82c69419dc358fbaeab322c32d18ee84656feb (diff)
downloadcoinos-server-412df5f844de11b3fc71bb7ffaf9c8e1d9f19d65.tar.gz
coinos-server-412df5f844de11b3fc71bb7ffaf9c8e1d9f19d65.zip
lightning api migration
Diffstat (limited to 'routes/lightning')
-rw-r--r--routes/lightning/channel.js15
-rw-r--r--routes/lightning/channelRequest.js2
-rw-r--r--routes/lightning/index.js2
-rw-r--r--routes/lightning/invoice.js14
-rw-r--r--routes/lightning/receive.js49
5 files changed, 48 insertions, 34 deletions
diff --git a/routes/lightning/channel.js b/routes/lightning/channel.js
index 5e8ed441..1c7bab10 100644
--- a/routes/lightning/channel.js
+++ b/routes/lightning/channel.js
@@ -1,15 +1,18 @@
import axios from "axios";
+import { addPeer, getIdentity } from "lightning";
+import lnd from "$lib/lnd";
export default async (req, res) => {
const { params } = req.body;
- const [pubkey, host] = params.uri.split("@");
+ const [public_key, socket] = params.uri.split("@");
- l("connecting to peer", req.user.username, pubkey, host);
+ l("connecting to peer", req.user.username, public_key, socket);
let result;
try {
- result = await lnp.connectPeer({
- addr: { pubkey, host },
- perm: true
+ result = await addPeer({
+ lnd,
+ socket,
+ public_key
});
} catch (e) {
if (!e.message.includes("already connected")) {
@@ -21,7 +24,7 @@ export default async (req, res) => {
const { callback, k1 } = params;
let remoteid;
try {
- remoteid = (await lnp.getInfo({})).identity_pubkey;
+ remoteid = (await getIdentity({ lnd })).public_key;
} catch (e) {
err("problem getting lightning node info", e.message);
return res.code(500).send(e.message);
diff --git a/routes/lightning/channelRequest.js b/routes/lightning/channelRequest.js
index 447a3946..530fad48 100644
--- a/routes/lightning/channelRequest.js
+++ b/routes/lightning/channelRequest.js
@@ -1,4 +1,4 @@
-import { lnurlServer } from "$lnurl";
+import { lnurlServer } from "$routes/lnurl";
export default async (req, res) => {
let { localAmt, pushAmt } = req.body;
diff --git a/routes/lightning/index.js b/routes/lightning/index.js
index 97c7c056..52a94398 100644
--- a/routes/lightning/index.js
+++ b/routes/lightning/index.js
@@ -1,12 +1,10 @@
import channel from "./channel";
-import channelRequest from "./channelRequest";
import invoice from "./invoice";
import query from "./query";
import send from "./send";
export default {
channel,
- channelRequest,
invoice,
query,
send
diff --git a/routes/lightning/invoice.js b/routes/lightning/invoice.js
index 40787e3c..f58169eb 100644
--- a/routes/lightning/invoice.js
+++ b/routes/lightning/invoice.js
@@ -1,3 +1,8 @@
+import config from "$config";
+import { createInvoice } from "lightning";
+import { err } from "$lib/logging";
+import lnd from "$lib/lnd";
+
export default async (req, res) => {
let { amount, memo, tip } = req.body;
if (!tip) tip = 0;
@@ -14,8 +19,13 @@ export default async (req, res) => {
);
res.send({ text: invoice.bolt11 });
} else {
- const invoice = await lnp.addInvoice({ value, memo });
- res.send({ text: invoice.payment_request });
+ const invoice = await createInvoice({
+ lnd,
+ tokens: value,
+ description: memo
+ });
+ console.log(invoice)
+ res.send({ text: invoice.request });
}
} catch (e) {
err("problem creating invoice", e.message, e.stack);
diff --git a/routes/lightning/receive.js b/routes/lightning/receive.js
index 790338d2..c01592d2 100644
--- a/routes/lightning/receive.js
+++ b/routes/lightning/receive.js
@@ -1,43 +1,43 @@
import config from "$config";
import { notify } from "$lib/notifications";
import { callWebhook } from "$lib/webhooks";
-import { computeConversionFee } from './conversionFee';
-import { sendLiquid } from "$liquid/send";
-import ln from "$lib/ln";
+import { computeConversionFee } from "./conversionFee";
+import { sendLiquid } from "$routes/liquid/send";
+import { subscribeToInvoices } from "lightning";
+import lnd from "$lib/lnd";
-const handlePayment = async (msg) => {
+const handlePayment = async msg => {
l("incoming lightning payment", msg.value, msg.payment_request, msg.settled);
if (!msg.settled) return;
let account, total, user;
const invoice = await db.Invoice.findOne({
where: {
- text: msg.payment_request,
- },
+ text: msg.payment_request
+ }
});
if (!invoice)
return warn("received lightning with no invoice", msg.payment_request);
try {
- await db.transaction(async (transaction) => {
+ await db.transaction(async transaction => {
const { text: hash, currency, memo, rate, tip, user_id } = invoice;
const amount = parseInt(msg.amt_paid_sat) - tip;
- if (amount < 0)
- throw new Error("amount out of range");
+ if (amount < 0) throw new Error("amount out of range");
account = await db.Account.findOne({
where: {
user_id,
asset: config.liquid.btcasset,
- pubkey: null,
+ pubkey: null
},
include: {
model: db.User,
- as: "user",
+ as: "user"
},
lock: transaction.LOCK.UPDATE,
- transaction,
+ transaction
});
({ user } = account);
@@ -58,7 +58,7 @@ const handlePayment = async (msg) => {
confirmed: true,
network: "lightning",
tip,
- invoice_id: invoice.id,
+ invoice_id: invoice.id
},
{ transaction }
);
@@ -66,12 +66,15 @@ const handlePayment = async (msg) => {
total = amount + tip;
invoice.received += total;
- invoice.status = 'paid';
+ invoice.status = "paid";
await invoice.save({ 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.increment(
+ { lightning_credits: computeConversionFee(total) },
+ { transaction }
+ );
await account.reload({ transaction });
await invoice.save({ transaction });
await payment.save({ transaction });
@@ -79,7 +82,7 @@ const handlePayment = async (msg) => {
payment = payment.get({ plain: true });
payment.account = account.get({ plain: true });
- payment.invoice= invoice.get({ plain: true });
+ payment.invoice = invoice.get({ plain: true });
callWebhook(invoice, payment);
@@ -111,7 +114,7 @@ const handlePayment = async (msg) => {
address: c.address,
amount: total - 100,
user,
- limit: total,
+ limit: total
});
} catch (e) {
err("problem sending liquid payment", e.message, e.stack);
@@ -123,14 +126,14 @@ const handlePayment = async (msg) => {
};
if (config.lna.clightning) {
- const poll = async (ln) => {
- const wait = async (i) => {
+ const poll = async ln => {
+ const wait = async i => {
const {
bolt11: payment_request,
pay_index,
status,
msatoshi_received,
- payment_preimage: r_preimage,
+ payment_preimage: r_preimage
} = await ln.waitanyinvoice(i);
let settled = status === "paid";
@@ -140,17 +143,17 @@ if (config.lna.clightning) {
payment_request,
settled,
amt_paid_sat,
- r_preimage,
+ r_preimage
});
wait(pay_index);
};
const { invoices } = await ln.listinvoices();
- wait(Math.max(...invoices.map((i) => i.pay_index).filter((n) => n)));
+ wait(Math.max(...invoices.map(i => i.pay_index).filter(n => n)));
};
poll(ln);
} else {
- const invoices = ln.subscribeInvoices({});
+ const invoices = subscribeToInvoices({ lnd });
invoices.on("data", handlePayment);
}