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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
|
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";
import { emit } from "$lib/sockets";
import bolt11 from "bolt11";
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", "profile"]
},
{
model: db.Request,
as: "request"
}
]
});
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", "profile"]
}
});
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,
request_id
} = invoice;
let address, unconfidential, 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");
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 (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);
}
if (request_id) {
let request = await db.Request.findOne({
where: { id: request_id },
include: {
model: db.User,
as: "requester",
attributes: ["username"]
}
});
if (request) {
let existing = request.invoice_id;
request.invoice_id = invoice.id;
await request.save();
if (!existing)
emit(request.requester.username, "invoice", {
amount: invoice.amount,
currency: invoice.currency,
rate: invoice.rate,
request_id,
uuid: invoice.uuid,
user: { username: user.username, profile: user.profile }
});
}
}
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;
}
);
app.post("/conversion",
async (req, res, next) => {
let { invoice_id, text } = req.body;
let conversion = await db.Conversion.create({ invoice_id, text });
res.send(conversion);
}
);
app.post("/invoice/decode",
async (req, res, next) => {
let {text} = req.body;
res.send(bolt11.decode(text))
}
)
|