2019-09-07 21:31:32 +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-09-07 21:31:32 +00:00
|
|
|
var options = {};
|
|
|
|
|
|
|
|
exports.getNewAddress = (req, res, next) => {
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'INFO', fileName: 'OnChain', msg: 'Generating New Address..'});
|
2019-09-07 21:31:32 +00:00
|
|
|
options = common.getOptions();
|
2020-08-18 14:34:40 +00:00
|
|
|
options.url = common.getSelLNServerUrl() + '/v1/newaddr?addrType=' + req.query.type;
|
2019-09-07 21:31:32 +00:00
|
|
|
request(options).then((body) => {
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'INFO', fileName: 'OnChain', msg: 'New Address Generated'});
|
2019-09-07 21:31:32 +00:00
|
|
|
res.status(200).json(body);
|
|
|
|
})
|
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.macaroon) {
|
|
|
|
delete err.options.headers.macaroon;
|
|
|
|
}
|
|
|
|
if (err.response && err.response.request && err.response.request.headers && err.response.request.headers.macaroon) {
|
|
|
|
delete err.response.request.headers.macaroon;
|
|
|
|
}
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'ERROR', fileName: 'OnChain', msg: 'OnChain New Address Error', error: err});
|
2019-09-07 21:31:32 +00:00
|
|
|
return res.status(500).json({
|
|
|
|
message: "Fetching new address failed!",
|
|
|
|
error: err.error
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
2019-09-14 19:58:21 +00:00
|
|
|
|
|
|
|
exports.onChainWithdraw = (req, res, next) => {
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'INFO', fileName: 'OnChain', msg: 'Withdrawing from On Chain..'});
|
2019-09-14 19:58:21 +00:00
|
|
|
options = common.getOptions();
|
2020-08-18 14:34:40 +00:00
|
|
|
options.url = common.getSelLNServerUrl() + '/v1/withdraw';
|
2019-09-14 19:58:21 +00:00
|
|
|
options.body = req.body;
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'DEBUG', fileName: 'OnChain', msg: 'OnChain Withdraw Options', data: options.body});
|
2019-09-14 19:58:21 +00:00
|
|
|
request.post(options).then((body) => {
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'DEBUG', fileName: 'OnChain', msg: 'OnChain Withdraw Response', data: body});
|
2020-03-10 17:25:52 +00:00
|
|
|
if(!body || body.error) {
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'ERROR', fileName: 'OnChain', msg: 'OnChain Withdraw Error', error: body.error});
|
2019-09-14 19:58:21 +00:00
|
|
|
res.status(500).json({
|
|
|
|
message: 'OnChain Withdraw Failed!',
|
2020-03-10 17:25:52 +00:00
|
|
|
error: (!body) ? 'Error From Server!' : body.error
|
2019-09-14 19:58:21 +00:00
|
|
|
});
|
|
|
|
} else {
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'INFO', fileName: 'OnChain', msg: 'Withdraw Finished'});
|
2019-09-14 19:58:21 +00:00
|
|
|
res.status(201).json(body);
|
|
|
|
}
|
|
|
|
})
|
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.macaroon) {
|
|
|
|
delete err.options.headers.macaroon;
|
|
|
|
}
|
|
|
|
if (err.response && err.response.request && err.response.request.headers && err.response.request.headers.macaroon) {
|
|
|
|
delete err.response.request.headers.macaroon;
|
|
|
|
}
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'ERROR', fileName: 'OnChain', msg: 'OnChain Withdraw Error', error: err});
|
2019-09-14 19:58:21 +00:00
|
|
|
return res.status(500).json({
|
|
|
|
message: 'OnChain Withdraw Failed!',
|
2020-08-14 20:55:37 +00:00
|
|
|
error: err
|
2019-09-14 19:58:21 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2020-08-11 18:25:37 +00:00
|
|
|
|
2021-02-21 19:02:31 +00:00
|
|
|
exports.getUTXOs = (req, res, next) => {
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'INFO', fileName: 'OnChain', msg: 'List Funds..'});
|
2020-08-11 18:25:37 +00:00
|
|
|
options = common.getOptions();
|
2020-08-18 14:34:40 +00:00
|
|
|
options.url = common.getSelLNServerUrl() + '/v1/listFunds';
|
2020-08-11 18:25:37 +00:00
|
|
|
request(options).then((body) => {
|
2020-08-14 20:55:37 +00:00
|
|
|
if (body.outputs) { body.outputs = common.sortDescByStrKey(body.outputs, 'status'); }
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'INFO', fileName: 'OnChain', msg: 'List Funds Received'});
|
2020-08-11 18:25:37 +00:00
|
|
|
res.status(200).json(body);
|
|
|
|
}).catch(errRes => {
|
|
|
|
let err = JSON.parse(JSON.stringify(errRes));
|
|
|
|
if (err.options && err.options.headers && err.options.headers.macaroon) {
|
|
|
|
delete err.options.headers.macaroon;
|
|
|
|
}
|
|
|
|
if (err.response && err.response.request && err.response.request.headers && err.response.request.headers.macaroon) {
|
|
|
|
delete err.response.request.headers.macaroon;
|
|
|
|
}
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'ERROR', fileName: 'OnChain', msg: 'OnChain List Funds Error', error: err});
|
2020-08-11 18:25:37 +00:00
|
|
|
return res.status(500).json({
|
|
|
|
message: "Fetching list funds failed!",
|
|
|
|
error: err.error
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|