diff --git a/controllers/payinvoice.ctrl.go b/controllers/payinvoice.ctrl.go index eaa07ba..36c4e7e 100644 --- a/controllers/payinvoice.ctrl.go +++ b/controllers/payinvoice.ctrl.go @@ -51,7 +51,7 @@ func (controller *PayInvoiceController) PayInvoice(c echo.Context) error { } paymentRequest := reqBody.Invoice - decodedPaymentRequest, err := controller.svc.DecodePaymentRequest(paymentRequest) + decodedPaymentRequest, err := controller.svc.DecodePaymentRequest(c.Request().Context(), paymentRequest) if err != nil { c.Logger().Errorf("Invalid payment request: %v", err) sentry.CaptureException(err) diff --git a/lib/service/invoices.go b/lib/service/invoices.go index 35af77f..669e03f 100644 --- a/lib/service/invoices.go +++ b/lib/service/invoices.go @@ -6,15 +6,12 @@ import ( "encoding/hex" "errors" "math/rand" - "strings" "time" - "github.com/btcsuite/btcd/chaincfg" "github.com/getAlby/lndhub.go/common" "github.com/getAlby/lndhub.go/db/models" "github.com/labstack/gommon/random" "github.com/lightningnetwork/lnd/lnrpc" - "github.com/lightningnetwork/lnd/zpay32" "github.com/uptrace/bun" "github.com/uptrace/bun/schema" ) @@ -223,32 +220,19 @@ func (svc *LndhubService) PayInvoice(ctx context.Context, invoice *models.Invoic return &paymentResponse, err } -func (svc *LndhubService) AddOutgoingInvoice(ctx context.Context, userID int64, paymentRequest string, decodedInvoice *zpay32.Invoice) (*models.Invoice, error) { +func (svc *LndhubService) AddOutgoingInvoice(ctx context.Context, userID int64, paymentRequest string, decodedInvoice *lnrpc.PayReq) (*models.Invoice, error) { // Initialize new DB invoice - destinationPubkeyHex := hex.EncodeToString(decodedInvoice.Destination.SerializeCompressed()) - expiresAt := decodedInvoice.Timestamp.Add(decodedInvoice.Expiry()) invoice := models.Invoice{ Type: common.InvoiceTypeOutgoing, UserID: userID, PaymentRequest: paymentRequest, + RHash: decodedInvoice.PaymentHash, + Amount: decodedInvoice.NumSatoshis, State: common.InvoiceStateInitialized, - DestinationPubkeyHex: destinationPubkeyHex, - ExpiresAt: bun.NullTime{Time: expiresAt}, - } - if decodedInvoice.Description != nil { - invoice.Memo = *decodedInvoice.Description - } - if decodedInvoice.DescriptionHash != nil { - dh := *decodedInvoice.DescriptionHash - invoice.DescriptionHash = hex.EncodeToString(dh[:]) - } - if decodedInvoice.PaymentHash != nil { - ph := *decodedInvoice.PaymentHash - invoice.RHash = hex.EncodeToString(ph[:]) - } - if decodedInvoice.MilliSat != nil { - msat := decodedInvoice.MilliSat - invoice.Amount = int64(msat.ToSatoshis()) + DestinationPubkeyHex: decodedInvoice.Destination, + DescriptionHash: decodedInvoice.DescriptionHash, + Memo: decodedInvoice.Description, + ExpiresAt: bun.NullTime{Time: time.Unix(decodedInvoice.Expiry, 0)}, } // Save invoice @@ -313,8 +297,8 @@ func (svc *LndhubService) AddIncomingInvoice(ctx context.Context, userID int64, return &invoice, nil } -func (svc *LndhubService) DecodePaymentRequest(bolt11 string) (*zpay32.Invoice, error) { - return zpay32.Decode(bolt11, ChainFromCurrency(bolt11[2:])) +func (svc *LndhubService) DecodePaymentRequest(ctx context.Context, bolt11 string) (*lnrpc.PayReq, error) { + return svc.LndClient.DecodeBolt11(ctx, bolt11) } const hexBytes = random.Hex @@ -326,15 +310,3 @@ func makePreimageHex() []byte { } return b } - -func ChainFromCurrency(currency string) *chaincfg.Params { - if strings.HasPrefix(currency, "bcrt") { - return &chaincfg.RegressionNetParams - } else if strings.HasPrefix(currency, "tb") { - return &chaincfg.TestNet3Params - } else if strings.HasPrefix(currency, "sb") { - return &chaincfg.SimNetParams - } else { - return &chaincfg.MainNetParams - } -} diff --git a/lnd/interface.go b/lnd/interface.go index c5a39a7..0c3733b 100644 --- a/lnd/interface.go +++ b/lnd/interface.go @@ -13,6 +13,7 @@ type LightningClientWrapper interface { AddInvoice(ctx context.Context, req *lnrpc.Invoice, options ...grpc.CallOption) (*lnrpc.AddInvoiceResponse, error) SubscribeInvoices(ctx context.Context, req *lnrpc.InvoiceSubscription, options ...grpc.CallOption) (SubscribeInvoicesWrapper, error) GetInfo(ctx context.Context, req *lnrpc.GetInfoRequest, options ...grpc.CallOption) (*lnrpc.GetInfoResponse, error) + DecodeBolt11(ctx context.Context, bolt11 string, options ...grpc.CallOption) (*lnrpc.PayReq, error) } type SubscribeInvoicesWrapper interface { diff --git a/lnd/lnd.go b/lnd/lnd.go index d62bd6e..ac61a41 100644 --- a/lnd/lnd.go +++ b/lnd/lnd.go @@ -15,6 +15,10 @@ import ( "gopkg.in/macaroon.v2" ) +const ( + MSAT_PER_SAT = 1000 +) + // LNDoptions are the options for the connection to the lnd node. type LNDoptions struct { Address string @@ -110,3 +114,9 @@ func (wrapper *LNDWrapper) SubscribeInvoices(ctx context.Context, req *lnrpc.Inv func (wrapper *LNDWrapper) GetInfo(ctx context.Context, req *lnrpc.GetInfoRequest, options ...grpc.CallOption) (*lnrpc.GetInfoResponse, error) { return wrapper.client.GetInfo(ctx, req, options...) } + +func (wrapper *LNDWrapper) DecodeBolt11(ctx context.Context, bolt11 string, options ...grpc.CallOption) (*lnrpc.PayReq, error) { + return wrapper.client.DecodePayReq(ctx, &lnrpc.PayReqString{ + PayReq: bolt11, + }) +}