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
|
// Lightning backend for the swap bridge: pay a bolt11 and report the
// preimage. Deliberately tiny — the bridge only ever needs to *send* over
// Lightning (an ark->LN swap), because an ASP's LN *receive* is already free.
//
// Two adapters, both CLN, both using named parameters:
// 'socket' — lightning-rpc unix socket (production: mount the socket)
// 'docker' — `docker exec <container> lightning-cli` (regtest convenience)
import { execFile } from 'node:child_process';
import { Socket } from 'node:net';
const run = (cmd, args) => new Promise((resolve, reject) => {
execFile(cmd, args, { maxBuffer: 8 << 20 }, (err, stdout, stderr) => {
if (err) return reject(new Error((stderr || err.message).trim().slice(0, 400)));
resolve(stdout);
});
});
// CLN prints progress lines before the JSON body on some commands.
const parseJson = (s) => {
const i = s.indexOf('{');
if (i < 0) throw new Error('no JSON in CLN response: ' + s.slice(0, 200));
return JSON.parse(s.slice(i));
};
function dockerAdapter({ container, network }) {
return {
async call(method, params = {}) {
const args = ['exec', container, 'lightning-cli', `--${network}`, method,
...Object.entries(params).map(([k, v]) => `${k}=${v}`)];
return parseJson(await run('docker', args));
},
};
}
// A socket that refuses every call for a minute is not coming back by itself
// when the socket FILE is bind-mounted into a container: lightningd makes a
// new file when it restarts, and the mount still points at the old one. The
// owner gets told through cfg.onDead (names exits there, and docker's restart
// mounts the new file). `quiet` is how long before it may fire: a minute once
// this process has reached the node, ten if it never has, so a node that is
// simply down doesn't restart its client in a loop.
export function deadSocketWatch(onDead, { streakMs = 60_000, coldMs = 600_000, now = Date.now } = {}) {
const born = now();
let since = 0, fails = 0, everUp = false, fired = false;
return {
ok() { everUp = true; since = 0; fails = 0; },
fail(err) {
if (!['ECONNREFUSED', 'ENOENT', 'ENOTSOCK'].includes(err?.code)) return;
const t = now();
if (!since) since = t;
fails++;
if (fired || fails < 3 || t - since < streakMs) return;
if (!everUp && t - born < coldMs) return;
fired = true;
onDead(err);
},
};
}
function socketAdapter({ path, onDead }) {
let id = 0;
const watch = onDead ? deadSocketWatch(onDead) : null;
return {
call(method, params = {}) {
return new Promise((resolve, reject) => {
// Listener before connect(): inside a Bun.serve handler, createConnection
// on a stale socket emits ENOENT before .on('error') is attached and the
// process dies with an uncaught exception.
const sock = new Socket();
let buf = '';
sock.on('error', (e) => { watch?.fail(e); reject(e); });
sock.connect(path);
sock.on('connect', () => { watch?.ok(); sock.write(JSON.stringify({
jsonrpc: '2.0', id: ++id, method, params,
}) + '\n'); });
sock.on('data', (d) => {
buf += d;
let nl;
while ((nl = buf.indexOf('\n')) >= 0) {
const line = buf.slice(0, nl).trim();
buf = buf.slice(nl + 1);
if (!line) continue;
let msg;
try { msg = JSON.parse(line); } catch { continue; }
sock.end();
if (msg.error) return reject(new Error(JSON.stringify(msg.error).slice(0, 400)));
return resolve(msg.result);
}
});
});
},
};
}
export function lnBackend(cfg) {
const rpc = cfg.kind === 'docker' ? dockerAdapter(cfg) : socketAdapter(cfg);
return {
// Raw RPC passthrough — the names registrar drives offers/waitanyinvoice.
call: (method, params) => rpc.call(method, params),
async info() {
const r = await rpc.call('getinfo');
return { id: r.id, alias: r.alias, blockheight: r.blockheight, network: r.network };
},
// Spendable outbound capacity, so the bridge can refuse a swap it can't
// route rather than taking an HTLC it will only have to refund.
async outboundSat() {
const r = await rpc.call('listpeerchannels');
return (r.channels || [])
.filter((c) => c.state === 'CHANNELD_NORMAL' && c.peer_connected)
.reduce((n, c) => n + Math.floor((c.spendable_msat ?? 0) / 1000), 0);
},
// Pay a bolt11. Resolves { preimage, feeSat }; throws otherwise.
// maxfee caps routing spend so a swap can't cost more than we charged.
async pay(bolt11, { maxfeeSat, retryFor = 60 } = {}) {
const params = { bolt11, retry_for: retryFor };
if (maxfeeSat != null) params.maxfee = `${Math.max(0, Math.floor(maxfeeSat))}sat`;
const r = await rpc.call('pay', params);
if (r.status && r.status !== 'complete') throw new Error(`payment not complete: ${r.status}`);
if (!r.payment_preimage) throw new Error('payment returned no preimage');
return {
preimage: r.payment_preimage,
feeSat: Math.ceil((((r.amount_sent_msat ?? 0) - (r.amount_msat ?? 0)) || 0) / 1000),
};
},
// Has this payment hash already been paid by us? Checked before paying so
// a crashed-and-resumed bridge never double-pays.
async paidPreimage(paymentHash) {
const r = await rpc.call('listpays', { payment_hash: paymentHash }).catch(() => null);
const done = (r?.pays || []).find((p) => p.status === 'complete');
return done?.preimage || null;
},
// Is a payment still in flight? A pending pay must not be retried and
// must not be refunded — its preimage may still arrive.
async isPending(paymentHash) {
const r = await rpc.call('listpays', { payment_hash: paymentHash }).catch(() => null);
return (r?.pays || []).some((p) => p.status === 'pending');
},
};
}
|