From bb0941d6e97c753301bd70373d0436b1520e2869 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Sun, 10 Jan 2021 09:55:19 -0400 Subject: [PATCH] new listChannels() --- src/routes.js | 30 +++++++++++------------------- utils/lightningServices/types.ts | 13 +++++++++++++ utils/lightningServices/v2.js | 24 +++++++++++++++++++++++- 3 files changed, 47 insertions(+), 20 deletions(-) diff --git a/src/routes.js b/src/routes.js index 19b24506..f10fd1eb 100644 --- a/src/routes.js +++ b/src/routes.js @@ -1144,25 +1144,17 @@ module.exports = async ( }) // get lnd node opened channels list - app.get('/api/lnd/listchannels', (req, res) => { - const { lightning } = LightningServices.services - lightning.listChannels({}, async (err, response) => { - if (err) { - logger.debug('ListChannels Error:', err) - const health = await checkHealth() - if (health.LNDStatus.success) { - res.status(400).json({ - field: 'listChannels', - errorMessage: sanitizeLNDError(err.message) - }) - } else { - res.status(500) - res.json({ errorMessage: 'LND is down' }) - } - } - logger.debug('ListChannels:', response) - res.json(response) - }) + app.get('/api/lnd/listchannels', async (_, res) => { + try { + return res.json({ + channels: await LV2.listChannels() + }) + } catch (e) { + console.log(e) + return res.status(500).json({ + errorMessage: e.message + }) + } }) // get lnd node pending channels list diff --git a/utils/lightningServices/types.ts b/utils/lightningServices/types.ts index 4b8b5781..faa7f0b9 100644 --- a/utils/lightningServices/types.ts +++ b/utils/lightningServices/types.ts @@ -1,6 +1,7 @@ /** * @format */ +import * as Common from 'shock-common' export interface PaymentV2 { payment_hash: string @@ -131,3 +132,15 @@ export interface Services { walletUnlocker: Record router: Record } + +export interface ListChannelsReq { + active_only: boolean + inactive_only: boolean + public_only: boolean + private_only: boolean + /** + * Filters the response for channels with a target peer's pubkey. If peer is + * empty, all channels will be returned. + */ + peer: Common.Bytes +} diff --git a/utils/lightningServices/v2.js b/utils/lightningServices/v2.js index e9d5d0cc..467a15c8 100644 --- a/utils/lightningServices/v2.js +++ b/utils/lightningServices/v2.js @@ -444,11 +444,33 @@ const listUnspent = (minConfs = 3, maxConfs = 6) => ) }) +/** + * @typedef {import('./types').ListChannelsReq} ListChannelsReq + */ + +/** + * @param {ListChannelsReq} req + * @returns {Promise} + */ +const listChannels = req => + Common.makePromise((res, rej) => { + const { lightning } = lightningServices.getServices() + + lightning.listChannels(req, (err, resp) => { + if (err) { + rej(new Error(err.message)) + } else { + res(resp.channels) + } + }) + }) + module.exports = { sendPaymentV2Keysend, sendPaymentV2Invoice, listPayments, decodePayReq, newAddress, - listUnspent + listUnspent, + listChannels }