aboutsummaryrefslogtreecommitdiffstats
path: root/routes/lightning/channel.js
blob: b6c4a25fd0ff6a9f496360143951b1b9d97fd04b (plain) (blame)
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
const axios = require("axios");

module.exports = ah(async (req, res) => {
  const { params } = req.body;
  const [pubkey, host] = params.uri.split("@");

  l.info("connecting to peer", req.user.username, pubkey, host);
  let result;
  try {
    result = await lna.connectPeer({
      addr: { pubkey, host },
      perm: true
    });
  } catch (e) {
    if (!e.message.includes('already connected')) {
      l.error("problem connecting to peer", e.message);
      return res.status(500).send(e.message);
    }
  }

  const { callback, k1 } = params;
  let remoteid;
  try {
    remoteid = (await lna.getInfo({})).identity_pubkey;
  } catch(e) {
    l.error("problem getting lightning node info", e.message);
    return res.status(500).send(e.message);
  } 

  let url = `${callback}?k1=${k1}&remoteid=${remoteid}&private=0`;

  try {
    let response = await axios.get(url);
    res.send(response.data);
  } catch(e) {
    l.error("problem sending channel request", e.message);
    return res.status(500).send({ status: "ERROR", reason: e.message });
  } 
});