var request = require ( 'request-promise' ) ;
var common = require ( '../../common' ) ;
var logger = require ( '../logger' ) ;
var options = { } ;
getAliasForChannel = ( channel , channelType ) => {
return new Promise ( function ( resolve , reject ) {
if ( undefined === channelType || channelType === 'all' ) {
options . url = common . getSelLNServerUrl ( ) + '/graph/node/' + channel . remote _pubkey ;
} else {
options . url = common . getSelLNServerUrl ( ) + '/graph/node/' + channel . channel . remote _node _pub ;
}
request ( options ) . then ( function ( aliasBody ) {
logger . info ( { fileName : 'Channels' , msg : 'Alias: ' + JSON . stringify ( aliasBody . node . alias ) } ) ;
if ( undefined === channelType || channelType === 'all' ) {
channel . remote _alias = aliasBody . node . alias ;
resolve ( aliasBody . node . alias ) ;
} else {
channel . channel . remote _alias = aliasBody . node . alias ;
resolve ( aliasBody . node . alias ) ;
}
} )
. catch ( err => resolve ( '' ) ) ;
} ) ;
}
exports . getChannels = ( req , res , next ) => {
options = common . getOptions ( ) ;
if ( undefined === req . params . channelType || req . params . channelType === 'all' ) {
options . url = common . getSelLNServerUrl ( ) + '/channels' ;
} else {
options . url = common . getSelLNServerUrl ( ) + '/channels/' + req . params . channelType ; // active_only, inactive_only, public_only, private_only, Not Implemented in Frontend yet
}
options . qs = req . query ;
request ( options ) . then ( function ( body ) {
let channels = [ ] ;
if ( undefined === req . params . channelType || req . params . channelType === 'all' ) {
channels = ( undefined === body . channels ) ? [ ] : body . channels ;
Promise . all (
channels . map ( channel => {
return getAliasForChannel ( channel , req . params . channelType ) ;
} )
)
. then ( function ( values ) {
logger . info ( { fileName : 'Channels' , msg : 'Channels with Alias: ' + JSON . stringify ( body ) } ) ;
res . status ( 200 ) . json ( body ) ;
} ) . catch ( err => {
console . error ( err . error ) ;
} ) ;
} else {
if ( undefined === body . total _limbo _balance ) {
body . total _limbo _balance = 0 ;
body . btc _total _limbo _balance = 0 ;
} else {
body . btc _total _limbo _balance = common . convertToBTC ( body . total _limbo _balance ) ;
}
if ( req . params . channelType === 'closed' && body . channels && body . channels . length > 0 ) {
body . channels . forEach ( channel => {
channel . close _type = ( undefined === channel . close _type ) ? 'COOPERATIVE_CLOSE' : channel . close _type ;
} ) ;
body . channels = common . sortDescByKey ( body . channels , 'close_type' ) ;
}
logger . info ( { fileName : 'Channels' , msg : 'Pending/Closed Channels: ' + JSON . stringify ( body ) } ) ;
res . status ( 200 ) . json ( body ) ;
}
} )
. catch ( function ( err ) {
logger . error ( { fileName : 'Channels' , lineNum : 68 , msg : 'Get Channel: ' + JSON . stringify ( err ) } ) ;
return res . status ( 500 ) . json ( {
message : 'Fetching Channels Failed!' ,
error : err . error
} ) ;
} ) ;
} ;
exports . postChannel = ( req , res , next ) => {
options = common . getOptions ( ) ;
options . url = common . getSelLNServerUrl ( ) + '/channels' ;
options . form = {
node _pubkey _string : req . body . node _pubkey ,
local _funding _amount : req . body . local _funding _amount ,
private : req . body . private ,
spend _unconfirmed : req . body . spend _unconfirmed
} ;
if ( req . body . trans _type === '1' ) {
options . form . target _conf = req . body . trans _type _value ;
} else if ( req . body . trans _type === '2' ) {
options . form . sat _per _byte = req . body . trans _type _value ;
}
options . form = JSON . stringify ( options . form ) ;
request . post ( options ) . then ( ( body ) => {
logger . info ( { fileName : 'Channels' , msg : 'Channel Open Response: ' + JSON . stringify ( body ) } ) ;
if ( undefined === body || body . error ) {
res . status ( 500 ) . json ( {
message : 'Open Channel Failed!' ,
error : ( undefined === body ) ? 'Error From Server!' : body . error
} ) ;
} else {
res . status ( 201 ) . json ( body ) ;
}
} )
. catch ( function ( err ) {
logger . error ( { fileName : 'Channels' , lineNum : 103 , msg : 'Open Channel: ' + JSON . stringify ( err ) } ) ;
return res . status ( 500 ) . json ( {
message : 'Open Channel Failed!' ,
error : err . error
} ) ;
} ) ;
} ;
exports . postTransactions = ( req , res , next ) => {
options = common . getOptions ( ) ;
options . url = common . getSelLNServerUrl ( ) + '/channels/transactions' ;
if ( req . body . paymentReq ) {
options . form = JSON . stringify ( {
payment _request : req . body . paymentReq
} ) ;
} else if ( req . body . paymentDecoded ) {
options . form = JSON . stringify ( {
payment _hash _string : req . body . paymentDecoded . payment _hash ,
final _cltv _delta : parseInt ( req . body . paymentDecoded . cltv _expiry ) ,
amt : req . body . paymentDecoded . num _satoshis ,
dest _string : req . body . paymentDecoded . destination
} ) ;
}
request . post ( options ) . then ( ( body ) => {
logger . info ( { fileName : 'Channels' , msg : 'Send Payment Response: ' + JSON . stringify ( body ) } ) ;
if ( undefined === body || body . error ) {
res . status ( 500 ) . json ( {
message : 'Send Payment Failed!' ,
error : ( undefined === body ) ? 'Error From Server!' : body . error
} ) ;
} else if ( body . payment _error ) {
res . status ( 500 ) . json ( {
message : 'Send Payment Failed!' ,
error : ( undefined === body ) ? 'Error From Server!' : body . payment _error
} ) ;
} else {
res . status ( 201 ) . json ( body ) ;
}
} )
. catch ( function ( err ) {
logger . error ( { fileName : 'Channels' , lineNum : 143 , msg : 'Send Payment: ' + JSON . stringify ( err ) } ) ;
return res . status ( 500 ) . json ( {
message : 'Send Payment Failed!' ,
error : err . error
} ) ;
} ) ;
} ;
exports . closeChannel = ( req , res , next ) => {
req . setTimeout ( 60000 * 10 ) ; // timeout 10 mins
options = common . getOptions ( ) ;
let channelpoint = req . params . channelPoint . replace ( ':' , '/' ) ;
options . url = common . getSelLNServerUrl ( ) + '/channels/' + channelpoint + '?force=' + req . query . force ;
logger . info ( { fileName : 'Channels' , msg : 'Closing Channel: ' + options . url } ) ;
request . delete ( options ) . then ( ( body ) => {
logger . info ( { fileName : 'Channels' , msg : 'Close Channel Response: ' + JSON . stringify ( body ) } ) ;
if ( undefined === body || body . error ) {
res . status ( 500 ) . json ( {
message : 'Close Channel Failed!' ,
error : ( undefined === body ) ? 'Error From Server!' : body . error
} ) ;
} else {
res . status ( 204 ) . json ( { message : 'Channel Closed!' } ) ;
}
} )
. catch ( function ( err ) {
logger . error ( { fileName : 'Channels' , lineNum : 169 , msg : 'Close Channel: ' + JSON . stringify ( err ) } ) ;
return res . status ( 500 ) . json ( {
message : 'Close Channel Failed!' ,
error : err . error
} ) ;
} ) ;
}
exports . postChanPolicy = ( req , res , next ) => {
options = common . getOptions ( ) ;
options . url = common . getSelLNServerUrl ( ) + '/chanpolicy' ;
if ( req . body . chanPoint === 'all' ) {
options . form = JSON . stringify ( {
global : true ,
base _fee _msat : req . body . baseFeeMsat ,
fee _rate : parseFloat ( req . body . feeRate / 1000000 ) ,
time _lock _delta : parseInt ( req . body . timeLockDelta )
} ) ;
} else {
let breakPoint = req . body . chanPoint . indexOf ( ':' ) ;
let txid _str = req . body . chanPoint . substring ( 0 , breakPoint ) ;
let output _idx = req . body . chanPoint . substring ( breakPoint + 1 , req . body . chanPoint . length ) ;
options . form = JSON . stringify ( {
base _fee _msat : req . body . baseFeeMsat ,
fee _rate : parseFloat ( req . body . feeRate / 1000000 ) ,
time _lock _delta : parseInt ( req . body . timeLockDelta ) ,
chan _point : { funding _txid _str : txid _str , output _index : parseInt ( output _idx ) }
} ) ;
}
logger . info ( { fileName : 'Channels' , msg : 'Update Channel Policy Options: ' + JSON . stringify ( options ) } ) ;
request . post ( options ) . then ( ( body ) => {
logger . info ( { fileName : 'Channels' , msg : 'Update Channel Policy: ' + JSON . stringify ( body ) } ) ;
if ( undefined === body || body . error ) {
res . status ( 500 ) . json ( {
message : 'Update Channel Failed!' ,
error : ( undefined === body ) ? 'Error From Server!' : body . error
} ) ;
} else {
res . status ( 201 ) . json ( body ) ;
}
} )
. catch ( function ( err ) {
logger . error ( { fileName : 'Channels' , lineNum : 211 , msg : 'Update Channel Policy: ' + JSON . stringify ( err ) } ) ;
return res . status ( 500 ) . json ( {
message : 'Update Channel Failed!' ,
error : err . error
} ) ;
} ) ;
} ;