From 102b2f70b875dc6221caac5698a29689ab76d769 Mon Sep 17 00:00:00 2001 From: hatim Date: Fri, 12 May 2023 22:12:20 +0200 Subject: [PATCH] locking bugs --- src/index.ts | 9 +++++++++ src/services/main/paymentManager.ts | 20 ++++++++++++++++---- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/index.ts b/src/index.ts index 36ce2853..d9f21f9a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -15,6 +15,15 @@ const start = async () => { const nostrSettings = LoadNosrtSettingsFromEnv() nostrMiddleware(serverMethods, mainHandler, nostrSettings) const Server = NewServer(serverMethods, serverOptions(mainHandler)) + if (process.argv[2] === 'unlock') { + const u = process.argv[3] + if (u) { + console.log("unlocking user", u) + await mainHandler.storage.userStorage.UnlockUser(u) + } else { + console.log("no user id found to unlock") + } + } Server.Listen(mainSettings.servicePort) } start() diff --git a/src/services/main/paymentManager.ts b/src/services/main/paymentManager.ts index 8c1aab29..1b82feb5 100644 --- a/src/services/main/paymentManager.ts +++ b/src/services/main/paymentManager.ts @@ -128,12 +128,18 @@ export default class { const routingFeeLimit = this.lnd.GetFeeLimitAmount(payAmount) await this.lockUserWithMinBalance(userId, totalAmountToDecrement + routingFeeLimit) - const payment = await this.lnd.PayInvoice(req.invoice, req.amount, routingFeeLimit) + let payment + try { + payment = await this.lnd.PayInvoice(req.invoice, req.amount, routingFeeLimit) + await this.storage.userStorage.UnlockUser(userId) + } catch (err) { + await this.storage.userStorage.UnlockUser(userId) + throw err + } await this.storage.userStorage.DecrementUserBalance(userId, totalAmountToDecrement + Number(payment.feeSat)) if (isAppUserPayment && serviceFee > 0) { await this.storage.userStorage.IncrementUserBalance(linkedApplication.owner.user_id, serviceFee) } - await this.storage.userStorage.UnlockUser(userId) await this.storage.paymentStorage.AddUserInvoicePayment(userId, req.invoice, payAmount, Number(payment.feeSat), serviceFee) return { preimage: payment.paymentPreimage, @@ -154,9 +160,15 @@ export default class { } const serviceFee = this.getServiceFee(Types.UserOperationType.OUTGOING_INVOICE, req.amoutSats, false) await this.lockUserWithMinBalance(userId, total + serviceFee) - const payment = await this.lnd.PayAddress(req.address, req.amoutSats, satPerVByte) + let payment + try { + payment = await this.lnd.PayAddress(req.address, req.amoutSats, satPerVByte) + await this.storage.userStorage.UnlockUser(userId) + } catch (err) { + await this.storage.userStorage.UnlockUser(userId) + throw err + } await this.storage.userStorage.DecrementUserBalance(userId, total + serviceFee) - await this.storage.userStorage.UnlockUser(userId) await this.storage.paymentStorage.AddUserTransactionPayment(userId, req.address, payment.txid, 0, req.amoutSats, chainFees, serviceFee) return { txId: payment.txid