From dd600af5b2a357d0a6dbb101d127b6033c2a46b1 Mon Sep 17 00:00:00 2001 From: Stefan Kostic Date: Fri, 4 Mar 2022 20:05:59 +0100 Subject: [PATCH] Refactor send payment sync a bit to reduce code duplication --- lib/service/invoices.go | 61 +++++++++++++++++++++++++++++------------ lib/service/ln.go | 51 ---------------------------------- 2 files changed, 43 insertions(+), 69 deletions(-) diff --git a/lib/service/invoices.go b/lib/service/invoices.go index b4acc5e..330dc94 100644 --- a/lib/service/invoices.go +++ b/lib/service/invoices.go @@ -2,6 +2,7 @@ package service import ( "context" + "crypto/sha256" "encoding/hex" "errors" "math/rand" @@ -98,29 +99,15 @@ func (svc *LndhubService) SendInternalPayment(ctx context.Context, invoice *mode } func (svc *LndhubService) SendPaymentSync(ctx context.Context, invoice *models.Invoice) (SendPaymentResponse, error) { - if invoice.KeySend { - return svc.KeySendPaymentSync(ctx, invoice) - } sendPaymentResponse := SendPaymentResponse{} - // TODO: set dynamic fee limit - feeLimit := lnrpc.FeeLimit{ - //Limit: &lnrpc.FeeLimit_Percent{ - // Percent: 2, - //}, - Limit: &lnrpc.FeeLimit_Fixed{ - Fixed: 300, - }, - } - // Prepare the LNRPC call - sendPaymentRequest := lnrpc.SendRequest{ - PaymentRequest: invoice.PaymentRequest, - Amt: invoice.Amount, - FeeLimit: &feeLimit, + sendPaymentRequest, err := createLnRpcSendRequest(invoice) + if err != nil { + return sendPaymentResponse, err } // Execute the payment - sendPaymentResult, err := svc.LndClient.SendPaymentSync(ctx, &sendPaymentRequest) + sendPaymentResult, err := svc.LndClient.SendPaymentSync(ctx, sendPaymentRequest) if err != nil { return sendPaymentResponse, err } @@ -140,6 +127,44 @@ func (svc *LndhubService) SendPaymentSync(ctx context.Context, invoice *models.I return sendPaymentResponse, nil } +func createLnRpcSendRequest(invoice *models.Invoice) (*lnrpc.SendRequest, error) { + // TODO: set dynamic fee limit + feeLimit := lnrpc.FeeLimit{ + //Limit: &lnrpc.FeeLimit_Percent{ + // Percent: 2, + //}, + Limit: &lnrpc.FeeLimit_Fixed{ + Fixed: 300, + }, + } + + if !invoice.KeySend { + return &lnrpc.SendRequest{ + PaymentRequest: invoice.PaymentRequest, + Amt: invoice.Amount, + FeeLimit: &feeLimit, + }, nil + } + + preImage := makePreimageHex() + pHash := sha256.New() + pHash.Write(preImage) + // Prepare the LNRPC call + //See: https://github.com/hsjoberg/blixt-wallet/blob/9fcc56a7dc25237bc14b85e6490adb9e044c009c/src/lndmobile/index.ts#L251-L270 + destBytes, err := hex.DecodeString(invoice.DestinationPubkeyHex) + if err != nil { + return nil, err + } + return &lnrpc.SendRequest{ + Dest: destBytes, + Amt: invoice.Amount, + PaymentHash: pHash.Sum(nil), + FeeLimit: &feeLimit, + DestFeatures: []lnrpc.FeatureBit{lnrpc.FeatureBit_TLV_ONION_REQ}, + DestCustomRecords: map[uint64][]byte{KEYSEND_CUSTOM_RECORD: preImage, TLV_WHATSAT_MESSAGE: []byte(invoice.Memo)}, + }, nil +} + func (svc *LndhubService) PayInvoice(ctx context.Context, invoice *models.Invoice) (*SendPaymentResponse, error) { userId := invoice.UserID diff --git a/lib/service/ln.go b/lib/service/ln.go index bce08a7..f139e2b 100644 --- a/lib/service/ln.go +++ b/lib/service/ln.go @@ -2,11 +2,7 @@ package service import ( "context" - "crypto/sha256" - "encoding/hex" - "errors" - "github.com/getAlby/lndhub.go/db/models" "github.com/lightningnetwork/lnd/lnrpc" ) @@ -20,50 +16,3 @@ const ( func (svc *LndhubService) GetInfo(ctx context.Context) (*lnrpc.GetInfoResponse, error) { return svc.LndClient.GetInfo(ctx, &lnrpc.GetInfoRequest{}) } - -func (svc *LndhubService) KeySendPaymentSync(ctx context.Context, invoice *models.Invoice) (result SendPaymentResponse, err error) { - sendPaymentResponse := SendPaymentResponse{} - // TODO: set dynamic fee limit - feeLimit := lnrpc.FeeLimit{ - Limit: &lnrpc.FeeLimit_Fixed{ - Fixed: 300, - }, - } - preImage := makePreimageHex() - pHash := sha256.New() - pHash.Write(preImage) - // Prepare the LNRPC call - //See: https://github.com/hsjoberg/blixt-wallet/blob/9fcc56a7dc25237bc14b85e6490adb9e044c009c/src/lndmobile/index.ts#L251-L270 - destBytes, err := hex.DecodeString(invoice.DestinationPubkeyHex) - if err != nil { - return sendPaymentResponse, err - } - sendPaymentRequest := lnrpc.SendRequest{ - Dest: destBytes, - Amt: invoice.Amount, - PaymentHash: pHash.Sum(nil), - FeeLimit: &feeLimit, - DestFeatures: []lnrpc.FeatureBit{lnrpc.FeatureBit_TLV_ONION_REQ}, - DestCustomRecords: map[uint64][]byte{KEYSEND_CUSTOM_RECORD: preImage, TLV_WHATSAT_MESSAGE: []byte(invoice.Memo)}, - } - - // Execute the payment - sendPaymentResult, err := svc.LndClient.SendPaymentSync(ctx, &sendPaymentRequest) - if err != nil { - return sendPaymentResponse, err - } - - // If there was a payment error we return an error - if sendPaymentResult.GetPaymentError() != "" || sendPaymentResult.GetPaymentPreimage() == nil { - return sendPaymentResponse, errors.New(sendPaymentResult.GetPaymentError()) - } - - preimage := sendPaymentResult.GetPaymentPreimage() - sendPaymentResponse.PaymentPreimage = preimage - sendPaymentResponse.PaymentPreimageStr = hex.EncodeToString(preimage[:]) - paymentHash := sendPaymentResult.GetPaymentHash() - sendPaymentResponse.PaymentHash = paymentHash - sendPaymentResponse.PaymentHashStr = hex.EncodeToString(paymentHash[:]) - sendPaymentResponse.PaymentRoute = &Route{TotalAmt: sendPaymentResult.PaymentRoute.TotalAmt, TotalFees: sendPaymentResult.PaymentRoute.TotalFees} - return sendPaymentResponse, nil -}