mirror of
https://github.com/aljazceru/rabbit.git
synced 2025-12-17 05:54:19 +01:00
feat: detailed error in verifyInvoice
This commit is contained in:
@@ -247,16 +247,11 @@ const ZapDialog: Component<ZapDialogProps> = (props) => {
|
||||
}
|
||||
|
||||
const invoice = callbackResponse.pr;
|
||||
console.log(callbackResponse, invoice);
|
||||
if (
|
||||
!(await verifyInvoice(invoice, {
|
||||
amountMilliSats,
|
||||
metadata: endpointData.metadata,
|
||||
zapRequest: callbackParams.zapRequest,
|
||||
}))
|
||||
) {
|
||||
throw new Error('Invalid invoice');
|
||||
}
|
||||
await verifyInvoice(invoice, {
|
||||
amountMilliSats,
|
||||
metadata: endpointData.metadata,
|
||||
zapRequest: callbackParams.zapRequest,
|
||||
});
|
||||
|
||||
return invoice;
|
||||
};
|
||||
|
||||
@@ -6,17 +6,26 @@ import sha256Hex from '@/utils/sha256Hex';
|
||||
const verifyInvoice = async (
|
||||
bolt11: string,
|
||||
requirements: { amountMilliSats: string; metadata: string; zapRequest?: NostrEvent },
|
||||
): Promise<boolean> => {
|
||||
): Promise<void> => {
|
||||
const payReq = parseBolt11(bolt11);
|
||||
|
||||
return (
|
||||
(requirements.zapRequest != null
|
||||
? payReq.tagsObject.purpose_commit_hash ===
|
||||
(await sha256Hex(JSON.stringify(requirements.zapRequest)))
|
||||
: payReq.tagsObject.purpose_commit_hash === (await sha256Hex(requirements.metadata))) &&
|
||||
payReq.millisatoshis != null &&
|
||||
payReq.millisatoshis === requirements.amountMilliSats
|
||||
);
|
||||
const description =
|
||||
requirements.zapRequest != null
|
||||
? JSON.stringify(requirements.zapRequest)
|
||||
: requirements.metadata;
|
||||
|
||||
if (payReq.tagsObject.description !== null && description === payReq.tagsObject.description) {
|
||||
throw new Error("invalid invoice: description and didn't match");
|
||||
}
|
||||
|
||||
const purposeCommitHash = await sha256Hex(description);
|
||||
if (purposeCommitHash !== payReq.tagsObject.purpose_commit_hash) {
|
||||
throw new Error("invalid invoice: hash value of purpose_commit_hash and didn't match");
|
||||
}
|
||||
|
||||
if (payReq.millisatoshis != null && payReq.millisatoshis !== requirements.amountMilliSats) {
|
||||
throw new Error("invalid invoice: amount didn't match");
|
||||
}
|
||||
};
|
||||
|
||||
export default verifyInvoice;
|
||||
|
||||
Reference in New Issue
Block a user