From 8bf1b24aad05e1a33f06f0f83640f54534d83964 Mon Sep 17 00:00:00 2001 From: Shusui MOYATANI Date: Sun, 11 Feb 2024 15:26:34 +0900 Subject: [PATCH] feat: detailed error in verifyInvoice --- src/components/modal/ZapRequestModal.tsx | 15 +++++-------- src/nostr/zap/verifyInvoice.ts | 27 ++++++++++++++++-------- 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/src/components/modal/ZapRequestModal.tsx b/src/components/modal/ZapRequestModal.tsx index 35c8b42..af2de5d 100644 --- a/src/components/modal/ZapRequestModal.tsx +++ b/src/components/modal/ZapRequestModal.tsx @@ -247,16 +247,11 @@ const ZapDialog: Component = (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; }; diff --git a/src/nostr/zap/verifyInvoice.ts b/src/nostr/zap/verifyInvoice.ts index f7ec270..60bbe2f 100644 --- a/src/nostr/zap/verifyInvoice.ts +++ b/src/nostr/zap/verifyInvoice.ts @@ -6,17 +6,26 @@ import sha256Hex from '@/utils/sha256Hex'; const verifyInvoice = async ( bolt11: string, requirements: { amountMilliSats: string; metadata: string; zapRequest?: NostrEvent }, -): Promise => { +): Promise => { 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;