From 169d527429ad9ffe759f322dd9906692cdf3f35e Mon Sep 17 00:00:00 2001 From: Alex Bosworth Date: Thu, 7 Jul 2022 17:21:45 -0700 Subject: [PATCH] merge changes --- README.md | 4 ++++ display/get_icons.js | 5 ++-- lnd/lnd_credentials.js | 6 ++--- nodes/adjust_tags.js | 6 ++--- nodes/constants.json | 3 --- nodes/delete_node_credentials.js | 7 ++---- nodes/get_saved_credentials.js | 4 +--- nodes/get_saved_nodes.js | 11 ++------- nodes/manage_saved_nodes.js | 5 +--- nodes/put_saved_credentials.js | 7 ++---- nodes/register_node.js | 10 ++++---- storage/home_path.js | 13 ++++++++--- swaps/rebalance.js | 5 +--- tags/get_tags.js | 5 +--- telegram/get_telegram_bot.js | 40 ++++++++++++++------------------ 15 files changed, 53 insertions(+), 78 deletions(-) delete mode 100644 nodes/constants.json diff --git a/README.md b/README.md index 5bd222a..0e2c10a 100644 --- a/README.md +++ b/README.md @@ -249,6 +249,10 @@ Use any shorthand you'd like when choosing this profile node name You can also set `cert_path` and `macaroon_path` to the path of the relevant files instead. +The BOS directory path can be overriden with an environment variable: + +`BOS_DATA_PATH=/path/to/bos/data/dir` + #### Umbrel Saved Node *Note: Umbrel is not FOSS software, use at your own risk.* diff --git a/display/get_icons.js b/display/get_icons.js index 24ec813..7c73e01 100644 --- a/display/get_icons.js +++ b/display/get_icons.js @@ -1,13 +1,12 @@ -const {join} = require('path'); - const asyncAuto = require('async/auto'); const {returnResult} = require('asyncjs-util'); const {homePath} = require('../storage'); + const flatten = arr => [].concat(...arr); const {isArray} = Array; const {parse} = JSON; -const tagFilePath = () => join(...[homePath({}), 'tags.json']); +const tagFilePath = () => homePath({file: 'tags.json'}).path; const uniq = arr => Array.from(new Set(arr)); /** Get icons for public keys from tags diff --git a/lnd/lnd_credentials.js b/lnd/lnd_credentials.js index c6364c0..fdc4574 100644 --- a/lnd/lnd_credentials.js +++ b/lnd/lnd_credentials.js @@ -1,5 +1,4 @@ const {homedir} = require('os'); -const {join} = require('path'); const {platform} = require('os'); const {publicEncrypt} = require('crypto'); const {readFile} = require('fs'); @@ -20,9 +19,9 @@ const getMacaroon = require('./get_macaroon'); const getPath = require('./get_path'); const {getSavedCredentials} = require('./../nodes'); const getSocket = require('./get_socket'); +const {homePath} = require('../storage'); const {noSpendPerms} = require('./constants'); const {permissionEntities} = require('./constants'); -const {homePath} = require('../storage'); const config = 'config.json'; const defaultLndDirPath = process.env.BOS_DEFAULT_LND_PATH; @@ -67,7 +66,8 @@ module.exports = (args, cbk) => { return cbk(null, defaultNodeName); } - const path = join(...[homePath({}), config]); + // Look for a config file to see if there is a default node + const {path} = homePath({file: config}); return fs.getFile(path, (err, res) => { // Exit early on errors, there is no config found diff --git a/nodes/adjust_tags.js b/nodes/adjust_tags.js index 4b9018f..52c2534 100644 --- a/nodes/adjust_tags.js +++ b/nodes/adjust_tags.js @@ -1,5 +1,3 @@ -const {homedir} = require('os'); -const {join} = require('path'); const {randomBytes} = require('crypto'); const asyncAuto = require('async/auto'); @@ -16,7 +14,7 @@ const makeId = () => randomBytes(32).toString('hex'); const makeTag = (alias, id) => ({alias, id}); const {parse} = JSON; const stringify = obj => JSON.stringify(obj, null, 2); -const tagFilePath = () => join(...[homePath({}), 'tags.json']); +const tagFilePath = () => homePath({file: 'tags.json'}).path; const uniq = arr => Array.from(new Set(arr)); /** Adjust tags @@ -82,7 +80,7 @@ module.exports = (args, cbk) => { // Register the home directory registerHomeDir: ['validate', ({}, cbk) => { - return args.fs.makeDirectory(join(...[homePath({})]), err => { + return args.fs.makeDirectory(homePath({}).path, err => { // Ignore errors, the directory may already be there return cbk(); }); diff --git a/nodes/constants.json b/nodes/constants.json deleted file mode 100644 index ce0f52b..0000000 --- a/nodes/constants.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "home": ".bos" -} diff --git a/nodes/delete_node_credentials.js b/nodes/delete_node_credentials.js index 8cc245f..2146008 100644 --- a/nodes/delete_node_credentials.js +++ b/nodes/delete_node_credentials.js @@ -1,4 +1,3 @@ -const {homedir} = require('os'); const {join} = require('path'); const asyncAuto = require('async/auto'); @@ -38,7 +37,7 @@ module.exports = ({fs, node}, cbk) => { // Remove credentials file removeCredentials: ['validate', ({}, cbk) => { - const path = join(...[homePath({}), node, credentialsFileName]); + const path = join(...[homePath({}).path, node, credentialsFileName]); return fs.removeFile(path, err => { if (!!err) { @@ -51,9 +50,7 @@ module.exports = ({fs, node}, cbk) => { // Remove credentials directory removeDirectory: ['removeCredentials', ({}, cbk) => { - const path = join(...[homePath({}), node]); - - return fs.removeDirectory(path, err => { + return fs.removeDirectory(homePath({file: node}).path, err => { if (!!err) { return cbk([503, 'FailedToRemoveCredentialsDirectory', {err}]); } diff --git a/nodes/get_saved_credentials.js b/nodes/get_saved_credentials.js index b7e23f6..ac7b9fb 100644 --- a/nodes/get_saved_credentials.js +++ b/nodes/get_saved_credentials.js @@ -1,4 +1,3 @@ -const {homedir} = require('os'); const {join} = require('path'); const asyncAuto = require('async/auto'); @@ -6,7 +5,6 @@ const {returnResult} = require('asyncjs-util'); const {homePath} = require('../storage'); - const credentials = 'credentials.json'; const {isArray} = Array; const {parse} = JSON; @@ -54,7 +52,7 @@ module.exports = ({fs, node}, cbk) => { // Get the credentials file getFile: ['validate', ({}, cbk) => { - const path = join(...[homePath({}), node, credentials]); + const path = join(...[homePath({}).path, node, credentials]); return fs.getFile(path, (err, res) => { // Exit early on errors, there is no credential found diff --git a/nodes/get_saved_nodes.js b/nodes/get_saved_nodes.js index 30c049c..1671f71 100644 --- a/nodes/get_saved_nodes.js +++ b/nodes/get_saved_nodes.js @@ -1,6 +1,3 @@ -const {homedir} = require('os'); -const {join} = require('path'); - const asyncAuto = require('async/auto'); const asyncFilter = require('async/filter'); const asyncMap = require('async/map'); @@ -58,9 +55,7 @@ module.exports = ({fs, network}, cbk) => { }, // Data directory - dataDir: ['validate', ({}, cbk) => { - return cbk(null, join(...[homePath({})])); - }], + dataDir: ['validate', ({}, cbk) => cbk(null, homePath({}).path)], // Check that the data directory exists checkDataDir: ['dataDir', ({dataDir}, cbk) => { @@ -81,9 +76,7 @@ module.exports = ({fs, network}, cbk) => { getDirs: ['checkDataDir', 'dataDir', ({dataDir}, cbk) => { return fs.getDirectoryFiles(dataDir, (err, files) => { return asyncFilter(files, (file, cbk) => { - const path = join(...[homePath({}), file]); - - return fs.getFileStatus(path, (err, res) => { + return fs.getFileStatus(homePath({file}).path, (err, res) => { if (!!err) { return cbk([503, 'UnexpectedErrCheckingForNodeDir', {err}]); } diff --git a/nodes/manage_saved_nodes.js b/nodes/manage_saved_nodes.js index 10f18d8..1e391c1 100644 --- a/nodes/manage_saved_nodes.js +++ b/nodes/manage_saved_nodes.js @@ -1,6 +1,3 @@ -const {homedir} = require('os'); -const {join} = require('path'); - const asyncAuto = require('async/auto'); const {generateKeyPair} = require('crypto'); const {privateDecrypt} = require('crypto'); @@ -96,7 +93,7 @@ module.exports = (args, cbk) => { // Make sure the home directory is there registerHomeDir: ['validate', ({}, cbk) => { - return args.fs.makeDirectory(join(...[homePath({})]), err => { + return args.fs.makeDirectory(homePath({}).path, err => { // Ignore errors, the directory may already be there return cbk(); }); diff --git a/nodes/put_saved_credentials.js b/nodes/put_saved_credentials.js index 6392b87..bc518d0 100644 --- a/nodes/put_saved_credentials.js +++ b/nodes/put_saved_credentials.js @@ -1,5 +1,4 @@ const {join} = require('path'); -const {homedir} = require('os'); const asyncAuto = require('async/auto'); const {returnResult} = require('asyncjs-util'); @@ -61,9 +60,7 @@ module.exports = (args, cbk) => { // Make sure the node directory is there registerDirectory: ['validate', ({}, cbk) => { - const nodeDirectory = join(...[homePath({}), args.node]); - - return args.fs.makeDirectory(nodeDirectory, err => { + return args.fs.makeDirectory(homePath({file: args.node}).path, err => { // Ignore errors, the directory may already be there return cbk(); }); @@ -79,7 +76,7 @@ module.exports = (args, cbk) => { socket: args.socket, }); - const path = join(...[homePath({}), args.node, credentials]); + const path = join(...[homePath({}).path, args.node, credentials]); return args.fs.writeFile(path, file, err => { if (!!err) { diff --git a/nodes/register_node.js b/nodes/register_node.js index 488f46a..7d9507d 100644 --- a/nodes/register_node.js +++ b/nodes/register_node.js @@ -1,6 +1,3 @@ -const {homedir} = require('os'); -const {join} = require('path'); - const asyncAuto = require('async/auto'); const {decodeFirst} = require('cbor'); const inquirer = require('inquirer'); @@ -62,7 +59,7 @@ module.exports = ({ask, cryptography, fs, logger, node}, cbk) => { // Make sure the home directory is there registerHomeDirectory: ['validate', ({}, cbk) => { - return fs.makeDirectory(join(...[homePath({})]), err => { + return fs.makeDirectory(homePath({}).path, err => { // Ignore errors, the directory may already be there return cbk(); }); @@ -293,9 +290,10 @@ module.exports = ({ask, cryptography, fs, logger, node}, cbk) => { nodeDir: [ 'details', 'nodeName', - 'registerHomeDirectory', ({nodeName}, cbk) => + 'registerHomeDirectory', + ({nodeName}, cbk) => { - return fs.makeDirectory(join(...[homePath({}), nodeName]), err => { + return fs.makeDirectory(homePath({file: nodeName}).path, err => { // Ignore errors, the directory may already be there return cbk(); }); diff --git a/storage/home_path.js b/storage/home_path.js index e0b1b7f..f422846 100644 --- a/storage/home_path.js +++ b/storage/home_path.js @@ -4,11 +4,18 @@ const {join} = require('path'); const home = join(...[homedir(), '.bos']); /** Get the path of the bos storage directory + + { + file: + } + @returns { - home: + path: } */ -module.exports = ({}) => { - return process.env.BOS_DIRECTORY || home; +module.exports = ({file}) => { + const dir = process.env.BOS_DATA_PATH || home; + + return {path: join(...[dir, file].filter(n => !!n))}; }; diff --git a/swaps/rebalance.js b/swaps/rebalance.js index ced208d..8c2176e 100644 --- a/swaps/rebalance.js +++ b/swaps/rebalance.js @@ -1,6 +1,3 @@ -const {homedir} = require('os'); -const {join} = require('path'); - const asyncAuto = require('async/auto'); const asyncMap = require('async/map'); const asyncMapSeries = require('async/mapSeries'); @@ -59,7 +56,7 @@ const pubKeyHexLength = 66; const rateDivisor = 1e6; const sample = a => !!a.length ? a[Math.floor(Math.random()*a.length)] : null; const sumOf = arr => arr.reduce((sum, n) => sum + n); -const tagFilePath = () => join(...[homePath({}), 'tags.json']); +const tagFilePath = () => homePath({file: 'tags.json'}).path; const times = 6; const tokAsBigTok = tokens => !tokens ? undefined : (tokens / 1e8).toFixed(8); const topOf = arr => arr.slice(0, Math.ceil(arr.length / 2)); diff --git a/tags/get_tags.js b/tags/get_tags.js index 43f561d..d20748d 100644 --- a/tags/get_tags.js +++ b/tags/get_tags.js @@ -1,6 +1,3 @@ -const {homedir} = require('os'); -const {join} = require('path'); - const asyncAuto = require('async/auto'); const {returnResult} = require('asyncjs-util'); @@ -9,7 +6,7 @@ const {homePath} = require('../storage'); const defaultTags = {tags: []}; const {isArray} = Array; const {parse} = JSON; -const tagFilePath = () => join(...[homePath({}), 'tags.json']); +const tagFilePath = () => homePath({file: 'tags.json'}).path; /** Get tagged nodes diff --git a/telegram/get_telegram_bot.js b/telegram/get_telegram_bot.js index 37288f8..8a66366 100644 --- a/telegram/get_telegram_bot.js +++ b/telegram/get_telegram_bot.js @@ -1,6 +1,3 @@ -const {homedir} = require('os'); -const {join} = require('path'); - const asyncAuto = require('async/auto'); const asyncReflect = require('async/reflect'); const {Bot} = require('grammy'); @@ -43,22 +40,9 @@ module.exports = ({fs, proxy}, cbk) => { return cbk(); }, - // Get proxy agent - getProxy: ['validate', ({}, cbk) => { - // Exit early if not using a proxy - if (!proxy) { - return cbk(); - } - - return getSocksProxy({fs, path: proxy}, cbk); - }], - - // Home directory path - path: ['validate', ({}, cbk) => cbk(null, join(...[homePath({})]))], - // Ask for an API key - apiKey: ['path', ({path}, cbk) => { - return fs.getFile(join(...[path, botKeyFile]), (err, res) => { + apiKey: ['validate', ({}, cbk) => { + return fs.getFile(homePath({file: botKeyFile}).path, (err, res) => { // Exit early when resetting the API key if (!!err || !res || !res.toString() || !!fs.is_reset_state) { const token = interaction.api_token_prompt; @@ -72,6 +56,16 @@ module.exports = ({fs, proxy}, cbk) => { }); }], + // Get proxy agent + getProxy: ['validate', ({}, cbk) => { + // Exit early if not using a proxy + if (!proxy) { + return cbk(); + } + + return getSocksProxy({fs, path: proxy}, cbk); + }], + // Create the bot createBot: ['apiKey', 'getProxy', ({apiKey, getProxy}, cbk) => { const {key} = apiKey; @@ -96,19 +90,21 @@ module.exports = ({fs, proxy}, cbk) => { }], // Make the home directory if it's not already present - makeDir: ['apiKey', 'path', 'test', asyncReflect(({path}, cbk) => { - return fs.makeDirectory(path, cbk); + makeDir: ['apiKey', 'test', asyncReflect(({}, cbk) => { + return fs.makeDirectory(homePath({}).path, cbk); })], // Save the bot API key so it doesn't need to be entered next run - saveKey: ['apiKey', 'path', 'makeDir', ({apiKey, path}, cbk) => { + saveKey: ['apiKey', 'makeDir', ({apiKey}, cbk) => { // Exit early when API key is already saved if (!!apiKey.is_saved) { return cbk(); } + const {path} = homePath({file: botKeyFile}).path; + // Ignore errors when making directory, it may already be present - return fs.writeFile(join(...[path, botKeyFile]), apiKey.key, err => { + return fs.writeFile(path, apiKey.key, err => { if (!!err) { return cbk([503, 'FailedToSaveTelegramApiToken', {err}]); }