const express = require('express'); const router = express.Router(); const lightning = require('../components/lightning'); /** * @swagger * /api/validate-address: * post: * summary: Validate a Lightning address * description: | * Validate a Lightning address without performing a redemption. * Checks format validity and tests LNURLp resolution. * * Returns information about the Lightning address capabilities * including min/max sendable amounts and comment allowance. * tags: [Validation] * requestBody: * required: true * content: * application/json: * schema: * $ref: '#/components/schemas/ValidateAddressRequest' * responses: * 200: * description: Validation completed (check 'valid' field for result) * content: * application/json: * schema: * $ref: '#/components/schemas/ValidateAddressResponse' * 400: * $ref: '#/components/responses/BadRequest' * 429: * $ref: '#/components/responses/TooManyRequests' */ router.post('/validate-address', async (req, res) => { const { lightningAddress } = req.body; if (!lightningAddress) { return res.status(400).json({ success: false, error: 'Lightning address is required' }); } try { const isValid = lightning.validateLightningAddress(lightningAddress); if (!isValid) { return res.json({ success: false, valid: false, error: 'Invalid Lightning address format' }); } const { domain } = lightning.parseLightningAddress(lightningAddress); const lnurlpUrl = lightning.getLNURLpEndpoint(lightningAddress); try { const lnurlpResponse = await lightning.fetchLNURLpResponse(lnurlpUrl); res.json({ success: true, valid: true, domain, minSendable: lightning.millisatsToSats(lnurlpResponse.minSendable), maxSendable: lightning.millisatsToSats(lnurlpResponse.maxSendable), commentAllowed: lnurlpResponse.commentAllowed || 0 }); } catch (error) { res.json({ success: false, valid: false, error: `Lightning address resolution failed: ${error.message}` }); } } catch (error) { res.status(400).json({ success: false, valid: false, error: error.message }); } }); module.exports = router;