fix: handle invalid invoice errors with try-catch (#680)
This commit is contained in:
@@ -3,16 +3,29 @@ 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 {
|
||||||
|
const _invoice = new Invoice({ pr: invoice })
|
||||||
return _invoice.satoshi
|
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 {
|
||||||
|
const _invoice = new Invoice({ pr: invoice })
|
||||||
return {
|
return {
|
||||||
amount: _invoice.satoshi,
|
amount: _invoice.satoshi,
|
||||||
description: _invoice.description
|
description: _invoice.description
|
||||||
}
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Invalid Lightning invoice:', error)
|
||||||
|
return {
|
||||||
|
amount: 0,
|
||||||
|
description: null
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function formatAmount(amount: number) {
|
export function formatAmount(amount: number) {
|
||||||
@@ -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)) {
|
||||||
|
|||||||
Reference in New Issue
Block a user