style: format invoicesrpc/addinvoice.go

This commit is contained in:
positiveblue 2022-09-09 08:56:21 -07:00
parent 6a4648b7aa
commit 0e803172d6
No known key found for this signature in database
GPG key ID: 4FFF2510928804DC

View file

@ -126,8 +126,8 @@ type AddInvoiceData struct {
// NOTE: Preimage should always be set to nil when this value is true. // NOTE: Preimage should always be set to nil when this value is true.
Amp bool Amp bool
// RouteHints are optional route hints that can each be individually used // RouteHints are optional route hints that can each be individually
// to assist in reaching the invoice's destination. // used to assist in reaching the invoice's destination.
RouteHints [][]zpay32.HopHint RouteHints [][]zpay32.HopHint
} }
@ -159,7 +159,9 @@ func (d *AddInvoiceData) paymentHashAndPreimage() (
// ampPaymentHashAndPreimage returns the payment hash to use for an AMP invoice. // ampPaymentHashAndPreimage returns the payment hash to use for an AMP invoice.
// The preimage will always be nil. // The preimage will always be nil.
func (d *AddInvoiceData) ampPaymentHashAndPreimage() (*lntypes.Preimage, lntypes.Hash, error) { func (d *AddInvoiceData) ampPaymentHashAndPreimage() (*lntypes.Preimage,
lntypes.Hash, error) {
switch { switch {
// Preimages cannot be set on AMP invoice. // Preimages cannot be set on AMP invoice.
case d.Preimage != nil: case d.Preimage != nil:
@ -184,7 +186,9 @@ func (d *AddInvoiceData) ampPaymentHashAndPreimage() (*lntypes.Preimage, lntypes
// mppPaymentHashAndPreimage returns the payment hash and preimage to use for an // mppPaymentHashAndPreimage returns the payment hash and preimage to use for an
// MPP invoice. // MPP invoice.
func (d *AddInvoiceData) mppPaymentHashAndPreimage() (*lntypes.Preimage, lntypes.Hash, error) { func (d *AddInvoiceData) mppPaymentHashAndPreimage() (*lntypes.Preimage,
lntypes.Hash, error) {
var ( var (
paymentPreimage *lntypes.Preimage paymentPreimage *lntypes.Preimage
paymentHash lntypes.Hash paymentHash lntypes.Hash
@ -235,11 +239,14 @@ func AddInvoice(ctx context.Context, cfg *AddInvoiceConfig,
// exceed the maximum values for either of the fields. // exceed the maximum values for either of the fields.
if len(invoice.Memo) > channeldb.MaxMemoSize { if len(invoice.Memo) > channeldb.MaxMemoSize {
return nil, nil, fmt.Errorf("memo too large: %v bytes "+ return nil, nil, fmt.Errorf("memo too large: %v bytes "+
"(maxsize=%v)", len(invoice.Memo), channeldb.MaxMemoSize) "(maxsize=%v)", len(invoice.Memo),
channeldb.MaxMemoSize)
} }
if len(invoice.DescriptionHash) > 0 && len(invoice.DescriptionHash) != 32 { if len(invoice.DescriptionHash) > 0 &&
return nil, nil, fmt.Errorf("description hash is %v bytes, must be 32", len(invoice.DescriptionHash) != 32 {
len(invoice.DescriptionHash))
return nil, nil, fmt.Errorf("description hash is %v bytes, "+
"must be 32", len(invoice.DescriptionHash))
} }
// We set the max invoice amount to 100k BTC, which itself is several // We set the max invoice amount to 100k BTC, which itself is several
@ -281,8 +288,8 @@ func AddInvoice(ctx context.Context, cfg *AddInvoiceConfig,
addr, err := btcutil.DecodeAddress(invoice.FallbackAddr, addr, err := btcutil.DecodeAddress(invoice.FallbackAddr,
cfg.ChainParams) cfg.ChainParams)
if err != nil { if err != nil {
return nil, nil, fmt.Errorf("invalid fallback address: %v", return nil, nil, fmt.Errorf("invalid fallback "+
err) "address: %v", err)
} }
options = append(options, zpay32.FallbackAddr(addr)) options = append(options, zpay32.FallbackAddr(addr))
} }
@ -314,7 +321,8 @@ func AddInvoice(ctx context.Context, cfg *AddInvoiceConfig,
// Otherwise, use the default AMP expiry. // Otherwise, use the default AMP expiry.
default: default:
options = append(options, zpay32.Expiry(DefaultAMPInvoiceExpiry)) defaultExpiry := zpay32.Expiry(DefaultAMPInvoiceExpiry)
options = append(options, defaultExpiry)
} }
// If the description hash is set, then we add it do the list of options. // If the description hash is set, then we add it do the list of options.
@ -333,8 +341,10 @@ func AddInvoice(ctx context.Context, cfg *AddInvoiceConfig,
// an option on the command line when creating an invoice. // an option on the command line when creating an invoice.
switch { switch {
case invoice.CltvExpiry > math.MaxUint16: case invoice.CltvExpiry > math.MaxUint16:
return nil, nil, fmt.Errorf("CLTV delta of %v is too large, max "+ return nil, nil, fmt.Errorf("CLTV delta of %v is too large, "+
"accepted is: %v", invoice.CltvExpiry, math.MaxUint16) "max accepted is: %v", invoice.CltvExpiry,
math.MaxUint16)
case invoice.CltvExpiry != 0: case invoice.CltvExpiry != 0:
// Disallow user-chosen final CLTV deltas below the required // Disallow user-chosen final CLTV deltas below the required
// minimum. // minimum.
@ -346,17 +356,18 @@ func AddInvoice(ctx context.Context, cfg *AddInvoiceConfig,
options = append(options, options = append(options,
zpay32.CLTVExpiry(invoice.CltvExpiry)) zpay32.CLTVExpiry(invoice.CltvExpiry))
default: default:
// TODO(roasbeef): assumes set delta between versions // TODO(roasbeef): assumes set delta between versions
defaultDelta := cfg.DefaultCLTVExpiry defaultCLTVExpiry := uint64(cfg.DefaultCLTVExpiry)
options = append(options, zpay32.CLTVExpiry(uint64(defaultDelta))) options = append(options, zpay32.CLTVExpiry(defaultCLTVExpiry))
} }
// We make sure that the given invoice routing hints number is within the // We make sure that the given invoice routing hints number is within
// valid range // the valid range
if len(invoice.RouteHints) > 20 { if len(invoice.RouteHints) > 20 {
return nil, nil, fmt.Errorf("number of routing hints must not exceed " + return nil, nil, fmt.Errorf("number of routing hints must " +
"maximum of 20") "not exceed maximum of 20")
} }
// We continue by populating the requested routing hints indexing their // We continue by populating the requested routing hints indexing their
@ -364,8 +375,8 @@ func AddInvoice(ctx context.Context, cfg *AddInvoiceConfig,
forcedHints := make(map[uint64]struct{}) forcedHints := make(map[uint64]struct{})
for _, h := range invoice.RouteHints { for _, h := range invoice.RouteHints {
if len(h) == 0 { if len(h) == 0 {
return nil, nil, fmt.Errorf("number of hop hint within a route must " + return nil, nil, fmt.Errorf("number of hop hint " +
"be positive") "within a route must be positive")
} }
options = append(options, zpay32.RouteHint(h)) options = append(options, zpay32.RouteHint(h))
@ -379,23 +390,25 @@ func AddInvoice(ctx context.Context, cfg *AddInvoiceConfig,
if invoice.Private { if invoice.Private {
openChannels, err := cfg.ChanDB.FetchAllChannels() openChannels, err := cfg.ChanDB.FetchAllChannels()
if err != nil { if err != nil {
return nil, nil, fmt.Errorf("could not fetch all channels") return nil, nil, fmt.Errorf("could not fetch all " +
"channels")
} }
if len(openChannels) > 0 { if len(openChannels) > 0 {
// We filter the channels by excluding the ones that were specified by // We filter the channels by excluding the ones that
// the caller and were already added. // were specified by the caller and were already added.
var filteredChannels []*HopHintInfo var filteredChannels []*HopHintInfo
for _, c := range openChannels { for _, c := range openChannels {
if _, ok := forcedHints[c.ShortChanID().ToUint64()]; ok { scid := c.ShortChanID().ToUint64()
if _, ok := forcedHints[scid]; ok {
continue continue
} }
// If this is a zero-conf channel, check if the // If this is a zero-conf channel, check if the
// confirmed SCID was used in forcedHints. // confirmed SCID was used in forcedHints.
realScid := c.ZeroConfRealScid().ToUint64()
if c.IsZeroConf() { if c.IsZeroConf() {
if _, ok := forcedHints[realScid]; ok { scid := c.ZeroConfRealScid().ToUint64()
if _, ok := forcedHints[scid]; ok {
continue continue
} }
} }