From cd2bee98ef1373ac3db74473602bc02f1b556307 Mon Sep 17 00:00:00 2001 From: Viktor Patchev Date: Fri, 7 Jan 2022 18:24:04 +0100 Subject: [PATCH 1/2] Add invoice endpoint --- database/models/invoice.go | 2 +- go.mod | 2 +- main.go | 8 +++- routes/addinvoice/addinvoice.ctrl.go | 71 +++++++++++++++++++++++++++- routes/routes.go | 9 ++-- 5 files changed, 83 insertions(+), 9 deletions(-) diff --git a/database/models/invoice.go b/database/models/invoice.go index 2358771..f04917c 100644 --- a/database/models/invoice.go +++ b/database/models/invoice.go @@ -8,7 +8,7 @@ type Invoice struct { Type string UserID uint TransactionEntryID uint - Amount int64 + Amount uint Memo string DescriptionHash string PaymentRequest string diff --git a/go.mod b/go.mod index 55ef0ce..11a8f75 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.17 require ( github.com/dgrijalva/jwt-go v3.2.0+incompatible github.com/go-playground/validator/v10 v10.10.0 + github.com/golang-jwt/jwt v3.2.2+incompatible github.com/joho/godotenv v1.4.0 github.com/labstack/echo/v4 v4.6.1 github.com/labstack/gommon v0.3.0 @@ -19,7 +20,6 @@ require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/go-playground/locales v0.14.0 // indirect github.com/go-playground/universal-translator v0.18.0 // indirect - github.com/golang-jwt/jwt v3.2.2+incompatible // indirect github.com/jackc/chunkreader/v2 v2.0.1 // indirect github.com/jackc/pgconn v1.10.1 // indirect github.com/jackc/pgio v1.0.0 // indirect diff --git a/main.go b/main.go index 2968ae7..577975a 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "github.com/labstack/echo/v4/middleware" "os" "github.com/bumi/lndhub.go/database" @@ -29,9 +30,12 @@ func main() { e.Validator = &lib.CustomValidator{Validator: validator.New()} e.Use(middlewares.ContextDB(db)) - //e.Use(middlewares.IsLoggedIn) - routes.Routes(e.Group("")) + jwt := e.Group("") + jwt.Use(middleware.JWT([]byte("secret"))) + + routes.NoJWTRoutes(e.Group("")) + routes.JWTRoutes(jwt) e.Logger.Fatal(e.Start(":3000")) } diff --git a/routes/addinvoice/addinvoice.ctrl.go b/routes/addinvoice/addinvoice.ctrl.go index 9211aff..0e50d5b 100644 --- a/routes/addinvoice/addinvoice.ctrl.go +++ b/routes/addinvoice/addinvoice.ctrl.go @@ -1,11 +1,78 @@ package addinvoice -import "github.com/labstack/echo/v4" +import ( + "gorm.io/gorm" + "math/rand" + "net/http" + + "github.com/bumi/lndhub.go/database/models" + "github.com/golang-jwt/jwt" + "github.com/labstack/echo/v4" + "github.com/labstack/gommon/random" +) // AddInvoiceRouter : Add invoice router struct type AddInvoiceRouter struct{} // AddInvoice : Add invoice Router func (AddInvoiceRouter) AddInvoice(c echo.Context) error { - return nil + user := c.Get("user").(*jwt.Token) + claims := user.Claims.(jwt.MapClaims) + userID := claims["sub"].(float64) + + type RequestBody struct { + Amt uint `json:"amt" validate:"required"` + Memo string `json:"memo"` + DescriptionHash string `json:"description_hash"` + } + + var body RequestBody + + if err := c.Bind(&body); err != nil { + return c.JSON(http.StatusBadRequest, echo.Map{ + "message": "failed to bind json, amt field with positive value is required", + }) + } + + if err := c.Validate(&body); err != nil { + return c.JSON(http.StatusBadRequest, echo.Map{ + "message": "amt with positive value is required", + }) + } + + db, _ := c.Get("db").(*gorm.DB) + + invoice := models.Invoice{ + Type: "", + UserID: uint(userID), + TransactionEntryID: 0, + Amount: body.Amt, + Memo: body.Memo, + DescriptionHash: body.DescriptionHash, + PaymentRequest: "", + RHash: "", + State: "", + } + + db.Create(&invoice) + + var responseBody struct { + RHash string `json:"r_hash"` + PaymentRequest string `json:"payment_request"` + PayReq string `json:"pay_req"` + } + + responseBody.PayReq = makePreimageHex() + + return c.JSON(http.StatusOK, &responseBody) +} + +const hexBytes = random.Hex + +func makePreimageHex() string { + b := make([]byte, 32) + for i := range b { + b[i] = hexBytes[rand.Intn(len(hexBytes))] + } + return string(b) } diff --git a/routes/routes.go b/routes/routes.go index 35da7d5..40f7b21 100644 --- a/routes/routes.go +++ b/routes/routes.go @@ -8,8 +8,11 @@ import ( ) // Routes : Init Routes -func Routes(g *echo.Group) { - g.POST("/auth", auth.AuthRouter{}.Auth) - g.POST("/create", create.CreateUserRouter{}.CreateUser) +func JWTRoutes(g *echo.Group) { g.POST("/addinvoice", addinvoice.AddInvoiceRouter{}.AddInvoice) } + +func NoJWTRoutes(g *echo.Group) { + g.POST("/auth", auth.AuthRouter{}.Auth) + g.POST("/create", create.CreateUserRouter{}.CreateUser) +} From 645a63ba28d3ebe2fee2fdd4d87e5a0f6d352982 Mon Sep 17 00:00:00 2001 From: Viktor Patchev Date: Sat, 8 Jan 2022 10:44:18 +0100 Subject: [PATCH 2/2] Fix adding jwt middleware --- main.go | 9 ++------- routes/routes.go | 8 +++----- 2 files changed, 5 insertions(+), 12 deletions(-) diff --git a/main.go b/main.go index 7be335c..2d94816 100644 --- a/main.go +++ b/main.go @@ -2,8 +2,8 @@ package main import ( "context" - "net/http" "github.com/labstack/echo/v4/middleware" + "net/http" "os" "os/signal" "time" @@ -35,13 +35,8 @@ func main() { e.Use(middlewares.ContextDB(db)) e.Use(middleware.RateLimiter(middleware.NewRateLimiterMemoryStore(20))) - //e.Use(middlewares.IsLoggedIn) - jwt := e.Group("") - jwt.Use(middleware.JWT([]byte("secret"))) - - routes.NoJWTRoutes(e.Group("")) - routes.JWTRoutes(jwt) + routes.Routes(e) // Start server go func() { diff --git a/routes/routes.go b/routes/routes.go index 40f7b21..ece10ae 100644 --- a/routes/routes.go +++ b/routes/routes.go @@ -5,14 +5,12 @@ import ( "github.com/bumi/lndhub.go/routes/auth" "github.com/bumi/lndhub.go/routes/create" "github.com/labstack/echo/v4" + "github.com/labstack/echo/v4/middleware" ) // Routes : Init Routes -func JWTRoutes(g *echo.Group) { - g.POST("/addinvoice", addinvoice.AddInvoiceRouter{}.AddInvoice) -} - -func NoJWTRoutes(g *echo.Group) { +func Routes(g *echo.Echo) { g.POST("/auth", auth.AuthRouter{}.Auth) g.POST("/create", create.CreateUserRouter{}.CreateUser) + g.POST("/addinvoice", addinvoice.AddInvoiceRouter{}.AddInvoice, middleware.JWT([]byte("secret"))) }