aboutsummaryrefslogtreecommitdiffstats
path: root/routes/payments.js
blob: ecc9a4703dd0d86d869c1486b53336a6272126c5 (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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
const fs = require("fs");
const BitcoinCore = require("@asoltys/bitcoin-core");
const { join } = require("path");

app.post("/send", auth, require("./send"));
app.get(
  "/except",
  adminAuth,
  ah((req, res) => {
    let s = fs.createWriteStream("exceptions", { flags: "a" });
    unaccounted.map(tx => s.write(tx.txid + "\n"));
    l.info("updated exceptions");
    res.send("updated exceptions");
  })
);

if (config.lna) {
  if (config.lna.clightning) {
    lna = require("clightning-client")(config.lna.dir);
  } else {
    const lnd = require("../lib/lnd");
    lna = lnd.default;
    lnp = [
      "addInvoice",
      "channelBalance",
      "connectPeer",
      "decodePayReq",
      "getInfo",
      "listInvoices",
      "listPayments",
      "sendPaymentSync",
      "walletBalance"
    ].reduce(
      (a, b) =>
        (a[b] = args => new Promise(r => lna[b](args, (e, v) => r(v)))) && a,
      {}
    );
  }

  app.post("/lightning/channel", require("./lightning/channel"));
  app.post("/lightning/channelRequest", require("./lightning/channelRequest"));
  app.post("/lightning/invoice", require("./lightning/invoice"));
  app.post("/lightning/query", auth, require("./lightning/query"));
  app.post("/lightning/send", auth, require("./lightning/send"));
  require("./lightning/receive");
}

if (config.bitcoin) {
  bc = new BitcoinCore(config.bitcoin);
  app.post("/bitcoin/broadcast", optionalAuth, require("./bitcoin/broadcast"));
  app.get("/bitcoin/generate", auth, require("./bitcoin/generate"));
  app.post("/bitcoin/sweep", auth, require("./bitcoin/sweep"));
  app.post("/bitcoin/fee", auth, require("./bitcoin/fee"));
  app.post("/bitcoin/send", auth, require("./bitcoin/send"));
  require("./bitcoin/receive");

  setTimeout(async () => {
    try {
      const address = await bc.getNewAddress();
      const { hdkeypath } = await bc.getAddressInfo(address);
      const parts = hdkeypath.split("/");
      app.set("bcAddressIndex", parts[parts.length - 1].replace("'", ""));
    } catch (e) {
      console.error(e);
    }
  }, 50);
}

if (config.liquid) {
  lq = new BitcoinCore(config.liquid);
  rare = new BitcoinCore(config.rare);
  app.post("/liquid/broadcast", optionalAuth, require("./liquid/broadcast"));
  app.get("/liquid/generate", auth, require("./liquid/generate"));
  app.post("/liquid/fee", auth, require("./liquid/fee"));
  app.post("/liquid/send", auth, require("./liquid/send"));
  app.post("/taxi", auth, require("./liquid/taxi"));
  require("./liquid/receive");

  setTimeout(async () => {
    try {
      const address = await lq.getNewAddress();
      const { hdkeypath } = await lq.getAddressInfo(address);
      const parts = hdkeypath.split("/");
      app.set("lqAddressIndex", parts[parts.length - 1].slice(0, -1));
    } catch (e) {
      l.warn("Problem getting liquid address index", e.message);
    }
  }, 50);
}

app.get(
  "/payments",
  auth,
  ah(async (req, res) => {
    if (!req.user.account_id) return res.send([]);
    let payments = await db.Payment.findAll({
      where: {
        account_id: req.user.account_id
      },
      order: [["id", "DESC"]],
      include: {
        model: db.Account,
        as: "account"
      }
    });

    res.send(payments);
  })
);

app.get(
  "/payment/:redeemcode",
  ah(async (req, res) => {
    try {
      const { redeemcode } = req.params;
      let payment = await db.Payment.findOne({
        where: {
          redeemcode
        },
        include: {
          model: db.Account,
          as: "account"
        }
      });

      if (!payment) fail("invalid code");

      res.send(payment);
    } catch (e) {
      res.status(500).send(e.message);
    }
  })
);