channeldb/invoices: add IsAMP and IsKeysend helpers

The only way to know if an invoice is AMP, Keysend, etc is to look at
its shape/characteristics. This commit adds a couple of helper functions
to encapsulate the logic of these checks.

If all these types cannot intersect: an invoice cannot be AMP and
Keysend or Keysend and Bolt12, etc it could be useful to add an extra
field to store this information instead of relying on checking how the
invoice looks like.
This commit is contained in:
positiveblue
2023-01-17 04:55:33 -08:00
parent 0eb07c944c
commit 8563e1f409
3 changed files with 74 additions and 44 deletions

View File

@@ -471,6 +471,24 @@ func (i *Invoice) HTLCSetCompliment(setID *[32]byte,
return htlcSet
}
// IsKeysend returns true if the invoice is a Keysend invoice.
func (i *Invoice) IsKeysend() bool {
// TODO(positiveblue): look for a more reliable way to tests if
// an invoice is keysend.
return len(i.PaymentRequest) == 0 && !i.IsAMP()
}
// IsAMP returns true if the invoice is an AMP invoice.
func (i *Invoice) IsAMP() bool {
if i.Terms.Features == nil {
return false
}
return i.Terms.Features.HasFeature(
lnwire.AMPRequired,
)
}
// HtlcState defines the states an htlc paying to an invoice can be in.
type HtlcState uint8
@@ -681,6 +699,8 @@ type InvoiceStateUpdateDesc struct {
// invoice.
type InvoiceUpdateCallback = func(invoice *Invoice) (*InvoiceUpdateDesc, error)
// ValidateInvoice assures the invoice passes the checks for all the relevant
// constraints.
func ValidateInvoice(i *Invoice, paymentHash lntypes.Hash) error {
// Avoid conflicts with all-zeroes magic value in the database.
if paymentHash == UnknownPreimage.Hash() {
@@ -705,13 +725,8 @@ func ValidateInvoice(i *Invoice, paymentHash lntypes.Hash) error {
return err
}
// AMP invoices and hodl invoices are allowed to have no preimage
// specified.
isAMP := i.Terms.Features.HasFeature(
lnwire.AMPOptional,
)
if i.Terms.PaymentPreimage == nil && !(i.HodlInvoice || isAMP) {
return errors.New("non-hodl invoices must have a preimage")
if i.requiresPreimage() && i.Terms.PaymentPreimage == nil {
return errors.New("this invoice must have a preimage")
}
if len(i.Htlcs) > 0 {
@@ -721,6 +736,17 @@ func ValidateInvoice(i *Invoice, paymentHash lntypes.Hash) error {
return nil
}
// requiresPreimage returns true if the invoice requires a preimage to be valid.
func (i *Invoice) requiresPreimage() bool {
// AMP invoices and hodl invoices are allowed to have no preimage
// specified.
if i.HodlInvoice || i.IsAMP() {
return false
}
return true
}
// IsPending returns true if the invoice is in ContractOpen state.
func (i *Invoice) IsPending() bool {
return i.State == ContractOpen || i.State == ContractAccepted