From 7b84f250c17abbaa1651194851d59ddaedc35ed9 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Tue, 3 Nov 2020 13:55:43 -0400 Subject: [PATCH] check for invoice amt mismatch --- services/gunDB/contact-api/actions.js | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/services/gunDB/contact-api/actions.js b/services/gunDB/contact-api/actions.js index c067b4d3..77d72556 100644 --- a/services/gunDB/contact-api/actions.js +++ b/services/gunDB/contact-api/actions.js @@ -9,7 +9,10 @@ const Gun = require('gun') const { ErrorCode } = Constants -const { sendPaymentV2Invoice } = require('../../../utils/lightningServices/v2') +const { + sendPaymentV2Invoice, + decodePayReq +} = require('../../../utils/lightningServices/v2') /** * @typedef {import('../../../utils/lightningServices/types').PaymentV2} PaymentV2 @@ -1040,6 +1043,21 @@ const sendSpontaneousPayment = async ( throw new Error(orderResponse.response) } + logger.info('Will now check for invoice amount mismatch') + + const encodedInvoice = orderResponse.response + + const { num_satoshis: decodedAmt } = await decodePayReq(encodedInvoice) + + if (decodedAmt !== amount.toString()) { + throw new Error('Invoice amount mismatch') + } + + // double check + if (Number(decodedAmt) !== amount) { + throw new Error('Invoice amount mismatch') + } + logger.info('Will now send payment through lightning') const payment = await sendPaymentV2Invoice({