2021-12-29 23:08:41 +00:00
|
|
|
import request from 'request-promise';
|
|
|
|
import { Logger, LoggerService } from '../../utils/logger.js';
|
|
|
|
import { Common, CommonService } from '../../utils/common.js';
|
2024-04-19 20:36:42 +00:00
|
|
|
import { SelectedNode } from '../../models/config.model.js';
|
2021-12-29 23:08:41 +00:00
|
|
|
let options = null;
|
|
|
|
const logger: LoggerService = Logger;
|
|
|
|
const common: CommonService = Common;
|
|
|
|
|
2024-04-19 20:36:42 +00:00
|
|
|
export const getSentInfoFromPaymentRequest = (selNode: SelectedNode, payment) => {
|
|
|
|
options.url = selNode.settings.lnServerUrl + '/getsentinfo';
|
2021-12-29 23:08:41 +00:00
|
|
|
options.form = { paymentHash: payment };
|
|
|
|
return request.post(options).then((body) => {
|
2022-01-16 20:55:50 +00:00
|
|
|
logger.log({ selectedNode: selNode, level: 'DEBUG', fileName: 'Payments', msg: 'Payment Sent Information Received', data: body });
|
2021-12-29 23:08:41 +00:00
|
|
|
body.forEach((sentPayment) => {
|
|
|
|
if (sentPayment.amount) { sentPayment.amount = Math.round(sentPayment.amount / 1000); }
|
|
|
|
if (sentPayment.recipientAmount) { sentPayment.recipientAmount = Math.round(sentPayment.recipientAmount / 1000); }
|
|
|
|
});
|
|
|
|
return body;
|
|
|
|
}).catch((err) => err);
|
|
|
|
};
|
|
|
|
|
2024-04-19 20:36:42 +00:00
|
|
|
export const getQueryNodes = (selNode: SelectedNode, nodeIds) => {
|
|
|
|
options.url = selNode.settings.lnServerUrl + '/nodes';
|
2022-08-17 22:48:04 +00:00
|
|
|
options.form = { nodeIds: nodeIds?.reduce((acc, curr) => acc + ',' + curr) };
|
2021-12-29 23:08:41 +00:00
|
|
|
return request.post(options).then((nodes) => {
|
2022-01-16 20:55:50 +00:00
|
|
|
logger.log({ selectedNode: selNode, level: 'DEBUG', fileName: 'Payments', msg: 'Query Nodes Received', data: nodes });
|
2021-12-29 23:08:41 +00:00
|
|
|
return nodes;
|
|
|
|
}).catch((err) => []);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const decodePayment = (req, res, next) => {
|
|
|
|
logger.log({ selectedNode: req.session.selectedNode, level: 'INFO', fileName: 'Payments', msg: 'Decoding Payment..' });
|
|
|
|
options = common.getOptions(req);
|
|
|
|
if (options.error) { return res.status(options.statusCode).json({ message: options.message, error: options.error }); }
|
2024-04-19 20:36:42 +00:00
|
|
|
options.url = req.session.selectedNode.settings.lnServerUrl + '/parseinvoice';
|
2021-12-29 23:08:41 +00:00
|
|
|
options.form = { invoice: req.params.invoice };
|
|
|
|
request.post(options).then((body) => {
|
2022-01-16 20:55:50 +00:00
|
|
|
logger.log({ selectedNode: req.session.selectedNode, level: 'INFO', fileName: 'Payments', msg: 'Payment Decoded', data: body });
|
2021-12-29 23:08:41 +00:00
|
|
|
if (body.amount) { body.amount = Math.round(body.amount / 1000); }
|
|
|
|
res.status(200).json(body);
|
|
|
|
}).catch((errRes) => {
|
|
|
|
const err = common.handleError(errRes, 'Payments', 'Decode Payment Error', req.session.selectedNode);
|
|
|
|
return res.status(err.statusCode).json({ message: err.message, error: err.error });
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export const postPayment = (req, res, next) => {
|
|
|
|
logger.log({ selectedNode: req.session.selectedNode, level: 'INFO', fileName: 'Payments', msg: 'Paying Invoice..' });
|
|
|
|
options = common.getOptions(req);
|
|
|
|
if (options.error) { return res.status(options.statusCode).json({ message: options.message, error: options.error }); }
|
2024-04-19 20:36:42 +00:00
|
|
|
options.url = req.session.selectedNode.settings.lnServerUrl + '/payinvoice';
|
2021-12-29 23:08:41 +00:00
|
|
|
options.form = req.body;
|
|
|
|
logger.log({ selectedNode: req.session.selectedNode, level: 'DEBUG', fileName: 'Payments', msg: 'Send Payment Options', data: options.form });
|
|
|
|
request.post(options).then((body) => {
|
2022-01-16 20:55:50 +00:00
|
|
|
logger.log({ selectedNode: req.session.selectedNode, level: 'INFO', fileName: 'Payments', msg: 'Invoice Paid', data: body });
|
2021-12-29 23:08:41 +00:00
|
|
|
res.status(201).json(body);
|
|
|
|
}).catch((errRes) => {
|
|
|
|
const err = common.handleError(errRes, 'Payments', 'Send Payment Error', req.session.selectedNode);
|
|
|
|
return res.status(err.statusCode).json({ message: err.message, error: err.error });
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export const queryPaymentRoute = (req, res, next) => {
|
|
|
|
logger.log({ selectedNode: req.session.selectedNode, level: 'INFO', fileName: 'Payments', msg: 'Querying Payment Route..' });
|
|
|
|
options = common.getOptions(req);
|
|
|
|
if (options.error) { return res.status(options.statusCode).json({ message: options.message, error: options.error }); }
|
2024-04-19 20:36:42 +00:00
|
|
|
options.url = req.session.selectedNode.settings.lnServerUrl + '/findroutetonode';
|
2021-12-29 23:08:41 +00:00
|
|
|
options.form = {
|
|
|
|
nodeId: req.query.nodeId,
|
|
|
|
amountMsat: req.query.amountMsat
|
|
|
|
};
|
|
|
|
logger.log({ selectedNode: req.session.selectedNode, level: 'DEBUG', fileName: 'Payments', msg: 'Query Payment Route Options', data: options.form });
|
|
|
|
request.post(options).then((body) => {
|
|
|
|
logger.log({ selectedNode: req.session.selectedNode, level: 'DEBUG', fileName: 'Payments', msg: 'Query Payment Route Received', data: body });
|
2022-05-01 17:35:20 +00:00
|
|
|
if (body && body.routes && body.routes.length) {
|
|
|
|
let allRoutesNodeIds = [];
|
2022-08-17 22:48:04 +00:00
|
|
|
allRoutesNodeIds = body.routes?.reduce((accRoutes, currRoute) => [...new Set([...accRoutes, ...currRoute.nodeIds])], []);
|
2022-05-01 17:35:20 +00:00
|
|
|
return getQueryNodes(req.session.selectedNode, allRoutesNodeIds).then((nodesWithAlias) => {
|
2021-12-29 23:08:41 +00:00
|
|
|
let foundPeer = null;
|
2022-05-01 17:35:20 +00:00
|
|
|
body.routes.forEach((route, i) => {
|
2022-08-17 22:48:04 +00:00
|
|
|
route.nodeIds?.map((node, j) => {
|
2022-05-01 17:35:20 +00:00
|
|
|
foundPeer = nodesWithAlias.find((nodeWithAlias) => node === nodeWithAlias.nodeId);
|
|
|
|
body.routes[i].nodeIds[j] = { nodeId: node, alias: foundPeer ? foundPeer.alias : '' };
|
|
|
|
return node;
|
|
|
|
});
|
2021-12-29 23:08:41 +00:00
|
|
|
});
|
2022-05-01 17:35:20 +00:00
|
|
|
logger.log({ selectedNode: req.session.selectedNode, level: 'INFO', fileName: 'Payments', msg: 'Query Routes with Alias Received', data: body });
|
|
|
|
res.status(200).json(body);
|
2021-12-29 23:08:41 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
logger.log({ selectedNode: req.session.selectedNode, level: 'INFO', fileName: 'Payments', msg: 'Empty Payment Route Information Received' });
|
2022-05-01 17:35:20 +00:00
|
|
|
res.status(200).json({ routes: [] });
|
2021-12-29 23:08:41 +00:00
|
|
|
}
|
|
|
|
}).catch((errRes) => {
|
|
|
|
const err = common.handleError(errRes, 'Payments', 'Query Route Error', req.session.selectedNode);
|
|
|
|
return res.status(err.statusCode).json({ message: err.message, error: err.error });
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getSentPaymentsInformation = (req, res, next) => {
|
2024-03-14 22:07:06 +00:00
|
|
|
const { payments } = req.body;
|
2021-12-29 23:08:41 +00:00
|
|
|
logger.log({ selectedNode: req.session.selectedNode, level: 'INFO', fileName: 'Payments', msg: 'Getting Sent Payment Information..' });
|
|
|
|
options = common.getOptions(req);
|
|
|
|
if (options.error) { return res.status(options.statusCode).json({ message: options.message, error: options.error }); }
|
2024-03-14 22:07:06 +00:00
|
|
|
if (payments) {
|
|
|
|
const paymentsArr = payments.split(',');
|
2022-08-17 22:48:04 +00:00
|
|
|
return Promise.all(paymentsArr?.map((payment) => getSentInfoFromPaymentRequest(req.session.selectedNode, payment))).
|
2021-12-29 23:08:41 +00:00
|
|
|
then((values) => {
|
2022-01-16 20:55:50 +00:00
|
|
|
logger.log({ selectedNode: req.session.selectedNode, level: 'INFO', fileName: 'Payments', msg: 'Payment Sent Information Received', data: values });
|
2021-12-29 23:08:41 +00:00
|
|
|
return res.status(200).json(values);
|
|
|
|
}).
|
|
|
|
catch((errRes) => {
|
|
|
|
const err = common.handleError(errRes, 'Payments', 'Sent Payment Error', req.session.selectedNode);
|
|
|
|
return res.status(err.statusCode).json({ message: err.message, error: err.error });
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
logger.log({ selectedNode: req.session.selectedNode, level: 'INFO', fileName: 'Payments', msg: 'Empty Sent Payment Information Received' });
|
|
|
|
return res.status(200).json([]);
|
|
|
|
}
|
|
|
|
};
|
2023-04-26 05:04:29 +00:00
|
|
|
|
2024-04-19 20:36:42 +00:00
|
|
|
export const sendPaymentToRouteRequestCall = (selectedNode: SelectedNode, shortChannelIds: string, invoice: string, amountMsat: number) => {
|
2023-04-26 05:04:29 +00:00
|
|
|
logger.log({ selectedNode: selectedNode, level: 'INFO', fileName: 'Invoices', msg: 'Creating Invoice..' });
|
2024-04-19 20:36:42 +00:00
|
|
|
options = selectedNode.authentication.options;
|
|
|
|
options.url = selectedNode.settings.lnServerUrl + '/sendtoroute';
|
2023-04-26 05:04:29 +00:00
|
|
|
options.form = { shortChannelIds: shortChannelIds, amountMsat: amountMsat, invoice: invoice };
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
logger.log({ selectedNode: selectedNode, level: 'DEBUG', fileName: 'Payments', msg: 'Send Payment To Route Options', data: options.form });
|
|
|
|
request.post(options).then((body) => {
|
|
|
|
logger.log({ selectedNode: selectedNode, level: 'INFO', fileName: 'Payments', msg: 'Payment Sent To Route', data: body });
|
|
|
|
resolve(body);
|
|
|
|
}).catch((errRes) => {
|
|
|
|
reject(common.handleError(errRes, 'Payments', 'Send Payment To Route Error', selectedNode));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export const sendPaymentToRoute = (req, res, next) => {
|
2024-03-14 22:07:06 +00:00
|
|
|
const { shortChannelIds, invoice, amountMsat } = req.body;
|
2023-04-26 05:04:29 +00:00
|
|
|
logger.log({ selectedNode: req.session.selectedNode, level: 'INFO', fileName: 'Payments', msg: 'Send Payment To Route..' });
|
|
|
|
options = common.getOptions(req);
|
|
|
|
if (options.error) { return res.status(options.statusCode).json({ message: options.message, error: options.error }); }
|
2024-03-14 22:07:06 +00:00
|
|
|
sendPaymentToRouteRequestCall(req.session.selectedNode, shortChannelIds, invoice, amountMsat).then((callRes) => {
|
2023-04-26 05:04:29 +00:00
|
|
|
res.status(200).json(callRes);
|
2023-05-03 00:34:41 +00:00
|
|
|
}).catch((err) => res.status(err.statusCode).json({ message: err.message, error: err.error }));
|
2023-04-26 05:04:29 +00:00
|
|
|
};
|