fix: handle invalid invoice errors with try-catch (#680)

This commit is contained in:
Sean
2025-11-25 01:47:01 +00:00
committed by GitHub
parent a8fa3e1ecd
commit 9d4df54b3e

View File

@@ -3,15 +3,28 @@ import { Invoice } from '@getalby/lightning-tools'
import { isEmail } from './utils' import { isEmail } from './utils'
export function getAmountFromInvoice(invoice: string): number { export function getAmountFromInvoice(invoice: string): number {
const _invoice = new Invoice({ pr: invoice }) // TODO: need to validate try {
return _invoice.satoshi 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 } { export function getInvoiceDetails(invoice: string): { amount: number; description: string | null } {
const _invoice = new Invoice({ pr: invoice }) // TODO: need to validate try {
return { const _invoice = new Invoice({ pr: invoice })
amount: _invoice.satoshi, return {
description: _invoice.description 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 const { lud16: a, lud06: b } = profile
let lud16: string | undefined let lud16: string | undefined
let lud06: string | undefined let lud06: string | undefined
if (a && isEmail(a)) { if (a && isEmail(a)) {
lud16 = a lud16 = a
} else if (b && isEmail(b)) { } else if (b && isEmail(b)) {