diff options
| author | Adam Soltys <[email protected]> | 2020-05-07 20:15:56 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2020-05-07 20:58:24 -0700 |
| commit | 47aa48402a0d34dd8ab11f0f315393fcc4f5c61b (patch) | |
| tree | 2e907e64628d26c4707660e854e9bc87b332ff08 /routes/lightning/receive.js | |
| parent | 2f0f9e640ead76518f7f8013b16704da89421ee4 (diff) | |
| download | coinos-server-47aa48402a0d34dd8ab11f0f315393fcc4f5c61b.tar.gz coinos-server-47aa48402a0d34dd8ab11f0f315393fcc4f5c61b.zip | |
c-lightning support
Diffstat (limited to 'routes/lightning/receive.js')
| -rw-r--r-- | routes/lightning/receive.js | 45 |
1 files changed, 39 insertions, 6 deletions
diff --git a/routes/lightning/receive.js b/routes/lightning/receive.js index 286541d2..3082c205 100644 --- a/routes/lightning/receive.js +++ b/routes/lightning/receive.js @@ -21,13 +21,15 @@ const handlePayment = async msg => { } }); + let preimage = msg.r_preimage.toString("hex"); + let payment = await db.Payment.create({ account_id: account.id, user_id, hash, amount, currency, - preimage: msg.r_preimage.toString('hex'), + preimage, rate, received: true, confirmed: true, @@ -54,12 +56,43 @@ const handlePayment = async msg => { "lightning payment received", user.username, payment.amount, - payment.tip, + payment.tip ); }; -const invoices = lna.subscribeInvoices({}); -invoices.on("data", handlePayment); +if (config.lna.clightning) { + const poll = async ln => { + const wait = async i => { + const { + bolt11: payment_request, + pay_index, + status, + msatoshi_received, + payment_preimage: r_preimage + } = await ln.waitanyinvoice(i); + + let settled = status === "paid"; + let amt_paid_sat = parseInt(msatoshi_received / 1000); + + await handlePayment({ + payment_request, + settled, + amt_paid_sat, + r_preimage + }); + wait(pay_index); + }; + + const { invoices } = await ln.listinvoices(); + wait(Math.max(...invoices.map(i => i.pay_index).filter(n => n))); + }; + + poll(lna); + poll(lnb); +} else { + const invoices = lna.subscribeInvoices({}); + invoices.on("data", handlePayment); -const invoicesb = lnb.subscribeInvoices({}); -invoicesb.on("data", handlePayment); + const invoicesb = lnb.subscribeInvoices({}); + invoicesb.on("data", handlePayment); +} |
