2024-09-16 17:13:23 -05:00
|
|
|
import axios from "axios";
|
|
|
|
import { finalizeEvent } from 'nostr-tools/pure';
|
|
|
|
import { SimplePool } from 'nostr-tools/pool';
|
2024-09-18 14:59:04 -05:00
|
|
|
import appConfig from "@/config/appConfig";
|
2024-09-16 17:13:23 -05:00
|
|
|
|
|
|
|
const LND_HOST = process.env.LND_HOST;
|
|
|
|
const LND_MACAROON = process.env.LND_MACAROON;
|
2024-09-18 14:59:04 -05:00
|
|
|
const ZAP_PRIVKEY = process.env.ZAP_PRIVKEY;
|
2024-09-16 17:13:23 -05:00
|
|
|
|
|
|
|
export default async function handler(req, res) {
|
|
|
|
try {
|
2024-09-18 14:59:04 -05:00
|
|
|
const { amount, description_hash, zap_request=null, name } = req.body;
|
|
|
|
|
|
|
|
// Find the custom lightning address
|
|
|
|
const customAddress = appConfig.customLightningAddresses.find(addr => addr.name === name);
|
|
|
|
|
|
|
|
if (!customAddress) {
|
|
|
|
res.status(404).json({ error: 'Lightning address not found' });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if amount is within allowed range
|
|
|
|
const minSendable = customAddress.minSendable || appConfig.defaultMinSendable || 1;
|
|
|
|
const maxSendable = customAddress.maxSendable || appConfig.defaultMaxSendable || Number.MAX_SAFE_INTEGER;
|
|
|
|
|
|
|
|
if (amount < minSendable || amount > maxSendable) {
|
|
|
|
res.status(400).json({ error: 'Amount out of allowed range' });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the custom address allows zaps
|
|
|
|
if (zap_request && !customAddress.allowsNostr) {
|
|
|
|
res.status(400).json({ error: 'Nostr zaps not allowed for this address' });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-09-16 17:13:23 -05:00
|
|
|
const response = await axios.post(`https://${LND_HOST}/v1/invoices`, {
|
2024-09-18 14:59:04 -05:00
|
|
|
value_msat: amount,
|
|
|
|
description_hash: description_hash
|
2024-09-16 17:13:23 -05:00
|
|
|
}, {
|
|
|
|
headers: {
|
|
|
|
'Grpc-Metadata-macaroon': LND_MACAROON,
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const invoice = response.data.payment_request;
|
|
|
|
|
|
|
|
// If this is a zap, publish a zap receipt
|
2024-09-18 14:59:04 -05:00
|
|
|
if (zap_request && customAddress.allowsNostr) {
|
|
|
|
console.log("ZAP REQUEST", zap_request)
|
|
|
|
const zapRequest = JSON.parse(zap_request);
|
2024-09-16 17:13:23 -05:00
|
|
|
const zapReceipt = {
|
|
|
|
kind: 9735,
|
|
|
|
created_at: Math.floor(Date.now() / 1000),
|
2024-09-18 14:59:04 -05:00
|
|
|
content: customAddress.zapMessage || appConfig.defaultZapMessage || '',
|
2024-09-16 17:13:23 -05:00
|
|
|
tags: [
|
|
|
|
['p', zapRequest.pubkey],
|
|
|
|
['e', zapRequest.id],
|
|
|
|
['bolt11', invoice],
|
|
|
|
['description', JSON.stringify(zapRequest)]
|
|
|
|
]
|
|
|
|
};
|
|
|
|
|
2024-09-18 14:59:04 -05:00
|
|
|
const signedZapReceipt = finalizeEvent(zapReceipt, customAddress.relayPrivkey || ZAP_PRIVKEY);
|
2024-09-16 17:13:23 -05:00
|
|
|
|
|
|
|
// Publish zap receipt to relays
|
|
|
|
const pool = new SimplePool();
|
2024-09-18 14:59:04 -05:00
|
|
|
const relays = customAddress.defaultRelays || appConfig.defaultRelayUrls || [];
|
|
|
|
await Promise.any(pool.publish(relays, signedZapReceipt));
|
2024-09-16 17:13:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
res.status(200).json(invoice);
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Error (server) fetching data from LND:', error.message);
|
|
|
|
res.status(500).json({ message: 'Error fetching data' });
|
|
|
|
}
|
|
|
|
}
|