From 6063dc31fc078b611b63af4816d6ef5d46b7b1e2 Mon Sep 17 00:00:00 2001 From: Joost Jager Date: Thu, 5 Dec 2019 12:52:11 +0100 Subject: [PATCH] lnrpc/invoicesrpc: support empty payment request fields Prepares for spontaneous key send payments that do not have a payment request. --- lnrpc/invoicesrpc/utils.go | 38 +++++++++++++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/lnrpc/invoicesrpc/utils.go b/lnrpc/invoicesrpc/utils.go index ddbbc1d5..3da0155d 100644 --- a/lnrpc/invoicesrpc/utils.go +++ b/lnrpc/invoicesrpc/utils.go @@ -2,6 +2,7 @@ package invoicesrpc import ( "encoding/hex" + "errors" "fmt" "github.com/btcsuite/btcd/chaincfg" @@ -11,15 +12,42 @@ import ( "github.com/lightningnetwork/lnd/zpay32" ) +// decodePayReq decodes the invoice payment request if present. This is needed, +// because not all information is stored in dedicated invoice fields. If there +// is no payment request present, a dummy request will be returned. This can +// happen with just-in-time inserted key send invoices. +func decodePayReq(invoice *channeldb.Invoice, + activeNetParams *chaincfg.Params) (*zpay32.Invoice, error) { + + paymentRequest := string(invoice.PaymentRequest) + if paymentRequest == "" { + preimage := invoice.Terms.PaymentPreimage + if preimage == channeldb.UnknownPreimage { + return nil, errors.New("cannot reconstruct pay req") + } + hash := [32]byte(preimage.Hash()) + return &zpay32.Invoice{ + PaymentHash: &hash, + }, nil + } + + var err error + decoded, err := zpay32.Decode(paymentRequest, activeNetParams) + if err != nil { + return nil, fmt.Errorf("unable to decode payment "+ + "request: %v", err) + } + return decoded, nil + +} + // CreateRPCInvoice creates an *lnrpc.Invoice from the *channeldb.Invoice. func CreateRPCInvoice(invoice *channeldb.Invoice, activeNetParams *chaincfg.Params) (*lnrpc.Invoice, error) { - paymentRequest := string(invoice.PaymentRequest) - decoded, err := zpay32.Decode(paymentRequest, activeNetParams) + decoded, err := decodePayReq(invoice, activeNetParams) if err != nil { - return nil, fmt.Errorf("unable to decode payment request: %v", - err) + return nil, err } var descHash []byte @@ -103,7 +131,7 @@ func CreateRPCInvoice(invoice *channeldb.Invoice, CreationDate: invoice.CreationDate.Unix(), SettleDate: settleDate, Settled: isSettled, - PaymentRequest: paymentRequest, + PaymentRequest: string(invoice.PaymentRequest), DescriptionHash: descHash, Expiry: int64(invoice.Terms.Expiry.Seconds()), CltvExpiry: uint64(invoice.Terms.FinalCltvDelta),