Repository navigation
Expand file tree
/
Copy pathserver.mjs
More file actions
520 lines (480 loc) · 14.3 KB
/
Copy pathserver.mjs
File metadata and controls
520 lines (480 loc) · 14.3 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
import http from 'node:http'
import {createPaymentHandler} from './payments.mjs'
import {IncomingInvoices, receiveSuccessStatuses} from './incoming.mjs'
import {refreshBalance} from './lightning.mjs'
import {SparkWallet, SparkWalletEvent} from '@buildonspark/spark-sdk'
const PORT = parseInt(process.env.SPARK_SIDECAR_PORT || '8765', 10)
const HOST = process.env.SPARK_SIDECAR_HOST || '127.0.0.1'
const API_KEY = process.env.SPARK_SIDECAR_API_KEY || ''
let mnemonic = process.env.SPARK_MNEMONIC || ''
const NETWORK = process.env.SPARK_NETWORK || 'MAINNET'
const MULTIPLICITY = parseInt(process.env.SPARK_MULTIPLICITY || '3', 10)
const PAY_WAIT_MS = parseInt(process.env.SPARK_PAY_WAIT_MS || '4000', 10)
const PAY_POLL_MS = parseInt(process.env.SPARK_PAY_POLL_MS || '500', 10)
const STREAM_KEEPALIVE_MS = parseInt(
process.env.SPARK_STREAM_KEEPALIVE_MS || '15000',
10
)
const STREAM_HEARTBEAT_MS = parseInt(
process.env.SPARK_STREAM_HEARTBEAT_MS || '30000',
10
)
const INVOICE_POLL_MS = parseInt(
process.env.SPARK_INVOICE_POLL_MS || '2000',
10
)
const INVOICE_POLL_LIMIT = parseInt(
process.env.SPARK_INVOICE_POLL_LIMIT || '100',
10
)
const TRANSFER_LOOKUP_CONCURRENCY = parseInt(
process.env.SPARK_TRANSFER_LOOKUP_CONCURRENCY || '20',
10
)
const TRANSFER_QUEUE_MAX = Math.max(
1,
parseInt(process.env.SPARK_TRANSFER_QUEUE_MAX || '5000', 10)
)
const ACCOUNT_NUMBER = process.env.SPARK_ACCOUNT_NUMBER
? parseInt(process.env.SPARK_ACCOUNT_NUMBER, 10)
: undefined
let mnemonicReadyResolve
const mnemonicReady = new Promise(resolve => {
mnemonicReadyResolve = resolve
})
if (mnemonic) {
mnemonicReadyResolve()
}
let server
let paymentHandler = null
let incomingInvoices
let walletPromise
let privacyPromise
let walletInstance
const sseClients = new Set()
const sseKeepaliveTimers = new Map()
const sseHeartbeatTimers = new Map()
const pendingTransferIds = new Set()
const transferQueue = []
let activeTransferLookups = 0
let walletListenersAttached = false
let droppedTransfers = 0
let lastDropLog = 0
let invoicePollTimer = null
let invoicePollInFlight = false
let lastSeenUpdatedAtMs = Date.now()
let invoiceScan = null
const DROP_LOG_INTERVAL_MS = 10000
function getRequestUpdatedAtMs(request) {
const stamp = request?.updatedAt || request?.createdAt
if (!stamp) {
return 0
}
const parsed = Date.parse(stamp)
return Number.isFinite(parsed) ? parsed : 0
}
function attachWalletListeners(wallet) {
if (walletListenersAttached) {
return
}
walletListenersAttached = true
wallet.on(SparkWalletEvent.TransferClaimed, transferId => {
if (!transferId) {
return
}
enqueueTransferLookup(transferId)
})
}
async function getWallet() {
await mnemonicReady
if (!walletPromise) {
console.log('Initializing Spark wallet...')
walletPromise = SparkWallet.initialize({
mnemonicOrSeed: mnemonic,
accountNumber: ACCOUNT_NUMBER,
options: {
network: NETWORK,
optimizationOptions: {
auto: true,
multiplicity: MULTIPLICITY
}
}
}).then(({wallet}) => {
attachWalletListeners(wallet)
console.log('Spark wallet initialized.')
return wallet
})
}
const wallet = await walletPromise
privacyPromise ||= wallet.setPrivacyEnabled(true).catch(error => {
privacyPromise = null
throw error
})
await privacyPromise
walletInstance = wallet
if (wallet && !walletListenersAttached) {
attachWalletListeners(wallet)
}
return wallet
}
let stopping = false
async function shutdown() {
if (stopping) return
stopping = true
server?.close()
try {
stopInvoicePolling()
await incomingInvoices?.close()
await paymentHandler?.close()
console.log('Shutting down Spark sidecar...')
if (walletPromise) {
const wallet = await walletPromise
if (wallet && typeof wallet.cleanupConnections === 'function') {
await wallet.cleanupConnections()
} else if (wallet && typeof wallet.cleanup === 'function') {
wallet.cleanup()
}
}
} catch {
console.error('Error during Spark sidecar shutdown')
} finally {
process.exit(0)
}
}
process.on('SIGINT', shutdown)
process.on('SIGTERM', shutdown)
function sendJson(res, statusCode, payload) {
res.writeHead(statusCode, {'content-type': 'application/json'})
res.end(JSON.stringify(payload))
}
async function readJson(req) {
const chunks = []
for await (const chunk of req) {
chunks.push(chunk)
}
if (chunks.length === 0) {
return {}
}
return JSON.parse(Buffer.concat(chunks).toString('utf8'))
}
function setMnemonic(nextMnemonic) {
if (!nextMnemonic) {
return {status: 'missing'}
}
if (mnemonic) {
if (mnemonic === nextMnemonic) {
return {status: 'already_set'}
}
return {status: 'conflict'}
}
mnemonic = nextMnemonic
mnemonicReadyResolve()
return {status: 'set'}
}
function enqueueTransferLookup(transferId) {
if (pendingTransferIds.has(transferId)) {
return
}
pendingTransferIds.add(transferId)
if (transferQueue.length >= TRANSFER_QUEUE_MAX) {
const dropped = transferQueue.shift()
if (dropped) {
pendingTransferIds.delete(dropped)
droppedTransfers += 1
const now = Date.now()
if (now - lastDropLog > DROP_LOG_INTERVAL_MS) {
console.warn(
`Dropping transfer events due to queue pressure: ${droppedTransfers}`
)
lastDropLog = now
}
}
}
transferQueue.push(transferId)
processTransferQueue()
}
function processTransferQueue() {
while (
activeTransferLookups < TRANSFER_LOOKUP_CONCURRENCY &&
transferQueue.length > 0
) {
const transferId = transferQueue.shift()
activeTransferLookups += 1
void handleTransferLookup(transferId).finally(() => {
activeTransferLookups -= 1
pendingTransferIds.delete(transferId)
processTransferQueue()
})
}
}
async function pollInvoiceUpdates() {
if (invoicePollInFlight || sseClients.size === 0 || stopping) return
invoicePollInFlight = true
try {
await incomingInvoices.retryPending(INVOICE_POLL_LIMIT)
const wallet = walletInstance || (await getWallet())
invoiceScan ||= {
cursor: undefined,
maxSeen: lastSeenUpdatedAtMs,
threshold: lastSeenUpdatedAtMs
}
// Bound each pass; retain the cursor to catch up across ticks under load.
for (let page = 0; page < 4; page++) {
const response = await wallet.getUserRequests({
first: INVOICE_POLL_LIMIT,
after: invoiceScan.cursor,
types: ['LIGHTNING_RECEIVE'],
statuses: ['SUCCEEDED']
})
const entities = response?.entities || []
let reachedKnown = false
const observations = []
for (const request of entities) {
if (
request?.typename !== 'LightningReceiveRequest' ||
!receiveSuccessStatuses.has(request.status)
)
continue
const stamp = getRequestUpdatedAtMs(request)
if (stamp && stamp < invoiceScan.threshold) {
reachedKnown = true
continue
}
invoiceScan.maxSeen = Math.max(invoiceScan.maxSeen, stamp)
observations.push(incomingInvoices.observe(request))
}
// Remember uncleared candidates for this stream session before advancing.
const results = await Promise.allSettled(observations)
const failed = results.find(result => result.status === 'rejected')
if (failed) throw failed.reason
const info = response?.pageInfo || {}
if (!info.hasNextPage || reachedKnown) {
lastSeenUpdatedAtMs = invoiceScan.maxSeen
invoiceScan = null
break
}
if (!info.endCursor || info.endCursor === invoiceScan.cursor)
throw new Error('Invalid invoice pagination')
invoiceScan.cursor = info.endCursor
}
} catch {
console.error('Error polling lightning invoices')
} finally {
invoicePollInFlight = false
}
}
function stopInvoicePolling() {
if (!invoicePollTimer) {
return
}
clearInterval(invoicePollTimer)
invoicePollTimer = null
}
async function handleTransferLookup(transferId) {
try {
const wallet = walletInstance || (await getWallet())
const transfer = await wallet.getTransferFromSsp(transferId)
const userRequest = transfer?.userRequest
if (!userRequest || userRequest.typename !== 'LightningReceiveRequest') {
return
}
if (!receiveSuccessStatuses.has(userRequest.status)) return
await incomingInvoices.observe(userRequest)
} catch {
console.error('Error handling transfer event')
}
}
function sendSseEvent(payload) {
if (sseClients.size === 0) return false
let sent = false
const data = `data: ${JSON.stringify(payload)}\n\n`
for (const res of sseClients) {
try {
if (!res.write(data)) {
res.destroy()
removeSseClient(res)
} else sent = true
} catch (error) {
removeSseClient(res)
}
}
return sent
}
function addSseClient(res) {
res.writeHead(200, {
'content-type': 'text/event-stream',
'cache-control': 'no-cache',
connection: 'keep-alive',
'x-accel-buffering': 'no'
})
res.write(':\n\n')
sseClients.add(res)
if (!invoicePollTimer) {
invoicePollTimer = setInterval(
() => void pollInvoiceUpdates(),
Math.max(1, INVOICE_POLL_MS)
)
void pollInvoiceUpdates()
}
if (STREAM_KEEPALIVE_MS > 0) {
const timer = setInterval(() => {
try {
res.write(':\n\n')
} catch (error) {
removeSseClient(res)
}
}, STREAM_KEEPALIVE_MS)
sseKeepaliveTimers.set(res, timer)
}
if (STREAM_HEARTBEAT_MS > 0) {
const timer = setInterval(() => {
try {
res.write(
`data: ${JSON.stringify({type: 'heartbeat', ts: Date.now()})}\n\n`
)
} catch (error) {
removeSseClient(res)
}
}, STREAM_HEARTBEAT_MS)
sseHeartbeatTimers.set(res, timer)
}
res.on('close', () => {
removeSseClient(res)
})
}
function removeSseClient(res) {
if (!sseClients.has(res)) {
return
}
sseClients.delete(res)
const timer = sseKeepaliveTimers.get(res)
if (timer) {
clearInterval(timer)
}
sseKeepaliveTimers.delete(res)
const heartbeatTimer = sseHeartbeatTimers.get(res)
if (heartbeatTimer) {
clearInterval(heartbeatTimer)
}
sseHeartbeatTimers.delete(res)
if (sseClients.size === 0) {
stopInvoicePolling()
}
}
paymentHandler = await createPaymentHandler({
getWallet,
network: NETWORK,
apiKey: API_KEY,
waitMs: PAY_WAIT_MS,
pollMs: PAY_POLL_MS,
concurrency: Number(process.env.SPARK_OPERATION_CONCURRENCY || 8),
fundsWaitMs: Math.max(
0,
parseInt(process.env.SPARK_FUNDS_WAIT_MS || '20000', 10)
)
})
incomingInvoices = new IncomingInvoices({
getWallet,
emit: sendSseEvent,
concurrency: Number(process.env.SPARK_OPERATION_CONCURRENCY || 8)
})
server = http.createServer(async (req, res) => {
const url = new URL(
req.url || '/',
`http://${req.headers.host || 'localhost'}`
)
if (API_KEY && req.headers['x-api-key'] !== API_KEY) {
console.log('Unauthorized request with invalid API key')
return sendJson(res, 401, {error: 'Unauthorized'})
}
console.log(`${req.method} ${url.pathname}`)
try {
if (paymentHandler && (await paymentHandler(req, res, url))) return
if (req.method === 'GET' && url.pathname === '/health') {
return sendJson(res, 200, {status: 'ok'})
}
if (req.method === 'POST' && url.pathname === '/v1/mnemonic') {
const body = await readJson(req)
const provided = body.mnemonic || body.mnemonic_or_seed || ''
const result = setMnemonic(provided)
if (result.status === 'missing') {
return sendJson(res, 400, {error: 'Missing mnemonic'})
}
if (result.status === 'conflict') {
return sendJson(res, 409, {error: 'Mnemonic already set'})
}
return sendJson(res, 200, {status: result.status})
}
if (req.method === 'GET' && url.pathname === '/v1/invoices/stream') {
await getWallet()
addSseClient(res)
return
}
if (req.method === 'POST' && url.pathname === '/v1/balance') {
if (!mnemonic) {
return sendJson(res, 200, {status: 'missing_mnemonic'})
}
const wallet = await getWallet()
const balance = await refreshBalance(wallet)
const sats = BigInt(balance.balance)
return sendJson(res, 200, {
balance_sats: sats.toString(),
balance_msat: (sats * 1000n).toString(),
status: 'ok'
})
}
if (req.method === 'POST' && url.pathname === '/v1/invoices') {
const wallet = await getWallet()
const body = await readJson(req)
const amountSats = Number(body.amount_sats)
if (!Number.isFinite(amountSats) || amountSats < 0) {
return sendJson(res, 400, {error: 'Invalid amount_sats'})
}
const invoice = await wallet.createLightningInvoice({
amountSats,
memo: body.memo || undefined,
descriptionHash: body.description_hash || undefined,
expirySeconds: body.expiry_seconds || undefined
})
return sendJson(res, 200, {
checking_id: invoice.id,
payment_request: invoice.invoice.encodedInvoice,
payment_hash: invoice.invoice.paymentHash,
status: invoice.status,
preimage: invoice.paymentPreimage || null
})
}
const parts = url.pathname.split('/').filter(Boolean)
if (parts.length === 3 && parts[0] === 'v1' && parts[1] === 'invoices') {
const invoice = await incomingInvoices.observe(
{id: decodeURIComponent(parts[2])},
{notify: false}
)
if (!invoice) {
return sendJson(res, 404, {error: 'Not found'})
}
return sendJson(res, 200, {
checking_id: invoice.id,
status: invoice.status,
payment_hash: invoice.invoice.paymentHash,
preimage: invoice.paymentPreimage || null
})
}
return sendJson(res, 404, {error: 'Not found'})
} catch {
// Parser and SDK errors may contain request bodies or wallet secrets.
// Never expose their messages, nested context, or stacks to logs/clients.
console.error('Error handling sidecar request')
return sendJson(res, 500, {error: 'Spark sidecar request failed'})
}
})
server.listen(PORT, HOST, () => {
console.log(`Spark sidecar listening on ${HOST}:${PORT}`)
})
server.on('error', err => {
if (err && err.code === 'EADDRINUSE') {
console.error(`Spark sidecar port ${HOST}:${PORT} already in use.`)
process.exit(1)
}
throw err
})