set up scaffolding

This commit is contained in:
kiwiidb
2022-02-06 15:20:36 +01:00
parent 514d33a0d1
commit 73aa3f4e97
4 changed files with 74 additions and 2 deletions

View File

@@ -4,6 +4,7 @@ import (
"context"
"net/http"
"github.com/getAlby/lndhub.go/lib/responses"
"github.com/getAlby/lndhub.go/lib/service"
"github.com/labstack/echo/v4"
)
@@ -12,6 +13,11 @@ import (
type Bolt12Controller struct {
svc *service.LndhubService
}
type FetchInvoiceRequestBody struct {
Amount int64 `json:"amt"` // amount in Satoshi
Memo string `json:"memo"`
Offer string `json:"offer"`
}
func NewBolt12Controller(svc *service.LndhubService) *Bolt12Controller {
return &Bolt12Controller{svc: svc}
@@ -26,3 +32,49 @@ func (controller *Bolt12Controller) Decode(c echo.Context) error {
}
return c.JSON(http.StatusOK, decoded)
}
// FetchInvoice: fetches an invoice from a bolt12 offer for a certain amount
func (controller *Bolt12Controller) FetchInvoice(c echo.Context) error {
var body FetchInvoiceRequestBody
if err := c.Bind(&body); err != nil {
c.Logger().Errorf("Failed to load fetchinvoice request body: %v", err)
return c.JSON(http.StatusBadRequest, responses.BadArgumentsError)
}
if err := c.Validate(&body); err != nil {
c.Logger().Errorf("Invalid fetchinvoice request body: %v", err)
return c.JSON(http.StatusBadRequest, responses.BadArgumentsError)
}
invoice, err := controller.svc.FetchBolt12Invoice(context.TODO(), body.Offer, body.Memo, body.Amount)
if err != nil {
return err
}
return c.JSON(http.StatusOK, invoice)
}
// PayOffer: fetches an invoice from a bolt12 offer for a certain amount, and pays it
func (controller *Bolt12Controller) PayOffer(c echo.Context) error {
var body FetchInvoiceRequestBody
if err := c.Bind(&body); err != nil {
c.Logger().Errorf("Failed to load fetchinvoice request body: %v", err)
return c.JSON(http.StatusBadRequest, responses.BadArgumentsError)
}
if err := c.Validate(&body); err != nil {
c.Logger().Errorf("Invalid fetchinvoice request body: %v", err)
return c.JSON(http.StatusBadRequest, responses.BadArgumentsError)
}
invoice, err := controller.svc.FetchBolt12Invoice(context.TODO(), body.Offer, body.Memo, body.Amount)
if err != nil {
return err
}
result, err := controller.svc.PayBolt12Invoice(context.TODO(), invoice.Invoice)
if err != nil {
return err
}
return c.JSON(http.StatusOK, result)
}

16
lib/service/bolt12.go Normal file
View File

@@ -0,0 +1,16 @@
package service
import (
"context"
"github.com/getAlby/lndhub.go/lnd"
"github.com/lightningnetwork/lnd/lnrpc"
)
func (svc *LndhubService) FetchBolt12Invoice(ctx context.Context, offer, memo string, amt int64) (result *lnd.Bolt12Invoice, err error) {
return nil, err
}
func (svc *LndhubService) PayBolt12Invoice(ctx context.Context, invoice string) (result *lnrpc.SendResponse, err error) {
return nil, err
}

View File

@@ -145,8 +145,8 @@ func (cl *CLNClient) SendPaymentSync(ctx context.Context, req *lnrpc.SendRequest
PaymentPreimage: []byte(result.Get("payment_preimage").String()),
PaymentHash: []byte(result.Get("payment_hash").String()),
PaymentRoute: &lnrpc.Route{
TotalFees: result.Get("amount_sent_msat").Int() - result.Get("amount_msat").Int(),
TotalAmt: result.Get("amount_sent_msat").Int(),
TotalFees: result.Get("msatoshi_sent").Int()/MSAT_PER_SAT - result.Get("msatoshi").Int()/MSAT_PER_SAT,
TotalAmt: result.Get("msatoshi_sent").Int() / MSAT_PER_SAT,
},
}, nil
}

View File

@@ -29,3 +29,7 @@ type Offer struct {
Vendor string `json:"vendor"`
Valid bool `json:"valid"`
}
type Bolt12Invoice struct {
Invoice string `json:"invoice"`
}