lnrpc/invoicesrpc: support empty payment request fields

Prepares for spontaneous key send payments that do not have a payment
request.
This commit is contained in:
Joost Jager 2019-12-05 12:52:11 +01:00
parent 15eedd2b1a
commit 6063dc31fc
No known key found for this signature in database
GPG Key ID: A61B9D4C393C59C7

@ -2,6 +2,7 @@ package invoicesrpc
import ( import (
"encoding/hex" "encoding/hex"
"errors"
"fmt" "fmt"
"github.com/btcsuite/btcd/chaincfg" "github.com/btcsuite/btcd/chaincfg"
@ -11,15 +12,42 @@ import (
"github.com/lightningnetwork/lnd/zpay32" "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. // CreateRPCInvoice creates an *lnrpc.Invoice from the *channeldb.Invoice.
func CreateRPCInvoice(invoice *channeldb.Invoice, func CreateRPCInvoice(invoice *channeldb.Invoice,
activeNetParams *chaincfg.Params) (*lnrpc.Invoice, error) { activeNetParams *chaincfg.Params) (*lnrpc.Invoice, error) {
paymentRequest := string(invoice.PaymentRequest) decoded, err := decodePayReq(invoice, activeNetParams)
decoded, err := zpay32.Decode(paymentRequest, activeNetParams)
if err != nil { if err != nil {
return nil, fmt.Errorf("unable to decode payment request: %v", return nil, err
err)
} }
var descHash []byte var descHash []byte
@ -103,7 +131,7 @@ func CreateRPCInvoice(invoice *channeldb.Invoice,
CreationDate: invoice.CreationDate.Unix(), CreationDate: invoice.CreationDate.Unix(),
SettleDate: settleDate, SettleDate: settleDate,
Settled: isSettled, Settled: isSettled,
PaymentRequest: paymentRequest, PaymentRequest: string(invoice.PaymentRequest),
DescriptionHash: descHash, DescriptionHash: descHash,
Expiry: int64(invoice.Terms.Expiry.Seconds()), Expiry: int64(invoice.Terms.Expiry.Seconds()),
CltvExpiry: uint64(invoice.Terms.FinalCltvDelta), CltvExpiry: uint64(invoice.Terms.FinalCltvDelta),