|
| 1 | +import Lnmessage from 'lnmessage'; |
| 2 | +import WebSocket from 'ws'; |
| 3 | +import fs from 'fs'; |
| 4 | +import crypto from 'crypto'; |
| 5 | +import { exit } from 'process'; |
| 6 | + |
| 7 | +const LIGHTNING_VARS_FILE = process.env.LIGHTNING_VARS_FILE || `${process.cwd()}/.commando`; |
| 8 | +const WS_PROTOCOL = process.env.LIGHTNING_WS_PROTOCOL; |
| 9 | +const NODE_IP = process.env.LIGHTNING_WS_HOST; |
| 10 | +const WS_PORT = process.env.LIGHTNING_WS_PORT; |
| 11 | +let NODE_PUBKEY; |
| 12 | +let RUNE; |
| 13 | + |
| 14 | +if (!LIGHTNING_VARS_FILE) { |
| 15 | + console.error(`[ERROR - ${new Date().toISOString()}]: LIGHTNING_VARS_FILE environment variable not set`); |
| 16 | + exit(1); |
| 17 | +} |
| 18 | + |
| 19 | +try { |
| 20 | + const fileContent = fs.readFileSync(LIGHTNING_VARS_FILE, 'utf8'); |
| 21 | + const lines = fileContent.split('\n'); |
| 22 | + |
| 23 | + for (const line of lines) { |
| 24 | + const trimmedLine = line.trim(); |
| 25 | + if (trimmedLine.startsWith('LIGHTNING_PUBKEY=')) { |
| 26 | + NODE_PUBKEY = trimmedLine.split('=')[1].replace(/"/g, ''); |
| 27 | + } else if (trimmedLine.startsWith('LIGHTNING_RUNE=')) { |
| 28 | + RUNE = trimmedLine.split('=')[1].replace(/"/g, ''); |
| 29 | + } |
| 30 | + } |
| 31 | +} catch (err) { |
| 32 | + console.error(`[ERROR - ${new Date().toISOString()}]: Failed to read file ${LIGHTNING_VARS_FILE}:`, err.message); |
| 33 | + exit(1); |
| 34 | +} |
| 35 | + |
| 36 | +if (!NODE_PUBKEY) { |
| 37 | + console.error(`[ERROR - ${new Date().toISOString()}]: LIGHTNING_PUBKEY not found in file`); |
| 38 | + exit(1); |
| 39 | +} |
| 40 | +if (!RUNE) { |
| 41 | + console.error(`[ERROR - ${new Date().toISOString()}]: LIGHTNING_RUNE not found in file`); |
| 42 | + exit(1); |
| 43 | +} |
| 44 | + |
| 45 | +class SecureWebSocket extends WebSocket { |
| 46 | + constructor(url) { |
| 47 | + const options = {}; |
| 48 | + options.rejectUnauthorized = false; |
| 49 | + options.cert = fs.readFileSync(process.env.LIGHTNING_WS_CLIENT_CERT_FILE); |
| 50 | + options.key = fs.readFileSync(process.env.LIGHTNING_WS_CLIENT_KEY_FILE); |
| 51 | + super(url, options); |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +if (WS_PROTOCOL === 'wss') { globalThis.WebSocket = SecureWebSocket; } |
| 56 | + |
| 57 | +let lnmessageOptions = { |
| 58 | + ip: NODE_IP, |
| 59 | + remoteNodePublicKey: NODE_PUBKEY, |
| 60 | + privateKey: crypto.randomBytes(32).toString('hex'), |
| 61 | + wsProxy: `${WS_PROTOCOL}://${NODE_IP}:${WS_PORT}`, |
| 62 | + port: WS_PORT, |
| 63 | + logger: { info: console.log, warn: console.log, error: console.error } |
| 64 | +} |
| 65 | +console.log(`[INFO - ${new Date().toISOString()}]: lnMessage Options `, lnmessageOptions); |
| 66 | + |
| 67 | +const ln = new Lnmessage(lnmessageOptions) |
| 68 | +console.log(`[INFO - ${new Date().toISOString()}]: Initialized lnMessage`); |
| 69 | + |
| 70 | +ln.connectionStatus$.subscribe(status => { |
| 71 | + if (status === 'failed') { |
| 72 | + console.error(`[ERROR - ${new Date().toISOString()}]: Failed to reconnect after maximum attempts`); |
| 73 | + exit(1); |
| 74 | + } |
| 75 | +}); |
| 76 | + |
| 77 | +console.log(`[INFO - ${new Date().toISOString()}]: Connecting...`); |
| 78 | +try { |
| 79 | + await ln.connect(); |
| 80 | + console.log(`[INFO - ${new Date().toISOString()}]: Connected`); |
| 81 | +} catch (err) { |
| 82 | + console.error(`[ERROR - ${new Date().toISOString()}]: Connection failed! Error:\n`, err); |
| 83 | + exit(1); |
| 84 | +} |
| 85 | + |
| 86 | +try { |
| 87 | + const getinfoResponse = await ln.commando({reqId: crypto.randomBytes(8).toString('hex'), method: 'getinfo', params: [], rune: RUNE}); |
| 88 | + console.log(`[INFO - ${new Date().toISOString()}]: Connection successful! Getinfo Response:\n`, getinfoResponse); |
| 89 | + exit(0); |
| 90 | +} catch (err) { |
| 91 | + console.error(`[ERROR - ${new Date().toISOString()}]: Connection failed! Getinfo Error:\n`, err); |
| 92 | + exit(1); |
| 93 | +} |
0 commit comments