49 lines
1.7 KiB
TypeScript
Raw Normal View History

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';
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() {
if (config.LIGHTNING.ENABLED) {
2023-03-21 15:49:38 +09:00
try {
const macaroon = fs.readFileSync(config.LND.MACAROON_PATH).toString('hex');
this.axiosConfig = {
headers: {
'Grpc-Metadata-macaroon': macaroon
},
httpsAgent: new Agent({
ca: fs.readFileSync(config.LND.TLS_CERT_PATH)
}),
timeout: config.LND.TIMEOUT
};
} catch (e) {
logger.err(`Could not initialize LND Macaroon/TLS Cert. Disabling LIGHTNING. ` + (e instanceof Error ? e.message : e));
config.LIGHTNING.ENABLED = false;
}
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
}
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
async $getNetworkGraph(): Promise<ILightningApi.NetworkGraph> {
2022-07-29 20:53:19 +02:00
return axios.get<ILightningApi.NetworkGraph>(config.LND.REST_API_URL + '/v1/graph', this.axiosConfig)
.then((response) => response.data);
2022-04-24 01:33:38 +04:00
}
2022-04-18 18:22:00 +04:00
}
export default LndApi;