You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
RTL/common.js

110 lines
3.2 KiB
JavaScript

var fs = require('fs');
var crypto = require('crypto');
var common = {};
common.multi_node_setup = false;
common.rtl_conf_file_path = '';
common.node_auth_type = 'DEFAULT';
common.rtl_pass = '';
common.rtl_sso = 0;
common.port = 3000;
common.rtl_cookie_path = '';
common.logout_redirect_link = '/login';
common.cookie = '';
common.secret_key = crypto.randomBytes(64).toString('hex');
common.nodes = [];
common.selectedNode = {};
5 years ago
common.getSelLNServerUrl = () => {
5 years ago
return common.selectedNode.ln_server_url;
};
common.getOptions = () => {
5 years ago
return common.selectedNode.options;
};
common.setOptions = () => {
5 years ago
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.toUpperCase() !== 'CLT') {
5 years ago
node.options.headers = { 'Grpc-Metadata-macaroon': fs.readFileSync(node.macaroon_path + '/admin.macaroon').toString('hex') };
} else {
5 years ago
node.options.headers = { 'macaroon': Buffer.from(fs.readFileSync(node.macaroon_path + '/access.macaroon')).toString("base64") };
}
5 years ago
});
// 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.ln_implementation.toUpperCase() !== 'CLT') {
5 years ago
common.selectedNode.options.headers = { 'Grpc-Metadata-macaroon': fs.readFileSync(common.selectedNode.macaroon_path + '/admin.macaroon').toString('hex') };
} else {
5 years ago
common.selectedNode.options.headers = { 'macaroon': Buffer.from(fs.readFileSync(common.selectedNode.macaroon_path + '/access.macaroon')).toString("base64") };
}
} 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: ''
};
}
}
common.findNode = (selNodeIndex) => {
5 years ago
return common.nodes.find(node => node.index == selNodeIndex);
}
common.convertToBTC = (num) => {
5 years ago
return (num / 100000000).toFixed(6);
};
common.convertTimestampToDate = (num) => {
5 years ago
return new Date(+num * 1000).toUTCString();
};
common.sortAscByKey = (array, key) => {
5 years ago
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) => {
5 years ago
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) => {
5 years ago
var index = array.findIndex(function (item) {
return item[key] === value
});
var newlyAddedRecord = array.splice(index, 1);
array.unshift(newlyAddedRecord[0]);
return array;
}
module.exports = common;