aboutsummaryrefslogtreecommitdiffstats
path: root/routes/ecash.ts
blob: 995a8dce1607b66d479b69c986cfb7745df7e3b6 (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
import { check, get, redeem } from "$lib/ecash";
import { err } from "$lib/logging";
import { bail, fail, getInvoice, getUser } from "$lib/utils";
import { getEncodedToken } from "@cashu/cashu-ts";

export default {
  // Stateless status lookup for a token the client is holding. This replaced
  // an unauthenticated POST that stored any submitted string under a fresh id
  // forever (thousands of forged and foreign tokens accumulated); `get` below
  // still serves the ids that were handed out while it existed.
  async status(req, res) {
    const {
      body: { token },
    } = req;
    try {
      if (typeof token !== "string" || !token.startsWith("cashu"))
        fail("Invalid token");
      res.send(await check(token));
    } catch (e) {
      err(e.message);
      bail(res, e.message);
    }
  },

  async get(req, res) {
    const {
      params: { id },
    } = req;
    try {
      const token = await get(id);
      if (!token) fail("Ecash not found");
      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 redeem({ token, user });
      res.send({ ok: true, amount });
    } catch (e) {
      err(e.message);
      bail(res, e.message);
    }
  },

  // NUT-18 payment-request transport: a wallet POSTs proofs for one of our
  // ecash-type invoices, which are melted into a Lightning invoice for the
  // invoice's owner under the same id.
  async receive(req, res) {
    try {
      const { id, proofs, mint, memo } = req.body;
      const invoice = await getInvoice(id);
      if (!invoice) fail("Invoice not found");
      const user = await getUser(invoice.uid);
      if (!user) fail("User not found");

      await redeem({
        token: getEncodedToken({ mint, proofs }),
        user,
        invoice,
        memo,
      });

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