fix invoice check

This commit is contained in:
hatim boufnichel 2024-03-14 21:55:28 +01:00
parent 8f66a263c4
commit 3a940df6a7
2 changed files with 32 additions and 6 deletions

View file

@ -261,7 +261,25 @@ export default class {
const users: Record<string, { ts: number, updatedBalance: number }> = {} const users: Record<string, { ts: number, updatedBalance: number }> = {}
for (let i = 0; i < events.length; i++) { for (let i = 0; i < events.length; i++) {
const e = events[i] const e = events[i]
if (e.type === 'balance_decrement' || e.type === 'balance_increment') { if (e.type === 'balance_decrement') {
users[e.userId] = this.checkUserEntry(e, users[e.userId])
if (LN_INVOICE_REGEX.test(e.data)) {
const invoiceEntry = await this.storage.paymentStorage.GetPaymentOwner(e.data)
if (!invoiceEntry) {
throw new Error("invoice entry not found for " + e.data)
}
if (invoiceEntry.paid_at_unix === 0) {
throw new Error("invoice was never paid " + e.data)
}
const entry = payments.payments.find(i => i.paymentRequest === e.data)
if (!entry) {
throw new Error("invoice not found in lnd " + e.data)
}
if (Number(entry.valueSat) !== e.amount) {
throw new Error(`invalid amounts got: ${Number(entry.valueSat)} expected: ${e.amount}`)
}
}
} else if (e.type === 'balance_increment') {
users[e.userId] = this.checkUserEntry(e, users[e.userId]) users[e.userId] = this.checkUserEntry(e, users[e.userId])
if (LN_INVOICE_REGEX.test(e.data)) { if (LN_INVOICE_REGEX.test(e.data)) {
const invoiceEntry = await this.storage.paymentStorage.GetInvoiceOwner(e.data) const invoiceEntry = await this.storage.paymentStorage.GetInvoiceOwner(e.data)
@ -271,11 +289,12 @@ export default class {
if (invoiceEntry.paid_at_unix === 0) { if (invoiceEntry.paid_at_unix === 0) {
throw new Error("invoice was never paid " + e.data) throw new Error("invoice was never paid " + e.data)
} }
if (!invoiceEntry.internal) { const entry = invoices.invoices.find(i => i.paymentRequest === e.data)
const amt = verifyWithLnd(e.type, e.data) if (!entry) {
if (amt !== e.amount) { throw new Error("invoice not found in lnd " + e.data)
throw new Error(`invalid amounts got: ${amt} expected: ${e.amount}`) }
} if (Number(entry.amtPaidSat) !== e.amount) {
throw new Error(`invalid amounts got: ${Number(entry.amtPaidSat)} expected: ${e.amount}`)
} }
} }
} else { } else {

View file

@ -121,6 +121,13 @@ export default class {
} }
}) })
} }
async GetPaymentOwner(paymentRequest: string, entityManager = this.DB): Promise<UserInvoicePayment | null> {
return entityManager.getRepository(UserInvoicePayment).findOne({
where: {
invoice: paymentRequest
}
})
}
async AddUserInvoicePayment(userId: string, invoice: string, amount: number, routingFees: number, serviceFees: number, internal: boolean, linkedApplication: Application): Promise<UserInvoicePayment> { async AddUserInvoicePayment(userId: string, invoice: string, amount: number, routingFees: number, serviceFees: number, internal: boolean, linkedApplication: Application): Promise<UserInvoicePayment> {
const newPayment = this.DB.getRepository(UserInvoicePayment).create({ const newPayment = this.DB.getRepository(UserInvoicePayment).create({