aboutsummaryrefslogtreecommitdiffstats
path: root/routes/invoices.js
blob: 3a7f16b550dc1aa6ee9fb568b43e3226f9be6d12 (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import app from "$app";
import db from "$db";
import store from "$lib/store";
import { optionalAuth } from "$lib/passport";
import { Op } from "@sequelize/core";
import axios from "axios";
import { err, l } from "$lib/logging";
import bc from "$lib/bitcoin";
import lq from "$lib/liquid";
import { SATS, bip21, deriveAddress, derivePayRequest } from "$lib/utils";

app.get("/invoice", async (req, res, next) => {
  try {
    const invoice = await db.Invoice.findOne({
      where: {
        uuid: req.query.uuid,
      },
      include: {
        model: db.User,
        as: "user",
        attributes: ["username", "currency"],
      },
    });

    res.send(invoice);
  } catch (e) {
    err("invoice not found", e);
    res.code(500).send("Invoice not found");
  }
});

app.get("/invoice/:text", async (req, res, next) => {
  try {
    let { text } = req.params;
    let where =
      text.split("-").length > 4
        ? { uuid: text }
        : text.startsWith("ln")
        ? { text }
        : {
            [Op.or]: [{ unconfidential: text }, { address: text }],
          };

    const invoice = await db.Invoice.findOne({
      where,
      include: {
        model: db.User,
        as: "user",
        attributes: ["username", "currency"],
      },
    });

    res.send(invoice);
  } catch (e) {
    err("invoice not found", e);
    res.code(500).send("Invoice not found");
  }
});

app.post("/invoice", optionalAuth, async (req, res, next) => {
  try {
    let { type = "bech32", liquidAddress, id, invoice, user, tx } = req.body;
    let { blindkey, currency, tip, amount, rate, network } = invoice;
    let address, unconfidential, text;

    if (amount < 0) throw new Error("amount out of range");
    if (tip > amount || tip > 1000000 || tip < 0)
      throw new Error("tip amount out of range");

    if (!user) ({ user } = req);
    else {
      user = await db.User.findOne({
        where: {
          username: user.username,
        },
        include: {
          model: db.Account,
          as: "account",
        },
      });
    }
    if (!user) throw new Error("user not provided");
    if (!currency) currency = user.currency;
    if (!rate) rate = store.rates[currency];
    if (tip > amount || tip > 1000000) throw new Error("tip is too large");
    if (tip < 0 || amount < 0) throw new Error("invalid amount");

    if (user.account.pubkey) {
      let { address, confidentialAddress } = await deriveAddress(
        user.account,
        type
      );
      if (confidentialAddress) {
        address = confidentialAddress;
        unconfidential = address;
      } else {
        address = address;
      }
    } else if (network !== "lightning") {
      address = await { bitcoin: bc, liquid: lq }[network].getNewAddress();
      if (network === "liquid")
        ({ unconfidential } = await lq.getAddressInfo(address));
    }

    invoice = {
      ...invoice,
      account_id: user.account.id,
      address,
      amount,
      currency,
      rate,
      tip,
      unconfidential,
      user_id: user.id,
    };

    if (network === "lightning") {
      invoice.text = await derivePayRequest(invoice);
    } else {
      invoice.text = bip21(invoice, user.account);
    }

    if (liquidAddress) {
      if (network === "lightning") {
        l("conversion request for", liquidAddress, invoice.text);
        store.convert[invoice.text] = { address: liquidAddress, tx };
      } else if (network === "liquid") {
        l("conversion request for", liquidAddress, invoice.unconfidential);
        store.convert[invoice.unconfidential] = { address: liquidAddress, tx };
      } else {
        l("conversion request for", liquidAddress, invoice.address);
        store.convert[invoice.address] = { address: liquidAddress, tx };
      }
    }

    l(
      "creating invoice",
      user.username,
      invoice.network,
      invoice.amount,
      invoice.tip,
      invoice.currency,
      invoice.text && `${invoice.text.substr(0, 8)}..${invoice.text.substr(-6)}`
    );

    if (!invoice.tip) invoice.tip = 0;

    invoice = await db.Invoice.create(invoice);
    store.addresses[invoice.address] = user.username;
    if (invoice.unconfidential) {
      store.addresses[invoice.unconfidential] = user.username;
      if (blindkey) await lq.importBlindingKey(invoice.address, blindkey);
    }

    res.send(invoice);
  } catch (e) {
    err(e.message, e.stack);
    res.code(500).send(`Problem during invoice creation: ${e.message}`);
  }
});

app.post(
  "/:username/:network/invoice",
  optionalAuth,
  async (req, res, next) => {
    let { network, username } = req.params;
  }
);