From 8a96a27a37e1ab23a509e109d3377c12ee020312 Mon Sep 17 00:00:00 2001 From: Nitesh Balusu <84944042+niteshbalusu11@users.noreply.github.com> Date: Thu, 20 Jan 2022 14:43:55 -0500 Subject: [PATCH] get saved nodes list --- .gitignore | 1 - .prettierignore | 2 + bos | 8 ++- lnd/get_saved_nodes.js | 127 +++++++++++++++++++++++++++++++++++++++++ lnd/index.js | 2 + 5 files changed, 137 insertions(+), 3 deletions(-) create mode 100644 .prettierignore create mode 100644 lnd/get_saved_nodes.js diff --git a/.gitignore b/.gitignore index aea35a2..dd53702 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,3 @@ node_modules .prettierrc .vscode/settings.json -.prettierignore diff --git a/.prettierignore b/.prettierignore new file mode 100644 index 0000000..06febd3 --- /dev/null +++ b/.prettierignore @@ -0,0 +1,2 @@ +bos +*.js \ No newline at end of file diff --git a/bos b/bos index 813e772..8430209 100755 --- a/bos +++ b/bos @@ -16,7 +16,7 @@ const fiat = importLazy('@alexbosworth/fiat'); const inquirer = importLazy('inquirer'); const lnService = importLazy('ln-service'); const lnSync = importLazy('ln-sync'); -const paidServices = importLazy('paid-services'); +const paidServices = require('../paid-services') const prog = require('@alexbosworth/caporal'); const commandConstants = require('./commands/constants'); @@ -300,10 +300,14 @@ prog .action((args, options, logger) => { return new Promise(async (resolve, reject) => { try { + const authLnd = (await lnd.authenticatedLnd({logger, node: options.node})).lnd + const savedNodes = await lnd.getSavedNodes({lnd: authLnd, logger}) + return paidServices.changeChannelCapacity({ logger, ask: (n, cbk) => inquirer.prompt([n]).then(res => cbk(res)), - lnd: (await lnd.authenticatedLnd({logger, node: options.node})).lnd, + lnd: authLnd, + saved_nodes: savedNodes, }, responses.returnObject({exit, logger, reject, resolve})); } catch (err) { diff --git a/lnd/get_saved_nodes.js b/lnd/get_saved_nodes.js new file mode 100644 index 0000000..f8d35bc --- /dev/null +++ b/lnd/get_saved_nodes.js @@ -0,0 +1,127 @@ +const asyncAuto = require('async/auto'); +const {returnResult} = require('asyncjs-util'); + +const {getNetwork} = require('ln-sync'); +const {getWalletInfo} = require('ln-service'); + +const getLnds = require('./get_lnds'); + +const fs = require('fs'); +const {join} = require('path'); +const {homedir} = require('os'); + +const home = '.bos'; +const fileName = 'credentials.json'; +const path = join(...[homedir(), home]); +const onlineLnds = []; +const savedNodeFolders = []; + + +module.exports = ({lnd, logger}, cbk) => { + return new Promise((resolve, reject) => { + return asyncAuto({ + //Check arguments + validate: cbk => { + if(!lnd) { + return cbk([400, 'ExpectedLndForGettingSavedNodes']); + } + + if(!logger) { + return cbk([400, 'ExpectedLoggerForGettingSavedNodes']); + } + + return cbk(); + }, + + //Get network of current lnd object + getCurrentNetwork: ['validate', ({}, cbk) => { + return getNetwork({lnd}, cbk); + }], + + //Get public key of current lnd object + getPublicKey: ['validate', ({}, cbk) => { + return getWalletInfo({lnd}, cbk); + }], + + //Get list of saved node folders + getSavedNodeFolders: ['validate', async ({}) => { + + //Read all files in .bos dir and check if its a directory + try { + const directories = (path) => { + return fs.readdirSync(path).filter(function (file) { + return fs.statSync(path+'/'+file).isDirectory(); + }); + } + const getDirectories = directories(path); + + //check if credentials.json file exists in each folder to know if its a saved node + getDirectories.forEach(directory => { + const filePath = join(...[homedir(), home, directory, fileName]); + + if(fs.existsSync(filePath)) { + savedNodeFolders.push(directory); + } + }); + + return {nodes: savedNodeFolders}; + } catch (err) { + logger.error({error: err}); + } + }], + + //Get lnd objects for each saved node + getLnds: ['getSavedNodeFolders', async ({getSavedNodeFolders}) => { + + if(!getSavedNodeFolders || !getSavedNodeFolders.nodes || !getSavedNodeFolders.nodes.length) { + return; + } + try { + const {nodes} = getSavedNodeFolders; + + const lnds = await getLnds({logger, nodes}); + + return lnds; + + } catch (err) { + logger.error({error: err}); + } + }], + + //Get the lnds that are online + getOnlineLnds: [ + 'getLnds', + 'getCurrentNetwork', + 'getPublicKey', + async ({getLnds, getCurrentNetwork, getPublicKey}) => { + + if(!getLnds || !getLnds.lnds || !getLnds.lnds.length) { + return; + } + + const {lnds} = getLnds; + + //Match the network of current lnd object with list of objects + for(let i=0; i< lnds.length; i++) { + try { + const {network} = await getNetwork({lnd: lnds[i]}); + + if(getCurrentNetwork.network === network) { + + const {alias, public_key} = await getWalletInfo({lnd: lnds[i]}); + + //remove the current lnd object from the list of objects + if(public_key !== getPublicKey.public_key) { + onlineLnds.push({alias: alias, public_key: public_key, lnd: lnds[i]}); + } + } + } catch(err) { + } + } + return onlineLnds; + }], + + }, + returnResult({reject, resolve, of: 'getOnlineLnds'}, cbk)); + }) +} \ No newline at end of file diff --git a/lnd/index.js b/lnd/index.js index 2bf9e39..fe50f42 100644 --- a/lnd/index.js +++ b/lnd/index.js @@ -4,6 +4,7 @@ const gateway = require('./gateway'); const getCertValidityDays = require('./get_cert_validity_days'); const getCredentials = require('./get_credentials'); const getLnds = require('./get_lnds'); +const getSavedNodes = require('./get_saved_nodes'); const lndCredentials = require('./lnd_credentials'); module.exports = { @@ -13,5 +14,6 @@ module.exports = { getCertValidityDays, getCredentials, getLnds, + getSavedNodes, lndCredentials, };