From 73aa3f4e97a6a2ab45999b57cc2ede5f9e32c5e6 Mon Sep 17 00:00:00 2001 From: kiwiidb Date: Sun, 6 Feb 2022 15:20:36 +0100 Subject: [PATCH] set up scaffolding --- controllers/bolt12.ctrl.go | 52 ++++++++++++++++++++++++++++++++++++++ lib/service/bolt12.go | 16 ++++++++++++ lnd/c-lightning.go | 4 +-- lnd/interface.go | 4 +++ 4 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 lib/service/bolt12.go diff --git a/controllers/bolt12.ctrl.go b/controllers/bolt12.ctrl.go index 630d11e..7120a9a 100644 --- a/controllers/bolt12.ctrl.go +++ b/controllers/bolt12.ctrl.go @@ -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) +} diff --git a/lib/service/bolt12.go b/lib/service/bolt12.go new file mode 100644 index 0000000..9470731 --- /dev/null +++ b/lib/service/bolt12.go @@ -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 +} diff --git a/lnd/c-lightning.go b/lnd/c-lightning.go index da85108..be2bd6e 100644 --- a/lnd/c-lightning.go +++ b/lnd/c-lightning.go @@ -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 } diff --git a/lnd/interface.go b/lnd/interface.go index a76828a..0f155c6 100644 --- a/lnd/interface.go +++ b/lnd/interface.go @@ -29,3 +29,7 @@ type Offer struct { Vendor string `json:"vendor"` Valid bool `json:"valid"` } + +type Bolt12Invoice struct { + Invoice string `json:"invoice"` +}