Repository navigation
Expand file tree
/
Copy pathopenInboundChannel.js
More file actions
59 lines (52 loc) · 1.6 KB
/
Copy pathopenInboundChannel.js
File metadata and controls
59 lines (52 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/* eslint-disable camelcase */
import config from '../config';
const getGatewayChannel = (lnd) => {
return lnd.lnrpc.listChannels({}).then(({ channels }) => {
return channels && channels.find(channel => (
channel.remote_pubkey === config.lnd.gateway.publicKey
));
});
};
const getPendingGatewayChannel = (lnd) => {
return lnd.lnrpc.pendingChannels({}).then(({ pending_open_channels }) => {
if (!pending_open_channels) {
return;
}
return pending_open_channels.find(pendingChannel => (
pendingChannel.channel.remote_node_pub === config.lnd.gateway.publicKey
));
});
};
/**
* Opens an inbound channel from Pine's gateway node
* to the user's lnd node. The gateway node will commit
* the funds rather than the user.
*/
const openInboundChannel = ({ lnd, gateway }) => {
if (!lnd) {
return Promise.reject(new Error('Missing lnd node'));
}
const { fundingAmountSats, pushAmountSats } = config.lnd.gateway.channel;
return getGatewayChannel(lnd)
.then(gatewayChannel => {
if (gatewayChannel) {
return Promise.reject(new Error('A channel has already been opened'));
}
})
.then(() => {
return getPendingGatewayChannel(lnd).then(pendingGatewayChannel => {
if (pendingGatewayChannel) {
return Promise.reject(new Error('A channel is already pending'));
}
});
})
.then(() => lnd.lnrpc.getInfo({}))
.then(({ identity_pubkey }) => {
return gateway.openChannel(
fundingAmountSats,
pushAmountSats,
identity_pubkey
);
});
};
export default openInboundChannel;