get saved nodes list

This commit is contained in:
Nitesh Balusu 2022-01-20 14:43:55 -05:00 committed by Alex Bosworth
parent 5a12d67e50
commit 8a96a27a37
No known key found for this signature in database
GPG key ID: E80D2F3F311FD87E
5 changed files with 137 additions and 3 deletions

1
.gitignore vendored
View file

@ -5,4 +5,3 @@
node_modules
.prettierrc
.vscode/settings.json
.prettierignore

2
.prettierignore Normal file
View file

@ -0,0 +1,2 @@
bos
*.js

8
bos
View file

@ -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) {

127
lnd/get_saved_nodes.js Normal file
View file

@ -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));
})
}

View file

@ -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,
};