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
|
import app from "app";
import store from "lib/store";
import { optionalAuth } from "lib/passport";
import { err, l } from "lib/logging";
import { SATS, derivePayRequest } from "lib/utils";
import { emit } from "lib/sockets";
import { v4 } from "uuid";
import { rd, g, s } from "lib/redis";
app.get("/invoice", async ({ query: { id } }, res) => {
let invoice = await g(`invoice:${id}`);
invoice.user = await g(`user:${invoice.user_id}`);
res.send(invoice);
});
app.post(
"/invoice",
optionalAuth,
async (
{
body: { type = "bech32", liquidAddress, id, invoice, user, tx },
user: me
},
res
) => {
let {
blindkey,
currency,
tip,
amount,
rate,
network,
request_id
} = invoice;
let text;
if (amount < 0) throw new Error("amount out of range");
if (tip > 5 * amount || tip < 0) throw new Error("tip amount out of range");
tip = tip || 0;
if (!user) user = me;
if (!user) throw new Error("user not provided");
user = await g(`user:${user.id}`);
if (!currency) currency = user.currency;
if (!rate) rate = store.rates[currency];
if (amount < 0) throw new Error("invalid amount");
invoice = {
...invoice,
id: v4(),
amount,
currency,
rate,
tip,
unconfidential,
user_id: user.id
};
invoice.text = await derivePayRequest(invoice);
l(
"creating invoice",
user.username,
network,
amount,
tip,
currency,
`${text.substr(0, 8)}..${text.substr(-6)}`
);
await rd.lPush(`${id}:invoices`, invoice.id);
if (request_id) {
let request = await g(`request:${request_id}`);
let { invoice_id: prev } = request;
request.invoice_id = invoice.id;
await s(`request:${request_id}`, request);
if (!prev) emit(request.requester.username, "invoice", invoice);
}
res.send(invoice);
}
);
|