mirror of
https://github.com/AustinKelsay/plebdevs.git
synced 2025-04-19 19:01:19 +00:00
lnurl verify, first impl
This commit is contained in:
parent
525416c380
commit
08dc929a5d
@ -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' });
|
||||
|
56
src/pages/api/lightning-address/verify/[name]/[slug].js
Normal file
56
src/pages/api/lightning-address/verify/[name]/[slug].js
Normal 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"
|
||||
});
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user