From 9d4df54b3e1fca7029f416d8e24cfe2dcf638a7d Mon Sep 17 00:00:00 2001 From: Sean <122105188+Thegreenpiranha@users.noreply.github.com> Date: Tue, 25 Nov 2025 01:47:01 +0000 Subject: [PATCH] fix: handle invalid invoice errors with try-catch (#680) --- src/lib/lightning.ts | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/src/lib/lightning.ts b/src/lib/lightning.ts index 55823de..76f967d 100644 --- a/src/lib/lightning.ts +++ b/src/lib/lightning.ts @@ -3,15 +3,28 @@ import { Invoice } from '@getalby/lightning-tools' import { isEmail } from './utils' export function getAmountFromInvoice(invoice: string): number { - const _invoice = new Invoice({ pr: invoice }) // TODO: need to validate - return _invoice.satoshi + try { + const _invoice = new Invoice({ pr: invoice }) + return _invoice.satoshi + } catch (error) { + console.error('Invalid Lightning invoice:', error) + return 0 + } } export function getInvoiceDetails(invoice: string): { amount: number; description: string | null } { - const _invoice = new Invoice({ pr: invoice }) // TODO: need to validate - return { - amount: _invoice.satoshi, - description: _invoice.description + try { + const _invoice = new Invoice({ pr: invoice }) + return { + amount: _invoice.satoshi, + description: _invoice.description + } + } catch (error) { + console.error('Invalid Lightning invoice:', error) + return { + amount: 0, + description: null + } } } @@ -26,6 +39,7 @@ export function getLightningAddressFromProfile(profile: TProfile) { const { lud16: a, lud06: b } = profile let lud16: string | undefined let lud06: string | undefined + if (a && isEmail(a)) { lud16 = a } else if (b && isEmail(b)) { @@ -37,4 +51,4 @@ export function getLightningAddressFromProfile(profile: TProfile) { } return lud16 || lud06 || undefined -} +} \ No newline at end of file