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
|
import { handleZap } from "$lib/nostr";
import ln from "$lib/ln";
import { err, warn } from "$lib/logging";
import { g, s } from "$lib/db";
import { credit, types } from "$lib/payments";
import { getPayment } from "$lib/utils";
export async function listenForLightning() {
const inv = await ln.waitanyinvoice((await g("pay_index")) || 0);
const {
bolt11,
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 = await g(`invoice:${bolt11}`);
const invoice = await g(`invoice:${id}`);
if (!invoice) return warn("received lightning with no invoice", bolt11);
if (invoice?.memo) {
try {
if (JSON.parse(description).kind === 9734) await handleZap(inv);
} catch (e) {
if (!e.message.startsWith("Unexpected"))
warn("failed to handle zap", e.message);
}
}
const p = await getPayment(bolt11);
if (p) return warn("already processed", bolt11);
await credit({
hash: bolt11,
amount: received,
memo: invoice.memo,
ref: preimage,
type: types.lightning,
});
} catch (e) {
console.log(e);
err("problem receiving lightning payment", e.message);
}
}
|