2018-09-15 03:31:01 +02:00
|
|
|
var fs = require('fs');
|
2018-12-10 03:26:11 +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-09-15 03:31:01 +02:00
|
|
|
|
2018-12-10 03:26:11 +01:00
|
|
|
getAliasForPeers = (peer) => {
|
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
options.url = common.lnd_server_url + '/graph/node/' + peer.pub_key;
|
|
|
|
request(options)
|
|
|
|
.then(function(aliasBody) {
|
|
|
|
console.log('Alias: ' + JSON.stringify(aliasBody.node.alias));
|
|
|
|
peer.alias = aliasBody.node.alias;
|
|
|
|
resolve(aliasBody.node.alias);
|
|
|
|
})
|
2018-12-10 03:49:17 +01:00
|
|
|
.catch(err => resolve(''));
|
2018-12-10 03:26:11 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-12-25 05:01:31 +01:00
|
|
|
exports.getPeers = (req, res, next) =>
|
|
|
|
{
|
2018-10-10 02:08:05 +02:00
|
|
|
options.url = common.lnd_server_url + '/peers';
|
2018-12-10 03:26:11 +01:00
|
|
|
request(options).then(function (body) {
|
|
|
|
let peers = (undefined === body.peers) ? [] : body.peers;
|
|
|
|
Promise.all(
|
|
|
|
peers.map(peer => {
|
|
|
|
return getAliasForPeers(peer);
|
|
|
|
}))
|
|
|
|
.then(function(values) {
|
|
|
|
console.log(`\nPeers Fetched with Alias: ${JSON.stringify(body)}`);
|
2018-09-15 03:31:01 +02:00
|
|
|
res.status(200).json(body.peers);
|
2018-12-10 03:26:11 +01:00
|
|
|
});
|
2019-01-01 17:26:51 +01:00
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
return res.status(500).json({
|
|
|
|
message: "Peers Fetched Failed!",
|
|
|
|
error: err.error
|
|
|
|
});
|
2018-09-15 03:31:01 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.postPeer = (req, res, next) => {
|
2018-10-10 02:08:05 +02:00
|
|
|
options.url = common.lnd_server_url + '/peers';
|
2018-09-15 03:31:01 +02:00
|
|
|
options.form = JSON.stringify({
|
|
|
|
addr: { host: req.body.host, pubkey: req.body.pubkey },
|
|
|
|
perm: req.body.perm
|
|
|
|
});
|
|
|
|
console.log('Options: ' + JSON.stringify(options));
|
|
|
|
request.post(options, (error, response, body) => {
|
|
|
|
console.log('Peer Add Response: ');
|
|
|
|
console.log(body);
|
|
|
|
if(undefined === body || body.error) {
|
|
|
|
res.status(500).json({
|
2018-12-25 05:01:31 +01:00
|
|
|
message: "Adding peer failed!",
|
2018-10-12 02:03:54 +02:00
|
|
|
error: (undefined === body) ? 'Error From Server!' : body.error
|
2018-09-15 03:31:01 +02:00
|
|
|
});
|
|
|
|
} else {
|
2018-12-20 02:54:52 +01:00
|
|
|
options.url = common.lnd_server_url + '/peers';
|
|
|
|
request(options).then(function (body) {
|
|
|
|
let peers = (undefined === body.peers) ? [] : body.peers;
|
|
|
|
Promise.all(
|
|
|
|
peers.map(peer => {
|
|
|
|
return getAliasForPeers(peer);
|
|
|
|
}))
|
|
|
|
.then(function(values) {
|
2019-01-01 17:26:51 +01:00
|
|
|
console.log(`\nPeer Added Succesfully!`);
|
2018-12-20 02:54:52 +01:00
|
|
|
console.log(`\nPeers Fetched with Alias: ${JSON.stringify(body)}`);
|
|
|
|
res.status(201).json(body.peers);
|
2019-01-01 17:26:51 +01:00
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
return res.status(500).json({
|
|
|
|
message: "Peer Add Failed!",
|
|
|
|
error: err.error
|
|
|
|
});
|
2018-12-20 02:54:52 +01:00
|
|
|
});
|
|
|
|
})
|
2018-09-15 03:31:01 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
2018-12-25 05:01:31 +01:00
|
|
|
|
|
|
|
exports.deletePeer = (req, res, next) => {
|
|
|
|
options.url = common.lnd_server_url + '/peers/' + req.params.peerPubKey;
|
|
|
|
console.log('Detach Peer Options: ');
|
|
|
|
console.log(options.url);
|
2019-01-01 17:26:51 +01:00
|
|
|
request.delete(options).then((body) => {
|
2018-12-25 05:01:31 +01:00
|
|
|
console.log('Detach Peer Response: ');
|
|
|
|
console.log(body);
|
|
|
|
if(undefined === body || body.error) {
|
|
|
|
res.status(500).json({
|
|
|
|
message: "Detach peer failed!",
|
|
|
|
error: (undefined === body) ? 'Error From Server!' : body.error
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
console.log('\nPeer Detached: ' + req.params.peerPubKey);
|
2019-01-01 17:26:51 +01:00
|
|
|
res.status(204).json({});
|
2018-12-25 05:01:31 +01:00
|
|
|
}
|
2019-01-01 17:26:51 +01:00
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
return res.status(500).json({
|
|
|
|
message: "Detach Peer Failed!",
|
|
|
|
error: err.error
|
|
|
|
});
|
2018-12-25 05:01:31 +01:00
|
|
|
});
|
|
|
|
};
|