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
|
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 { tbDebit } from "$lib/tb";
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;
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(c) {
const { token } = await c.req.json();
try {
const id = v4();
await s(`cash:${id}`, token);
return c.json({ id });
} catch (e) {
err(e.message);
return bail(c, e.message);
}
},
async get(c) {
const id = c.req.param("id");
try {
const token = await get(id);
const status = await check(token);
return c.json({ token, status });
} catch (e) {
err(e.message);
return bail(c, e.message);
}
},
async claim(c) {
const { token } = await c.req.json();
const user = c.get("user");
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 });
return c.json({ ok: true });
} catch (e) {
err(e.message);
return bail(c, e.message);
}
},
async mint(c) {
const body = await c.req.json();
const user = c.get("user");
const amount = parseInt(body.amount);
fail("disabled");
return c.json(await sendCash({ amount, user }));
},
async melt(c) {
const body = await c.req.json();
const user = c.get("user");
let { amount, bolt11: hash, preimage } = body;
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 tbDebit(uid, uid, type, amount || 0, 0, 0, 0, 0, "Insufficient funds");
const rates = await g("rates");
const rate = rates[currency];
if ((ourfee as any).err) fail((ourfee as any).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);
return c.json(p);
} catch (e) {
return bail(c, e.message);
}
},
async receive(c) {
try {
const body = await c.req.json();
const { id, proofs, mint, memo } = body;
const { uid: ref } = await getInvoice(id);
const amount = await claim(
getEncodedToken({
mint,
proofs,
}),
);
await credit({ hash: id, amount, memo, ref, type });
return c.json({ id });
} catch (e) {
console.log(e);
err(e.message);
return bail(c, e.message);
}
},
};
|