86 lines
3.4 KiB
JavaScript
Raw Normal View History

2024-09-16 17:13:23 -05:00
import axios from "axios";
import crypto from "crypto";
import { verifyEvent } from 'nostr-tools/pure';
2024-09-18 14:59:04 -05:00
import appConfig from "@/config/appConfig";
import { runMiddleware, corsMiddleware } from "@/utils/corsMiddleware";
import { getLightningAddressByName } from "@/db/models/lightningAddressModels";
2024-09-16 17:13:23 -05:00
const BACKEND_URL = process.env.BACKEND_URL;
export default async function handler(req, res) {
await runMiddleware(req, res, corsMiddleware);
const { slug, ...queryParams } = req.query;
let foundAddress = null;
2024-09-18 14:59:04 -05:00
const customAddress = appConfig.customLightningAddresses.find(addr => addr.name === slug);
if (customAddress) {
foundAddress = customAddress;
} else {
foundAddress = await getLightningAddressByName(slug);
}
if (!foundAddress) {
res.status(404).json({ error: 'Lightning address not found' });
return;
}
if (foundAddress) {
2024-09-16 17:13:23 -05:00
if (queryParams.amount) {
const amount = parseInt(queryParams.amount);
let metadata, metadataString, hash, descriptionHash;
2024-09-18 14:59:04 -05:00
if (queryParams?.nostr) {
2024-09-16 17:13:23 -05:00
// This is a zap request
const zapRequest = JSON.parse(decodeURIComponent(queryParams.nostr));
2024-09-18 14:59:04 -05:00
console.log("ZAP REQUEST", zapRequest)
2024-09-16 17:13:23 -05:00
// Verify the zap request
if (!verifyEvent(zapRequest)) {
res.status(400).json({ error: 'Invalid zap request' });
return;
}
// Validate zap request
if (zapRequest.kind !== 9734) {
res.status(400).json({ error: 'Invalid zap request' });
return;
}
metadataString = JSON.stringify(zapRequest);
hash = crypto.createHash('sha256').update(metadataString).digest('hex');
descriptionHash = Buffer.from(hash, 'hex').toString('base64');
} else {
// This is a regular lnurl-pay request
metadata = [
["text/plain", `${foundAddress.name}'s LNURL endpoint, CHEERS!`]
2024-09-16 17:13:23 -05:00
];
metadataString = JSON.stringify(metadata);
hash = crypto.createHash('sha256').update(metadataString).digest('hex');
descriptionHash = Buffer.from(hash, 'hex').toString('base64');
}
// Convert amount from millisatoshis to satoshis
if (amount < (foundAddress.minSendable)) {
2024-09-16 17:13:23 -05:00
res.status(400).json({ error: 'Amount too low' });
return;
} else if (amount > (foundAddress.maxSendable || Number.MAX_SAFE_INTEGER)) {
2024-09-18 14:59:04 -05:00
res.status(400).json({ error: 'Amount too high' });
return;
2024-09-16 17:13:23 -05:00
} else {
try {
2024-09-18 14:59:04 -05:00
const response = await axios.post(`${BACKEND_URL}/api/lightning-address/lnd`, { amount: amount, description_hash: descriptionHash, name: slug, zap_request: queryParams?.nostr ? queryParams.nostr : null });
2024-09-16 17:13:23 -05:00
res.status(200).json({ pr: response.data });
} catch (error) {
console.error(error);
res.status(500).json({ error: 'Failed to generate invoice' });
}
}
} else {
res.status(400).json({ error: 'Amount not specified' });
}
2024-09-18 14:59:04 -05:00
} else {
res.status(404).json({ error: 'Lightning address not found' });
2024-09-16 17:13:23 -05:00
}
}