2022-07-29 20:53:19 +02:00
|
|
|
import axios, { AxiosRequestConfig } from 'axios';
|
|
|
|
import { Agent } from 'https';
|
|
|
|
import * as fs from 'fs';
|
2022-04-18 18:22:00 +04:00
|
|
|
import { AbstractLightningApi } from '../lightning-api-abstract-factory';
|
|
|
|
import { ILightningApi } from '../lightning-api.interface';
|
2022-04-27 02:52:23 +04:00
|
|
|
import config from '../../../config';
|
2023-03-21 15:49:38 +09:00
|
|
|
import logger from '../../../logger';
|
2022-04-18 18:22:00 +04:00
|
|
|
|
|
|
|
class LndApi implements AbstractLightningApi {
|
2022-07-29 20:53:19 +02:00
|
|
|
axiosConfig: AxiosRequestConfig = {};
|
2022-04-18 18:22:00 +04:00
|
|
|
|
2022-07-29 20:53:19 +02:00
|
|
|
constructor() {
|
2023-03-21 15:50:33 +09:00
|
|
|
if (!config.LIGHTNING.ENABLED) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
this.axiosConfig = {
|
|
|
|
headers: {
|
|
|
|
'Grpc-Metadata-macaroon': fs.readFileSync(config.LND.MACAROON_PATH).toString('hex'),
|
|
|
|
},
|
|
|
|
httpsAgent: new Agent({
|
|
|
|
ca: fs.readFileSync(config.LND.TLS_CERT_PATH)
|
|
|
|
}),
|
|
|
|
timeout: config.LND.TIMEOUT
|
|
|
|
};
|
|
|
|
} catch (e) {
|
|
|
|
config.LIGHTNING.ENABLED = false;
|
2023-03-21 16:21:11 +09:00
|
|
|
logger.updateNetwork();
|
|
|
|
logger.err(`Could not initialize LND Macaroon/TLS Cert. Disabling LIGHTNING. ` + (e instanceof Error ? e.message : e));
|
2022-04-18 18:22:00 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-24 01:33:38 +04:00
|
|
|
async $getNetworkInfo(): Promise<ILightningApi.NetworkInfo> {
|
2022-07-29 20:53:19 +02:00
|
|
|
return axios.get<ILightningApi.NetworkInfo>(config.LND.REST_API_URL + '/v1/graph/info', this.axiosConfig)
|
|
|
|
.then((response) => response.data);
|
2022-04-18 18:22:00 +04:00
|
|
|
}
|
|
|
|
|
2022-07-06 21:43:47 +02:00
|
|
|
async $getInfo(): Promise<ILightningApi.Info> {
|
2022-07-29 20:53:19 +02:00
|
|
|
return axios.get<ILightningApi.Info>(config.LND.REST_API_URL + '/v1/getinfo', this.axiosConfig)
|
|
|
|
.then((response) => response.data);
|
2022-04-18 18:22:00 +04:00
|
|
|
}
|
2022-04-24 01:33:38 +04:00
|
|
|
|
2022-07-06 21:43:47 +02:00
|
|
|
async $getNetworkGraph(): Promise<ILightningApi.NetworkGraph> {
|
2023-07-05 11:28:13 +02:00
|
|
|
const graph = await axios.get<ILightningApi.NetworkGraph>(config.LND.REST_API_URL + '/v1/graph', this.axiosConfig)
|
2022-07-29 20:53:19 +02:00
|
|
|
.then((response) => response.data);
|
2023-07-05 11:28:13 +02:00
|
|
|
|
|
|
|
for (const node of graph.nodes) {
|
|
|
|
const nodeFeatures: ILightningApi.Feature[] = [];
|
2023-07-05 14:00:30 +02:00
|
|
|
for (const bit in node.features) {
|
2023-07-05 11:28:13 +02:00
|
|
|
nodeFeatures.push({
|
|
|
|
bit: parseInt(bit, 10),
|
2023-07-05 14:00:30 +02:00
|
|
|
name: node.features[bit].name,
|
2023-07-05 11:28:13 +02:00
|
|
|
is_required: node.features[bit].is_required,
|
|
|
|
is_known: node.features[bit].is_known,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
node.features = nodeFeatures;
|
|
|
|
}
|
|
|
|
|
|
|
|
return graph;
|
2022-04-24 01:33:38 +04:00
|
|
|
}
|
2022-04-18 18:22:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
export default LndApi;
|