aboutsummaryrefslogtreecommitdiffstats
path: root/lib/invoices.ts
blob: 27e25c48c6b82f8cbb1731e519f066625d1e2e85 (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
import config from "$config";
import { db, g, s } from "$lib/db";
import { request } from "$lib/ecash";
import { deriveAddress } from "$lib/esplora";
import ln from "$lib/ln";
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);

export const generate = async ({ invoice, user }) => {
  let {
    address_type,
    bolt11,
    bolt12,
    aid,
    currency,
    expiry,
    fiat,
    id,
    hash,
    tip,
    amount,
    items = [],
    own,
    rate,
    request_id,
    memo,
    memoPrompt,
    prompt,
    type = PaymentType.lightning,
    webhook,
    secret,
    forward,
  } = 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) {
    const acct = aid ? await g(`account:${aid}`) : null;
    currency = acct?.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");

  if (!id) id = v4();

  let text;
  let path;
  let paymentHash;

  if (type === PaymentType.ark && account.type !== "ark") {
    // Custodial account receiving via Ark: use vault's ark address
    const accounts = await db.lRange(`${user.id}:accounts`, 0, -1);
    let vaultAddress;
    for (const accId of accounts) {
      const acc = await g(`account:${accId}`);
      if (acc?.type === "ark" && acc.arkAddress) {
        vaultAddress = acc.arkAddress;
        break;
      }
    }
    if (!vaultAddress) fail("No ark vault found");
    text = vaultAddress;
    hash = id;
    await s(`custodial-ark-invoice:${vaultAddress}`, id);
  } else if (account.type === "ark") {
    type = PaymentType.ark;
    text = account.arkAddress;
    hash = id;
    await db.del(`custodial-ark-invoice:${account.arkAddress}`);
  } else if (account.seed || (account.pubkey && account.fingerprint)) {
    type = PaymentType.bitcoin;
    const nextIndex = account.nextIndex || 0;
    const { address, path: hdpath } = deriveAddress(account.pubkey, account.fingerprint, nextIndex);
    hash = address;
    path = hdpath;
    text = bip21(hash, invoice);

    account.nextIndex = nextIndex + 1;
    await s(`account:${aid}`, account);
    await s(`invoice:${hash}`, id);
    await db.sAdd("watching", hash);
  } else if (type === PaymentType.lightning) {
    let r;
    if (bolt11) {
      const { 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} ${user.username} ${Date.now()}`,
        description: memo || "",
        expiry,
        deschashonly: true,
        cltv: 19,
      });
    }

    hash = r.bolt11;
    text = r.bolt11;
    paymentHash = r.payment_hash;
  } else if (type === PaymentType.bolt12) {
    let r;
    if (bolt12) {
      const { id: nodeid } = await ln.getinfo();
      r = await ln.decode(bolt12);
      if (r.invoice_node_id !== nodeid) fail("invalid invoice");
      amount = Math.round(r.invoice_amount_msat / 1000);
      r.bolt12 = bolt12;
    } else {
      r = await ln.offer({
        amount: amount ? `${amount + tip}sat` : "any",
        label: `${id} ${user.username} ${new Date()}`,
        description: memo || id,
      });

      if (await getInvoice(r.offer_id)) 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);
    await db.sAdd("watching", hash);
  } else if (type === PaymentType.liquid) {
    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 if (type === PaymentType.ark) {
    hash = id;
  } else {
    fail(`unrecognized type ${type}`);
  }

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

  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;
};