aboutsummaryrefslogtreecommitdiffstats
path: root/routes/ecash.ts
blob: e1e70490b6cb0ff838e7c98f1beeb5527dc08545 (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
import { db, g, s } from "$lib/db";
import { check, claim, get, mint } from "$lib/ecash";
import { err, l } from "$lib/logging";
import { credit, debit } from "$lib/payments";
import { emit } from "$lib/sockets";
import { bail, fail, getInvoice } from "$lib/utils";
import { getEncodedToken } from "@cashu/cashu-ts";
import { v4 } from "uuid";

import { PaymentType } from "$lib/types";
const { ecash: type } = PaymentType;

Error.stackTraceLimit = 100; // Set this to the desired limit

const sendCash = async ({ amount, user }) => {
  const id = v4();
  const hash = v4();

  const p = await debit({ hash, amount, user, type });
  const token = await mint(parseInt(amount));
  p.memo = id;
  const { id: pid } = p;
  await s(`payment:${pid}`, p);
  s(`cash:${id}`, token);

  return { id, token, pid };
};

export default {
  async save(req, res) {
    const {
      body: { token },
    } = req;
    try {
      const id = v4();
      await s(`cash:${id}`, token);
      res.send({ id });
    } catch (e) {
      err(e.message);
      bail(res, e.message);
    }
  },

  async get(req, res) {
    const {
      params: { id },
    } = req;
    try {
      const token = await get(id);
      const status = await check(token);
      res.send({ token, status });
    } catch (e) {
      err(e.message);
      bail(res, e.message);
    }
  },

  async claim(req, res) {
    const {
      body: { token },
      user,
    } = req;
    try {
      const amount = await claim(token);

      let memo;
      const hash = v4();
      const { currency, id: uid } = user;
      const rates = await g("rates");
      await s(`invoice:${hash}`, {
        currency,
        id: hash,
        hash,
        rate: rates[currency],
        uid,
        received: 0,
      });

      await credit({ hash, amount, memo, ref: user.id, type });

      res.send({ ok: true });
    } catch (e) {
      err(e.message);
      bail(res, e.message);
    }
  },

  async mint(req, res) {
    const { body, user } = req;
    const amount = parseInt(body.amount);
    fail("disabled");
    res.send(await sendCash({ amount, user }));
  },

  async melt(req, res) {
    let {
      body: { amount, bolt11: hash, preimage },
      user,
    } = req;
    try {
      amount = Math.round(amount / 1000);
      const ref = preimage;
      const { lightning: type } = PaymentType;
      if (user.username !== "mint") fail("unauthorized");
      const { id: uid, currency } = user;
      const ourfee = await db.debit(
        `balance:${uid}`,
        `credit:${type}:${uid}`,
        "Insufficient funds",
        amount || 0,
        0,
        0,
        0,
        0
      );

      const rates = await g("rates");
      const rate = rates[currency];

      if (ourfee.err) fail(ourfee.err);

      const id = v4();
      const p = {
        id,
        amount: -amount,
        hash,
        ourfee,
        uid,
        confirmed: true,
        rate,
        currency,
        type,
        ref,
        created: Date.now(),
      };

      await s(`payment:${hash}`, id);
      await s(`payment:${id}`, p);
      await db.lPush(`${uid}:payments`, id);

      l(user.username, "sent", type, amount);
      emit(user.id, "payment", p);

      res.send(p);
    } catch (e) {
      bail(res, e.message);
    }
  },

  async receive(req, res) {
    try {
      const { id, proofs, mint, memo } = req.body;
      const { uid: ref } = await getInvoice(id);

      const amount = await claim(
        getEncodedToken({
          mint,
          proofs,
        }),
      );

      await credit({ hash: id, amount, memo, ref, type });

      res.send({ id });
    } catch (e) {
      err(e.message);
      bail(res, e.message);
    }
  },
};