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
|
import config from "$config";
import { emit } from "$lib/sockets";
import { db, g, s } from "$lib/db";
import { getUser, bip21, fail, SATS } from "$lib/utils";
import { types } from "$lib/payments";
import { v4 } from "uuid";
import rpc from "@coinos/rpc";
import ln from "$lib/ln";
let bc = rpc(config.bitcoin);
let lq = rpc(config.liquid);
export let generate = async ({ invoice, user }) => {
let {
bolt11,
aid,
currency,
expiry,
fiat,
tip,
amount,
items = [],
rate,
request_id,
memo,
memoPrompt,
prompt,
type = types.lightning,
webhook,
secret,
} = invoice;
amount = parseInt(amount || 0);
tip = parseInt(tip) || null;
if (user) user = await getUser(user.username);
if (!user) fail("user not provided");
let account = await g(`account:${aid}`);
if (!account) account = await g(`account:${user.id}`);
if (!account) fail("account not found");
let rates = await g("rates");
if (!currency) currency = user.currency;
if (!rate) rate = rates[currency];
if (fiat) amount = Math.round((SATS * fiat) / rate);
if (amount < 0) fail("invalid amount");
if (tip < 0) fail("invalid tip");
if (rate < 0) fail("invalid rate");
if (memo && memo.length > 5000) fail("memo too long");
let id = v4();
let hash, text, path;
if (account.seed) {
type = "bitcoin";
let node = rpc({ ...config[type], wallet: aid });
hash = await node.getNewAddress();
text = bip21(hash, invoice);
({ hdkeypath: path } = await node.getAddressInfo(hash));
} else if (type === types.lightning) {
let r;
if (bolt11) {
let { id: nodeid } = await ln.getinfo();
r = await ln.decode(bolt11);
if (r.payee !== nodeid) fail("invalid invoice");
amount = Math.round(r.amount_msat / 1000);
r.bolt11 = bolt11;
} else {
expiry ||= 60 * 60 * 24 * 30;
r = await ln.invoice({
amount_msat: amount ? `${amount + tip}sat` : "any",
label: id,
description: memo || "",
expiry,
deschashonly: true,
cltv: 19,
});
}
hash = r.bolt11;
text = r.bolt11;
} else if (type === types.bitcoin) {
hash = await bc.getNewAddress();
text = bip21(hash, invoice);
} else if (type === types.liquid) {
hash = await lq.getNewAddress();
text = bip21(hash, invoice);
} else if (type === types.internal) {
hash = id;
} else {
fail("unrecognized type");
}
invoice = {
amount,
aid,
created: Date.now(),
currency,
hash,
expiry,
id,
items,
memo,
rate,
pending: 0,
received: 0,
request_id,
memoPrompt,
path,
prompt,
secret,
text,
tip,
type,
uid: user.id,
webhook,
};
if (type === "liquid") {
let { unconfidential } = await lq.getAddressInfo(hash);
await s(`invoice:${unconfidential}`, id);
}
await s(`invoice:${hash}`, id);
await s(`invoice:${id}`, invoice);
await db.lPush(`${user.id}:invoices`, id);
if (request_id) {
let request = await g(`request:${request_id}`);
if (request) {
let { invoice_id: prev } = request;
request.invoice_id = id;
await s(`request:${request_id}`, request);
if (!prev) emit(request.requester_id, "invoice", invoice);
}
}
return invoice;
};
|