2020-07-07 17:57:15 +00:00
var request = require ( 'request-promise' ) ;
2021-06-20 20:27:08 +00:00
var common = require ( '../../routes/common' ) ;
2021-02-21 19:02:31 +00:00
var logger = require ( '../shared/logger' ) ;
2020-07-07 17:57:15 +00:00
var options = { } ;
exports . getInfo = ( req , res , next ) => {
2021-06-20 20:27:08 +00:00
logger . log ( { level : 'INFO' , fileName : 'GetInfo' , msg : 'Getting Eclair Node Information..' } ) ;
2020-07-07 17:57:15 +00:00
common . setOptions ( ) ;
options = common . getOptions ( ) ;
options . url = common . getSelLNServerUrl ( ) + '/getinfo' ;
options . form = { } ;
2021-06-20 20:27:08 +00:00
logger . log ( { level : 'DEBUG' , fileName : 'GetInfo' , msg : 'Selected Node' , data : common . selectedNode . ln _node } ) ;
logger . log ( { level : 'DEBUG' , fileName : 'GetInfo' , msg : 'Calling Info from Eclair server url' , data : options . url } ) ;
2021-02-21 19:02:31 +00:00
if ( common . read _dummy _data ) {
common . getDummyData ( 'GetInfo' ) . then ( function ( data ) {
data . currency _unit = 'BTC' ;
data . smaller _currency _unit = 'Sats' ;
data . lnImplementation = 'Eclair' ;
res . status ( 200 ) . json ( data ) ;
2020-07-07 17:57:15 +00:00
} ) ;
} else {
2021-02-21 19:02:31 +00:00
if ( ! options . headers || ! options . headers . authorization ) {
2021-06-20 20:27:08 +00:00
logger . log ( { level : 'ERROR' , fileName : 'GetInfo' , msg : 'Eclair Get info failed due to missing or wrong password!' , error : { error : 'Missing or wrong password.' } } ) ;
2021-02-21 19:02:31 +00:00
res . status ( 502 ) . json ( {
message : "Fetching Info Failed!" ,
error : "Missing Or Wrong Password"
2020-07-07 17:57:15 +00:00
} ) ;
2021-02-21 19:02:31 +00:00
} else {
request . post ( options ) . then ( ( body ) => {
2021-06-20 20:27:08 +00:00
logger . log ( { level : 'DEBUG' , fileName : 'GetInfo' , msg : 'Get Info Response' , data : body } ) ;
2021-02-21 19:02:31 +00:00
const body _str = ( ! body ) ? '' : JSON . stringify ( body ) ;
const search _idx = ( ! body ) ? - 1 : body _str . search ( 'Not Found' ) ;
body . currency _unit = 'BTC' ;
body . smaller _currency _unit = 'Sats' ;
body . lnImplementation = 'Eclair' ;
2021-06-20 20:27:08 +00:00
logger . log ( { level : 'INFO' , fileName : 'GetInfo' , msg : 'Eclair Node Information Received' } ) ;
2021-02-21 19:02:31 +00:00
res . status ( 200 ) . json ( body ) ;
} )
. catch ( errRes => {
let err = JSON . parse ( JSON . stringify ( errRes ) ) ;
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 ;
}
2021-06-20 20:27:08 +00:00
logger . log ( { level : 'ERROR' , fileName : 'GetInfo' , msg : 'Get Info Error' , error : err } ) ;
2021-02-21 19:02:31 +00:00
return res . status ( err . statusCode ? err . statusCode : 500 ) . json ( {
message : "Fetching Info failed!" ,
error : err . error && err . error . error ? err . error . error : err . error ? err . error : "Unknown Server Error"
} ) ;
} ) ;
}
2020-07-07 17:57:15 +00:00
}
} ;