diff --git a/apps/machine/electron/fund-atm.ts b/apps/machine/electron/fund-atm.ts index f184a02..ce8b1e0 100644 --- a/apps/machine/electron/fund-atm.ts +++ b/apps/machine/electron/fund-atm.ts @@ -1,20 +1,20 @@ #!/usr/bin/env node /** - * fund-atm — Generate a Lightning invoice to fund the ATM's account. + * fund-atm — Generate a Lightning invoice to fund the ATM's LNbits wallet. * * Usage: * fund-atm * fund-atm 100000 * fund-atm 100000 sats * - * Reads ATM identity and Lightning.Pub config from /var/lib/lamassu-atm/.env - * Connects to Lightning.Pub via Nostr relay, generates an invoice, - * displays a QR code in the terminal and prints the BOLT11. + * Reads ATM identity and LNbits transport config from /var/lib/bitspire/.env. + * Connects to LNbits via the Nostr nostr-native-transport, generates an + * invoice, displays a QR code in the terminal, and prints the BOLT11. */ import { readFileSync } from 'node:fs' import { NostrClient, loadIdentityFromHex } from '@bitSpire/nostr-client' -import { LightningPubClient } from '@bitSpire/lightning' +import { LnbitsClient } from '@bitSpire/lnbits' // @ts-ignore — qrcode is a transitive dep (via qrcode.vue), no types needed import QRCode from 'qrcode' @@ -41,27 +41,24 @@ function loadEnv(path: string): Record { } async function main() { - // Parse amount — accept "fund-atm 1000" or "fund-atm 1000 sats" const arg = process.argv[2] || '' const amountSats = parseInt(arg, 10) if (!amountSats || amountSats <= 0) { console.error('Usage: fund-atm ') console.error(' e.g., fund-atm 100000') - console.error(' fund-atm 100000 sats') process.exit(1) } - const envPath = process.env.ATM_ENV_PATH || '/var/lib/lamassu-atm/.env' + const envPath = process.env['ATM_ENV_PATH'] || '/var/lib/bitspire/.env' const env = loadEnv(envPath) - const relayUrl = env.VITE_RELAY_URL - const lpPubkey = env.VITE_LIGHTNING_PUB_PUBKEY - const atmPrivateKey = env.VITE_ATM_PRIVATE_KEY - const appId = env.VITE_APP_ID + const relayUrl = env['VITE_RELAY_URL'] + const lnbitsServerPubkey = env['VITE_LNBITS_SERVER_PUBKEY'] + const atmPrivateKey = env['VITE_ATM_PRIVATE_KEY'] - if (!relayUrl || !lpPubkey || !atmPrivateKey) { + if (!relayUrl || !lnbitsServerPubkey || !atmPrivateKey) { console.error('Missing required config in', envPath) - console.error('Need: VITE_RELAY_URL, VITE_LIGHTNING_PUB_PUBKEY, VITE_ATM_PRIVATE_KEY') + console.error('Need: VITE_RELAY_URL, VITE_LNBITS_SERVER_PUBKEY, VITE_ATM_PRIVATE_KEY') process.exit(1) } @@ -73,28 +70,35 @@ async function main() { relays: [{ url: relayUrl }], identity, }) - await nostrClient.connect() - const lightningPub = new LightningPubClient({ - accountPubkey: lpPubkey, + const lnbits = new LnbitsClient({ + serverPubkey: lnbitsServerPubkey, relays: [relayUrl], - appId: appId || undefined, }) + lnbits.initialize(nostrClient, identity) - lightningPub.initialize(nostrClient, identity) + const wallets = await lnbits.listWallets() + const wallet = wallets[0] + if (!wallet) { + console.error('No LNbits wallet found for this ATM identity') + process.exit(1) + } - const invoice = await lightningPub.createInvoice({ - amountSats, - description: `Fund ATM (${amountSats} sats)`, + const payment = await lnbits.createInvoice(wallet.id, { + amount: amountSats, + memo: `Fund bitSpire (${amountSats} sats)`, + unit: 'sat', }) + const bolt11 = payment.payment_request + if (!bolt11) { + console.error('LNbits create_invoice returned empty payment_request') + process.exit(1) + } - const bolt11 = invoice.paymentRequest - - // Display QR code in terminal try { const qr = await QRCode.toString(bolt11.toUpperCase(), { type: 'terminal', small: true }) - console.error('') // blank line + console.error('') console.error(qr) console.error(` ${amountSats} sats`) console.error('') @@ -102,10 +106,8 @@ async function main() { // Fall back to just printing the invoice } - // Print BOLT11 to stdout (pipeable) console.log(bolt11) - // Clean up nostrClient.disconnect() process.exit(0) }