aboutsummaryrefslogtreecommitdiffstats
path: root/routes/lightning/channel.js
blob: 107896f478bd9e36c026f5752b16379c4087b21a (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
40
41
42
43
44
45
46
47
import axios from "axios";
import { addPeer, getIdentity } from "lightning";
import lnd from "$lib/lnd";
import ln from "$lib/ln";

export default async (req, res) => {
  const { params } = req.body;
  const [public_key, socket] = params.uri.split("@");

  l("connecting to peer", req.user.username, public_key, socket);
  let result;
  try {
    result = await addPeer({
      lnd,
      socket,
      public_key
    });
  } catch (e) {
    if (!e.message.includes("already connected")) {
      err("problem connecting to peer", e.message);
      return res.code(500).send(e.message);
    }
  }

  const { callback, k1 } = params;
  let remoteid;
  try {
    if (config.lna.clightning) {
      remoteid = (await ln.getinfo()).id;
    } else {
      remoteid = (await getIdentity({ lnd })).public_key;
    }
  } catch (e) {
    err("problem getting lightning node info", e.message);
    return res.code(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) {
    err("problem sending channel request", e.message);
    return res.code(500).send({ status: "ERROR", reason: e.message });
  }
};