Ride-The-Lightning-RTL/controllers/channels.js

197 lines
7.4 KiB
JavaScript
Raw Normal View History

2019-01-09 04:04:34 +01:00
var request = require('request-promise');
var options = require('../connect');
var common = require('../common');
2019-01-13 23:55:25 +01:00
var logger = require('./logger');
getAliasForChannel = (channel, channelType) => {
return new Promise(function(resolve, reject) {
if (undefined === channelType || channelType === 'all') {
options.url = common.lnd_server_url + '/graph/node/' + channel.remote_pubkey;
} else {
options.url = common.lnd_server_url + '/graph/node/' + channel.channel.remote_node_pub;
}
2019-01-01 17:26:51 +01:00
request(options).then(function(aliasBody) {
2019-01-13 23:55:25 +01:00
logger.info('\r\nChannels: 13: ' + JSON.stringify(Date.now()) + ': INFO: 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(''));
});
}
2018-09-15 03:31:01 +02:00
exports.getChannels = (req, res, next) => {
if (undefined === req.params.channelType || req.params.channelType === 'all') {
options.url = common.lnd_server_url + '/channels';
2018-09-15 03:31:01 +02:00
} else {
options.url = common.lnd_server_url + '/channels/' + req.params.channelType;
2018-09-15 03:31:01 +02:00
}
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;
2018-12-25 05:01:31 +01:00
Promise.all(
channels.map(channel => {
return getAliasForChannel(channel, req.params.channelType);
})
)
.then(function(values) {
2019-01-13 23:55:25 +01:00
logger.info('\r\nChannels: 43: ' + JSON.stringify(Date.now()) + ': INFO: Channels with Alias: ' + JSON.stringify(body));
2018-12-25 05:01:31 +01:00
res.status(200).json(body);
}).catch(err => {
console.error(err.error);
});
} else {
2018-12-25 05:01:31 +01:00
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);
}
2019-01-13 23:55:25 +01:00
logger.info('\r\nChannels: 55: ' + JSON.stringify(Date.now()) + ': INFO: Pending Channels: ' + JSON.stringify(body));
2018-09-15 03:31:01 +02:00
res.status(200).json(body);
2018-12-25 05:01:31 +01:00
}
2019-01-01 17:26:51 +01:00
})
.catch(function (err) {
logger.info('\r\nChannels: 61: ' + JSON.stringify(Date.now()) + ': ERROR: Get Channel: ' + JSON.stringify(err));
2019-01-01 17:26:51 +01:00
return res.status(500).json({
2019-01-09 04:04:34 +01:00
message: 'Fetching Channels Failed!',
2019-01-01 17:26:51 +01:00
error: err.error
});
2018-09-15 03:31:01 +02:00
});
};
exports.postChannel = (req, res, next) => {
options.url = common.lnd_server_url + '/channels';
2018-09-15 03:31:01 +02:00
options.form = JSON.stringify({
node_pubkey_string: req.body.node_pubkey,
local_funding_amount: req.body.local_funding_amount
});
2019-01-01 17:26:51 +01:00
request.post(options).then((body) => {
2019-01-13 23:55:25 +01:00
logger.info('\r\nChannels: 74: ' + JSON.stringify(Date.now()) + ': INFO: Channel Open Response: ' + JSON.stringify(body));
2018-09-15 03:31:01 +02:00
if(undefined === body || body.error) {
res.status(500).json({
2019-01-09 04:04:34 +01:00
message: 'Open Channel Failed!',
error: (undefined === body) ? 'Error From Server!' : body.error
});
} else {
res.status(201).json(body);
}
2019-01-01 17:26:51 +01:00
})
.catch(function (err) {
logger.info('\r\nChannels: 86: ' + JSON.stringify(Date.now()) + ': ERROR: Open Channel: ' + JSON.stringify(err));
2019-01-01 17:26:51 +01:00
return res.status(500).json({
2019-01-09 04:04:34 +01:00
message: 'Open Channel Failed!',
2019-01-01 17:26:51 +01:00
error: err.error
});
});
};
exports.postTransactions = (req, res, next) => {
options.url = common.lnd_server_url + '/channels/transactions';
2019-01-06 02:44:59 +01:00
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
});
}
2019-01-01 17:26:51 +01:00
request.post(options).then((body) => {
2019-01-13 23:55:25 +01:00
logger.info('\r\nChannels: 107: ' + JSON.stringify(Date.now()) + ': INFO: Send Payment Response: ' + JSON.stringify(body));
if(undefined === body || body.error) {
res.status(500).json({
2019-01-09 04:04:34 +01:00
message: 'Send Payment Failed!',
error: (undefined === body) ? 'Error From Server!' : body.error
2018-09-15 03:31:01 +02:00
});
2019-01-06 02:44:59 +01:00
} else if (body.payment_error) {
res.status(500).json({
2019-01-09 04:04:34 +01:00
message: 'Send Payment Failed!',
2019-01-06 02:44:59 +01:00
error: (undefined === body) ? 'Error From Server!' : body.payment_error
});
2018-09-15 03:31:01 +02:00
} else {
res.status(201).json(body);
}
2019-01-01 17:26:51 +01:00
})
.catch(function (err) {
logger.info('\r\nChannels: 124: ' + JSON.stringify(Date.now()) + ': ERROR: Send Payment: ' + JSON.stringify(err));
2019-01-01 17:26:51 +01:00
return res.status(500).json({
2019-01-09 04:04:34 +01:00
message: 'Send Payment Failed!',
2019-01-01 17:26:51 +01:00
error: err.error
});
2018-09-15 03:31:01 +02:00
});
};
exports.closeChannel = (req, res, next) => {
2019-01-09 04:04:34 +01:00
let channelpoint = req.params.channelPoint.replace(':', '/');
options.url = common.lnd_server_url + '/channels/' + channelpoint + '?force=' + req.query.force;
2019-01-01 17:26:51 +01:00
request.delete(options).then((body) => {
2019-01-13 23:55:25 +01:00
logger.info('\r\nChannels: 134: ' + JSON.stringify(Date.now()) + ': INFO: Close Channel Response: ' + JSON.stringify(body));
if(undefined === body || body.error) {
res.status(500).json({
2019-01-09 04:04:34 +01:00
message: 'Close Channel Failed!',
error: (undefined === body) ? 'Error From Server!' : body.error
});
} else {
res.status(204).json({message: 'Channel Closed!'});
}
2019-01-01 17:26:51 +01:00
})
.catch(function (err) {
logger.info('\r\nChannels: 146: ' + JSON.stringify(Date.now()) + ': ERROR: Close Channel: ' + JSON.stringify(err));
2019-01-01 17:26:51 +01:00
return res.status(500).json({
2019-01-09 04:04:34 +01:00
message: 'Close Channel Failed!',
2019-01-01 17:26:51 +01:00
error: err.error
});
});
}
exports.postChanPolicy = (req, res, next) => {
options.url = common.lnd_server_url + '/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('\r\nChannels: 161: ' + JSON.stringify(Date.now()) + ': INFO: Update Channel Policy Options: ' + JSON.stringify(options));
request.post(options).then((body) => {
logger.info('\r\nChannels: 163: ' + JSON.stringify(Date.now()) + ': INFO: 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.info('\r\nChannels: 177: ' + JSON.stringify(Date.now()) + ': ERROR: Update Channel Policy: ' + JSON.stringify(err));
return res.status(500).json({
message: 'Update Channel Failed!',
error: err.error
});
});
};