2020-05-21 09:14:56 +02:00
|
|
|
import { getNode, getChannel } from 'ln-service';
|
2020-05-31 10:23:26 +02:00
|
|
|
import { logger } from 'server/helpers/logger';
|
|
|
|
import { toWithError } from 'server/helpers/async';
|
2020-07-21 23:31:12 +02:00
|
|
|
import {
|
|
|
|
LndObject,
|
|
|
|
GetChannelType,
|
|
|
|
GetNodeType,
|
|
|
|
} from 'server/types/ln-service.types';
|
2020-05-21 09:14:56 +02:00
|
|
|
|
|
|
|
const errorNode = {
|
|
|
|
alias: 'Partner node not found',
|
|
|
|
color: '#000000',
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getNodeFromChannel = async (
|
|
|
|
id: string,
|
|
|
|
publicKey: string,
|
2020-08-03 16:31:20 +02:00
|
|
|
lnd: LndObject | null
|
2020-05-21 09:14:56 +02:00
|
|
|
) => {
|
|
|
|
const [channelInfo, channelError] = await toWithError(
|
|
|
|
getChannel({
|
|
|
|
lnd,
|
|
|
|
id,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
2020-07-21 23:31:12 +02:00
|
|
|
if (channelError || !channelInfo) {
|
2020-05-21 09:14:56 +02:00
|
|
|
logger.verbose(`Error getting channel with id ${id}: %o`, channelError);
|
|
|
|
return errorNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
const partnerPublicKey =
|
2020-07-21 23:31:12 +02:00
|
|
|
(channelInfo as GetChannelType).policies[0].public_key !== publicKey
|
|
|
|
? (channelInfo as GetChannelType).policies[0].public_key
|
|
|
|
: (channelInfo as GetChannelType).policies[1].public_key;
|
2020-05-21 09:14:56 +02:00
|
|
|
|
|
|
|
const [nodeInfo, nodeError] = await toWithError(
|
|
|
|
getNode({
|
|
|
|
lnd,
|
|
|
|
is_omitting_channels: true,
|
|
|
|
public_key: partnerPublicKey,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
2020-07-21 23:31:12 +02:00
|
|
|
if (nodeError || !nodeInfo) {
|
2020-05-21 09:14:56 +02:00
|
|
|
logger.verbose(
|
|
|
|
`Error getting node with public key ${partnerPublicKey}: %o`,
|
|
|
|
nodeError
|
|
|
|
);
|
|
|
|
return errorNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2020-07-21 23:31:12 +02:00
|
|
|
alias: (nodeInfo as GetNodeType).alias,
|
|
|
|
color: (nodeInfo as GetNodeType).color,
|
2020-05-21 09:14:56 +02:00
|
|
|
};
|
|
|
|
};
|