2018-11-23 02:30:30 +01:00
|
|
|
var request = require("request-promise");
|
2018-09-15 03:31:01 +02:00
|
|
|
var options = require("../connect");
|
2018-10-10 02:08:05 +02:00
|
|
|
var common = require('../common');
|
2018-11-23 02:30:30 +01:00
|
|
|
|
2018-12-10 03:26:11 +01:00
|
|
|
getAliasForChannel = (channel, channelType) => {
|
|
|
|
console.log('CHANNEL: ');
|
|
|
|
console.log(channel);
|
2018-11-24 06:41:21 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
console.log('URL: ' + options.url);
|
|
|
|
request(options)
|
|
|
|
.then(function(aliasBody) {
|
|
|
|
console.log('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 => reject(err));
|
|
|
|
});
|
|
|
|
}
|
2018-09-15 03:31:01 +02:00
|
|
|
|
|
|
|
exports.getChannels = (req, res, next) => {
|
|
|
|
if (undefined === req.params.channelType || req.params.channelType === 'all') {
|
2018-10-10 02:08:05 +02:00
|
|
|
options.url = common.lnd_server_url + '/channels';
|
2018-09-15 03:31:01 +02:00
|
|
|
} else {
|
2018-10-10 02:08:05 +02:00
|
|
|
options.url = common.lnd_server_url + '/channels/' + req.params.channelType;
|
2018-09-15 03:31:01 +02:00
|
|
|
}
|
|
|
|
options.qs = req.query;
|
2018-11-23 02:30:30 +01:00
|
|
|
request(options).then(function (body) {
|
2018-11-24 06:41:21 +01:00
|
|
|
let channels = [];
|
|
|
|
if (undefined === req.params.channelType || req.params.channelType === 'all') {
|
|
|
|
channels = (undefined === body.channels) ? [] : body.channels;
|
|
|
|
} else {
|
|
|
|
channels = (undefined === body.pending_open_channels) ? [] : body.pending_open_channels;
|
|
|
|
}
|
2018-11-23 02:30:30 +01:00
|
|
|
Promise.all(
|
|
|
|
channels.map(channel => {
|
2018-12-10 03:26:11 +01:00
|
|
|
// console.log(`\nChannel before getting Alias: ${JSON.stringify(channel)} \nAnd Channel Type: ${req.params.channelType}`);
|
|
|
|
return getAliasForChannel(channel, req.params.channelType);
|
|
|
|
})
|
|
|
|
)
|
2018-11-23 02:30:30 +01:00
|
|
|
.then(function(values) {
|
2018-11-24 06:41:21 +01:00
|
|
|
console.log(`\nChannels Fetched with Alias: ${JSON.stringify(body)}`);
|
2018-09-15 03:31:01 +02:00
|
|
|
res.status(200).json(body);
|
2018-12-10 03:26:11 +01:00
|
|
|
}).catch(err => {
|
|
|
|
console.error(err.error);
|
2018-11-23 02:30:30 +01:00
|
|
|
});
|
2018-09-15 03:31:01 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.postChannel = (req, res, next) => {
|
2018-09-16 08:29:21 +02:00
|
|
|
// setTimeout(()=>{res.status(201).json({message: 'Channel Open!'});}, 5000);
|
2018-10-10 02:08:05 +02:00
|
|
|
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
|
|
|
|
});
|
|
|
|
request.post(options, (error, response, body) => {
|
|
|
|
console.log('Channel Open Response: ');
|
|
|
|
console.log(body);
|
|
|
|
if(undefined === body || body.error) {
|
|
|
|
res.status(500).json({
|
2018-10-12 02:03:54 +02:00
|
|
|
message: "Open Channel Failed!",
|
|
|
|
error: (undefined === body) ? 'Error From Server!' : body.error
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
res.status(201).json(body);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.postTransactions = (req, res, next) => {
|
|
|
|
options.url = common.lnd_server_url + '/channels/transactions';
|
|
|
|
options.form = JSON.stringify({
|
|
|
|
payment_request: req.body.paymentReq
|
|
|
|
});
|
|
|
|
console.log('Send Payment Options Form:' + options.form);
|
|
|
|
request.post(options, (error, response, body) => {
|
|
|
|
console.log('Send Payment Response: ');
|
|
|
|
console.log(body);
|
|
|
|
if(undefined === body || body.error) {
|
|
|
|
res.status(500).json({
|
|
|
|
message: "Send Payment Failed!",
|
|
|
|
error: (undefined === body) ? 'Error From Server!' : body.error
|
2018-09-15 03:31:01 +02:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
res.status(201).json(body);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
2018-11-05 00:47:00 +01:00
|
|
|
|
|
|
|
exports.closeChannel = (req, res, next) => {
|
|
|
|
let channelpoint = req.params.channelPoint.replace(":", "/");
|
2018-11-12 04:17:30 +01:00
|
|
|
options.url = common.lnd_server_url + '/channels/' + channelpoint + '?force=' + req.query.force;
|
|
|
|
console.log('\nClosing Channel URL: ');
|
|
|
|
console.log(options.url);
|
2018-11-05 00:47:00 +01:00
|
|
|
request.delete(options, (error, response, body) => {
|
2018-11-12 04:17:30 +01:00
|
|
|
console.log('\nClose Channel Response: ');
|
2018-11-05 00:47:00 +01:00
|
|
|
console.log(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!'});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|