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';
|
|
|
|
import { LNDWSClient, LNDWebSocketClient } from './webSocketClient.js';
|
|
|
|
|
|
|
|
let options = null;
|
|
|
|
const logger: LoggerService = Logger;
|
|
|
|
const common: CommonService = Common;
|
|
|
|
const lndWsClient: LNDWebSocketClient = LNDWSClient;
|
|
|
|
|
2022-05-01 17:35:20 +00:00
|
|
|
export const invoiceLookup = (req, res, next) => {
|
2021-12-29 23:08:41 +00:00
|
|
|
logger.log({ selectedNode: req.session.selectedNode, level: 'INFO', fileName: 'Invoice', msg: 'Getting Invoice Information..' });
|
|
|
|
options = common.getOptions(req);
|
|
|
|
if (options.error) { return res.status(options.statusCode).json({ message: options.message, error: options.error }); }
|
2022-05-01 17:35:20 +00:00
|
|
|
options.url = req.session.selectedNode.ln_server_url + '/v2/invoices/lookup';
|
|
|
|
if (req.query.payment_addr) {
|
|
|
|
options.url = options.url + '?payment_addr=' + req.query.payment_addr;
|
|
|
|
} else {
|
|
|
|
options.url = options.url + '?payment_hash=' + req.query.payment_hash;
|
|
|
|
}
|
2021-12-29 23:08:41 +00:00
|
|
|
request(options).then((body) => {
|
|
|
|
body.r_preimage = body.r_preimage ? Buffer.from(body.r_preimage, 'base64').toString('hex') : '';
|
|
|
|
body.r_hash = body.r_hash ? Buffer.from(body.r_hash, 'base64').toString('hex') : '';
|
|
|
|
body.description_hash = body.description_hash ? Buffer.from(body.description_hash, 'base64').toString('hex') : null;
|
2022-01-16 20:55:50 +00:00
|
|
|
logger.log({ selectedNode: req.session.selectedNode, level: 'INFO', fileName: 'Invoice', msg: 'Invoice Information Received', data: body });
|
2021-12-29 23:08:41 +00:00
|
|
|
res.status(200).json(body);
|
|
|
|
}).catch((errRes) => {
|
2022-05-01 17:35:20 +00:00
|
|
|
const err = common.handleError(errRes, 'Invoices', 'Invoice Lookup Error', req.session.selectedNode);
|
2021-12-29 23:08:41 +00:00
|
|
|
return res.status(err.statusCode).json({ message: err.message, error: err.error });
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export const listInvoices = (req, res, next) => {
|
|
|
|
logger.log({ selectedNode: req.session.selectedNode, level: 'INFO', fileName: 'Invoice', msg: 'Getting List Invoices..' });
|
|
|
|
options = common.getOptions(req);
|
|
|
|
if (options.error) { return res.status(options.statusCode).json({ message: options.message, error: options.error }); }
|
|
|
|
options.url = req.session.selectedNode.ln_server_url + '/v1/invoices?num_max_invoices=' + req.query.num_max_invoices + '&index_offset=' + req.query.index_offset +
|
|
|
|
'&reversed=' + req.query.reversed;
|
|
|
|
request(options).then((body) => {
|
|
|
|
logger.log({ selectedNode: req.session.selectedNode, level: 'DEBUG', fileName: 'Invoice', msg: 'Invoices List Received', data: body });
|
|
|
|
if (body.invoices && body.invoices.length > 0) {
|
|
|
|
body.invoices.forEach((invoice) => {
|
|
|
|
invoice.r_preimage = invoice.r_preimage ? Buffer.from(invoice.r_preimage, 'base64').toString('hex') : '';
|
|
|
|
invoice.r_hash = invoice.r_hash ? Buffer.from(invoice.r_hash, 'base64').toString('hex') : '';
|
|
|
|
invoice.description_hash = invoice.description_hash ? Buffer.from(invoice.description_hash, 'base64').toString('hex') : null;
|
|
|
|
});
|
|
|
|
}
|
2022-01-16 20:55:50 +00:00
|
|
|
logger.log({ selectedNode: req.session.selectedNode, level: 'INFO', fileName: 'Invoice', msg: 'Sorted Invoices List Received', data: body });
|
2021-12-29 23:08:41 +00:00
|
|
|
res.status(200).json(body);
|
|
|
|
}).catch((errRes) => {
|
|
|
|
const err = common.handleError(errRes, 'Invoices', 'List Invoices Error', req.session.selectedNode);
|
|
|
|
return res.status(err.statusCode).json({ message: err.message, error: err.error });
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export const addInvoice = (req, res, next) => {
|
|
|
|
logger.log({ selectedNode: req.session.selectedNode, level: 'INFO', fileName: 'Invoice', msg: 'Adding Invoice..' });
|
|
|
|
options = common.getOptions(req);
|
|
|
|
if (options.error) { return res.status(options.statusCode).json({ message: options.message, error: options.error }); }
|
|
|
|
options.url = req.session.selectedNode.ln_server_url + '/v1/invoices';
|
|
|
|
options.form = {
|
|
|
|
memo: req.body.memo,
|
|
|
|
private: req.body.private,
|
|
|
|
expiry: req.body.expiry
|
|
|
|
};
|
|
|
|
if (req.body.amount > 0 && req.body.amount < 1) {
|
|
|
|
options.form.value_msat = req.body.amount * 1000;
|
|
|
|
} else {
|
|
|
|
options.form.value = req.body.amount;
|
|
|
|
}
|
|
|
|
options.form = JSON.stringify(options.form);
|
|
|
|
request.post(options).then((body) => {
|
2022-01-16 20:55:50 +00:00
|
|
|
logger.log({ selectedNode: req.session.selectedNode, level: 'INFO', fileName: 'Invoice', msg: 'Invoice Added', data: body });
|
2021-12-29 23:08:41 +00:00
|
|
|
try {
|
|
|
|
if (body.r_hash) {
|
|
|
|
lndWsClient.subscribeToInvoice(options, req.session.selectedNode, body.r_hash);
|
|
|
|
}
|
|
|
|
} catch (errRes) {
|
|
|
|
const err = common.handleError(errRes, 'Invoices', 'Subscribe to Newly Added Invoice Error', req.session.selectedNode);
|
|
|
|
logger.log({ selectedNode: req.session.selectedNode, level: 'ERROR', fileName: 'Invoice', msg: 'Subscribe to Newly Added Invoice Error', error: err });
|
|
|
|
}
|
|
|
|
body.r_preimage = body.r_preimage ? Buffer.from(body.r_preimage, 'base64').toString('hex') : '';
|
|
|
|
body.r_hash = body.r_hash ? Buffer.from(body.r_hash, 'base64').toString('hex') : '';
|
|
|
|
body.description_hash = body.description_hash ? Buffer.from(body.description_hash, 'base64').toString('hex') : null;
|
|
|
|
res.status(201).json(body);
|
|
|
|
}).catch((errRes) => {
|
|
|
|
const err = common.handleError(errRes, 'Invoices', 'Add Invoice Error', req.session.selectedNode);
|
|
|
|
return res.status(err.statusCode).json({ message: err.message, error: err.error });
|
|
|
|
});
|
|
|
|
};
|