Ride-The-Lightning-RTL/common.js

111 lines
3.4 KiB
JavaScript
Raw Normal View History

var fs = require('fs');
var crypto = require('crypto');
var path = require('path');
var common = {};
2019-04-06 04:52:00 +02:00
common.multi_node_setup = false;
2019-02-09 21:37:36 +01:00
common.rtl_conf_file_path = '';
2019-02-12 14:36:04 +01:00
common.node_auth_type = 'DEFAULT';
2019-02-16 23:43:12 +01:00
common.rtl_pass = '';
2019-02-12 14:36:04 +01:00
common.rtl_sso = 0;
common.port = 3000;
2019-02-13 00:13:09 +01:00
common.rtl_cookie_path = '';
2019-02-12 14:36:04 +01:00
common.logout_redirect_link = '/login';
common.cookie = '';
2019-02-24 18:00:39 +01:00
common.secret_key = crypto.randomBytes(64).toString('hex');
2019-04-06 04:52:00 +02:00
common.nodes = [];
common.selectedNode = {};
2019-09-02 06:11:37 +02:00
common.getSelLNServerUrl = () => {
2019-09-07 23:31:32 +02:00
return common.selectedNode.ln_server_url;
};
common.getOptions = () => {
2019-09-07 23:31:32 +02:00
return common.selectedNode.options;
2019-04-06 04:52:00 +02:00
};
common.setOptions = () => {
2019-09-07 23:31:32 +02:00
if (undefined !== common.nodes[0].options && undefined !== common.nodes[0].options.headers) { return; }
try {
common.nodes.forEach(node => {
node.options = {
url: '',
rejectUnauthorized: false,
json: true,
form: ''
};
if (node.ln_implementation && node.ln_implementation.toUpperCase() === 'CLT') {
node.options.headers = { 'macaroon': Buffer.from(fs.readFileSync(path.join(node.macaroon_path, 'access.macaroon'))).toString("base64") };
} else {
node.options.headers = { 'Grpc-Metadata-macaroon': fs.readFileSync(path.join(node.macaroon_path, 'admin.macaroon')).toString('hex') };
}
2019-09-07 23:31:32 +02:00
});
// Options cannot be set before selected node initializes. Updating selected node's options separatly
common.selectedNode.options = {
url: '',
rejectUnauthorized: false,
json: true,
form: ''
};
if (common.selectedNode && common.selectedNode.ln_implementation && common.selectedNode.ln_implementation.toUpperCase() === 'CLT') {
common.selectedNode.options.headers = { 'macaroon': Buffer.from(fs.readFileSync(path.join(common.selectedNode.macaroon_path, 'access.macaroon'))).toString("base64") };
} else {
common.selectedNode.options.headers = { 'Grpc-Metadata-macaroon': fs.readFileSync(path.join(common.selectedNode.macaroon_path, 'admin.macaroon')).toString('hex') };
2019-09-07 23:31:32 +02:00
}
} catch (err) {
console.error('Common Set Options Error:' + JSON.stringify(err));
common.nodes.forEach(node => {
node.options = {
url: '',
rejectUnauthorized: false,
json: true,
form: ''
};
});
// Options cannot be set before selected node initializes. Updating selected node's options separatly
common.selectedNode.options = {
url: '',
rejectUnauthorized: false,
json: true,
form: ''
};
}
}
2019-04-06 04:52:00 +02:00
common.findNode = (selNodeIndex) => {
2019-09-07 23:31:32 +02:00
return common.nodes.find(node => node.index == selNodeIndex);
2019-04-06 04:52:00 +02:00
}
common.convertToBTC = (num) => {
2019-09-07 23:31:32 +02:00
return (num / 100000000).toFixed(6);
};
common.convertTimestampToDate = (num) => {
2019-09-07 23:31:32 +02:00
return new Date(+num * 1000).toUTCString();
};
common.sortAscByKey = (array, key) => {
2019-09-07 23:31:32 +02:00
return array.sort(function (a, b) {
var x = a[key]; var y = b[key];
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
});
}
common.sortDescByKey = (array, key) => {
2019-09-07 23:31:32 +02:00
const temp = array.sort(function (a, b) {
var x = a[key]; var y = b[key];
return ((x > y) ? -1 : ((x < y) ? 1 : 0));
});
return temp;
}
common.newestOnTop = (array, key, value) => {
2019-09-07 23:31:32 +02:00
var index = array.findIndex(function (item) {
return item[key] === value
});
var newlyAddedRecord = array.splice(index, 1);
array.unshift(newlyAddedRecord[0]);
return array;
}
2019-02-24 18:00:39 +01:00
module.exports = common;