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
|
import { db, g, s } from "$lib/db";
import ln from "$lib/ln";
import { err, warn } from "$lib/logging";
import { handleZap } from "$lib/nostr";
import { credit, types } from "$lib/payments";
import { getPayment, getUser } from "$lib/utils";
export async function listenForLightning() {
const inv = await ln.waitanyinvoice((await g("pay_index")) || 0);
const {
local_offer_id,
bolt11,
bolt12,
description,
pay_index,
amount_received_msat,
payment_preimage: preimage,
} = inv;
await s("pay_index", pay_index);
setTimeout(listenForLightning);
const received = Math.round(amount_received_msat / 1000);
try {
if (!preimage) return;
const id = bolt11 ? (await g(`invoice:${bolt11}`)) : (await g(`invoice:${local_offer_id}`));
const invoice = await g(`invoice:${id}`);
if (!invoice) return warn("received lightning with no invoice", bolt11);
const p = await getPayment(bolt11 || bolt12);
if (p) return warn("already processed", bolt11 || bolt12);
if (invoice?.memo) {
try {
if (JSON.parse(description).kind === 9734) {
const { pubkey } = await getUser(invoice.uid);
handleZap(inv, pubkey);
}
} catch (e) {
if (!e.message.includes("Unexpected"))
warn("failed to handle zap", e.message);
}
}
await credit({
hash: bolt11 || bolt12,
amount: received,
memo: invoice.memo,
ref: preimage,
type: bolt12 ? types.bolt12 : types.lightning,
});
} catch (e) {
console.log(e);
err("problem receiving lightning payment", e.message);
}
}
export async function replay(index) {
const inv = await ln.waitanyinvoice(index - 1);
const {
local_offer_id,
bolt11,
bolt12,
description,
amount_received_msat,
payment_preimage: preimage,
} = inv;
const received = Math.round(amount_received_msat / 1000);
try {
if (!preimage) return;
const id = bolt11 ? (await g(`invoice:${bolt11}`)) : (await g(`invoice:${local_offer_id}`));
const invoice = await g(`invoice:${id}`);
if (!invoice) return warn("received lightning with no invoice", bolt11);
const p = await getPayment(bolt11 || bolt12);
if (p) return warn("already processed", bolt11 || bolt12);
if (invoice?.memo) {
try {
if (JSON.parse(description).kind === 9734) {
const { pubkey } = await getUser(invoice.uid);
handleZap(inv, pubkey);
}
} catch (e) {
if (!e.message.includes("Unexpected"))
warn("failed to handle zap", e.message);
}
}
await credit({
hash: bolt11 || bolt12,
amount: received,
memo: invoice.memo,
ref: preimage,
type: bolt12 ? types.bolt12 : types.lightning,
});
} catch (e) {
console.log(e);
err("problem receiving lightning payment", e.message);
}
}
export const fixBolt12 = async (_, res) => {
for await (const k of db.scanIterator({ MATCH: "payment:*" })) {
const p = await g(k);
if (p.type === "bolt12") {
console.log(k);
const { invoices } = await ln.listinvoices({ invstring: p.hash });
const { local_offer_id } = invoices[0];
const oid = await g(`payment:${local_offer_id}`);
const op = await g(`payment:${oid}`);
if (op) {
db.del(`payment:${oid}`);
db.del(`payment:${local_offer_id}`);
db.decrBy(`balance:${op.uid}`, op.amount);
}
}
}
res.send({});
};
|