Repository navigation
Expand file tree
/
Copy pathopenChannel.js
More file actions
59 lines (53 loc) · 1.59 KB
/
Copy pathopenChannel.js
File metadata and controls
59 lines (53 loc) · 1.59 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 a channel from the user's node to Pine's gateway node.
*/
const openChannel = ({ request, lnd }) => {
if (!lnd) {
return Promise.reject(new Error('Missing lnd node'));
}
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(() => {
return lnd.lnrpc.openChannelSync({
node_pubkey_string: config.lnd.gateway.publicKey,
local_funding_amount: request.sats,
sat_per_byte: request.satsPerByte,
private: true
});
})
.then(channel => ({
fundingTxid: channel.funding_txid_str,
outputIndex: channel.output_index
}));
};
export default openChannel;