Merge Lightning backend into Mempool backend
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
import { ILightningApi } from './lightning-api.interface';
|
||||
|
||||
export interface AbstractLightningApi {
|
||||
$getNetworkInfo(): Promise<ILightningApi.NetworkInfo>;
|
||||
$getNetworkGraph(): Promise<ILightningApi.NetworkGraph>;
|
||||
$getChanInfo(id: string): Promise<ILightningApi.Channel>;
|
||||
}
|
||||
13
backend/src/api/lightning/lightning-api-factory.ts
Normal file
13
backend/src/api/lightning/lightning-api-factory.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import config from '../../config';
|
||||
import { AbstractLightningApi } from './lightning-api-abstract-factory';
|
||||
import LndApi from './lnd/lnd-api';
|
||||
|
||||
function lightningApiFactory(): AbstractLightningApi {
|
||||
switch (config.LIGHTNING.BACKEND) {
|
||||
case 'lnd':
|
||||
default:
|
||||
return new LndApi();
|
||||
}
|
||||
}
|
||||
|
||||
export default lightningApiFactory();
|
||||
53
backend/src/api/lightning/lightning-api.interface.ts
Normal file
53
backend/src/api/lightning/lightning-api.interface.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
export namespace ILightningApi {
|
||||
export interface NetworkInfo {
|
||||
average_channel_size: number;
|
||||
channel_count: number;
|
||||
max_channel_size: number;
|
||||
median_channel_size: number;
|
||||
min_channel_size: number;
|
||||
node_count: number;
|
||||
not_recently_updated_policy_count: number;
|
||||
total_capacity: number;
|
||||
}
|
||||
|
||||
export interface NetworkGraph {
|
||||
channels: Channel[];
|
||||
nodes: Node[];
|
||||
}
|
||||
|
||||
export interface Channel {
|
||||
id: string;
|
||||
capacity: number;
|
||||
policies: Policy[];
|
||||
transaction_id: string;
|
||||
transaction_vout: number;
|
||||
updated_at: string;
|
||||
}
|
||||
|
||||
interface Policy {
|
||||
public_key: string;
|
||||
base_fee_mtokens?: number;
|
||||
cltv_delta?: number;
|
||||
fee_rate?: number;
|
||||
is_disabled?: boolean;
|
||||
max_htlc_mtokens?: number;
|
||||
min_htlc_mtokens?: number;
|
||||
updated_at?: string;
|
||||
}
|
||||
|
||||
export interface Node {
|
||||
alias: string;
|
||||
color: string;
|
||||
features: Feature[];
|
||||
public_key: string;
|
||||
sockets: string[];
|
||||
updated_at: string;
|
||||
}
|
||||
|
||||
interface Feature {
|
||||
bit: number;
|
||||
is_known: boolean;
|
||||
is_required: boolean;
|
||||
type: string;
|
||||
}
|
||||
}
|
||||
44
backend/src/api/lightning/lnd/lnd-api.ts
Normal file
44
backend/src/api/lightning/lnd/lnd-api.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import { AbstractLightningApi } from '../lightning-api-abstract-factory';
|
||||
import { ILightningApi } from '../lightning-api.interface';
|
||||
import * as fs from 'fs';
|
||||
import * as lnService from 'ln-service';
|
||||
import config from '../../../config';
|
||||
import logger from '../../../logger';
|
||||
|
||||
class LndApi implements AbstractLightningApi {
|
||||
private lnd: any;
|
||||
constructor() {
|
||||
if (!config.LIGHTNING.ENABLED) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const tls = fs.readFileSync(config.LND_NODE_AUTH.TLS_CERT_PATH).toString('base64');
|
||||
const macaroon = fs.readFileSync(config.LND_NODE_AUTH.MACAROON_PATH).toString('base64');
|
||||
|
||||
const { lnd } = lnService.authenticatedLndGrpc({
|
||||
cert: tls,
|
||||
macaroon: macaroon,
|
||||
socket: config.LND_NODE_AUTH.SOCKET,
|
||||
});
|
||||
|
||||
this.lnd = lnd;
|
||||
} catch (e) {
|
||||
logger.err('Could not initiate the LND service handler: ' + (e instanceof Error ? e.message : e));
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
async $getNetworkInfo(): Promise<ILightningApi.NetworkInfo> {
|
||||
return await lnService.getNetworkInfo({ lnd: this.lnd });
|
||||
}
|
||||
|
||||
async $getNetworkGraph(): Promise<ILightningApi.NetworkGraph> {
|
||||
return await lnService.getNetworkGraph({ lnd: this.lnd });
|
||||
}
|
||||
|
||||
async $getChanInfo(id: string): Promise<ILightningApi.Channel> {
|
||||
return await lnService.getChannel({ lnd: this.lnd, id });
|
||||
}
|
||||
}
|
||||
|
||||
export default LndApi;
|
||||
Reference in New Issue
Block a user