aboutsummaryrefslogtreecommitdiffstats
path: root/lib/invoices.ts
blob: ba55b77c1f817ad43b69974a4b4d0bfe8561defb (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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import config from "$config";
import { db, g, s } from "$lib/db";
import { request } from "$lib/ecash";
import ln from "$lib/ln";
import { warn } from "$lib/logging";
import { emit } from "$lib/sockets";
import { SATS, bip21, fail, getInvoice, getUser } from "$lib/utils";
import rpc from "@coinos/rpc";
import { v4 } from "uuid";

import { PaymentType } from "$lib/types";

const bc = rpc(config.bitcoin);
const lq = rpc(config.liquid);

// Preimage queue entries are {preimage, price, fiat, currency} JSON, but
// entries queued before prices existed are bare hex strings — treat those
// as priceless
export const parseEntry = (e) => {
  try {
    const parsed = JSON.parse(e);
    if (parsed?.preimage)
      return { price: null, fiat: null, currency: null, ...parsed };
  } catch {}
  return { preimage: e, price: null, fiat: null, currency: null };
};

export const generate = async ({ invoice, user }) => {
  let {
    address_type,
    bolt11,
    bolt12,
    aid,
    currency,
    expiry,
    fiat,
    id,
    tip,
    amount,
    items = [],
    own,
    rate,
    request_id,
    memo,
    memoPrompt,
    prompt,
    type = PaymentType.lightning,
    usePreimage,
    webhook,
    secret,
  } = invoice;

  amount = Number.parseInt(amount || 0);
  tip = tip == null ? null : Number.parseInt(tip);

  if (user) user = await getUser(user.username);
  if (!user) fail("user not provided");
  if (typeof prompt === "undefined") prompt = user.prompt;

  let account = await g(`account:${aid}`);
  if (!account) account = await g(`account:${user.id}`);
  if (!account) fail("account not found");
  aid = account.id;

  const 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");
  // On-chain deposits below 300 sats are silently dropped by the dust filter in
  // the /confirm credit path (routes/payments.ts), so reject creating a bitcoin/
  // liquid invoice for a specific sub-300-sat amount rather than letting a
  // deposit vanish. amount = 0 (any-amount address) is unaffected.
  if (
    (type === PaymentType.bitcoin || type === PaymentType.liquid) &&
    amount > 0 &&
    amount < 300
  )
    fail(
      "On-chain deposits must be at least 300 sats. Use a Lightning invoice for smaller amounts.",
    );
  if (tip < 0) fail("invalid tip");
  if (rate < 0) fail("invalid rate");
  if (memo && memo.length > 5000) fail("memo too long");

  if (!id) id = v4();

  let hash;
  let text;
  let paymentHash;

  if (type === PaymentType.lightning) {
    // SECURITY: never accept a caller-supplied bolt11. It let anyone register any
    // node-payable invoice (incl. a nutshell mint quote) as their own coinos
    // deposit — which both double-credited (coinos balance + mint ecash) and let
    // an attacker hijack another user's incoming payment (there was no ownership
    // guard, so it overwrote invoice:<bolt11>). Always mint the invoice here.
    if (bolt11) fail("Cannot provide your own Lightning invoice");
    let r;
    {
      expiry ||= 60 * 60 * 24 * 30;

      const args = {
        label: `${id} ${user.username} ${Date.now()}`,
        description: memo || "",
        expiry,
        deschashonly: true,
        cltv: 19,
      };

      if (usePreimage) {
        // Consume the merchant's pre-loaded preimages (FIFO) so the invoice's
        // payment hash is sha256(preimage) and paying it reveals a secret the
        // merchant chose. Never fall back to a random preimage — a buyer would
        // be paying for a secret that unlocks nothing.
        const queue = `${user.id}:preimages`;
        let entry;
        while ((entry = await db.lPop(queue))) {
          const {
            preimage,
            price,
            fiat: entryFiat,
            currency: entryCurrency,
          } = parseEntry(entry);
          // The invoice creator is unauthenticated, so a merchant-set price
          // must win over the amount in the request — otherwise a buyer
          // could name their own price for the secret. Fiat prices convert
          // at the current rate, and the invoice takes on the merchant's
          // currency so receipts show the fiat price as set.
          if (price) amount = price;
          else if (entryFiat) {
            if (!rates[entryCurrency]) {
              await db.lPush(queue, entry);
              fail(`no rate available for ${entryCurrency}`);
            }
            currency = entryCurrency;
            rate = rates[currency];
            amount = Math.round((SATS * entryFiat) / rate);
          }
          try {
            r = await ln.invoice({
              ...args,
              amount_msat: amount ? `${amount + tip}sat` : "any",
              preimage,
            });
            // Keep the preimage recoverable for internal settlements, where
            // cln never pays the invoice and so never reveals it — credit()
            // picks this up as the payment ref
            await db.set(`preimage:${r.payment_hash}`, preimage, {
              EX: expiry,
            });
            break;
          } catch (e) {
            // cln rejects a preimage whose payment hash it has already seen —
            // that one is permanently unusable, so drop it and try the next
            if (/preimage/i.test(e.message)) {
              warn("discarding unusable preimage", user.username, e.message);
              continue;
            }
            await db.lPush(queue, entry);
            throw e;
          }
        }
        if (!r) fail("no preimages available");
      } else {
        r = await ln.invoice({
          ...args,
          amount_msat: amount ? `${amount + tip}sat` : "any",
        });
      }
    }

    hash = r.bolt11;
    text = r.bolt11;
    paymentHash = r.payment_hash;
  } else if (type === PaymentType.bolt12) {
    // SECURITY: same as bolt11 — never accept a caller-supplied bolt12. Always
    // mint the offer server-side.
    if (bolt12) fail("Cannot provide your own bolt12 invoice");
    let r;
    {
      r = await ln.offer({
        amount: amount ? `${amount + tip}sat` : "any",
        label: `${id} ${user.username} ${new Date()}`,
        description: memo || id,
      });

      // CLN returns the same offer_id for identical offer contents, so a
      // re-create of an offer we already track must not register a second
      // invoice for it (duplicate :invoices entries — see kaliyi's OCEAN
      // offer). If it's the same user's offer, hand back the existing record.
      const existing = await getInvoice(r.offer_id);
      if (existing) {
        if (existing.uid === user.id) return existing;
        fail("Duplicate offer exists");
      }
      await s(`invoice:${r.offer_id}`, id);
    }

    hash = r.bolt12;
    text = r.bolt12;
  } else if (type === PaymentType.bitcoin) {
    address_type ||= "bech32";
    hash = await bc.getNewAddress({ address_type });
    text = bip21(hash, invoice);
  } else if (type === PaymentType.liquid) {
    if (await g("liquid:deposits:disabled"))
      fail("Liquid deposits are temporarily disabled");
    address_type ||= "blech32";
    hash = await lq.getNewAddress({ address_type });
    text = bip21(hash, invoice);
  } else if (type === PaymentType.internal) {
    hash = id;
  } else if (type === PaymentType.ecash) {
    hash = id;
    text = request(id, amount, memo);
  } else {
    fail(`unrecognized type ${type}`);
  }

  invoice = {
    amount,
    aid,
    address_type,
    created: Date.now(),
    currency,
    hash,
    expiry,
    id,
    items,
    memo,
    rate,
    paymentHash,
    pending: 0,
    received: 0,
    request_id,
    memoPrompt,
    own,
    prompt,
    secret,
    text,
    tip,
    type,
    uid: user.id,
    webhook,
  };

  if (type === "liquid") {
    const { unconfidential } = await lq.getAddressInfo(hash);
    await s(`invoice:${unconfidential}`, id);
  }

  await s(`invoice:${hash}`, id);
  await s(`invoice:${id}`, invoice);
  // generate() is also called on UPDATE (re-using an existing invoice id, e.g.
  // a tip/webhook change or a repeated bolt12 offer create), which must NOT
  // re-append the id to the list. Only add it the first time — otherwise the
  // user's :invoices accumulates duplicate entries and the same offer/invoice
  // renders multiple times (see kaliyi's OCEAN offer, listed 2-6x).
  if ((await db.lPos(`${aid}:invoices`, id)) === null)
    await db.lPush(`${aid}:invoices`, id);

  if (request_id) {
    const request = await g(`request:${request_id}`);
    if (request) {
      const { invoice_id: prev } = request;
      request.invoice_id = id;
      await s(`request:${request_id}`, request);

      if (!prev) emit(request.requester_id, "invoice", invoice);
    }
  }

  return invoice;
};

// Each user gets one standing amountless bolt12 offer (lno1...) they can hand
// out as a reusable payment code — e.g. published in a kind 10058 list for
// NIP-177 bolt12 zaps. Created lazily on first request, then reused; incoming
// payments credit through the regular bolt12 receive path via local_offer_id.
export const getUserOffer = async (user) => {
  const key = `${user.id}:offer`;
  const existingId = await g(key);
  if (existingId) {
    const existing = await g(`invoice:${existingId}`);
    if (existing?.hash) return existing;
  }

  let domain;
  try {
    domain = new globalThis.URL(process.env.URL).host;
  } catch (e) {}

  const invoice = await generate({
    invoice: {
      type: PaymentType.bolt12,
      memo: domain ? `${user.username}@${domain}` : user.username,
    },
    user,
  });

  await s(key, invoice.id);
  return invoice;
};