diff --git a/controllers/gettxs.ctrl.go b/controllers/gettxs.ctrl.go index d4949e5..ed004c5 100644 --- a/controllers/gettxs.ctrl.go +++ b/controllers/gettxs.ctrl.go @@ -1,17 +1,21 @@ package controllers import ( + "context" "net/http" + "github.com/getAlby/lndhub.go/lib" "github.com/getAlby/lndhub.go/lib/service" "github.com/labstack/echo/v4" ) // GetTXSController : GetTXSController struct -type GetTXSController struct{} +type GetTXSController struct { + svc *service.LndhubService +} func NewGetTXSController(svc *service.LndhubService) *GetTXSController { - return &GetTXSController{} + return &GetTXSController{svc: svc} } // GetTXS : Get TXS Controller @@ -21,6 +25,27 @@ func (controller *GetTXSController) GetTXS(c echo.Context) error { } func (controller *GetTXSController) GetUserInvoices(c echo.Context) error { - transactions := []string{} - return c.JSON(http.StatusOK, &transactions) + userId := c.Get("UserID").(int64) + + invoices, err := controller.svc.InvoicesFor(context.TODO(), userId, "incoming") + if err != nil { + return err + } + + response := make([]echo.Map, len(invoices)) + for i, invoice := range invoices { + rhash, _ := lib.ToJavaScriptBuffer(invoice.RHash) + response[i] = echo.Map{ + "r_hash": rhash, + "payment_request": invoice.PaymentRequest, + "pay_req": invoice.PaymentRequest, + "description": invoice.Memo, + "payment_hash": invoice.RHash, + "amt": invoice.Amount, + "expire_time": 3600 * 24, + "timestamp": invoice.CreatedAt.Unix(), + "type": "user_invoice", + } + } + return c.JSON(http.StatusOK, &response) } diff --git a/lib/javascriptbuffer.go b/lib/javascriptbuffer.go new file mode 100644 index 0000000..82b8f09 --- /dev/null +++ b/lib/javascriptbuffer.go @@ -0,0 +1,32 @@ +package lib + +import ( + "encoding/hex" + "fmt" + "strings" +) + +type JavaScriptBuffer struct { + Data []uint8 +} + +func (buf *JavaScriptBuffer) MarshalJSON() ([]byte, error) { + var array string + if buf.Data == nil { + array = "null" + } else { + array = strings.Join(strings.Fields(fmt.Sprintf("%d", buf.Data)), ",") + } + jsonResult := fmt.Sprintf(`{"type": "Buffer", "data":%s}`, array) + return []byte(jsonResult), nil +} + +func ToJavaScriptBuffer(hexString string) (*JavaScriptBuffer, error) { + buf := JavaScriptBuffer{} + hexArray, err := hex.DecodeString(hexString) + if err != nil { + return &buf, err + } + buf.Data = hexArray + return &buf, nil +} diff --git a/lib/service/user.go b/lib/service/user.go index a229360..b38f01d 100644 --- a/lib/service/user.go +++ b/lib/service/user.go @@ -69,6 +69,21 @@ func (svc *LndhubService) AccountFor(ctx context.Context, accountType string, us return account, err } +func (svc *LndhubService) InvoicesFor(ctx context.Context, userId int64, invoiceType string) ([]models.Invoice, error) { + var invoices []models.Invoice + + query := svc.DB.NewSelect().Model(&invoices).Where("user_id = ?", userId) + if invoiceType != "" { + query.Where("type = ?", invoiceType) + } + query.OrderExpr("id DESC").Limit(100) + err := query.Scan(ctx) + if err != nil { + return nil, err + } + return invoices, nil +} + func randStringBytes(n int) string { b := make([]byte, n) for i := range b {