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 22bd243..8146493 100644 --- a/main.go +++ b/main.go @@ -40,7 +40,7 @@ func main() { e.Use(middleware.BodyLimit("250K")) e.Use(middleware.RateLimiter(middleware.NewRateLimiterMemoryStore(20))) - routes.Routes(e.Group("")) + routes.Routes(e) // Start server go func() { 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..ece10ae 100644 --- a/routes/routes.go +++ b/routes/routes.go @@ -5,11 +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 Routes(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) + g.POST("/addinvoice", addinvoice.AddInvoiceRouter{}.AddInvoice, middleware.JWT([]byte("secret"))) }