2019-01-01 16:26:51 +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');
|
2019-03-02 17:55:19 +00:00
|
|
|
var options = {};
|
2018-09-27 02:02:46 +00:00
|
|
|
|
|
|
|
exports.getNewAddress = (req, res, next) => {
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'INFO', fileName: 'NewAddress', msg: 'Getting New Address..'});
|
2019-04-07 00:20:40 +00:00
|
|
|
options = common.getOptions();
|
2020-08-18 14:34:40 +00:00
|
|
|
options.url = common.getSelLNServerUrl() + '/v1/newaddress?type=' + req.query.type;
|
2019-01-01 16:26:51 +00:00
|
|
|
request(options).then((body) => {
|
2020-03-10 17:25:52 +00:00
|
|
|
const body_str = (!body) ? '' : JSON.stringify(body);
|
|
|
|
const search_idx = (!body) ? -1 : body_str.search('Not Found');
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'DEBUG', fileName: 'NewAddress', msg: 'New Address Received', data: body_str});
|
2020-03-10 17:25:52 +00:00
|
|
|
if(!body || search_idx > -1 || body.error) {
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'ERROR', fileName: 'NewAddress', msg: 'New Address Error', error: body.error});
|
2018-09-27 02:02:46 +00:00
|
|
|
res.status(500).json({
|
|
|
|
message: "Fetching new address failed!",
|
2020-03-10 17:25:52 +00:00
|
|
|
error: (!body || search_idx > -1) ? 'Error From Server!' : body.error
|
2018-09-27 02:02:46 +00:00
|
|
|
});
|
|
|
|
} else {
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'INFO', fileName: 'NewAddress', msg: 'New Address Received'});
|
2020-05-03 19:52:38 +00:00
|
|
|
res.status(200).json(body);
|
2018-09-27 02:02:46 +00:00
|
|
|
}
|
2019-01-01 16:26:51 +00:00
|
|
|
})
|
2020-05-03 19:52:38 +00:00
|
|
|
.catch(errRes => {
|
|
|
|
let err = JSON.parse(JSON.stringify(errRes));
|
|
|
|
if (err.options && err.options.headers && err.options.headers['Grpc-Metadata-macaroon']) {
|
|
|
|
delete err.options.headers['Grpc-Metadata-macaroon'];
|
|
|
|
}
|
|
|
|
if (err.response && err.response.request && err.response.request.headers && err.response.request.headers['Grpc-Metadata-macaroon']) {
|
|
|
|
delete err.response.request.headers['Grpc-Metadata-macaroon'];
|
|
|
|
}
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'ERROR', fileName: 'NewAddress', msg: 'New Address Error', error: err});
|
2019-01-01 16:26:51 +00:00
|
|
|
return res.status(500).json({
|
|
|
|
message: "Fetching new address failed!",
|
|
|
|
error: err.error
|
|
|
|
});
|
2018-09-27 02:02:46 +00:00
|
|
|
});
|
|
|
|
};
|