2021-12-30 00:08:41 +01:00
import * as fs from 'fs' ;
import { join , dirname , isAbsolute , resolve , sep } from 'path' ;
import { fileURLToPath } from 'url' ;
import * as crypto from 'crypto' ;
import request from 'request-promise' ;
import { Logger } from './logger.js' ;
export class CommonService {
constructor ( ) {
this . logger = Logger ;
this . nodes = [ ] ;
this . initSelectedNode = null ;
this . rtl _conf _file _path = '' ;
this . port = 3000 ;
this . host = null ;
this . rtl _pass = '' ;
this . flg _allow _password _update = true ;
this . rtl _secret2fa = '' ;
this . rtl _sso = 0 ;
this . rtl _cookie _path = '' ;
this . logout _redirect _link = '' ;
2022-05-01 19:35:20 +02:00
this . cookie _value = '' ;
2021-12-30 00:08:41 +01:00
this . api _version = '' ;
this . secret _key = crypto . randomBytes ( 64 ) . toString ( 'hex' ) ;
this . read _dummy _data = false ;
this . baseHref = '/rtl' ;
this . dummy _data _array _from _file = [ ] ;
2022-11-02 23:59:37 +01:00
this . MONTHS = [
{ name : 'JAN' , days : 31 } , { name : 'FEB' , days : 28 } , { name : 'MAR' , days : 31 } , { name : 'APR' , days : 30 } , { name : 'MAY' , days : 31 } , { name : 'JUN' , days : 30 } ,
{ name : 'JUL' , days : 31 } , { name : 'AUG' , days : 31 } , { name : 'SEP' , days : 30 } , { name : 'OCT' , days : 31 } , { name : 'NOV' , days : 30 } , { name : 'DEC' , days : 31 }
] ;
2021-12-30 00:08:41 +01:00
this . getSwapServerOptions = ( req ) => {
const swapOptions = {
url : req . session . selectedNode . swap _server _url ,
rejectUnauthorized : false ,
json : true ,
headers : { 'Grpc-Metadata-macaroon' : '' }
} ;
if ( req . session . selectedNode . swap _macaroon _path ) {
try {
swapOptions . headers = { 'Grpc-Metadata-macaroon' : fs . readFileSync ( join ( req . session . selectedNode . swap _macaroon _path , 'loop.macaroon' ) ) . toString ( 'hex' ) } ;
}
catch ( err ) {
2022-01-16 21:55:50 +01:00
this . logger . log ( { selectedNode : this . initSelectedNode , level : 'ERROR' , fileName : 'Common' , msg : 'Loop macaroon Error' , error : err } ) ;
2021-12-30 00:08:41 +01:00
}
}
2022-01-16 21:55:50 +01:00
this . logger . log ( { selectedNode : this . initSelectedNode , level : 'INFO' , fileName : 'Common' , msg : 'Swap Options' , data : swapOptions } ) ;
2021-12-30 00:08:41 +01:00
return swapOptions ;
} ;
this . getBoltzServerOptions = ( req ) => {
const boltzOptions = {
url : req . session . selectedNode . boltz _server _url ,
rejectUnauthorized : false ,
json : true ,
headers : { 'Grpc-Metadata-macaroon' : '' }
} ;
if ( req . session . selectedNode . boltz _macaroon _path ) {
try {
boltzOptions . headers = { 'Grpc-Metadata-macaroon' : fs . readFileSync ( join ( req . session . selectedNode . boltz _macaroon _path , 'admin.macaroon' ) ) . toString ( 'hex' ) } ;
}
catch ( err ) {
2022-01-16 21:55:50 +01:00
this . logger . log ( { selectedNode : this . initSelectedNode , level : 'ERROR' , fileName : 'Common' , msg : 'Boltz macaroon Error' , error : err } ) ;
2021-12-30 00:08:41 +01:00
}
}
2022-01-16 21:55:50 +01:00
this . logger . log ( { selectedNode : this . initSelectedNode , level : 'INFO' , fileName : 'Common' , msg : 'Boltz Options' , data : boltzOptions } ) ;
2021-12-30 00:08:41 +01:00
return boltzOptions ;
} ;
this . getOptions = ( req ) => {
if ( req . session . selectedNode && req . session . selectedNode . options ) {
req . session . selectedNode . options . method = ( req . session . selectedNode . ln _implementation && req . session . selectedNode . ln _implementation . toUpperCase ( ) !== 'ECL' ) ? 'GET' : 'POST' ;
delete req . session . selectedNode . options . form ;
req . session . selectedNode . options . qs = { } ;
return req . session . selectedNode . options ;
}
2022-01-16 21:55:50 +01:00
return this . handleError ( { statusCode : 401 , message : 'Session expired after a day\'s inactivity' } , 'Session Expired' , 'Session Expiry Error' , this . initSelectedNode ) ;
2021-12-30 00:08:41 +01:00
} ;
this . updateSelectedNodeOptions = ( req ) => {
if ( ! req . session . selectedNode ) {
req . session . selectedNode = { } ;
}
req . session . selectedNode . options = {
url : '' ,
rejectUnauthorized : false ,
json : true ,
form : null
} ;
try {
if ( req . session . selectedNode && req . session . selectedNode . ln _implementation ) {
switch ( req . session . selectedNode . ln _implementation . toUpperCase ( ) ) {
2022-05-01 19:35:20 +02:00
case 'CLN' :
2021-12-30 00:08:41 +01:00
req . session . selectedNode . options . headers = { macaroon : Buffer . from ( fs . readFileSync ( join ( req . session . selectedNode . macaroon _path , 'access.macaroon' ) ) ) . toString ( 'base64' ) } ;
break ;
case 'ECL' :
req . session . selectedNode . options . headers = { authorization : 'Basic ' + Buffer . from ( ':' + req . session . selectedNode . ln _api _password ) . toString ( 'base64' ) } ;
break ;
default :
req . session . selectedNode . options . headers = { 'Grpc-Metadata-macaroon' : fs . readFileSync ( join ( req . session . selectedNode . macaroon _path , 'admin.macaroon' ) ) . toString ( 'hex' ) } ;
break ;
}
}
if ( req . session . selectedNode ) {
2022-01-16 21:55:50 +01:00
this . logger . log ( { selectedNode : this . initSelectedNode , level : 'INFO' , fileName : 'Common' , msg : 'Updated Node Options for ' + req . session . selectedNode . ln _node , data : req . session . selectedNode . options } ) ;
2021-12-30 00:08:41 +01:00
}
2022-01-16 21:55:50 +01:00
return { status : 200 , message : 'Updated Successfully' } ;
2021-12-30 00:08:41 +01:00
}
catch ( err ) {
req . session . selectedNode . options = {
url : '' ,
rejectUnauthorized : false ,
json : true ,
form : null
} ;
2022-01-16 21:55:50 +01:00
this . logger . log ( { selectedNode : this . initSelectedNode , level : 'ERROR' , fileName : 'Common' , msg : 'Update Selected Node Options Error' , error : err } ) ;
2021-12-30 00:08:41 +01:00
return { status : 502 , message : err } ;
}
} ;
this . setOptions = ( req ) => {
if ( this . nodes [ 0 ] . options && this . nodes [ 0 ] . options . headers ) {
return ;
}
if ( this . nodes && this . nodes . length > 0 ) {
this . nodes . forEach ( ( node ) => {
node . options = {
url : '' ,
rejectUnauthorized : false ,
json : true ,
form : null
} ;
try {
if ( node . ln _implementation ) {
switch ( node . ln _implementation . toUpperCase ( ) ) {
2022-05-01 19:35:20 +02:00
case 'CLN' :
2021-12-30 00:08:41 +01:00
node . options . headers = { macaroon : Buffer . from ( fs . readFileSync ( join ( node . macaroon _path , 'access.macaroon' ) ) ) . toString ( 'base64' ) } ;
break ;
case 'ECL' :
node . options . headers = { authorization : 'Basic ' + Buffer . from ( ':' + node . ln _api _password ) . toString ( 'base64' ) } ;
break ;
default :
node . options . headers = { 'Grpc-Metadata-macaroon' : fs . readFileSync ( join ( node . macaroon _path , 'admin.macaroon' ) ) . toString ( 'hex' ) } ;
break ;
}
}
}
catch ( err ) {
2022-01-16 21:55:50 +01:00
this . logger . log ( { selectedNode : this . initSelectedNode , level : 'ERROR' , fileName : 'Common' , msg : 'Common Set Options Error' , error : err } ) ;
2021-12-30 00:08:41 +01:00
node . options = {
url : '' ,
rejectUnauthorized : false ,
json : true ,
form : ''
} ;
}
2022-01-16 21:55:50 +01:00
this . logger . log ( { selectedNode : this . initSelectedNode , level : 'INFO' , fileName : 'Common' , msg : 'Set Node Options for ' + node . ln _node , data : node . options } ) ;
2021-12-30 00:08:41 +01:00
} ) ;
this . updateSelectedNodeOptions ( req ) ;
}
} ;
this . findNode = ( selNodeIndex ) => this . nodes . find ( ( node ) => node . index === selNodeIndex ) ;
this . replaceNode = ( req , newNode ) => {
const foundIndex = this . nodes . findIndex ( ( node ) => node . index === req . session . selectedNode . index ) ;
this . nodes . splice ( foundIndex , 1 , newNode ) ;
req . session . selectedNode = this . findNode ( req . session . selectedNode . index ) ;
} ;
this . convertTimeToEpoch = ( timeToConvert ) => Math . floor ( timeToConvert . getTime ( ) / 1000 ) ;
this . convertTimestampToTime = ( num ) => {
const myDate = new Date ( + num * 1000 ) ;
let days = myDate . getDate ( ) . toString ( ) ;
days = + days < 10 ? '0' + days : days ;
let hours = myDate . getHours ( ) . toString ( ) ;
hours = + hours < 10 ? '0' + hours : hours ;
let minutes = myDate . getMinutes ( ) . toString ( ) ;
minutes = + minutes < 10 ? '0' + minutes : minutes ;
let seconds = myDate . getSeconds ( ) . toString ( ) ;
seconds = + seconds < 10 ? '0' + seconds : seconds ;
return days + '/' + this . MONTHS [ myDate . getMonth ( ) ] . name + '/' + myDate . getFullYear ( ) + ' ' + hours + ':' + minutes + ':' + seconds ;
} ;
this . sortAscByKey = ( array , key ) => array . sort ( ( a , b ) => {
const x = + a [ key ] ;
const y = + b [ key ] ;
return ( ( x < y ) ? - 1 : ( ( x > y ) ? 1 : 0 ) ) ;
} ) ;
this . sortAscByStrKey = ( array , key ) => array . sort ( ( a , b ) => {
const x = a [ key ] ? a [ key ] . toUpperCase ( ) : '' ;
const y = b [ key ] ? b [ key ] . toUpperCase ( ) : '' ;
return ( ( x < y ) ? - 1 : ( ( x > y ) ? 1 : 0 ) ) ;
} ) ;
this . sortDescByKey = ( array , key ) => {
const temp = array . sort ( ( a , b ) => {
const x = + a [ key ] ? + a [ key ] : 0 ;
const y = + b [ key ] ? + b [ key ] : 0 ;
return ( x > y ) ? - 1 : ( ( x < y ) ? 1 : 0 ) ;
} ) ;
return temp ;
} ;
this . sortDescByStrKey = ( array , key ) => {
const temp = array . sort ( ( a , b ) => {
const x = a [ key ] ? a [ key ] . toUpperCase ( ) : '' ;
const y = b [ key ] ? b [ key ] . toUpperCase ( ) : '' ;
return ( x > y ) ? - 1 : ( ( x < y ) ? 1 : 0 ) ;
} ) ;
return temp ;
} ;
this . newestOnTop = ( array , key , value ) => {
const newlyAddedRecord = array . splice ( array . findIndex ( ( item ) => item [ key ] === value ) , 1 ) ;
2022-11-24 03:20:23 +01:00
array ? . unshift ( newlyAddedRecord [ 0 ] ) ;
2021-12-30 00:08:41 +01:00
return array ;
} ;
2022-11-24 03:20:23 +01:00
this . camelCase = ( str ) => str ? . replace ( /(?:^\w|[A-Z]|\b\w)/g , ( word , index ) => ( word . toUpperCase ( ) ) ) ? . replace ( /\s+/g , '' ) ? . replace ( /-/g , ' ' ) ;
2022-06-22 02:29:30 +02:00
this . titleCase = ( str ) => {
if ( str . indexOf ( '!\n' ) > 0 || str . indexOf ( '.\n' ) > 0 ) {
2022-11-24 03:20:23 +01:00
return str . split ( '\n' ) ? . reduce ( ( accumulator , currentStr ) => accumulator + currentStr . charAt ( 0 ) . toUpperCase ( ) + currentStr . substring ( 1 ) . toLowerCase ( ) + '\n' , '' ) ;
2022-06-22 02:29:30 +02:00
}
else {
if ( str . indexOf ( ' ' ) > 0 ) {
2022-11-24 03:20:23 +01:00
return str . split ( ' ' ) ? . reduce ( ( accumulator , currentStr ) => accumulator + currentStr . charAt ( 0 ) . toUpperCase ( ) + currentStr . substring ( 1 ) . toLowerCase ( ) + ' ' , '' ) ;
2022-06-22 02:29:30 +02:00
}
else {
return str . charAt ( 0 ) . toUpperCase ( ) + str . substring ( 1 ) . toLowerCase ( ) ;
}
}
} ;
2021-12-30 00:08:41 +01:00
this . handleError = ( errRes , fileName , errMsg , selectedNode ) => {
const err = JSON . parse ( JSON . stringify ( errRes ) ) ;
if ( ! selectedNode ) {
2022-01-16 21:55:50 +01:00
selectedNode = this . initSelectedNode ;
2021-12-30 00:08:41 +01:00
}
switch ( selectedNode . ln _implementation ) {
case 'LND' :
if ( err . options && err . options . headers && err . options . headers [ 'Grpc-Metadata-macaroon' ] ) {
delete err . options . headers [ 'Grpc-Metadata-macaroon' ] ;
}
if ( err . response && err . response . request && err . response . request . headers && err . response . request . headers [ 'Grpc-Metadata-macaroon' ] ) {
delete err . response . request . headers [ 'Grpc-Metadata-macaroon' ] ;
}
break ;
2022-05-01 19:35:20 +02:00
case 'CLN' :
2021-12-30 00:08:41 +01:00
if ( err . options && err . options . headers && err . options . headers . macaroon ) {
delete err . options . headers . macaroon ;
}
if ( err . response && err . response . request && err . response . request . headers && err . response . request . headers . macaroon ) {
delete err . response . request . headers . macaroon ;
}
break ;
case 'ECL' :
if ( err . options && err . options . headers && err . options . headers . authorization ) {
delete err . options . headers . authorization ;
}
if ( err . response && err . response . request && err . response . request . headers && err . response . request . headers . authorization ) {
delete err . response . request . headers . authorization ;
}
break ;
default :
if ( err . options && err . options . headers ) {
delete err . options . headers ;
}
break ;
}
2022-01-16 21:55:50 +01:00
this . logger . log ( { selectedNode : selectedNode , level : 'ERROR' , fileName : fileName , msg : errMsg , error : ( typeof err === 'object' ? JSON . stringify ( err ) : ( typeof err === 'string' ) ? err : 'Unknown Error' ) } ) ;
2022-11-03 22:31:09 +01:00
let newErrorObj = { statusCode : 500 , message : '' , error : '' } ;
2022-11-02 23:59:37 +01:00
if ( err . code && err . code === 'ENOENT' ) {
newErrorObj = {
statusCode : 500 ,
message : 'No such file or directory ' + ( err . path ? err . path : '' ) ,
error : 'No such file or directory ' + ( err . path ? err . path : '' )
} ;
}
else {
newErrorObj = {
statusCode : err . statusCode ? err . statusCode : err . status ? err . status : ( err . error && err . error . code && err . error . code === 'ECONNREFUSED' ) ? 503 : 500 ,
message : ( err . error && err . error . message ) ? err . error . message : err . message ? err . message : errMsg ,
error : ( ( err . error && err . error . error && err . error . error . error && typeof err . error . error . error === 'string' ) ? err . error . error . error :
( err . error && err . error . error && typeof err . error . error === 'string' ) ? err . error . error :
( err . error && err . error . error && err . error . error . message && typeof err . error . error . message === 'string' ) ? err . error . error . message :
( err . error && err . error . message && typeof err . error . message === 'string' ) ? err . error . message :
( err . error && typeof err . error === 'string' ) ? err . error :
( err . message && typeof err . message === 'string' ) ? err . message : ( typeof err === 'string' ) ? err : 'Unknown Error' )
} ;
}
2022-11-16 22:02:43 +01:00
if ( selectedNode . ln _implementation === 'ECL' && err . message . indexOf ( 'Authentication Error' ) < 0 && err . name === 'StatusCodeError' ) {
newErrorObj . statusCode = 500 ;
}
2021-12-30 00:08:41 +01:00
return newErrorObj ;
} ;
this . getRequestIP = ( req ) => ( ( typeof req . headers [ 'x-forwarded-for' ] === 'string' && req . headers [ 'x-forwarded-for' ] . split ( ',' ) . shift ( ) ) ||
req . ip ||
req . connection . remoteAddress ||
req . socket . remoteAddress ||
( req . connection . socket ? req . connection . socket . remoteAddress : null ) ) ;
this . getDummyData = ( dataKey , lnImplementation ) => {
const dummyDataFile = this . rtl _conf _file _path + sep + 'ECLDummyData.log' ;
return new Promise ( ( resolve , reject ) => {
if ( this . dummy _data _array _from _file . length === 0 ) {
fs . readFile ( dummyDataFile , 'utf8' , ( err , data ) => {
if ( err ) {
if ( err . code === 'ENOENT' ) {
2022-01-16 21:55:50 +01:00
this . logger . log ( { selectedNode : this . initSelectedNode , level : 'ERROR' , fileName : 'Common' , msg : 'Dummy data file does not exist' } ) ;
2021-12-30 00:08:41 +01:00
}
else {
2022-01-16 21:55:50 +01:00
this . logger . log ( { selectedNode : this . initSelectedNode , level : 'ERROR' , fileName : 'Common' , msg : 'Getting dummy data failed' } ) ;
2021-12-30 00:08:41 +01:00
}
}
else {
this . dummy _data _array _from _file = data . split ( '\n' ) ;
resolve ( this . filterData ( dataKey , lnImplementation ) ) ;
}
} ) ;
}
else {
resolve ( this . filterData ( dataKey , lnImplementation ) ) ;
}
} ) ;
} ;
this . readCookie = ( ) => {
const exists = fs . existsSync ( this . rtl _cookie _path ) ;
if ( exists ) {
try {
2022-05-01 19:35:20 +02:00
this . cookie _value = fs . readFileSync ( this . rtl _cookie _path , 'utf-8' ) ;
2021-12-30 00:08:41 +01:00
}
catch ( err ) {
this . logger . log ( { selectedNode : this . initSelectedNode , level : 'ERROR' , fileName : 'Config' , msg : 'Something went wrong while reading cookie: \n' + err } ) ;
throw new Error ( err ) ;
}
}
else {
try {
const directoryName = dirname ( this . rtl _cookie _path ) ;
this . createDirectory ( directoryName ) ;
fs . writeFileSync ( this . rtl _cookie _path , crypto . randomBytes ( 64 ) . toString ( 'hex' ) ) ;
2022-05-01 19:35:20 +02:00
this . cookie _value = fs . readFileSync ( this . rtl _cookie _path , 'utf-8' ) ;
2021-12-30 00:08:41 +01:00
}
catch ( err ) {
this . logger . log ( { selectedNode : this . initSelectedNode , level : 'ERROR' , fileName : 'Config' , msg : 'Something went wrong while reading the cookie: \n' + err } ) ;
throw new Error ( err ) ;
}
}
} ;
this . refreshCookie = ( ) => {
try {
fs . writeFileSync ( this . rtl _cookie _path , crypto . randomBytes ( 64 ) . toString ( 'hex' ) ) ;
2022-05-01 19:35:20 +02:00
this . cookie _value = fs . readFileSync ( this . rtl _cookie _path , 'utf-8' ) ;
2021-12-30 00:08:41 +01:00
}
catch ( err ) {
2022-01-16 21:55:50 +01:00
this . logger . log ( { selectedNode : this . initSelectedNode , level : 'ERROR' , fileName : 'Common' , msg : 'Something went wrong while refreshing cookie' , error : err } ) ;
2021-12-30 00:08:41 +01:00
throw new Error ( err ) ;
}
} ;
this . createDirectory = ( directoryName ) => {
const initDir = isAbsolute ( directoryName ) ? sep : '' ;
2022-11-24 03:20:23 +01:00
directoryName . split ( sep ) ? . reduce ( ( parentDir , childDir ) => {
2021-12-30 00:08:41 +01:00
const curDir = resolve ( parentDir , childDir ) ;
try {
if ( ! fs . existsSync ( curDir ) ) {
fs . mkdirSync ( curDir ) ;
}
}
catch ( err ) {
if ( err . code !== 'EEXIST' ) {
if ( err . code === 'ENOENT' ) {
throw new Error ( ` ENOENT: No such file or directory, mkdir ' ${ directoryName } '. Ensure that the path separator is ' ${ sep } ' ` ) ;
}
else {
throw err ;
}
}
}
return curDir ;
} , initDir ) ;
} ;
this . replacePasswordWithHash = ( multiPassHashed ) => {
this . rtl _conf _file _path = process . env . RTL _CONFIG _PATH ? process . env . RTL _CONFIG _PATH : join ( dirname ( fileURLToPath ( import . meta . url ) ) , '../..' ) ;
try {
const RTLConfFile = this . rtl _conf _file _path + sep + 'RTL-Config.json' ;
const config = JSON . parse ( fs . readFileSync ( RTLConfFile , 'utf-8' ) ) ;
config . multiPassHashed = multiPassHashed ;
delete config . multiPass ;
fs . writeFileSync ( RTLConfFile , JSON . stringify ( config , null , 2 ) , 'utf-8' ) ;
2022-01-16 21:55:50 +01:00
this . logger . log ( { selectedNode : this . initSelectedNode , level : 'INFO' , fileName : 'Common' , msg : 'Please note that, RTL has encrypted the plaintext password into its corresponding hash' } ) ;
2021-12-30 00:08:41 +01:00
return config . multiPassHashed ;
}
catch ( err ) {
2022-01-16 21:55:50 +01:00
this . logger . log ( { selectedNode : this . initSelectedNode , level : 'ERROR' , fileName : 'Common' , msg : 'Password hashing failed' , error : err } ) ;
2021-12-30 00:08:41 +01:00
}
} ;
this . getAllNodeAllChannelBackup = ( node ) => {
const channel _backup _file = node . channel _backup _path + sep + 'channel-all.bak' ;
const options = {
url : node . ln _server _url + '/v1/channels/backup' ,
rejectUnauthorized : false ,
json : true ,
headers : { 'Grpc-Metadata-macaroon' : fs . readFileSync ( node . macaroon _path + '/admin.macaroon' ) . toString ( 'hex' ) }
} ;
2022-01-16 21:55:50 +01:00
this . logger . log ( { selectedNode : this . initSelectedNode , level : 'INFO' , fileName : 'Common' , msg : 'Getting Channel Backup for Node ' + node . ln _node + '..' } ) ;
2021-12-30 00:08:41 +01:00
request ( options ) . then ( ( body ) => {
fs . writeFile ( channel _backup _file , JSON . stringify ( body ) , ( err ) => {
if ( err ) {
if ( node . ln _node ) {
2022-01-16 21:55:50 +01:00
this . logger . log ( { selectedNode : this . initSelectedNode , level : 'ERROR' , fileName : 'Common' , msg : 'Error in Channel Backup for Node ' + node . ln _node , error : err } ) ;
2021-12-30 00:08:41 +01:00
}
else {
2022-01-16 21:55:50 +01:00
this . logger . log ( { selectedNode : this . initSelectedNode , level : 'ERROR' , fileName : 'Common' , msg : 'Error in Channel Backup for File ' + channel _backup _file , error : err } ) ;
2021-12-30 00:08:41 +01:00
}
}
else {
if ( node . ln _node ) {
2022-01-16 21:55:50 +01:00
this . logger . log ( { selectedNode : this . initSelectedNode , level : 'INFO' , fileName : 'Common' , msg : 'Successful in Channel Backup for Node ' + node . ln _node , data : body } ) ;
2021-12-30 00:08:41 +01:00
}
else {
2022-01-16 21:55:50 +01:00
this . logger . log ( { selectedNode : this . initSelectedNode , level : 'INFO' , fileName : 'Common' , msg : 'Successful in Channel Backup for File ' + channel _backup _file , data : body } ) ;
2021-12-30 00:08:41 +01:00
}
}
} ) ;
} , ( err ) => {
2022-01-16 21:55:50 +01:00
this . logger . log ( { selectedNode : this . initSelectedNode , level : 'ERROR' , fileName : 'Common' , msg : 'Error in Channel Backup for Node ' + node . ln _node , error : err } ) ;
fs . writeFile ( channel _backup _file , '' , ( ) => { } ) ;
2021-12-30 00:08:41 +01:00
} ) ;
} ;
this . isVersionCompatible = ( currentVersion , checkVersion ) => {
if ( currentVersion ) {
2022-11-24 03:20:23 +01:00
const versionsArr = currentVersion . trim ( ) ? . replace ( 'v' , '' ) . split ( '-' ) [ 0 ] . split ( '.' ) || [ ] ;
2021-12-30 00:08:41 +01:00
const checkVersionsArr = checkVersion . split ( '.' ) ;
return ( + versionsArr [ 0 ] > + checkVersionsArr [ 0 ] ) ||
( + versionsArr [ 0 ] === + checkVersionsArr [ 0 ] && + versionsArr [ 1 ] > + checkVersionsArr [ 1 ] ) ||
( + versionsArr [ 0 ] === + checkVersionsArr [ 0 ] && + versionsArr [ 1 ] === + checkVersionsArr [ 1 ] && + versionsArr [ 2 ] >= + checkVersionsArr [ 2 ] ) ;
}
return false ;
} ;
this . getMonthDays = ( selMonth , selYear ) => ( ( selMonth === 1 && selYear % 4 === 0 ) ? ( this . MONTHS [ selMonth ] . days + 1 ) : this . MONTHS [ selMonth ] . days ) ;
this . logEnvVariables = ( req ) => {
const selNode = req . session . selectedNode ;
if ( selNode && selNode . index ) {
2022-01-16 21:55:50 +01:00
this . logger . log ( { selectedNode : selNode , level : 'INFO' , fileName : 'Config Setup Variable' , msg : 'PORT: ' + this . port } ) ;
this . logger . log ( { selectedNode : selNode , level : 'INFO' , fileName : 'Config Setup Variable' , msg : 'HOST: ' + this . host } ) ;
this . logger . log ( { selectedNode : selNode , level : 'INFO' , fileName : 'Config Setup Variable' , msg : 'SSO: ' + this . rtl _sso } ) ;
this . logger . log ( { selectedNode : selNode , level : 'INFO' , fileName : 'Config Setup Variable' , msg : 'DEFAULT NODE INDEX: ' + selNode . index } ) ;
this . logger . log ( { selectedNode : selNode , level : 'INFO' , fileName : 'Config Setup Variable' , msg : 'INDEX: ' + selNode . index } ) ;
this . logger . log ( { selectedNode : selNode , level : 'INFO' , fileName : 'Config Setup Variable' , msg : 'LN NODE: ' + selNode . ln _node } ) ;
this . logger . log ( { selectedNode : selNode , level : 'INFO' , fileName : 'Config Setup Variable' , msg : 'LN IMPLEMENTATION: ' + selNode . ln _implementation } ) ;
this . logger . log ( { selectedNode : selNode , level : 'INFO' , fileName : 'Config Setup Variable' , msg : 'FIAT CONVERSION: ' + selNode . fiat _conversion } ) ;
this . logger . log ( { selectedNode : selNode , level : 'INFO' , fileName : 'Config Setup Variable' , msg : 'CURRENCY UNIT: ' + selNode . currency _unit } ) ;
this . logger . log ( { selectedNode : selNode , level : 'INFO' , fileName : 'Config Setup Variable' , msg : 'LN SERVER URL: ' + selNode . ln _server _url } ) ;
this . logger . log ( { selectedNode : selNode , level : 'INFO' , fileName : 'Config Setup Variable' , msg : 'LOGOUT REDIRECT LINK: ' + this . logout _redirect _link + '\r\n' } ) ;
2021-12-30 00:08:41 +01:00
}
} ;
this . filterData = ( dataKey , lnImplementation ) => {
let search _string = '' ;
if ( lnImplementation === 'ECL' ) {
switch ( dataKey ) {
case 'GetInfo' :
search _string = 'INFO: GetInfo => Get Info Response: ' ;
break ;
case 'Fees' :
search _string = 'INFO: Fees => Fee Response: ' ;
break ;
case 'Payments' :
search _string = 'INFO: Fees => Payments Response: ' ;
break ;
case 'Invoices' :
search _string = 'INFO: Invoice => Invoices List Received: ' ;
break ;
case 'OnChainBalance' :
search _string = 'INFO: Onchain => Balance Received: ' ;
break ;
case 'Peers' :
search _string = 'INFO: Peers => Peers with Alias: ' ;
break ;
case 'Channels' :
search _string = 'INFO: Channels => Simplified Channels with Alias: ' ;
break ;
default :
search _string = 'Random Line' ;
break ;
}
}
2022-05-01 19:35:20 +02:00
else if ( lnImplementation === 'CLN' ) {
2021-12-30 00:08:41 +01:00
switch ( dataKey ) {
case 'GetInfo' :
search _string = 'DEBUG: GetInfo => Node Information. ' ;
break ;
case 'Fees' :
search _string = 'DEBUG: Fees => Fee Received. ' ;
break ;
case 'Payments' :
search _string = 'DEBUG: Payments => Payment List Received: ' ;
break ;
case 'Invoices' :
search _string = 'DEBUG: Invoice => Invoices List Received. ' ;
break ;
case 'ChannelBalance' :
search _string = 'DEBUG: Channels => Local Remote Balance. ' ;
break ;
case 'Peers' :
search _string = 'DEBUG: Peers => Peers with Alias: ' ;
break ;
case 'Channels' :
search _string = 'DEBUG: Channels => List Channels: ' ;
break ;
case 'Balance' :
search _string = 'DEBUG: Balance => Balance Received. ' ;
break ;
case 'ForwardingHistory' :
search _string = 'DEBUG: Channels => Forwarding History Received: ' ;
break ;
case 'UTXOs' :
search _string = 'DEBUG: OnChain => List Funds Received. ' ;
break ;
case 'FeeRateperkb' :
search _string = 'DEBUG: Network => Network Fee Rates Received for perkb. ' ;
break ;
case 'FeeRateperkw' :
search _string = 'DEBUG: Network => Network Fee Rates Received for perkw. ' ;
break ;
default :
search _string = 'Random Line' ;
break ;
}
}
const foundDataLine = this . dummy _data _array _from _file . find ( ( dataItem ) => dataItem . includes ( search _string ) ) ;
const dataStr = foundDataLine ? foundDataLine . substring ( ( foundDataLine . indexOf ( search _string ) ) + search _string . length ) : '{}' ;
return JSON . parse ( dataStr ) ;
} ;
}
}
export const Common = new CommonService ( ) ;