• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lightningnetwork / lnd / 13035292482

29 Jan 2025 03:59PM UTC coverage: 49.3% (-9.5%) from 58.777%
13035292482

Pull #9456

github

mohamedawnallah
docs: update release-notes-0.19.0.md

In this commit, we warn users about the removal
of RPCs `SendToRoute`, `SendToRouteSync`, `SendPayment`,
and `SendPaymentSync` in the next release 0.20.
Pull Request #9456: lnrpc+docs: deprecate warning `SendToRoute`, `SendToRouteSync`, `SendPayment`, and `SendPaymentSync` in Release 0.19

100634 of 204126 relevant lines covered (49.3%)

1.54 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

84.78
/routing/blinding.go
1
package routing
2

3
import (
4
        "bytes"
5
        "errors"
6
        "fmt"
7

8
        "github.com/btcsuite/btcd/btcec/v2"
9
        "github.com/decred/dcrd/dcrec/secp256k1/v4"
10
        sphinx "github.com/lightningnetwork/lightning-onion"
11
        "github.com/lightningnetwork/lnd/graph/db/models"
12
        "github.com/lightningnetwork/lnd/input"
13
        "github.com/lightningnetwork/lnd/lnwire"
14
        "github.com/lightningnetwork/lnd/routing/route"
15
)
16

17
// BlindedPathNUMSHex is the hex encoded version of the blinded path target
18
// NUMs key (in compressed format) which has no known private key.
19
// This was generated using the following script:
20
// https://github.com/lightninglabs/lightning-node-connect/tree/master/
21
// mailbox/numsgen, with the seed phrase "Lightning Blinded Path".
22
const BlindedPathNUMSHex = "02667a98ef82ecb522f803b17a74f14508a48b25258f9831" +
23
        "dd6e95f5e299dfd54e"
24

25
var (
26
        // ErrNoBlindedPath is returned when the blinded path in a blinded
27
        // payment is missing.
28
        ErrNoBlindedPath = errors.New("blinded path required")
29

30
        // ErrInsufficientBlindedHops is returned when a blinded path does
31
        // not have enough blinded hops.
32
        ErrInsufficientBlindedHops = errors.New("blinded path requires " +
33
                "at least one hop")
34

35
        // ErrHTLCRestrictions is returned when a blinded path has invalid
36
        // HTLC maximum and minimum values.
37
        ErrHTLCRestrictions = errors.New("invalid htlc minimum and maximum")
38

39
        // BlindedPathNUMSKey is a NUMS key (nothing up my sleeves number) that
40
        // has no known private key.
41
        BlindedPathNUMSKey = input.MustParsePubKey(BlindedPathNUMSHex)
42

43
        // CompressedBlindedPathNUMSKey is the compressed version of the
44
        // BlindedPathNUMSKey.
45
        CompressedBlindedPathNUMSKey = BlindedPathNUMSKey.SerializeCompressed()
46
)
47

48
// BlindedPaymentPathSet groups the data we need to handle sending to a set of
49
// blinded paths provided by the recipient of a payment.
50
//
51
// NOTE: for now this only holds a single BlindedPayment. By the end of the PR
52
// series, it will handle multiple paths.
53
type BlindedPaymentPathSet struct {
54
        // paths is the set of blinded payment paths for a single payment.
55
        // NOTE: For now this will always only have a single entry. By the end
56
        // of this PR, it can hold multiple.
57
        paths []*BlindedPayment
58

59
        // targetPubKey is the ephemeral node pub key that we will inject into
60
        // each path as the last hop. This is only for the sake of path finding.
61
        // Once the path has been found, the original destination pub key is
62
        // used again. In the edge case where there is only a single hop in the
63
        // path (the introduction node is the destination node), then this will
64
        // just be the introduction node's real public key.
65
        targetPubKey *btcec.PublicKey
66

67
        // features is the set of relay features available for the payment.
68
        // This is extracted from the set of blinded payment paths. At the
69
        // moment we require that all paths for the same payment have the
70
        // same feature set.
71
        features *lnwire.FeatureVector
72

73
        // finalCLTV is the final hop's expiry delta of _any_ path in the set.
74
        // For any multi-hop path, the final CLTV delta should be seen as zero
75
        // since the final hop's final CLTV delta is accounted for in the
76
        // accumulated path policy values. The only edge case is for when the
77
        // final hop in the path is also the introduction node in which case
78
        // that path's FinalCLTV must be the non-zero min CLTV of the final hop
79
        // so that it is accounted for in path finding. For this reason, if
80
        // we have any single path in the set with only one hop, then we throw
81
        // away all the other paths. This should be fine to do since if there is
82
        // a path where the intro node is also the destination node, then there
83
        // isn't any need to try any other longer blinded path. In other words,
84
        // if this value is non-zero, then there is only one path in this
85
        // blinded path set and that path only has a single hop: the
86
        // introduction node.
87
        finalCLTV uint16
88
}
89

90
// NewBlindedPaymentPathSet constructs a new BlindedPaymentPathSet from a set of
91
// BlindedPayments. For blinded paths which have more than one single hop a
92
// dummy hop via a NUMS key is appeneded to allow for MPP path finding via
93
// multiple blinded paths.
94
func NewBlindedPaymentPathSet(paths []*BlindedPayment) (*BlindedPaymentPathSet,
95
        error) {
3✔
96

3✔
97
        if len(paths) == 0 {
3✔
98
                return nil, ErrNoBlindedPath
×
99
        }
×
100

101
        // For now, we assert that all the paths have the same set of features.
102
        features := paths[0].Features
3✔
103
        noFeatures := features == nil || features.IsEmpty()
3✔
104
        for i := 1; i < len(paths); i++ {
6✔
105
                noFeats := paths[i].Features == nil ||
3✔
106
                        paths[i].Features.IsEmpty()
3✔
107

3✔
108
                if noFeatures && !noFeats {
3✔
109
                        return nil, fmt.Errorf("all blinded paths must have " +
×
110
                                "the same set of features")
×
111
                }
×
112

113
                if noFeatures {
6✔
114
                        continue
3✔
115
                }
116

117
                if !features.RawFeatureVector.Equals(
×
118
                        paths[i].Features.RawFeatureVector,
×
119
                ) {
×
120

×
121
                        return nil, fmt.Errorf("all blinded paths must have " +
×
122
                                "the same set of features")
×
123
                }
×
124
        }
125

126
        // Deep copy the paths to avoid mutating the original paths.
127
        pathSet := make([]*BlindedPayment, len(paths))
3✔
128
        for i, path := range paths {
6✔
129
                pathSet[i] = path.deepCopy()
3✔
130
        }
3✔
131

132
        // For blinded paths we use the NUMS key as a target if the blinded
133
        // path has more hops than just the introduction node.
134
        targetPub := &BlindedPathNUMSKey
3✔
135

3✔
136
        var finalCLTVDelta uint16
3✔
137

3✔
138
        // In case the paths do NOT include a single hop route we append a
3✔
139
        // dummy hop via a NUMS key to allow for MPP path finding via multiple
3✔
140
        // blinded paths. A unified target is needed to use all blinded paths
3✔
141
        // during the payment lifecycle. A dummy hop is solely added for the
3✔
142
        // path finding process and is removed after the path is found. This
3✔
143
        // ensures that we still populate the mission control with the correct
3✔
144
        // data and also respect these mc entries when looking for a path.
3✔
145
        for _, path := range pathSet {
6✔
146
                pathLength := len(path.BlindedPath.BlindedHops)
3✔
147

3✔
148
                // If any provided blinded path only has a single hop (ie, the
3✔
149
                // destination node is also the introduction node), then we
3✔
150
                // discard all other paths since we know the real pub key of the
3✔
151
                // destination node. We also then set the final CLTV delta to
3✔
152
                // the path's delta since there are no other edge hints that
3✔
153
                // will account for it.
3✔
154
                if pathLength == 1 {
6✔
155
                        pathSet = []*BlindedPayment{path}
3✔
156
                        finalCLTVDelta = path.CltvExpiryDelta
3✔
157
                        targetPub = path.BlindedPath.IntroductionPoint
3✔
158

3✔
159
                        break
3✔
160
                }
161

162
                lastHop := path.BlindedPath.BlindedHops[pathLength-1]
3✔
163
                path.BlindedPath.BlindedHops = append(
3✔
164
                        path.BlindedPath.BlindedHops,
3✔
165
                        &sphinx.BlindedHopInfo{
3✔
166
                                BlindedNodePub: &BlindedPathNUMSKey,
3✔
167
                                // We add the last hop's cipher text so that
3✔
168
                                // the payload size of the final hop is equal
3✔
169
                                // to the real last hop.
3✔
170
                                CipherText: lastHop.CipherText,
3✔
171
                        },
3✔
172
                )
3✔
173
        }
174

175
        return &BlindedPaymentPathSet{
3✔
176
                paths:        pathSet,
3✔
177
                targetPubKey: targetPub,
3✔
178
                features:     features,
3✔
179
                finalCLTV:    finalCLTVDelta,
3✔
180
        }, nil
3✔
181
}
182

183
// TargetPubKey returns the public key to be used as the destination node's
184
// public key during pathfinding.
185
func (s *BlindedPaymentPathSet) TargetPubKey() *btcec.PublicKey {
3✔
186
        return s.targetPubKey
3✔
187
}
3✔
188

189
// Features returns the set of relay features available for the payment.
190
func (s *BlindedPaymentPathSet) Features() *lnwire.FeatureVector {
3✔
191
        return s.features
3✔
192
}
3✔
193

194
// IntroNodeOnlyPath can be called if it is expected that the path set only
195
// contains a single payment path which itself only has one hop. It errors if
196
// this is not the case.
197
func (s *BlindedPaymentPathSet) IntroNodeOnlyPath() (*BlindedPayment, error) {
3✔
198
        if len(s.paths) != 1 {
3✔
199
                return nil, fmt.Errorf("expected only a single path in the "+
×
200
                        "blinded payment set, got %d", len(s.paths))
×
201
        }
×
202

203
        if len(s.paths[0].BlindedPath.BlindedHops) > 1 {
3✔
204
                return nil, fmt.Errorf("an intro node only path cannot have " +
×
205
                        "more than one hop")
×
206
        }
×
207

208
        return s.paths[0], nil
3✔
209
}
210

211
// IsIntroNode returns true if the given vertex is an introduction node for one
212
// of the paths in the blinded payment path set.
213
func (s *BlindedPaymentPathSet) IsIntroNode(source route.Vertex) bool {
3✔
214
        for _, path := range s.paths {
6✔
215
                introVertex := route.NewVertex(
3✔
216
                        path.BlindedPath.IntroductionPoint,
3✔
217
                )
3✔
218
                if source == introVertex {
3✔
219
                        return true
×
220
                }
×
221
        }
222

223
        return false
3✔
224
}
225

226
// FinalCLTVDelta is the minimum CLTV delta to use for the final hop on the
227
// route. In most cases this will return zero since the value is accounted for
228
// in the path's accumulated CLTVExpiryDelta. Only in the edge case of the path
229
// set only including a single path which only includes an introduction node
230
// will this return a non-zero value.
231
func (s *BlindedPaymentPathSet) FinalCLTVDelta() uint16 {
3✔
232
        return s.finalCLTV
3✔
233
}
3✔
234

235
// LargestLastHopPayloadPath returns the BlindedPayment in the set that has the
236
// largest last-hop payload. This is to be used for onion size estimation in
237
// path finding.
238
func (s *BlindedPaymentPathSet) LargestLastHopPayloadPath() (*BlindedPayment,
239
        error) {
3✔
240

3✔
241
        var (
3✔
242
                largestPath *BlindedPayment
3✔
243
                currentMax  int
3✔
244
        )
3✔
245

3✔
246
        if len(s.paths) == 0 {
3✔
247
                return nil, fmt.Errorf("no blinded paths in the set")
×
248
        }
×
249

250
        // We set the largest path to make sure we always return a path even
251
        // if the cipher text is empty.
252
        largestPath = s.paths[0]
3✔
253

3✔
254
        for _, path := range s.paths {
6✔
255
                numHops := len(path.BlindedPath.BlindedHops)
3✔
256
                lastHop := path.BlindedPath.BlindedHops[numHops-1]
3✔
257

3✔
258
                if len(lastHop.CipherText) > currentMax {
6✔
259
                        largestPath = path
3✔
260
                        currentMax = len(lastHop.CipherText)
3✔
261
                }
3✔
262
        }
263

264
        return largestPath, nil
3✔
265
}
266

267
// ToRouteHints converts the blinded path payment set into a RouteHints map so
268
// that the blinded payment paths can be treated like route hints throughout the
269
// code base.
270
func (s *BlindedPaymentPathSet) ToRouteHints() (RouteHints, error) {
3✔
271
        hints := make(RouteHints)
3✔
272

3✔
273
        for _, path := range s.paths {
6✔
274
                pathHints, err := path.toRouteHints()
3✔
275
                if err != nil {
3✔
276
                        return nil, err
×
277
                }
×
278

279
                for from, edges := range pathHints {
6✔
280
                        hints[from] = append(hints[from], edges...)
3✔
281
                }
3✔
282
        }
283

284
        if len(hints) == 0 {
6✔
285
                return nil, nil
3✔
286
        }
3✔
287

288
        return hints, nil
3✔
289
}
290

291
// IsBlindedRouteNUMSTargetKey returns true if the given public key is the
292
// NUMS key used as a target for blinded path final hops.
293
func IsBlindedRouteNUMSTargetKey(pk []byte) bool {
3✔
294
        return bytes.Equal(pk, CompressedBlindedPathNUMSKey)
3✔
295
}
3✔
296

297
// BlindedPayment provides the path and payment parameters required to send a
298
// payment along a blinded path.
299
type BlindedPayment struct {
300
        // BlindedPath contains the unblinded introduction point and blinded
301
        // hops for the blinded section of the payment.
302
        BlindedPath *sphinx.BlindedPath
303

304
        // BaseFee is the total base fee to be paid for payments made over the
305
        // blinded path.
306
        BaseFee uint32
307

308
        // ProportionalFeeRate is the aggregated proportional fee rate for
309
        // payments made over the blinded path.
310
        ProportionalFeeRate uint32
311

312
        // CltvExpiryDelta is the total expiry delta for the blinded path. This
313
        // field includes the CLTV for the blinded hops *and* the final cltv
314
        // delta for the receiver.
315
        CltvExpiryDelta uint16
316

317
        // HtlcMinimum is the highest HLTC minimum supported along the blinded
318
        // path (while some hops may have lower values, we're effectively
319
        // bounded by the highest minimum).
320
        HtlcMinimum uint64
321

322
        // HtlcMaximum is the lowest HTLC maximum supported along the blinded
323
        // path (while some hops may have higher values, we're effectively
324
        // bounded by the lowest maximum).
325
        HtlcMaximum uint64
326

327
        // Features is the set of relay features available for the payment.
328
        Features *lnwire.FeatureVector
329
}
330

331
// Validate performs validation on a blinded payment.
332
func (b *BlindedPayment) Validate() error {
3✔
333
        if b.BlindedPath == nil {
3✔
334
                return ErrNoBlindedPath
×
335
        }
×
336

337
        // The sphinx library inserts the introduction node as the first hop,
338
        // so we expect at least one hop.
339
        if len(b.BlindedPath.BlindedHops) < 1 {
3✔
340
                return fmt.Errorf("%w got: %v", ErrInsufficientBlindedHops,
×
341
                        len(b.BlindedPath.BlindedHops))
×
342
        }
×
343

344
        if b.HtlcMaximum < b.HtlcMinimum {
3✔
345
                return fmt.Errorf("%w: %v < %v", ErrHTLCRestrictions,
×
346
                        b.HtlcMaximum, b.HtlcMinimum)
×
347
        }
×
348

349
        for _, hop := range b.BlindedPath.BlindedHops {
6✔
350
                // The first hop of the blinded path does not necessarily have
3✔
351
                // blinded node pub key because it is the introduction point.
3✔
352
                if hop.BlindedNodePub == nil {
3✔
353
                        continue
×
354
                }
355

356
                if IsBlindedRouteNUMSTargetKey(
3✔
357
                        hop.BlindedNodePub.SerializeCompressed(),
3✔
358
                ) {
3✔
359

×
360
                        return fmt.Errorf("blinded path cannot include NUMS "+
×
361
                                "key: %s", BlindedPathNUMSHex)
×
362
                }
×
363
        }
364

365
        return nil
3✔
366
}
367

368
// toRouteHints produces a set of chained route hints that represent a blinded
369
// path. In the case of a single hop blinded route (which is paying directly
370
// to the introduction point), no hints will be returned. In this case callers
371
// *must* account for the blinded route's CLTV delta elsewhere (as this is
372
// effectively the final_cltv_delta for the receiving introduction node). In
373
// the case of multiple blinded hops, CLTV delta is fully accounted for in the
374
// hints (both for intermediate hops and the final_cltv_delta for the receiving
375
// node).
376
func (b *BlindedPayment) toRouteHints() (RouteHints, error) {
3✔
377
        // If we just have a single hop in our blinded route, it just contains
3✔
378
        // an introduction node (this is a valid path according to the spec).
3✔
379
        // Since we have the un-blinded node ID for the introduction node, we
3✔
380
        // don't need to add any route hints.
3✔
381
        if len(b.BlindedPath.BlindedHops) == 1 {
6✔
382
                return nil, nil
3✔
383
        }
3✔
384

385
        hintCount := len(b.BlindedPath.BlindedHops) - 1
3✔
386
        hints := make(
3✔
387
                RouteHints, hintCount,
3✔
388
        )
3✔
389

3✔
390
        // Start at the unblinded introduction node, because our pathfinding
3✔
391
        // will be able to locate this point in the graph.
3✔
392
        fromNode := route.NewVertex(b.BlindedPath.IntroductionPoint)
3✔
393

3✔
394
        features := lnwire.EmptyFeatureVector()
3✔
395
        if b.Features != nil {
6✔
396
                features = b.Features.Clone()
3✔
397
        }
3✔
398

399
        // Use the total aggregate relay parameters for the entire blinded
400
        // route as the policy for the hint from our introduction node. This
401
        // will ensure that pathfinding provides sufficient fees/delay for the
402
        // blinded portion to the introduction node.
403
        firstBlindedHop := b.BlindedPath.BlindedHops[1].BlindedNodePub
3✔
404
        edgePolicy := &models.CachedEdgePolicy{
3✔
405
                TimeLockDelta: b.CltvExpiryDelta,
3✔
406
                MinHTLC:       lnwire.MilliSatoshi(b.HtlcMinimum),
3✔
407
                MaxHTLC:       lnwire.MilliSatoshi(b.HtlcMaximum),
3✔
408
                FeeBaseMSat:   lnwire.MilliSatoshi(b.BaseFee),
3✔
409
                FeeProportionalMillionths: lnwire.MilliSatoshi(
3✔
410
                        b.ProportionalFeeRate,
3✔
411
                ),
3✔
412
                ToNodePubKey: func() route.Vertex {
6✔
413
                        return route.NewVertex(
3✔
414
                                // The first node in this slice is
3✔
415
                                // the introduction node, so we start
3✔
416
                                // at index 1 to get the first blinded
3✔
417
                                // relaying node.
3✔
418
                                firstBlindedHop,
3✔
419
                        )
3✔
420
                },
3✔
421
                ToNodeFeatures: features,
422
        }
423

424
        lastEdge, err := NewBlindedEdge(edgePolicy, b, 0)
3✔
425
        if err != nil {
3✔
426
                return nil, err
×
427
        }
×
428

429
        hints[fromNode] = []AdditionalEdge{lastEdge}
3✔
430

3✔
431
        // Start at an offset of 1 because the first node in our blinded hops
3✔
432
        // is the introduction node and terminate at the second-last node
3✔
433
        // because we're dealing with hops as pairs.
3✔
434
        for i := 1; i < hintCount; i++ {
6✔
435
                // Set our origin node to the current
3✔
436
                fromNode = route.NewVertex(
3✔
437
                        b.BlindedPath.BlindedHops[i].BlindedNodePub,
3✔
438
                )
3✔
439

3✔
440
                // Create a hint which has no fee or cltv delta. We
3✔
441
                // specifically want zero values here because our relay
3✔
442
                // parameters are expressed in encrypted blobs rather than the
3✔
443
                // route itself for blinded routes.
3✔
444
                nextHopIdx := i + 1
3✔
445
                nextNode := route.NewVertex(
3✔
446
                        b.BlindedPath.BlindedHops[nextHopIdx].BlindedNodePub,
3✔
447
                )
3✔
448

3✔
449
                edgePolicy := &models.CachedEdgePolicy{
3✔
450
                        ToNodePubKey: func() route.Vertex {
6✔
451
                                return nextNode
3✔
452
                        },
3✔
453
                        ToNodeFeatures: features,
454
                }
455

456
                lastEdge, err = NewBlindedEdge(edgePolicy, b, i)
3✔
457
                if err != nil {
3✔
458
                        return nil, err
×
459
                }
×
460

461
                hints[fromNode] = []AdditionalEdge{lastEdge}
3✔
462
        }
463

464
        return hints, nil
3✔
465
}
466

467
// deepCopy returns a deep copy of the BlindedPayment.
468
func (b *BlindedPayment) deepCopy() *BlindedPayment {
3✔
469
        if b == nil {
3✔
470
                return nil
×
471
        }
×
472

473
        cpyPayment := &BlindedPayment{
3✔
474
                BaseFee:             b.BaseFee,
3✔
475
                ProportionalFeeRate: b.ProportionalFeeRate,
3✔
476
                CltvExpiryDelta:     b.CltvExpiryDelta,
3✔
477
                HtlcMinimum:         b.HtlcMinimum,
3✔
478
                HtlcMaximum:         b.HtlcMaximum,
3✔
479
        }
3✔
480

3✔
481
        // Deep copy the BlindedPath if it exists
3✔
482
        if b.BlindedPath != nil {
6✔
483
                cpyPayment.BlindedPath = &sphinx.BlindedPath{
3✔
484
                        BlindedHops: make([]*sphinx.BlindedHopInfo,
3✔
485
                                len(b.BlindedPath.BlindedHops)),
3✔
486
                }
3✔
487

3✔
488
                if b.BlindedPath.IntroductionPoint != nil {
6✔
489
                        cpyPayment.BlindedPath.IntroductionPoint =
3✔
490
                                copyPublicKey(b.BlindedPath.IntroductionPoint)
3✔
491
                }
3✔
492

493
                if b.BlindedPath.BlindingPoint != nil {
6✔
494
                        cpyPayment.BlindedPath.BlindingPoint =
3✔
495
                                copyPublicKey(b.BlindedPath.BlindingPoint)
3✔
496
                }
3✔
497

498
                // Copy each blinded hop info.
499
                for i, hop := range b.BlindedPath.BlindedHops {
6✔
500
                        if hop == nil {
3✔
501
                                continue
×
502
                        }
503

504
                        cpyHop := &sphinx.BlindedHopInfo{
3✔
505
                                CipherText: hop.CipherText,
3✔
506
                        }
3✔
507

3✔
508
                        if hop.BlindedNodePub != nil {
6✔
509
                                cpyHop.BlindedNodePub =
3✔
510
                                        copyPublicKey(hop.BlindedNodePub)
3✔
511
                        }
3✔
512

513
                        cpyHop.CipherText = make([]byte, len(hop.CipherText))
3✔
514
                        copy(cpyHop.CipherText, hop.CipherText)
3✔
515

3✔
516
                        cpyPayment.BlindedPath.BlindedHops[i] = cpyHop
3✔
517
                }
518
        }
519

520
        // Deep copy the Features if they exist
521
        if b.Features != nil {
6✔
522
                cpyPayment.Features = b.Features.Clone()
3✔
523
        }
3✔
524

525
        return cpyPayment
3✔
526
}
527

528
// copyPublicKey makes a deep copy of a public key.
529
//
530
// TODO(ziggie): Remove this function if this is available in the btcec library.
531
func copyPublicKey(pk *btcec.PublicKey) *btcec.PublicKey {
3✔
532
        var result secp256k1.JacobianPoint
3✔
533
        pk.AsJacobian(&result)
3✔
534
        result.ToAffine()
3✔
535

3✔
536
        return btcec.NewPublicKey(&result.X, &result.Y)
3✔
537
}
3✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc