From 5cab6deeb17c04b0243cac229365394e481a519b Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Sun, 10 Jan 2021 13:27:03 -0400 Subject: [PATCH] new listPeers() --- src/routes.js | 30 +++++++++++------------------- utils/lightningServices/v2.js | 28 +++++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 20 deletions(-) diff --git a/src/routes.js b/src/routes.js index 7de4143e..99ed7dcf 100644 --- a/src/routes.js +++ b/src/routes.js @@ -1053,25 +1053,17 @@ module.exports = async ( }) // get lnd node active channels list - app.get('/api/lnd/listpeers', (req, res) => { - const { lightning } = LightningServices.services - lightning.listPeers({}, async (err, response) => { - if (err) { - logger.debug('ListPeers Error:', err) - const health = await checkHealth() - if (health.LNDStatus.success) { - res.status(400).json({ - field: 'listPeers', - errorMessage: sanitizeLNDError(err.message) - }) - } else { - res.status(500) - res.json({ errorMessage: 'LND is down' }) - } - } - logger.debug('ListPeers:', response) - res.json(response) - }) + app.get('/api/lnd/listpeers', async (req, res) => { + try { + return res.json({ + peers: await LV2.listPeers(req.body.latestError) + }) + } catch (e) { + console.log(e) + return res.status(500).json({ + errorMessage: e.message + }) + } }) // newaddress diff --git a/utils/lightningServices/v2.js b/utils/lightningServices/v2.js index 6857d870..a158aed1 100644 --- a/utils/lightningServices/v2.js +++ b/utils/lightningServices/v2.js @@ -490,6 +490,31 @@ const getChanInfo = chanID => ) }) +/** + * https://api.lightning.community/#listpeers + * @param {boolean=} latestError If true, only the last error that our peer sent + * us will be returned with the peer's information, rather than the full set of + * historic errors we have stored. + * @returns {Promise} + */ +const listPeers = latestError => + Common.makePromise((res, rej) => { + const { lightning } = lightningServices.getServices() + + lightning.listPeers( + { + latest_error: latestError + }, + (err, resp) => { + if (err) { + rej(new Error(err.message)) + } else { + res(resp.peers) + } + } + ) + }) + module.exports = { sendPaymentV2Keysend, sendPaymentV2Invoice, @@ -498,5 +523,6 @@ module.exports = { newAddress, listUnspent, listChannels, - getChanInfo + getChanInfo, + listPeers }