-
Notifications
You must be signed in to change notification settings - Fork 4
/
lnd.js
120 lines (92 loc) · 2.36 KB
/
lnd.js
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
const LndGrpc = require("lnd-grpc");
const dotenv = require("dotenv");
dotenv.config();
const options = {
host: process.env.LND_HOST,
cert: process.env.LND_CERT,
macaroon: process.env.LND_MACAROON,
};
const lnd = new LndGrpc(options);
const connect = async () => {
await lnd.connect();
console.log("LND gRPC client is ready to use");
};
const getInfo = async () => {
const info = await lnd.services.Lightning.getInfo();
return info;
};
const getBalances = async () => {
const walletBalance = await lnd.services.Lightning.walletBalance();
const channelBalance = await lnd.services.Lightning.channelBalance();
return {
walletBalance,
channelBalance,
};
};
const getChannels = async () => {
const channels = await lnd.services.Lightning.listChannels();
return channels;
};
const createInvoice = async (amount, memo = "") => {
const invoice = await lnd.services.Lightning.addInvoice({
value: amount,
memo,
});
return invoice;
};
const payInvoice = async (paymentRequest) => {
const payment = await lnd.services.Lightning.sendPaymentSync({
payment_request: paymentRequest,
});
return payment;
};
const removePeer = async (pubkey) => {
const removePeer = await lnd.services.Lightning.disconnectPeer({
pub_key: pubkey,
});
return removePeer;
};
const addPeer = async (pubkey, host) => {
const addPeer = await lnd.services.Lightning.connectPeer({
addr: {
pubkey,
host: host,
},
});
return addPeer;
};
const openChannel = async (pubkey, amount) => {
const channelOpen = await lnd.services.Lightning.openChannel({
node_pubkey: pubkey,
local_funding_amount: amount,
});
return channelOpen;
};
const closeChannel = async (channelPoint) => {
// first get the channel point from the channel
const channel = await lnd.services.Lightning.getChanInfo({
chan_id: channelPoint,
});
const fundingTxId = channel.chan_point.split(":")[0];
const outputIndex = parseInt(channel.chan_point.split(":")[1]);
// then close the channel
const channelClose = await lnd.services.Lightning.closeChannel({
channel_point: {
funding_txid_str: fundingTxId,
output_index: outputIndex,
},
});
return channelClose;
};
module.exports = {
connect,
getInfo,
getBalances,
createInvoice,
payInvoice,
getChannels,
addPeer,
removePeer,
openChannel,
closeChannel,
};