From 5a8c49ad031dcb175ff4a0dec71c818eeab0b491 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Mon, 2 Dec 2024 22:50:11 -0800 Subject: detect duplicate keysend --- lib/nwc.ts | 5 +++-- lib/payments.ts | 8 ++++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/nwc.ts b/lib/nwc.ts index ef5e156e..6313e05d 100644 --- a/lib/nwc.ts +++ b/lib/nwc.ts @@ -56,7 +56,7 @@ export default () => { const user = await g(`user:${uid}`); try { - const result = await handle(method, params, user); + const result = await handle(method, params, user, ev); const payload = JSON.stringify({ result_type: method, ...result }); content = await nip04.encrypt(sk, pubkey, payload); @@ -88,7 +88,7 @@ export default () => { }); }; -const handle = (method, params, user) => +const handle = (method, params, user, ev) => ({ async pay_invoice() { const { invoice: pr } = params; @@ -145,6 +145,7 @@ const handle = (method, params, user) => const { amount, pubkey } = params; const { payment_hash } = await sendKeysend({ + hash: ev.id, amount, pubkey, user, diff --git a/lib/payments.ts b/lib/payments.ts index 7f0ab968..76a7d1fa 100644 --- a/lib/payments.ts +++ b/lib/payments.ts @@ -416,6 +416,7 @@ export const sendOnchain = async (params) => { }; export const sendKeysend = async ({ + hash, amount, pubkey, fee = undefined, @@ -424,8 +425,11 @@ export const sendKeysend = async ({ }) => { fee = Math.max(parseInt(fee || amount * 0.005), 5); - const p = await debit({ - hash: v4(), + let p = await g(`payment:${hash}`); + if (p) fail("duplicate keysend"); + + p = await debit({ + hash, amount, fee, memo, -- cgit v1.2.3