mirror of
https://github.com/getAlby/lndhub.go.git
synced 2026-01-16 19:35:40 +01:00
Merge pull request #11 from bumi/addinvoice-endpoint
Add invoice endpoint
This commit is contained in:
@@ -8,7 +8,7 @@ type Invoice struct {
|
||||
Type string
|
||||
UserID uint
|
||||
TransactionEntryID uint
|
||||
Amount int64
|
||||
Amount uint
|
||||
Memo string
|
||||
DescriptionHash string
|
||||
PaymentRequest string
|
||||
|
||||
2
go.mod
2
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
|
||||
|
||||
2
main.go
2
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() {
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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")))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user