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'
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
}
}