mirror of
https://github.com/lightningnetwork/lnd.git
synced 2024-11-19 09:53:54 +01:00
lnrpc/invoicesrpc: extract paymentHashAndPreimage helper
This commit is contained in:
parent
0d3253c410
commit
9020a4d2a5
@ -104,11 +104,18 @@ type AddInvoiceData struct {
|
|||||||
RouteHints [][]zpay32.HopHint
|
RouteHints [][]zpay32.HopHint
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddInvoice attempts to add a new invoice to the invoice database. Any
|
// paymentHashAndPreimage returns the payment hash and preimage for this invoice
|
||||||
// duplicated invoices are rejected, therefore all invoices *must* have a
|
// depending on the configuration.
|
||||||
// unique payment preimage.
|
//
|
||||||
func AddInvoice(ctx context.Context, cfg *AddInvoiceConfig,
|
// For MPP invoices (when Amp flag is false), this method may return nil
|
||||||
invoice *AddInvoiceData) (*lntypes.Hash, *channeldb.Invoice, error) {
|
// preimage when create a hodl invoice, but otherwise will always return a
|
||||||
|
// non-nil preimage and the corresponding payment hash. The valid combinations
|
||||||
|
// are parsed as follows:
|
||||||
|
// - Preimage == nil && Hash == nil -> (random preimage, H(random preimage))
|
||||||
|
// - Preimage != nil && Hash == nil -> (Preimage, H(Preimage))
|
||||||
|
// - Preimage == nil && Hash != nil -> (nil, Hash)
|
||||||
|
func (d *AddInvoiceData) paymentHashAndPreimage() (
|
||||||
|
*lntypes.Preimage, lntypes.Hash, error) {
|
||||||
|
|
||||||
var (
|
var (
|
||||||
paymentPreimage *lntypes.Preimage
|
paymentPreimage *lntypes.Preimage
|
||||||
@ -118,28 +125,42 @@ func AddInvoice(ctx context.Context, cfg *AddInvoiceConfig,
|
|||||||
switch {
|
switch {
|
||||||
|
|
||||||
// Only either preimage or hash can be set.
|
// Only either preimage or hash can be set.
|
||||||
case invoice.Preimage != nil && invoice.Hash != nil:
|
case d.Preimage != nil && d.Hash != nil:
|
||||||
return nil, nil,
|
return nil, lntypes.Hash{},
|
||||||
errors.New("preimage and hash both set")
|
errors.New("preimage and hash both set")
|
||||||
|
|
||||||
// If no hash or preimage is given, generate a random preimage.
|
// If no hash or preimage is given, generate a random preimage.
|
||||||
case invoice.Preimage == nil && invoice.Hash == nil:
|
case d.Preimage == nil && d.Hash == nil:
|
||||||
paymentPreimage = &lntypes.Preimage{}
|
paymentPreimage = &lntypes.Preimage{}
|
||||||
if _, err := rand.Read(paymentPreimage[:]); err != nil {
|
if _, err := rand.Read(paymentPreimage[:]); err != nil {
|
||||||
return nil, nil, err
|
return nil, lntypes.Hash{}, err
|
||||||
}
|
}
|
||||||
paymentHash = paymentPreimage.Hash()
|
paymentHash = paymentPreimage.Hash()
|
||||||
|
|
||||||
// If just a hash is given, we create a hold invoice by setting the
|
// If just a hash is given, we create a hold invoice by setting the
|
||||||
// preimage to unknown.
|
// preimage to unknown.
|
||||||
case invoice.Preimage == nil && invoice.Hash != nil:
|
case d.Preimage == nil && d.Hash != nil:
|
||||||
paymentHash = *invoice.Hash
|
paymentHash = *d.Hash
|
||||||
|
|
||||||
// A specific preimage was supplied. Use that for the invoice.
|
// A specific preimage was supplied. Use that for the invoice.
|
||||||
case invoice.Preimage != nil && invoice.Hash == nil:
|
case d.Preimage != nil && d.Hash == nil:
|
||||||
preimage := *invoice.Preimage
|
preimage := *d.Preimage
|
||||||
paymentPreimage = &preimage
|
paymentPreimage = &preimage
|
||||||
paymentHash = invoice.Preimage.Hash()
|
paymentHash = d.Preimage.Hash()
|
||||||
|
}
|
||||||
|
|
||||||
|
return paymentPreimage, paymentHash, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddInvoice attempts to add a new invoice to the invoice database. Any
|
||||||
|
// duplicated invoices are rejected, therefore all invoices *must* have a
|
||||||
|
// unique payment preimage.
|
||||||
|
func AddInvoice(ctx context.Context, cfg *AddInvoiceConfig,
|
||||||
|
invoice *AddInvoiceData) (*lntypes.Hash, *channeldb.Invoice, error) {
|
||||||
|
|
||||||
|
paymentPreimage, paymentHash, err := invoice.paymentHashAndPreimage()
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// The size of the memo, receipt and description hash attached must not
|
// The size of the memo, receipt and description hash attached must not
|
||||||
|
Loading…
Reference in New Issue
Block a user