From 5fd0d66a54d7d0b4c9e4c378faac1d983da7e6a0 Mon Sep 17 00:00:00 2001 From: im-adithya Date: Thu, 7 Dec 2023 14:46:12 +0530 Subject: [PATCH] chore: further refactoring --- cmd/server/main.go | 1 - controllers_v2/keysend.ctrl.go | 3 ++- db/models/user.go | 8 ++++++++ integration_tests/util.go | 2 +- lib/service/user.go | 18 +++++++++--------- lnd/config.go | 8 -------- 6 files changed, 20 insertions(+), 20 deletions(-) diff --git a/cmd/server/main.go b/cmd/server/main.go index 059a3464..fa5a61ab 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -153,7 +153,6 @@ func main() { logMw := transport.CreateLoggingMiddleware(logger) // strict rate limit for requests for sending payments strictRateLimitMiddleware := transport.CreateRateLimitMiddleware(c.StrictRateLimit, c.BurstRateLimit) - secured := e.Group("", tokens.Middleware(c.JWTSecret), logMw) securedWithStrictRateLimit := e.Group("", tokens.Middleware(c.JWTSecret), strictRateLimitMiddleware, logMw) diff --git a/controllers_v2/keysend.ctrl.go b/controllers_v2/keysend.ctrl.go index 9df44105..38e9a2ee 100644 --- a/controllers_v2/keysend.ctrl.go +++ b/controllers_v2/keysend.ctrl.go @@ -8,6 +8,7 @@ import ( "strconv" "github.com/getAlby/lndhub.go/common" + "github.com/getAlby/lndhub.go/db/models" "github.com/getAlby/lndhub.go/lib/responses" "github.com/getAlby/lndhub.go/lib/service" "github.com/getAlby/lndhub.go/lnd" @@ -164,7 +165,7 @@ func (controller *KeySendController) MultiKeySend(c echo.Context) error { return c.JSON(status, result) } -func (controller *KeySendController) checkKeysendPaymentAllowed(ctx context.Context, amount, userID int64, limits *lnd.Limits) (resp *responses.ErrorResponse) { +func (controller *KeySendController) checkKeysendPaymentAllowed(ctx context.Context, amount, userID int64, limits *models.Limits) (resp *responses.ErrorResponse) { syntheticPayReq := &lnd.LNPayReq{ PayReq: &lnrpc.PayReq{ NumSatoshis: amount, diff --git a/db/models/user.go b/db/models/user.go index 98939f09..a9f55af8 100644 --- a/db/models/user.go +++ b/db/models/user.go @@ -21,6 +21,14 @@ type User struct { Deactivated bool } +type Limits struct { + MaxSendVolume int64 + MaxSendAmount int64 + MaxReceiveVolume int64 + MaxReceiveAmount int64 + MaxAccountBalance int64 +} + func (u *User) BeforeAppendModel(ctx context.Context, query bun.Query) error { switch query.(type) { case *bun.UpdateQuery: diff --git a/integration_tests/util.go b/integration_tests/util.go index 7e83a877..2fe3e7d7 100644 --- a/integration_tests/util.go +++ b/integration_tests/util.go @@ -45,7 +45,7 @@ const ( ) func LndHubTestServiceInit(lndClientMock lnd.LightningClientWrapper) (svc *service.LndhubService, err error) { - dbUri := "postgresql://im-adithya:password@localhost:5432/lndhub?sslmode=disable" + dbUri := "postgresql://user:password@localhost/lndhub?sslmode=disable" c := &service.Config{ DatabaseUri: dbUri, DatabaseMaxConns: 1, diff --git a/lib/service/user.go b/lib/service/user.go index 4165fd8a..8f25cca4 100644 --- a/lib/service/user.go +++ b/lib/service/user.go @@ -126,7 +126,7 @@ func (svc *LndhubService) FindUserByLogin(ctx context.Context, login string) (*m return &user, nil } -func (svc *LndhubService) CheckOutgoingPaymentAllowed(ctx context.Context, lnpayReq *lnd.LNPayReq, userId int64, limits *lnd.Limits) (result *responses.ErrorResponse, err error) { +func (svc *LndhubService) CheckOutgoingPaymentAllowed(ctx context.Context, lnpayReq *lnd.LNPayReq, userId int64, limits *models.Limits) (result *responses.ErrorResponse, err error) { if limits.MaxSendAmount > 0 { if lnpayReq.PayReq.NumSatoshis > limits.MaxSendAmount { svc.Logger.Errorf("Max send amount exceeded for user_id %v (amount:%v)", userId, lnpayReq.PayReq.NumSatoshis) @@ -157,7 +157,7 @@ func (svc *LndhubService) CheckOutgoingPaymentAllowed(ctx context.Context, lnpay return svc.CheckVolumeAllowed(ctx, userId, limits.MaxSendVolume, common.InvoiceTypeOutgoing) } -func (svc *LndhubService) CheckIncomingPaymentAllowed(ctx context.Context, amount, userId int64, limits *lnd.Limits) (result *responses.ErrorResponse, err error) { +func (svc *LndhubService) CheckIncomingPaymentAllowed(ctx context.Context, amount, userId int64, limits *models.Limits) (result *responses.ErrorResponse, err error) { if limits.MaxReceiveAmount > 0 { if amount > limits.MaxReceiveAmount { svc.Logger.Errorf("Max receive amount exceeded for user_id %d", userId) @@ -275,27 +275,27 @@ func (svc *LndhubService) GetVolumeOverPeriod(ctx context.Context, userId int64, return result, nil } -func (svc *LndhubService) GetLimits(c echo.Context) (limits *lnd.Limits) { - limits = &lnd.Limits{ +func (svc *LndhubService) GetLimits(c echo.Context) (limits *models.Limits) { + limits = &models.Limits{ MaxSendVolume: svc.Config.MaxSendVolume, MaxSendAmount: svc.Config.MaxSendAmount, MaxReceiveVolume: svc.Config.MaxReceiveVolume, MaxReceiveAmount: svc.Config.MaxReceiveAmount, MaxAccountBalance: svc.Config.MaxAccountBalance, } - if val, ok := c.Get("MaxSendVolume").(int64); ok { + if val, ok := c.Get("MaxSendVolume").(int64); ok && val > 0 { limits.MaxSendVolume = val } - if val, ok := c.Get("MaxSendAmount").(int64); ok { + if val, ok := c.Get("MaxSendAmount").(int64); ok && val > 0 { limits.MaxSendAmount = val } - if val, ok := c.Get("MaxReceiveVolume").(int64); ok { + if val, ok := c.Get("MaxReceiveVolume").(int64); ok && val > 0 { limits.MaxReceiveVolume = val } - if val, ok := c.Get("MaxReceiveAmount").(int64); ok { + if val, ok := c.Get("MaxReceiveAmount").(int64); ok && val > 0 { limits.MaxReceiveAmount = val } - if val, ok := c.Get("MaxAccountBalance").(int64); ok { + if val, ok := c.Get("MaxAccountBalance").(int64); ok && val > 0 { limits.MaxAccountBalance = val } diff --git a/lnd/config.go b/lnd/config.go index 509b1a7e..41cabceb 100644 --- a/lnd/config.go +++ b/lnd/config.go @@ -22,14 +22,6 @@ type Config struct { LNDClusterPubkeys string `envconfig:"LND_CLUSTER_PUBKEYS"` //comma-seperated list of public keys of the cluster } -type Limits struct { - MaxSendVolume int64 - MaxSendAmount int64 - MaxReceiveVolume int64 - MaxReceiveAmount int64 - MaxAccountBalance int64 -} - func LoadConfig() (c *Config, err error) { c = &Config{} err = envconfig.Process("", c)