lnurl verify, first impl

This commit is contained in:
austinkelsay 2024-11-06 16:53:21 -06:00
parent 525416c380
commit 08dc929a5d
No known key found for this signature in database
GPG Key ID: 44CB4EC6D9F2FA02
2 changed files with 57 additions and 1 deletions

View File

@ -76,7 +76,7 @@ export default async function handler(req, res) {
'Authorization': PLEBDEVS_API_KEY
}
});
res.status(200).json({ pr: response.data });
res.status(200).json({ pr: response.data, verify: `${BACKEND_URL}/api/lightning-address/verify/${slug}/${response.data.payment_hash}` });
} catch (error) {
console.error(error);
res.status(500).json({ error: 'Failed to generate invoice' });

View File

@ -0,0 +1,56 @@
import axios from "axios";
import { getLightningAddressByName } from "@/db/models/lightningAddressModels";
import appConfig from "@/config/appConfig";
export default async function handler(req, res) {
try {
const { name, slug } = req.query;
// Find the lightning address
let foundAddress = null;
const customAddress = appConfig.customLightningAddresses.find(addr => addr.name === name);
if (customAddress) {
foundAddress = customAddress;
} else {
foundAddress = await getLightningAddressByName(name);
}
if (!foundAddress) {
res.status(404).json({ error: 'Lightning address not found' });
return;
}
// Convert hex payment hash to base64
const paymentHashBuffer = Buffer.from(slug, 'hex');
const paymentHashBase64 = paymentHashBuffer.toString('base64');
// Call LND to check payment status
const response = await axios.get(
`https://${foundAddress.lndHost}/v1/invoice/${paymentHashBase64}`,
{
headers: {
'Grpc-Metadata-macaroon': foundAddress.invoiceMacaroon,
}
}
);
// According to LNURL-pay spec, we should return { status: "OK" } if paid
// or { status: "ERROR", reason: "error message" } if not paid or error
if (response.data.state === 'SETTLED') {
res.status(200).json({ status: "OK" });
} else {
res.status(200).json({
status: "ERROR",
reason: "Invoice not paid"
});
}
} catch (error) {
console.error('Error verifying payment:', error.message);
res.status(200).json({
status: "ERROR",
reason: error.message || "Error verifying payment"
});
}
}