Fetch funding tx for clightning channels

This commit is contained in:
nymkappa
2022-08-02 16:39:34 +02:00
parent 06a404d9ba
commit d9077412e2
4 changed files with 29 additions and 18 deletions

View File

@@ -245,7 +245,7 @@ export default class CLightningClient extends EventEmitter implements AbstractLi
async $getNetworkGraph(): Promise<ILightningApi.NetworkGraph> {
const listnodes: any[] = await this.call('listnodes');
const listchannels: any[] = await this.call('listchannels');
const channelsList = convertAndmergeBidirectionalChannels(listchannels['channels']);
const channelsList = await convertAndmergeBidirectionalChannels(listchannels['channels']);
return {
nodes: listnodes['nodes'].map(node => convertNode(node)),

View File

@@ -1,4 +1,5 @@
import { ILightningApi } from '../lightning-api.interface';
import FundingTxFetcher from '../../../tasks/lightning/sync-tasks/funding-tx-fetcher';
/**
* Convert a clightning "listnode" entry to a lnd node entry
@@ -22,7 +23,7 @@ export function convertNode(clNode: any): ILightningApi.Node {
/**
* Convert clightning "listchannels" response to lnd "describegraph.edges" format
*/
export function convertAndmergeBidirectionalChannels(clChannels: any[]): ILightningApi.Channel[] {
export async function convertAndmergeBidirectionalChannels(clChannels: any[]): Promise<ILightningApi.Channel[]> {
const consolidatedChannelList: ILightningApi.Channel[] = [];
const clChannelsDict = {};
const clChannelsDictCount = {};
@@ -33,14 +34,14 @@ export function convertNode(clNode: any): ILightningApi.Node {
clChannelsDictCount[clChannel.short_channel_id] = 1;
} else {
consolidatedChannelList.push(
buildFullChannel(clChannel, clChannelsDict[clChannel.short_channel_id])
await buildFullChannel(clChannel, clChannelsDict[clChannel.short_channel_id])
);
delete clChannelsDict[clChannel.short_channel_id];
clChannelsDictCount[clChannel.short_channel_id]++;
}
}
for (const short_channel_id of Object.keys(clChannelsDict)) {
consolidatedChannelList.push(buildIncompleteChannel(clChannelsDict[short_channel_id]));
consolidatedChannelList.push(await buildIncompleteChannel(clChannelsDict[short_channel_id]));
}
return consolidatedChannelList;
@@ -55,16 +56,20 @@ export function convertChannelId(channelId): string {
* Convert two clightning "getchannels" entries into a full a lnd "describegraph.edges" format
* In this case, clightning knows the channel policy for both nodes
*/
function buildFullChannel(clChannelA: any, clChannelB: any): ILightningApi.Channel {
async function buildFullChannel(clChannelA: any, clChannelB: any): Promise<ILightningApi.Channel> {
const lastUpdate = Math.max(clChannelA.last_update ?? 0, clChannelB.last_update ?? 0);
const tx = await FundingTxFetcher.$fetchChannelOpenTx(clChannelA.short_channel_id);
const parts = clChannelA.short_channel_id.split('x');
const outputIdx = parts[2];
return {
channel_id: clChannelA.short_channel_id,
capacity: clChannelA.satoshis,
last_update: lastUpdate,
node1_policy: convertPolicy(clChannelA),
node2_policy: convertPolicy(clChannelB),
chan_point: ':0', // TODO
chan_point: `${tx.txid}:${outputIdx}`,
node1_pub: clChannelA.source,
node2_pub: clChannelB.source,
};
@@ -74,14 +79,18 @@ function buildFullChannel(clChannelA: any, clChannelB: any): ILightningApi.Chann
* Convert one clightning "getchannels" entry into a full a lnd "describegraph.edges" format
* In this case, clightning knows the channel policy of only one node
*/
function buildIncompleteChannel(clChannel: any): ILightningApi.Channel {
async function buildIncompleteChannel(clChannel: any): Promise<ILightningApi.Channel> {
const tx = await FundingTxFetcher.$fetchChannelOpenTx(clChannel.short_channel_id);
const parts = clChannel.short_channel_id.split('x');
const outputIdx = parts[2];
return {
channel_id: clChannel.short_channel_id,
capacity: clChannel.satoshis,
last_update: clChannel.last_update ?? 0,
node1_policy: convertPolicy(clChannel),
node2_policy: null,
chan_point: ':0', // TODO
chan_point: `${tx.txid}:${outputIdx}`,
node1_pub: clChannel.source,
node2_pub: clChannel.destination,
};