Refactor send payment sync a bit to reduce code duplication

This commit is contained in:
Stefan Kostic
2022-03-04 20:05:59 +01:00
parent 8b077cd5ea
commit dd600af5b2
2 changed files with 43 additions and 69 deletions

View File

@@ -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

View File

@@ -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
}