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

lightningnetwork / lnd / 15736109134

18 Jun 2025 02:46PM UTC coverage: 58.197% (-10.1%) from 68.248%
15736109134

Pull #9752

github

web-flow
Merge d2634a68c into 31c74f20f
Pull Request #9752: routerrpc: reject payment to invoice that don't have payment secret or blinded paths

6 of 13 new or added lines in 2 files covered. (46.15%)

28331 existing lines in 455 files now uncovered.

97860 of 168153 relevant lines covered (58.2%)

1.81 hits per line

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

85.53
/lnwallet/commitment.go
1
package lnwallet
2

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

7
        "github.com/btcsuite/btcd/blockchain"
8
        "github.com/btcsuite/btcd/btcec/v2"
9
        "github.com/btcsuite/btcd/btcutil"
10
        "github.com/btcsuite/btcd/chaincfg/chainhash"
11
        "github.com/btcsuite/btcd/txscript"
12
        "github.com/btcsuite/btcd/wire"
13
        "github.com/lightningnetwork/lnd/channeldb"
14
        "github.com/lightningnetwork/lnd/fn/v2"
15
        "github.com/lightningnetwork/lnd/input"
16
        "github.com/lightningnetwork/lnd/lntypes"
17
        "github.com/lightningnetwork/lnd/lnwallet/chainfee"
18
        "github.com/lightningnetwork/lnd/lnwire"
19
)
20

21
// AnchorSize is the constant anchor output size.
22
const AnchorSize = btcutil.Amount(330)
23

24
// DefaultAnchorsCommitMaxFeeRateSatPerVByte is the default max fee rate in
25
// sat/vbyte the initiator will use for anchor channels. This should be enough
26
// to ensure propagation before anchoring down the commitment transaction.
27
const DefaultAnchorsCommitMaxFeeRateSatPerVByte = 10
28

29
// CommitmentKeyRing holds all derived keys needed to construct commitment and
30
// HTLC transactions. The keys are derived differently depending whether the
31
// commitment transaction is ours or the remote peer's. Private keys associated
32
// with each key may belong to the commitment owner or the "other party" which
33
// is referred to in the field comments, regardless of which is local and which
34
// is remote.
35
type CommitmentKeyRing struct {
36
        // CommitPoint is the "per commitment point" used to derive the tweak
37
        // for each base point.
38
        CommitPoint *btcec.PublicKey
39

40
        // LocalCommitKeyTweak is the tweak used to derive the local public key
41
        // from the local payment base point or the local private key from the
42
        // base point secret. This may be included in a SignDescriptor to
43
        // generate signatures for the local payment key.
44
        //
45
        // NOTE: This will always refer to "our" local key, regardless of
46
        // whether this is our commit or not.
47
        LocalCommitKeyTweak []byte
48

49
        // TODO(roasbeef): need delay tweak as well?
50

51
        // LocalHtlcKeyTweak is the tweak used to derive the local HTLC key
52
        // from the local HTLC base point. This value is needed in order to
53
        // derive the final key used within the HTLC scripts in the commitment
54
        // transaction.
55
        //
56
        // NOTE: This will always refer to "our" local HTLC key, regardless of
57
        // whether this is our commit or not.
58
        LocalHtlcKeyTweak []byte
59

60
        // LocalHtlcKey is the key that will be used in any clause paying to
61
        // our node of any HTLC scripts within the commitment transaction for
62
        // this key ring set.
63
        //
64
        // NOTE: This will always refer to "our" local HTLC key, regardless of
65
        // whether this is our commit or not.
66
        LocalHtlcKey *btcec.PublicKey
67

68
        // RemoteHtlcKey is the key that will be used in clauses within the
69
        // HTLC script that send money to the remote party.
70
        //
71
        // NOTE: This will always refer to "their" remote HTLC key, regardless
72
        // of whether this is our commit or not.
73
        RemoteHtlcKey *btcec.PublicKey
74

75
        // ToLocalKey is the commitment transaction owner's key which is
76
        // included in HTLC success and timeout transaction scripts. This is
77
        // the public key used for the to_local output of the commitment
78
        // transaction.
79
        //
80
        // NOTE: Who's key this is depends on the current perspective. If this
81
        // is our commitment this will be our key.
82
        ToLocalKey *btcec.PublicKey
83

84
        // ToRemoteKey is the non-owner's payment key in the commitment tx.
85
        // This is the key used to generate the to_remote output within the
86
        // commitment transaction.
87
        //
88
        // NOTE: Who's key this is depends on the current perspective. If this
89
        // is our commitment this will be their key.
90
        ToRemoteKey *btcec.PublicKey
91

92
        // RevocationKey is the key that can be used by the other party to
93
        // redeem outputs from a revoked commitment transaction if it were to
94
        // be published.
95
        //
96
        // NOTE: Who can sign for this key depends on the current perspective.
97
        // If this is our commitment, it means the remote node can sign for
98
        // this key in case of a breach.
99
        RevocationKey *btcec.PublicKey
100
}
101

102
// DeriveCommitmentKeys generates a new commitment key set using the base points
103
// and commitment point. The keys are derived differently depending on the type
104
// of channel, and whether the commitment transaction is ours or the remote
105
// peer's.
106
func DeriveCommitmentKeys(commitPoint *btcec.PublicKey,
107
        whoseCommit lntypes.ChannelParty, chanType channeldb.ChannelType,
108
        localChanCfg, remoteChanCfg *channeldb.ChannelConfig) *CommitmentKeyRing {
3✔
109

3✔
110
        tweaklessCommit := chanType.IsTweakless()
3✔
111

3✔
112
        // Depending on if this is our commit or not, we'll choose the correct
3✔
113
        // base point.
3✔
114
        localBasePoint := localChanCfg.PaymentBasePoint
3✔
115
        if whoseCommit.IsLocal() {
6✔
116
                localBasePoint = localChanCfg.DelayBasePoint
3✔
117
        }
3✔
118

119
        // First, we'll derive all the keys that don't depend on the context of
120
        // whose commitment transaction this is.
121
        keyRing := &CommitmentKeyRing{
3✔
122
                CommitPoint: commitPoint,
3✔
123

3✔
124
                LocalCommitKeyTweak: input.SingleTweakBytes(
3✔
125
                        commitPoint, localBasePoint.PubKey,
3✔
126
                ),
3✔
127
                LocalHtlcKeyTweak: input.SingleTweakBytes(
3✔
128
                        commitPoint, localChanCfg.HtlcBasePoint.PubKey,
3✔
129
                ),
3✔
130
                LocalHtlcKey: input.TweakPubKey(
3✔
131
                        localChanCfg.HtlcBasePoint.PubKey, commitPoint,
3✔
132
                ),
3✔
133
                RemoteHtlcKey: input.TweakPubKey(
3✔
134
                        remoteChanCfg.HtlcBasePoint.PubKey, commitPoint,
3✔
135
                ),
3✔
136
        }
3✔
137

3✔
138
        // We'll now compute the to_local, to_remote, and revocation key based
3✔
139
        // on the current commitment point. All keys are tweaked each state in
3✔
140
        // order to ensure the keys from each state are unlinkable. To create
3✔
141
        // the revocation key, we take the opposite party's revocation base
3✔
142
        // point and combine that with the current commitment point.
3✔
143
        var (
3✔
144
                toLocalBasePoint    *btcec.PublicKey
3✔
145
                toRemoteBasePoint   *btcec.PublicKey
3✔
146
                revocationBasePoint *btcec.PublicKey
3✔
147
        )
3✔
148
        if whoseCommit.IsLocal() {
6✔
149
                toLocalBasePoint = localChanCfg.DelayBasePoint.PubKey
3✔
150
                toRemoteBasePoint = remoteChanCfg.PaymentBasePoint.PubKey
3✔
151
                revocationBasePoint = remoteChanCfg.RevocationBasePoint.PubKey
3✔
152
        } else {
6✔
153
                toLocalBasePoint = remoteChanCfg.DelayBasePoint.PubKey
3✔
154
                toRemoteBasePoint = localChanCfg.PaymentBasePoint.PubKey
3✔
155
                revocationBasePoint = localChanCfg.RevocationBasePoint.PubKey
3✔
156
        }
3✔
157

158
        // With the base points assigned, we can now derive the actual keys
159
        // using the base point, and the current commitment tweak.
160
        keyRing.ToLocalKey = input.TweakPubKey(toLocalBasePoint, commitPoint)
3✔
161
        keyRing.RevocationKey = input.DeriveRevocationPubkey(
3✔
162
                revocationBasePoint, commitPoint,
3✔
163
        )
3✔
164

3✔
165
        // If this commitment should omit the tweak for the remote point, then
3✔
166
        // we'll use that directly, and ignore the commitPoint tweak.
3✔
167
        if tweaklessCommit {
6✔
168
                keyRing.ToRemoteKey = toRemoteBasePoint
3✔
169

3✔
170
                // If this is not our commitment, the above ToRemoteKey will be
3✔
171
                // ours, and we blank out the local commitment tweak to
3✔
172
                // indicate that the key should not be tweaked when signing.
3✔
173
                if whoseCommit.IsRemote() {
6✔
174
                        keyRing.LocalCommitKeyTweak = nil
3✔
175
                }
3✔
176
        } else {
3✔
177
                keyRing.ToRemoteKey = input.TweakPubKey(
3✔
178
                        toRemoteBasePoint, commitPoint,
3✔
179
                )
3✔
180
        }
3✔
181

182
        return keyRing
3✔
183
}
184

185
// WitnessScriptDesc holds the output script and the witness script for p2wsh
186
// outputs.
187
type WitnessScriptDesc struct {
188
        // OutputScript is the output's PkScript.
189
        OutputScript []byte
190

191
        // WitnessScript is the full script required to properly redeem the
192
        // output. This field should be set to the full script if a p2wsh
193
        // output is being signed. For p2wkh it should be set equal to the
194
        // PkScript.
195
        WitnessScript []byte
196
}
197

198
// PkScript is the public key script that commits to the final
199
// contract.
200
func (w *WitnessScriptDesc) PkScript() []byte {
3✔
201
        return w.OutputScript
3✔
202
}
3✔
203

204
// WitnessScriptToSign returns the witness script that we'll use when signing
205
// for the remote party, and also verifying signatures on our transactions. As
206
// an example, when we create an outgoing HTLC for the remote party, we want to
207
// sign their success path.
208
func (w *WitnessScriptDesc) WitnessScriptToSign() []byte {
3✔
209
        return w.WitnessScript
3✔
210
}
3✔
211

212
// WitnessScriptForPath returns the witness script for the given spending path.
213
// An error is returned if the path is unknown. This is useful as when
214
// constructing a control block for a given path, one also needs witness script
215
// being signed.
216
func (w *WitnessScriptDesc) WitnessScriptForPath(
217
        _ input.ScriptPath) ([]byte, error) {
3✔
218

3✔
219
        return w.WitnessScript, nil
3✔
220
}
3✔
221

222
// CommitScriptToSelf constructs the public key script for the output on the
223
// commitment transaction paying to the "owner" of said commitment transaction.
224
// The `initiator` argument should correspond to the owner of the commitment
225
// transaction which we are generating the to_local script for. If the other
226
// party learns of the preimage to the revocation hash, then they can claim all
227
// the settled funds in the channel, plus the unsettled funds.
228
func CommitScriptToSelf(chanType channeldb.ChannelType, initiator bool,
229
        selfKey, revokeKey *btcec.PublicKey, csvDelay, leaseExpiry uint32,
230
        auxLeaf input.AuxTapLeaf) (input.ScriptDescriptor, error) {
3✔
231

3✔
232
        switch {
3✔
233
        // For taproot scripts, we'll need to make a slightly modified script
234
        // where a NUMS key is used to force a script path reveal of either the
235
        // revocation or the CSV timeout.
236
        //
237
        // Our "redeem" script here is just the taproot witness program.
238
        case chanType.IsTaproot():
3✔
239
                return input.NewLocalCommitScriptTree(
3✔
240
                        csvDelay, selfKey, revokeKey, auxLeaf,
3✔
241
                )
3✔
242

243
        // If we are the initiator of a leased channel, then we have an
244
        // additional CLTV requirement in addition to the usual CSV
245
        // requirement.
246
        case initiator && chanType.HasLeaseExpiration():
3✔
247
                toLocalRedeemScript, err := input.LeaseCommitScriptToSelf(
3✔
248
                        selfKey, revokeKey, csvDelay, leaseExpiry,
3✔
249
                )
3✔
250
                if err != nil {
3✔
251
                        return nil, err
×
252
                }
×
253

254
                toLocalScriptHash, err := input.WitnessScriptHash(
3✔
255
                        toLocalRedeemScript,
3✔
256
                )
3✔
257
                if err != nil {
3✔
258
                        return nil, err
×
259
                }
×
260

261
                return &WitnessScriptDesc{
3✔
262
                        OutputScript:  toLocalScriptHash,
3✔
263
                        WitnessScript: toLocalRedeemScript,
3✔
264
                }, nil
3✔
265

266
        default:
3✔
267
                toLocalRedeemScript, err := input.CommitScriptToSelf(
3✔
268
                        csvDelay, selfKey, revokeKey,
3✔
269
                )
3✔
270
                if err != nil {
3✔
271
                        return nil, err
×
272
                }
×
273

274
                toLocalScriptHash, err := input.WitnessScriptHash(
3✔
275
                        toLocalRedeemScript,
3✔
276
                )
3✔
277
                if err != nil {
3✔
278
                        return nil, err
×
279
                }
×
280

281
                return &WitnessScriptDesc{
3✔
282
                        OutputScript:  toLocalScriptHash,
3✔
283
                        WitnessScript: toLocalRedeemScript,
3✔
284
                }, nil
3✔
285
        }
286
}
287

288
// CommitScriptToRemote derives the appropriate to_remote script based on the
289
// channel's commitment type. The `initiator` argument should correspond to the
290
// owner of the commitment transaction which we are generating the to_remote
291
// script for. The second return value is the CSV delay of the output script,
292
// what must be satisfied in order to spend the output.
293
func CommitScriptToRemote(chanType channeldb.ChannelType, initiator bool,
294
        remoteKey *btcec.PublicKey, leaseExpiry uint32,
295
        auxLeaf input.AuxTapLeaf) (input.ScriptDescriptor, uint32, error) {
3✔
296

3✔
297
        switch {
3✔
298
        // If we are not the initiator of a leased channel, then the remote
299
        // party has an additional CLTV requirement in addition to the 1 block
300
        // CSV requirement.
301
        case chanType.HasLeaseExpiration() && !initiator:
3✔
302
                script, err := input.LeaseCommitScriptToRemoteConfirmed(
3✔
303
                        remoteKey, leaseExpiry,
3✔
304
                )
3✔
305
                if err != nil {
3✔
306
                        return nil, 0, err
×
307
                }
×
308

309
                p2wsh, err := input.WitnessScriptHash(script)
3✔
310
                if err != nil {
3✔
311
                        return nil, 0, err
×
312
                }
×
313

314
                return &WitnessScriptDesc{
3✔
315
                        OutputScript:  p2wsh,
3✔
316
                        WitnessScript: script,
3✔
317
                }, 1, nil
3✔
318

319
        // For taproot channels, we'll use a slightly different format, where
320
        // we use a NUMS key to force the remote party to take a script path,
321
        // with the sole tap leaf enforcing the 1 CSV delay.
322
        case chanType.IsTaproot():
3✔
323
                toRemoteScriptTree, err := input.NewRemoteCommitScriptTree(
3✔
324
                        remoteKey, auxLeaf,
3✔
325
                )
3✔
326
                if err != nil {
3✔
327
                        return nil, 0, err
×
328
                }
×
329

330
                return toRemoteScriptTree, 1, nil
3✔
331

332
        // If this channel type has anchors, we derive the delayed to_remote
333
        // script.
334
        case chanType.HasAnchors():
3✔
335
                script, err := input.CommitScriptToRemoteConfirmed(remoteKey)
3✔
336
                if err != nil {
3✔
337
                        return nil, 0, err
×
338
                }
×
339

340
                p2wsh, err := input.WitnessScriptHash(script)
3✔
341
                if err != nil {
3✔
342
                        return nil, 0, err
×
343
                }
×
344

345
                return &WitnessScriptDesc{
3✔
346
                        OutputScript:  p2wsh,
3✔
347
                        WitnessScript: script,
3✔
348
                }, 1, nil
3✔
349

350
        default:
3✔
351
                // Otherwise the to_remote will be a simple p2wkh.
3✔
352
                p2wkh, err := input.CommitScriptUnencumbered(remoteKey)
3✔
353
                if err != nil {
3✔
354
                        return nil, 0, err
×
355
                }
×
356

357
                // Since this is a regular P2WKH, the WitnessScipt and PkScript
358
                // should both be set to the script hash.
359
                return &WitnessScriptDesc{
3✔
360
                        OutputScript:  p2wkh,
3✔
361
                        WitnessScript: p2wkh,
3✔
362
                }, 0, nil
3✔
363
        }
364
}
365

366
// HtlcSigHashType returns the sighash type to use for HTLC success and timeout
367
// transactions given the channel type.
368
func HtlcSigHashType(chanType channeldb.ChannelType) txscript.SigHashType {
3✔
369
        if chanType.HasAnchors() {
6✔
370
                return txscript.SigHashSingle | txscript.SigHashAnyOneCanPay
3✔
371
        }
3✔
372

373
        return txscript.SigHashAll
3✔
374
}
375

376
// HtlcSignDetails converts the passed parameters to a SignDetails valid for
377
// this channel type. For non-anchor channels this will return nil.
378
func HtlcSignDetails(chanType channeldb.ChannelType, signDesc input.SignDescriptor,
379
        sigHash txscript.SigHashType, peerSig input.Signature) *input.SignDetails {
3✔
380

3✔
381
        // Non-anchor channels don't need sign details, as the HTLC second
3✔
382
        // level cannot be altered.
3✔
383
        if !chanType.HasAnchors() {
6✔
384
                return nil
3✔
385
        }
3✔
386

387
        return &input.SignDetails{
3✔
388
                SignDesc:    signDesc,
3✔
389
                SigHashType: sigHash,
3✔
390
                PeerSig:     peerSig,
3✔
391
        }
3✔
392
}
393

394
// HtlcSecondLevelInputSequence dictates the sequence number we must use on the
395
// input to a second level HTLC transaction.
396
func HtlcSecondLevelInputSequence(chanType channeldb.ChannelType) uint32 {
3✔
397
        if chanType.HasAnchors() {
6✔
398
                return 1
3✔
399
        }
3✔
400

401
        return 0
3✔
402
}
403

404
// sweepSigHash returns the sign descriptor to use when signing a sweep
405
// transaction. For taproot channels, we'll use this to always sweep with
406
// sighash default.
407
func sweepSigHash(chanType channeldb.ChannelType) txscript.SigHashType {
3✔
408
        if chanType.IsTaproot() {
6✔
409
                return txscript.SigHashDefault
3✔
410
        }
3✔
411

412
        return txscript.SigHashAll
3✔
413
}
414

415
// SecondLevelHtlcScript derives the appropriate second level HTLC script based
416
// on the channel's commitment type. It is the uniform script that's used as the
417
// output for the second-level HTLC transactions. The second level transaction
418
// act as a sort of covenant, ensuring that a 2-of-2 multi-sig output can only
419
// be spent in a particular way, and to a particular output. The `initiator`
420
// argument should correspond to the owner of the commitment transaction which
421
// we are generating the to_local script for.
422
func SecondLevelHtlcScript(chanType channeldb.ChannelType, initiator bool,
423
        revocationKey, delayKey *btcec.PublicKey, csvDelay, leaseExpiry uint32,
424
        auxLeaf input.AuxTapLeaf) (input.ScriptDescriptor, error) {
3✔
425

3✔
426
        switch {
3✔
427
        // For taproot channels, the pkScript is a segwit v1 p2tr output.
428
        case chanType.IsTaproot():
3✔
429
                return input.TaprootSecondLevelScriptTree(
3✔
430
                        revocationKey, delayKey, csvDelay, auxLeaf,
3✔
431
                )
3✔
432

433
        // If we are the initiator of a leased channel, then we have an
434
        // additional CLTV requirement in addition to the usual CSV
435
        // requirement.
436
        case initiator && chanType.HasLeaseExpiration():
3✔
437
                witnessScript, err := input.LeaseSecondLevelHtlcScript(
3✔
438
                        revocationKey, delayKey, csvDelay, leaseExpiry,
3✔
439
                )
3✔
440
                if err != nil {
3✔
441
                        return nil, err
×
442
                }
×
443

444
                pkScript, err := input.WitnessScriptHash(witnessScript)
3✔
445
                if err != nil {
3✔
446
                        return nil, err
×
447
                }
×
448

449
                return &WitnessScriptDesc{
3✔
450
                        OutputScript:  pkScript,
3✔
451
                        WitnessScript: witnessScript,
3✔
452
                }, nil
3✔
453

454
        default:
3✔
455
                witnessScript, err := input.SecondLevelHtlcScript(
3✔
456
                        revocationKey, delayKey, csvDelay,
3✔
457
                )
3✔
458
                if err != nil {
3✔
459
                        return nil, err
×
460
                }
×
461

462
                pkScript, err := input.WitnessScriptHash(witnessScript)
3✔
463
                if err != nil {
3✔
464
                        return nil, err
×
465
                }
×
466

467
                return &WitnessScriptDesc{
3✔
468
                        OutputScript:  pkScript,
3✔
469
                        WitnessScript: witnessScript,
3✔
470
                }, nil
3✔
471
        }
472
}
473

474
// CommitWeight returns the base commitment weight before adding HTLCs.
475
func CommitWeight(chanType channeldb.ChannelType) lntypes.WeightUnit {
3✔
476
        switch {
3✔
477
        case chanType.IsTaproot():
3✔
478
                return input.TaprootCommitWeight
3✔
479

480
        // If this commitment has anchors, it will be slightly heavier.
481
        case chanType.HasAnchors():
3✔
482
                return input.AnchorCommitWeight
3✔
483

484
        default:
3✔
485
                return input.CommitWeight
3✔
486
        }
487
}
488

489
// HtlcTimeoutFee returns the fee in satoshis required for an HTLC timeout
490
// transaction based on the current fee rate.
491
func HtlcTimeoutFee(chanType channeldb.ChannelType,
492
        feePerKw chainfee.SatPerKWeight) btcutil.Amount {
3✔
493

3✔
494
        switch {
3✔
495
        // For zero-fee HTLC channels, this will always be zero, regardless of
496
        // feerate.
497
        case chanType.ZeroHtlcTxFee() || chanType.IsTaproot():
3✔
498
                return 0
3✔
499

UNCOV
500
        case chanType.HasAnchors():
×
UNCOV
501
                return feePerKw.FeeForWeight(input.HtlcTimeoutWeightConfirmed)
×
502

503
        default:
3✔
504
                return feePerKw.FeeForWeight(input.HtlcTimeoutWeight)
3✔
505
        }
506
}
507

508
// HtlcSuccessFee returns the fee in satoshis required for an HTLC success
509
// transaction based on the current fee rate.
510
func HtlcSuccessFee(chanType channeldb.ChannelType,
511
        feePerKw chainfee.SatPerKWeight) btcutil.Amount {
3✔
512

3✔
513
        switch {
3✔
514
        // For zero-fee HTLC channels, this will always be zero, regardless of
515
        // feerate.
516
        case chanType.ZeroHtlcTxFee() || chanType.IsTaproot():
3✔
517
                return 0
3✔
518

UNCOV
519
        case chanType.HasAnchors():
×
UNCOV
520
                return feePerKw.FeeForWeight(input.HtlcSuccessWeightConfirmed)
×
521

522
        default:
3✔
523
                return feePerKw.FeeForWeight(input.HtlcSuccessWeight)
3✔
524
        }
525
}
526

527
// CommitScriptAnchors return the scripts to use for the local and remote
528
// anchor.
529
func CommitScriptAnchors(chanType channeldb.ChannelType,
530
        localChanCfg, remoteChanCfg *channeldb.ChannelConfig,
531
        keyRing *CommitmentKeyRing) (
532
        input.ScriptDescriptor, input.ScriptDescriptor, error) {
3✔
533

3✔
534
        var (
3✔
535
                anchorScript func(
3✔
536
                        key *btcec.PublicKey) (input.ScriptDescriptor, error)
3✔
537

3✔
538
                keySelector func(*channeldb.ChannelConfig,
3✔
539
                        bool) *btcec.PublicKey
3✔
540
        )
3✔
541

3✔
542
        switch {
3✔
543
        // For taproot channels, the anchor is slightly different: the top
544
        // level key is now the (relative) local delay and remote public key,
545
        // since these are fully revealed once the commitment hits the chain.
546
        case chanType.IsTaproot():
3✔
547
                anchorScript = func(
3✔
548
                        key *btcec.PublicKey) (input.ScriptDescriptor, error) {
6✔
549

3✔
550
                        return input.NewAnchorScriptTree(key)
3✔
551
                }
3✔
552

553
                keySelector = func(cfg *channeldb.ChannelConfig,
3✔
554
                        local bool) *btcec.PublicKey {
6✔
555

3✔
556
                        if local {
6✔
557
                                return keyRing.ToLocalKey
3✔
558
                        }
3✔
559

560
                        return keyRing.ToRemoteKey
3✔
561
                }
562

563
        // For normal channels we'll use the multi-sig keys since those are
564
        // revealed when the channel closes
565
        default:
3✔
566
                // For normal channels, we'll create a p2wsh script based on
3✔
567
                // the target key.
3✔
568
                anchorScript = func(
3✔
569
                        key *btcec.PublicKey) (input.ScriptDescriptor, error) {
6✔
570

3✔
571
                        script, err := input.CommitScriptAnchor(key)
3✔
572
                        if err != nil {
3✔
573
                                return nil, err
×
574
                        }
×
575

576
                        scriptHash, err := input.WitnessScriptHash(script)
3✔
577
                        if err != nil {
3✔
578
                                return nil, err
×
579
                        }
×
580

581
                        return &WitnessScriptDesc{
3✔
582
                                OutputScript:  scriptHash,
3✔
583
                                WitnessScript: script,
3✔
584
                        }, nil
3✔
585
                }
586

587
                // For the existing channels, we'll always select the multi-sig
588
                // key from the party's channel config.
589
                keySelector = func(cfg *channeldb.ChannelConfig,
3✔
590
                        _ bool) *btcec.PublicKey {
6✔
591

3✔
592
                        return cfg.MultiSigKey.PubKey
3✔
593
                }
3✔
594
        }
595

596
        // Get the script used for the anchor output spendable by the local
597
        // node.
598
        localAnchor, err := anchorScript(keySelector(localChanCfg, true))
3✔
599
        if err != nil {
3✔
600
                return nil, nil, err
×
601
        }
×
602

603
        // And the anchor spendable by the remote node.
604
        remoteAnchor, err := anchorScript(keySelector(remoteChanCfg, false))
3✔
605
        if err != nil {
3✔
606
                return nil, nil, err
×
607
        }
×
608

609
        return localAnchor, remoteAnchor, nil
3✔
610
}
611

612
// CommitmentBuilder is a type that wraps the type of channel we are dealing
613
// with, and abstracts the various ways of constructing commitment
614
// transactions.
615
type CommitmentBuilder struct {
616
        // chanState is the underlying channel's state struct, used to
617
        // determine the type of channel we are dealing with, and relevant
618
        // parameters.
619
        chanState *channeldb.OpenChannel
620

621
        // obfuscator is a 48-bit state hint that's used to obfuscate the
622
        // current state number on the commitment transactions.
623
        obfuscator [StateHintSize]byte
624

625
        // auxLeafStore is an interface that allows us to fetch auxiliary
626
        // tapscript leaves for the commitment output.
627
        auxLeafStore fn.Option[AuxLeafStore]
628
}
629

630
// NewCommitmentBuilder creates a new CommitmentBuilder from chanState.
631
func NewCommitmentBuilder(chanState *channeldb.OpenChannel,
632
        leafStore fn.Option[AuxLeafStore]) *CommitmentBuilder {
3✔
633

3✔
634
        // The anchor channel type MUST be tweakless.
3✔
635
        if chanState.ChanType.HasAnchors() && !chanState.ChanType.IsTweakless() {
3✔
636
                panic("invalid channel type combination")
×
637
        }
638

639
        return &CommitmentBuilder{
3✔
640
                chanState:    chanState,
3✔
641
                obfuscator:   createStateHintObfuscator(chanState),
3✔
642
                auxLeafStore: leafStore,
3✔
643
        }
3✔
644
}
645

646
// createStateHintObfuscator derives and assigns the state hint obfuscator for
647
// the channel, which is used to encode the commitment height in the sequence
648
// number of commitment transaction inputs.
649
func createStateHintObfuscator(state *channeldb.OpenChannel) [StateHintSize]byte {
3✔
650
        if state.IsInitiator {
6✔
651
                return DeriveStateHintObfuscator(
3✔
652
                        state.LocalChanCfg.PaymentBasePoint.PubKey,
3✔
653
                        state.RemoteChanCfg.PaymentBasePoint.PubKey,
3✔
654
                )
3✔
655
        }
3✔
656

657
        return DeriveStateHintObfuscator(
3✔
658
                state.RemoteChanCfg.PaymentBasePoint.PubKey,
3✔
659
                state.LocalChanCfg.PaymentBasePoint.PubKey,
3✔
660
        )
3✔
661
}
662

663
// unsignedCommitmentTx is the final commitment created from evaluating an HTLC
664
// view at a given height, along with some meta data.
665
type unsignedCommitmentTx struct {
666
        // txn is the final, unsigned commitment transaction for this view.
667
        txn *wire.MsgTx
668

669
        // fee is the total fee of the commitment transaction.
670
        fee btcutil.Amount
671

672
        // ourBalance is our balance on this commitment *after* subtracting
673
        // commitment fees and anchor outputs. This can be different than the
674
        // balances before creating the commitment transaction as one party must
675
        // pay the commitment fee.
676
        ourBalance lnwire.MilliSatoshi
677

678
        // theirBalance is their balance of this commitment *after* subtracting
679
        // commitment fees and anchor outputs. This can be different than the
680
        // balances before creating the commitment transaction as one party must
681
        // pay the commitment fee.
682
        theirBalance lnwire.MilliSatoshi
683

684
        // cltvs is a sorted list of CLTV deltas for each HTLC on the commitment
685
        // transaction. Any non-htlc outputs will have a CLTV delay of zero.
686
        cltvs []uint32
687
}
688

689
// createUnsignedCommitmentTx generates the unsigned commitment transaction for
690
// a commitment view and returns it as part of the unsignedCommitmentTx. The
691
// passed in balances should be balances *before* subtracting any commitment
692
// fees, but after anchor outputs.
693
func (cb *CommitmentBuilder) createUnsignedCommitmentTx(ourBalance,
694
        theirBalance lnwire.MilliSatoshi, whoseCommit lntypes.ChannelParty,
695
        feePerKw chainfee.SatPerKWeight, height uint64, originalHtlcView,
696
        filteredHTLCView *HtlcView, keyRing *CommitmentKeyRing,
697
        prevCommit *commitment) (*unsignedCommitmentTx, error) {
3✔
698

3✔
699
        dustLimit := cb.chanState.LocalChanCfg.DustLimit
3✔
700
        if whoseCommit.IsRemote() {
6✔
701
                dustLimit = cb.chanState.RemoteChanCfg.DustLimit
3✔
702
        }
3✔
703

704
        numHTLCs := int64(0)
3✔
705
        for _, htlc := range filteredHTLCView.Updates.Local {
6✔
706
                if HtlcIsDust(
3✔
707
                        cb.chanState.ChanType, false, whoseCommit, feePerKw,
3✔
708
                        htlc.Amount.ToSatoshis(), dustLimit,
3✔
709
                ) {
6✔
710

3✔
711
                        continue
3✔
712
                }
713

714
                numHTLCs++
3✔
715
        }
716
        for _, htlc := range filteredHTLCView.Updates.Remote {
6✔
717
                if HtlcIsDust(
3✔
718
                        cb.chanState.ChanType, true, whoseCommit, feePerKw,
3✔
719
                        htlc.Amount.ToSatoshis(), dustLimit,
3✔
720
                ) {
6✔
721

3✔
722
                        continue
3✔
723
                }
724

725
                numHTLCs++
3✔
726
        }
727

728
        // Next, we'll calculate the fee for the commitment transaction based
729
        // on its total weight. Once we have the total weight, we'll multiply
730
        // by the current fee-per-kw, then divide by 1000 to get the proper
731
        // fee.
732
        totalCommitWeight := CommitWeight(cb.chanState.ChanType) +
3✔
733
                lntypes.WeightUnit(input.HTLCWeight*numHTLCs)
3✔
734

3✔
735
        // With the weight known, we can now calculate the commitment fee,
3✔
736
        // ensuring that we account for any dust outputs trimmed above.
3✔
737
        commitFee := feePerKw.FeeForWeight(totalCommitWeight)
3✔
738
        commitFeeMSat := lnwire.NewMSatFromSatoshis(commitFee)
3✔
739

3✔
740
        // Currently, within the protocol, the initiator always pays the fees.
3✔
741
        // So we'll subtract the fee amount from the balance of the current
3✔
742
        // initiator. If the initiator is unable to pay the fee fully, then
3✔
743
        // their entire output is consumed.
3✔
744
        switch {
3✔
745
        case cb.chanState.IsInitiator && commitFee > ourBalance.ToSatoshis():
×
746
                ourBalance = 0
×
747

748
        case cb.chanState.IsInitiator:
3✔
749
                ourBalance -= commitFeeMSat
3✔
750

751
        case !cb.chanState.IsInitiator && commitFee > theirBalance.ToSatoshis():
×
752
                theirBalance = 0
×
753

754
        case !cb.chanState.IsInitiator:
3✔
755
                theirBalance -= commitFeeMSat
3✔
756
        }
757

758
        var commitTx *wire.MsgTx
3✔
759

3✔
760
        // Before we create the commitment transaction below, we'll try to see
3✔
761
        // if there're any aux leaves that need to be a part of the tapscript
3✔
762
        // tree. We'll only do this if we have a custom blob defined though.
3✔
763
        auxResult, err := fn.MapOptionZ(
3✔
764
                cb.auxLeafStore,
3✔
765
                func(s AuxLeafStore) fn.Result[CommitDiffAuxResult] {
3✔
UNCOV
766
                        return auxLeavesFromView(
×
UNCOV
767
                                s, cb.chanState, prevCommit.customBlob,
×
UNCOV
768
                                originalHtlcView, whoseCommit, ourBalance,
×
UNCOV
769
                                theirBalance, *keyRing,
×
UNCOV
770
                        )
×
UNCOV
771
                },
×
772
        ).Unpack()
773
        if err != nil {
3✔
774
                return nil, fmt.Errorf("unable to fetch aux leaves: %w", err)
×
775
        }
×
776

777
        // Depending on whether the transaction is ours or not, we call
778
        // CreateCommitTx with parameters matching the perspective, to generate
779
        // a new commitment transaction with all the latest unsettled/un-timed
780
        // out HTLCs.
781
        var leaseExpiry uint32
3✔
782
        if cb.chanState.ChanType.HasLeaseExpiration() {
6✔
783
                leaseExpiry = cb.chanState.ThawHeight
3✔
784
        }
3✔
785
        if whoseCommit.IsLocal() {
6✔
786
                commitTx, err = CreateCommitTx(
3✔
787
                        cb.chanState.ChanType, fundingTxIn(cb.chanState), keyRing,
3✔
788
                        &cb.chanState.LocalChanCfg, &cb.chanState.RemoteChanCfg,
3✔
789
                        ourBalance.ToSatoshis(), theirBalance.ToSatoshis(),
3✔
790
                        numHTLCs, cb.chanState.IsInitiator, leaseExpiry,
3✔
791
                        auxResult.AuxLeaves,
3✔
792
                )
3✔
793
        } else {
6✔
794
                commitTx, err = CreateCommitTx(
3✔
795
                        cb.chanState.ChanType, fundingTxIn(cb.chanState), keyRing,
3✔
796
                        &cb.chanState.RemoteChanCfg, &cb.chanState.LocalChanCfg,
3✔
797
                        theirBalance.ToSatoshis(), ourBalance.ToSatoshis(),
3✔
798
                        numHTLCs, !cb.chanState.IsInitiator, leaseExpiry,
3✔
799
                        auxResult.AuxLeaves,
3✔
800
                )
3✔
801
        }
3✔
802
        if err != nil {
3✔
803
                return nil, err
×
804
        }
×
805

806
        // Similarly, we'll now attempt to extract the set of aux leaves for
807
        // the set of incoming and outgoing HTLCs.
808
        incomingAuxLeaves := fn.MapOption(
3✔
809
                func(leaves CommitAuxLeaves) input.HtlcAuxLeaves {
3✔
810
                        return leaves.IncomingHtlcLeaves
×
811
                },
×
812
        )(auxResult.AuxLeaves)
813
        outgoingAuxLeaves := fn.MapOption(
3✔
814
                func(leaves CommitAuxLeaves) input.HtlcAuxLeaves {
3✔
815
                        return leaves.OutgoingHtlcLeaves
×
816
                },
×
817
        )(auxResult.AuxLeaves)
818

819
        // We'll now add all the HTLC outputs to the commitment transaction.
820
        // Each output includes an off-chain 2-of-2 covenant clause, so we'll
821
        // need the objective local/remote keys for this particular commitment
822
        // as well. For any non-dust HTLCs that are manifested on the commitment
823
        // transaction, we'll also record its CLTV which is required to sort the
824
        // commitment transaction below. The slice is initially sized to the
825
        // number of existing outputs, since any outputs already added are
826
        // commitment outputs and should correspond to zero values for the
827
        // purposes of sorting.
828
        cltvs := make([]uint32, len(commitTx.TxOut))
3✔
829
        htlcIndexes := make([]input.HtlcIndex, len(commitTx.TxOut))
3✔
830
        for _, htlc := range filteredHTLCView.Updates.Local {
6✔
831
                if HtlcIsDust(
3✔
832
                        cb.chanState.ChanType, false, whoseCommit, feePerKw,
3✔
833
                        htlc.Amount.ToSatoshis(), dustLimit,
3✔
834
                ) {
6✔
835

3✔
836
                        continue
3✔
837
                }
838

839
                auxLeaf := fn.FlatMapOption(
3✔
840
                        func(leaves input.HtlcAuxLeaves) input.AuxTapLeaf {
3✔
841
                                return leaves[htlc.HtlcIndex].AuxTapLeaf
×
842
                        },
×
843
                )(outgoingAuxLeaves)
844

845
                err := addHTLC(
3✔
846
                        commitTx, whoseCommit, false, htlc, keyRing,
3✔
847
                        cb.chanState.ChanType, auxLeaf,
3✔
848
                )
3✔
849
                if err != nil {
3✔
850
                        return nil, err
×
851
                }
×
852

853
                // We want to add the CLTV and HTLC index to their respective
854
                // slices, even if we already pre-allocated them.
855
                cltvs = append(cltvs, htlc.Timeout)               //nolint
3✔
856
                htlcIndexes = append(htlcIndexes, htlc.HtlcIndex) //nolint
3✔
857
        }
858
        for _, htlc := range filteredHTLCView.Updates.Remote {
6✔
859
                if HtlcIsDust(
3✔
860
                        cb.chanState.ChanType, true, whoseCommit, feePerKw,
3✔
861
                        htlc.Amount.ToSatoshis(), dustLimit,
3✔
862
                ) {
6✔
863

3✔
864
                        continue
3✔
865
                }
866

867
                auxLeaf := fn.FlatMapOption(
3✔
868
                        func(leaves input.HtlcAuxLeaves) input.AuxTapLeaf {
3✔
869
                                return leaves[htlc.HtlcIndex].AuxTapLeaf
×
870
                        },
×
871
                )(incomingAuxLeaves)
872

873
                err := addHTLC(
3✔
874
                        commitTx, whoseCommit, true, htlc, keyRing,
3✔
875
                        cb.chanState.ChanType, auxLeaf,
3✔
876
                )
3✔
877
                if err != nil {
3✔
878
                        return nil, err
×
879
                }
×
880

881
                // We want to add the CLTV and HTLC index to their respective
882
                // slices, even if we already pre-allocated them.
883
                cltvs = append(cltvs, htlc.Timeout)               //nolint
3✔
884
                htlcIndexes = append(htlcIndexes, htlc.HtlcIndex) //nolint
3✔
885
        }
886

887
        // Set the state hint of the commitment transaction to facilitate
888
        // quickly recovering the necessary penalty state in the case of an
889
        // uncooperative broadcast.
890
        err = SetStateNumHint(commitTx, height, cb.obfuscator)
3✔
891
        if err != nil {
3✔
892
                return nil, err
×
893
        }
×
894

895
        // Sort the transactions according to the agreed upon canonical
896
        // ordering (which might be customized for custom channel types, but
897
        // deterministic and both parties will arrive at the same result). This
898
        // lets us skip sending the entire transaction over, instead we'll just
899
        // send signatures.
900
        commitSort := auxResult.CommitSortFunc.UnwrapOr(DefaultCommitSort)
3✔
901
        err = commitSort(commitTx, cltvs, htlcIndexes)
3✔
902
        if err != nil {
3✔
903
                return nil, fmt.Errorf("unable to sort commitment "+
×
904
                        "transaction: %w", err)
×
905
        }
×
906

907
        // Next, we'll ensure that we don't accidentally create a commitment
908
        // transaction which would be invalid by consensus.
909
        uTx := btcutil.NewTx(commitTx)
3✔
910
        if err := blockchain.CheckTransactionSanity(uTx); err != nil {
3✔
911
                return nil, err
×
912
        }
×
913

914
        // Finally, we'll assert that were not attempting to draw more out of
915
        // the channel that was originally placed within it.
916
        var totalOut btcutil.Amount
3✔
917
        for _, txOut := range commitTx.TxOut {
6✔
918
                totalOut += btcutil.Amount(txOut.Value)
3✔
919
        }
3✔
920
        if totalOut+commitFee > cb.chanState.Capacity {
3✔
921
                return nil, fmt.Errorf("height=%v, for ChannelPoint(%v) "+
×
922
                        "attempts to consume %v while channel capacity is %v",
×
923
                        height, cb.chanState.FundingOutpoint,
×
924
                        totalOut+commitFee, cb.chanState.Capacity)
×
925
        }
×
926

927
        return &unsignedCommitmentTx{
3✔
928
                txn:          commitTx,
3✔
929
                fee:          commitFee,
3✔
930
                ourBalance:   ourBalance,
3✔
931
                theirBalance: theirBalance,
3✔
932
                cltvs:        cltvs,
3✔
933
        }, nil
3✔
934
}
935

936
// CreateCommitTx creates a commitment transaction, spending from specified
937
// funding output. The commitment transaction contains two outputs: one local
938
// output paying to the "owner" of the commitment transaction which can be
939
// spent after a relative block delay or revocation event, and a remote output
940
// paying the counterparty within the channel, which can be spent immediately
941
// or after a delay depending on the commitment type. The `initiator` argument
942
// should correspond to the owner of the commitment transaction we are creating.
943
func CreateCommitTx(chanType channeldb.ChannelType,
944
        fundingOutput wire.TxIn, keyRing *CommitmentKeyRing,
945
        localChanCfg, remoteChanCfg *channeldb.ChannelConfig,
946
        amountToLocal, amountToRemote btcutil.Amount,
947
        numHTLCs int64, initiator bool, leaseExpiry uint32,
948
        auxLeaves fn.Option[CommitAuxLeaves]) (*wire.MsgTx, error) {
3✔
949

3✔
950
        // First, we create the script for the delayed "pay-to-self" output.
3✔
951
        // This output has 2 main redemption clauses: either we can redeem the
3✔
952
        // output after a relative block delay, or the remote node can claim
3✔
953
        // the funds with the revocation key if we broadcast a revoked
3✔
954
        // commitment transaction.
3✔
955
        localAuxLeaf := fn.MapOption(func(l CommitAuxLeaves) input.AuxTapLeaf {
3✔
956
                return l.LocalAuxLeaf
×
957
        })(auxLeaves)
×
958
        toLocalScript, err := CommitScriptToSelf(
3✔
959
                chanType, initiator, keyRing.ToLocalKey, keyRing.RevocationKey,
3✔
960
                uint32(localChanCfg.CsvDelay), leaseExpiry,
3✔
961
                fn.FlattenOption(localAuxLeaf),
3✔
962
        )
3✔
963
        if err != nil {
3✔
964
                return nil, err
×
965
        }
×
966

967
        // Next, we create the script paying to the remote.
968
        remoteAuxLeaf := fn.MapOption(func(l CommitAuxLeaves) input.AuxTapLeaf {
3✔
969
                return l.RemoteAuxLeaf
×
970
        })(auxLeaves)
×
971
        toRemoteScript, _, err := CommitScriptToRemote(
3✔
972
                chanType, initiator, keyRing.ToRemoteKey, leaseExpiry,
3✔
973
                fn.FlattenOption(remoteAuxLeaf),
3✔
974
        )
3✔
975
        if err != nil {
3✔
976
                return nil, err
×
977
        }
×
978

979
        // Now that both output scripts have been created, we can finally create
980
        // the transaction itself. We use a transaction version of 2 since CSV
981
        // will fail unless the tx version is >= 2.
982
        commitTx := wire.NewMsgTx(2)
3✔
983
        commitTx.AddTxIn(&fundingOutput)
3✔
984

3✔
985
        // Avoid creating dust outputs within the commitment transaction.
3✔
986
        localOutput := amountToLocal >= localChanCfg.DustLimit
3✔
987
        if localOutput {
6✔
988
                commitTx.AddTxOut(&wire.TxOut{
3✔
989
                        PkScript: toLocalScript.PkScript(),
3✔
990
                        Value:    int64(amountToLocal),
3✔
991
                })
3✔
992
        }
3✔
993

994
        remoteOutput := amountToRemote >= localChanCfg.DustLimit
3✔
995
        if remoteOutput {
6✔
996
                commitTx.AddTxOut(&wire.TxOut{
3✔
997
                        PkScript: toRemoteScript.PkScript(),
3✔
998
                        Value:    int64(amountToRemote),
3✔
999
                })
3✔
1000
        }
3✔
1001

1002
        // If this channel type has anchors, we'll also add those.
1003
        if chanType.HasAnchors() {
6✔
1004
                localAnchor, remoteAnchor, err := CommitScriptAnchors(
3✔
1005
                        chanType, localChanCfg, remoteChanCfg, keyRing,
3✔
1006
                )
3✔
1007
                if err != nil {
3✔
1008
                        return nil, err
×
1009
                }
×
1010

1011
                // Add local anchor output only if we have a commitment output
1012
                // or there are HTLCs.
1013
                if localOutput || numHTLCs > 0 {
6✔
1014
                        commitTx.AddTxOut(&wire.TxOut{
3✔
1015
                                PkScript: localAnchor.PkScript(),
3✔
1016
                                Value:    int64(AnchorSize),
3✔
1017
                        })
3✔
1018
                }
3✔
1019

1020
                // Add anchor output to remote only if they have a commitment
1021
                // output or there are HTLCs.
1022
                if remoteOutput || numHTLCs > 0 {
6✔
1023
                        commitTx.AddTxOut(&wire.TxOut{
3✔
1024
                                PkScript: remoteAnchor.PkScript(),
3✔
1025
                                Value:    int64(AnchorSize),
3✔
1026
                        })
3✔
1027
                }
3✔
1028
        }
1029

1030
        return commitTx, nil
3✔
1031
}
1032

1033
// CoopCloseBalance returns the final balances that should be used to create
1034
// the cooperative close tx, given the channel type and transaction fee.
1035
func CoopCloseBalance(chanType channeldb.ChannelType, isInitiator bool,
1036
        coopCloseFee, ourBalance, theirBalance, commitFee btcutil.Amount,
1037
        feePayer fn.Option[lntypes.ChannelParty],
1038
) (btcutil.Amount, btcutil.Amount, error) {
3✔
1039

3✔
1040
        // We'll make sure we account for the complete balance by adding the
3✔
1041
        // current dangling commitment fee to the balance of the initiator.
3✔
1042
        initiatorDelta := commitFee
3✔
1043

3✔
1044
        // Since the initiator's balance also is stored after subtracting the
3✔
1045
        // anchor values, add that back in case this was an anchor commitment.
3✔
1046
        if chanType.HasAnchors() {
6✔
1047
                initiatorDelta += 2 * AnchorSize
3✔
1048
        }
3✔
1049

1050
        // To start with, we'll add the anchor and/or commitment fee to the
1051
        // balance of the initiator.
1052
        if isInitiator {
6✔
1053
                ourBalance += initiatorDelta
3✔
1054
        } else {
6✔
1055
                theirBalance += initiatorDelta
3✔
1056
        }
3✔
1057

1058
        // With the initiator's balance credited, we'll now subtract the closing
1059
        // fee from the closing party. By default, the initiator pays the full
1060
        // amount, but this can be overridden by the feePayer option.
1061
        defaultPayer := func() lntypes.ChannelParty {
6✔
1062
                if isInitiator {
6✔
1063
                        return lntypes.Local
3✔
1064
                }
3✔
1065

1066
                return lntypes.Remote
3✔
1067
        }()
1068
        payer := feePayer.UnwrapOr(defaultPayer)
3✔
1069

3✔
1070
        // Based on the payer computed above, we'll subtract the closing fee.
3✔
1071
        switch payer {
3✔
1072
        case lntypes.Local:
3✔
1073
                ourBalance -= coopCloseFee
3✔
1074
        case lntypes.Remote:
3✔
1075
                theirBalance -= coopCloseFee
3✔
1076
        }
1077

1078
        // During fee negotiation it should always be verified that the
1079
        // initiator can pay the proposed fee, but we do a sanity check just to
1080
        // be sure here.
1081
        if ourBalance < 0 || theirBalance < 0 {
3✔
UNCOV
1082
                return 0, 0, fmt.Errorf("initiator cannot afford proposed " +
×
UNCOV
1083
                        "coop close fee")
×
UNCOV
1084
        }
×
1085

1086
        return ourBalance, theirBalance, nil
3✔
1087
}
1088

1089
// genSegwitV0HtlcScript generates the HTLC scripts for a normal segwit v0
1090
// channel.
1091
func genSegwitV0HtlcScript(chanType channeldb.ChannelType,
1092
        isIncoming bool, whoseCommit lntypes.ChannelParty, timeout uint32,
1093
        rHash [32]byte, keyRing *CommitmentKeyRing,
1094
) (*WitnessScriptDesc, error) {
3✔
1095

3✔
1096
        var (
3✔
1097
                witnessScript []byte
3✔
1098
                err           error
3✔
1099
        )
3✔
1100

3✔
1101
        // Choose scripts based on channel type.
3✔
1102
        confirmedHtlcSpends := false
3✔
1103
        if chanType.HasAnchors() {
6✔
1104
                confirmedHtlcSpends = true
3✔
1105
        }
3✔
1106

1107
        // Generate the proper redeem scripts for the HTLC output modified by
1108
        // two-bits denoting if this is an incoming HTLC, and if the HTLC is
1109
        // being applied to their commitment transaction or ours.
1110
        switch {
3✔
1111
        // The HTLC is paying to us, and being applied to our commitment
1112
        // transaction. So we need to use the receiver's version of the HTLC
1113
        // script.
1114
        case isIncoming && whoseCommit.IsLocal():
3✔
1115
                witnessScript, err = input.ReceiverHTLCScript(
3✔
1116
                        timeout, keyRing.RemoteHtlcKey, keyRing.LocalHtlcKey,
3✔
1117
                        keyRing.RevocationKey, rHash[:], confirmedHtlcSpends,
3✔
1118
                )
3✔
1119

1120
        // We're being paid via an HTLC by the remote party, and the HTLC is
1121
        // being added to their commitment transaction, so we use the sender's
1122
        // version of the HTLC script.
1123
        case isIncoming && whoseCommit.IsRemote():
3✔
1124
                witnessScript, err = input.SenderHTLCScript(
3✔
1125
                        keyRing.RemoteHtlcKey, keyRing.LocalHtlcKey,
3✔
1126
                        keyRing.RevocationKey, rHash[:], confirmedHtlcSpends,
3✔
1127
                )
3✔
1128

1129
        // We're sending an HTLC which is being added to our commitment
1130
        // transaction. Therefore, we need to use the sender's version of the
1131
        // HTLC script.
1132
        case !isIncoming && whoseCommit.IsLocal():
3✔
1133
                witnessScript, err = input.SenderHTLCScript(
3✔
1134
                        keyRing.LocalHtlcKey, keyRing.RemoteHtlcKey,
3✔
1135
                        keyRing.RevocationKey, rHash[:], confirmedHtlcSpends,
3✔
1136
                )
3✔
1137

1138
        // Finally, we're paying the remote party via an HTLC, which is being
1139
        // added to their commitment transaction. Therefore, we use the
1140
        // receiver's version of the HTLC script.
1141
        case !isIncoming && whoseCommit.IsRemote():
3✔
1142
                witnessScript, err = input.ReceiverHTLCScript(
3✔
1143
                        timeout, keyRing.LocalHtlcKey, keyRing.RemoteHtlcKey,
3✔
1144
                        keyRing.RevocationKey, rHash[:], confirmedHtlcSpends,
3✔
1145
                )
3✔
1146
        }
1147
        if err != nil {
3✔
1148
                return nil, err
×
1149
        }
×
1150

1151
        // Now that we have the redeem scripts, create the P2WSH public key
1152
        // script for the output itself.
1153
        htlcP2WSH, err := input.WitnessScriptHash(witnessScript)
3✔
1154
        if err != nil {
3✔
1155
                return nil, err
×
1156
        }
×
1157

1158
        return &WitnessScriptDesc{
3✔
1159
                OutputScript:  htlcP2WSH,
3✔
1160
                WitnessScript: witnessScript,
3✔
1161
        }, nil
3✔
1162
}
1163

1164
// GenTaprootHtlcScript generates the HTLC scripts for a taproot+musig2
1165
// channel.
1166
func GenTaprootHtlcScript(isIncoming bool, whoseCommit lntypes.ChannelParty,
1167
        timeout uint32, rHash [32]byte, keyRing *CommitmentKeyRing,
1168
        auxLeaf input.AuxTapLeaf) (*input.HtlcScriptTree, error) {
3✔
1169

3✔
1170
        var (
3✔
1171
                htlcScriptTree *input.HtlcScriptTree
3✔
1172
                err            error
3✔
1173
        )
3✔
1174

3✔
1175
        // Generate the proper redeem scripts for the HTLC output modified by
3✔
1176
        // two-bits denoting if this is an incoming HTLC, and if the HTLC is
3✔
1177
        // being applied to their commitment transaction or ours.
3✔
1178
        switch {
3✔
1179
        // The HTLC is paying to us, and being applied to our commitment
1180
        // transaction. So we need to use the receiver's version of HTLC the
1181
        // script.
1182
        case isIncoming && whoseCommit.IsLocal():
3✔
1183
                htlcScriptTree, err = input.ReceiverHTLCScriptTaproot(
3✔
1184
                        timeout, keyRing.RemoteHtlcKey, keyRing.LocalHtlcKey,
3✔
1185
                        keyRing.RevocationKey, rHash[:], whoseCommit, auxLeaf,
3✔
1186
                )
3✔
1187

1188
        // We're being paid via an HTLC by the remote party, and the HTLC is
1189
        // being added to their commitment transaction, so we use the sender's
1190
        // version of the HTLC script.
1191
        case isIncoming && whoseCommit.IsRemote():
3✔
1192
                htlcScriptTree, err = input.SenderHTLCScriptTaproot(
3✔
1193
                        keyRing.RemoteHtlcKey, keyRing.LocalHtlcKey,
3✔
1194
                        keyRing.RevocationKey, rHash[:], whoseCommit, auxLeaf,
3✔
1195
                )
3✔
1196

1197
        // We're sending an HTLC which is being added to our commitment
1198
        // transaction. Therefore, we need to use the sender's version of the
1199
        // HTLC script.
1200
        case !isIncoming && whoseCommit.IsLocal():
3✔
1201
                htlcScriptTree, err = input.SenderHTLCScriptTaproot(
3✔
1202
                        keyRing.LocalHtlcKey, keyRing.RemoteHtlcKey,
3✔
1203
                        keyRing.RevocationKey, rHash[:], whoseCommit, auxLeaf,
3✔
1204
                )
3✔
1205

1206
        // Finally, we're paying the remote party via an HTLC, which is being
1207
        // added to their commitment transaction. Therefore, we use the
1208
        // receiver's version of the HTLC script.
1209
        case !isIncoming && whoseCommit.IsRemote():
3✔
1210
                htlcScriptTree, err = input.ReceiverHTLCScriptTaproot(
3✔
1211
                        timeout, keyRing.LocalHtlcKey, keyRing.RemoteHtlcKey,
3✔
1212
                        keyRing.RevocationKey, rHash[:], whoseCommit, auxLeaf,
3✔
1213
                )
3✔
1214
        }
1215

1216
        return htlcScriptTree, err
3✔
1217
}
1218

1219
// genHtlcScript generates the proper P2WSH public key scripts for the HTLC
1220
// output modified by two-bits denoting if this is an incoming HTLC, and if the
1221
// HTLC is being applied to their commitment transaction or ours. A script
1222
// multiplexer for the various spending paths is returned. The script path that
1223
// we need to sign for the remote party (2nd level HTLCs) is also returned
1224
// along side the multiplexer.
1225
func genHtlcScript(chanType channeldb.ChannelType, isIncoming bool,
1226
        whoseCommit lntypes.ChannelParty, timeout uint32, rHash [32]byte,
1227
        keyRing *CommitmentKeyRing,
1228
        auxLeaf input.AuxTapLeaf) (input.ScriptDescriptor, error) {
3✔
1229

3✔
1230
        if !chanType.IsTaproot() {
6✔
1231
                return genSegwitV0HtlcScript(
3✔
1232
                        chanType, isIncoming, whoseCommit, timeout, rHash,
3✔
1233
                        keyRing,
3✔
1234
                )
3✔
1235
        }
3✔
1236

1237
        return GenTaprootHtlcScript(
3✔
1238
                isIncoming, whoseCommit, timeout, rHash, keyRing, auxLeaf,
3✔
1239
        )
3✔
1240
}
1241

1242
// addHTLC adds a new HTLC to the passed commitment transaction. One of four
1243
// full scripts will be generated for the HTLC output depending on if the HTLC
1244
// is incoming and if it's being applied to our commitment transaction or that
1245
// of the remote node's. Additionally, in order to be able to efficiently
1246
// locate the added HTLC on the commitment transaction from the
1247
// paymentDescriptor that generated it, the generated script is stored within
1248
// the descriptor itself.
1249
func addHTLC(commitTx *wire.MsgTx, whoseCommit lntypes.ChannelParty,
1250
        isIncoming bool, paymentDesc *paymentDescriptor,
1251
        keyRing *CommitmentKeyRing, chanType channeldb.ChannelType,
1252
        auxLeaf input.AuxTapLeaf) error {
3✔
1253

3✔
1254
        timeout := paymentDesc.Timeout
3✔
1255
        rHash := paymentDesc.RHash
3✔
1256

3✔
1257
        scriptInfo, err := genHtlcScript(
3✔
1258
                chanType, isIncoming, whoseCommit, timeout, rHash, keyRing,
3✔
1259
                auxLeaf,
3✔
1260
        )
3✔
1261
        if err != nil {
3✔
1262
                return err
×
1263
        }
×
1264

1265
        pkScript := scriptInfo.PkScript()
3✔
1266

3✔
1267
        // Add the new HTLC outputs to the respective commitment transactions.
3✔
1268
        amountPending := int64(paymentDesc.Amount.ToSatoshis())
3✔
1269
        commitTx.AddTxOut(wire.NewTxOut(amountPending, pkScript))
3✔
1270

3✔
1271
        // Store the pkScript of this particular paymentDescriptor so we can
3✔
1272
        // quickly locate it within the commitment transaction later.
3✔
1273
        if whoseCommit.IsLocal() {
6✔
1274
                paymentDesc.ourPkScript = pkScript
3✔
1275

3✔
1276
                paymentDesc.ourWitnessScript = scriptInfo.WitnessScriptToSign()
3✔
1277
        } else {
6✔
1278
                paymentDesc.theirPkScript = pkScript
3✔
1279

3✔
1280
                //nolint:ll
3✔
1281
                paymentDesc.theirWitnessScript = scriptInfo.WitnessScriptToSign()
3✔
1282
        }
3✔
1283

1284
        return nil
3✔
1285
}
1286

1287
// findOutputIndexesFromRemote finds the index of our and their outputs from
1288
// the remote commitment transaction. It derives the key ring to compute the
1289
// output scripts and compares them against the outputs inside the commitment
1290
// to find the match.
1291
func findOutputIndexesFromRemote(revocationPreimage *chainhash.Hash,
1292
        chanState *channeldb.OpenChannel,
1293
        leafStore fn.Option[AuxLeafStore]) (uint32, uint32, error) {
3✔
1294

3✔
1295
        // Init the output indexes as empty.
3✔
1296
        ourIndex := uint32(channeldb.OutputIndexEmpty)
3✔
1297
        theirIndex := uint32(channeldb.OutputIndexEmpty)
3✔
1298

3✔
1299
        chanCommit := chanState.RemoteCommitment
3✔
1300
        _, commitmentPoint := btcec.PrivKeyFromBytes(revocationPreimage[:])
3✔
1301

3✔
1302
        // With the commitment point generated, we can now derive the king ring
3✔
1303
        // which will be used to generate the output scripts.
3✔
1304
        keyRing := DeriveCommitmentKeys(
3✔
1305
                commitmentPoint, lntypes.Remote, chanState.ChanType,
3✔
1306
                &chanState.LocalChanCfg, &chanState.RemoteChanCfg,
3✔
1307
        )
3✔
1308

3✔
1309
        // Since it's remote commitment chain, we'd used the mirrored values.
3✔
1310
        //
3✔
1311
        // We use the remote's channel config for the csv delay.
3✔
1312
        theirDelay := uint32(chanState.RemoteChanCfg.CsvDelay)
3✔
1313

3✔
1314
        // If we are the initiator of this channel, then it's be false from the
3✔
1315
        // remote's PoV.
3✔
1316
        isRemoteInitiator := !chanState.IsInitiator
3✔
1317

3✔
1318
        var leaseExpiry uint32
3✔
1319
        if chanState.ChanType.HasLeaseExpiration() {
6✔
1320
                leaseExpiry = chanState.ThawHeight
3✔
1321
        }
3✔
1322

1323
        // If we have a custom blob, then we'll attempt to fetch the aux leaves
1324
        // for this state.
1325
        auxResult, err := fn.MapOptionZ(
3✔
1326
                leafStore, func(a AuxLeafStore) fn.Result[CommitDiffAuxResult] {
3✔
UNCOV
1327
                        return a.FetchLeavesFromCommit(
×
UNCOV
1328
                                NewAuxChanState(chanState), chanCommit,
×
UNCOV
1329
                                *keyRing, lntypes.Remote,
×
UNCOV
1330
                        )
×
UNCOV
1331
                },
×
1332
        ).Unpack()
1333
        if err != nil {
3✔
1334
                return ourIndex, theirIndex, fmt.Errorf("unable to fetch aux "+
×
1335
                        "leaves: %w", err)
×
1336
        }
×
1337

1338
        // Map the scripts from our PoV. When facing a local commitment, the
1339
        // to_local output belongs to us and the to_remote output belongs to
1340
        // them. When facing a remote commitment, the to_local output belongs to
1341
        // them and the to_remote output belongs to us.
1342

1343
        // Compute the to_local script. From our PoV, when facing a remote
1344
        // commitment, the to_local output belongs to them.
1345
        localAuxLeaf := fn.FlatMapOption(
3✔
1346
                func(l CommitAuxLeaves) input.AuxTapLeaf {
3✔
1347
                        return l.LocalAuxLeaf
×
1348
                },
×
1349
        )(auxResult.AuxLeaves)
1350
        theirScript, err := CommitScriptToSelf(
3✔
1351
                chanState.ChanType, isRemoteInitiator, keyRing.ToLocalKey,
3✔
1352
                keyRing.RevocationKey, theirDelay, leaseExpiry, localAuxLeaf,
3✔
1353
        )
3✔
1354
        if err != nil {
3✔
1355
                return ourIndex, theirIndex, err
×
1356
        }
×
1357

1358
        // Compute the to_remote script. From our PoV, when facing a remote
1359
        // commitment, the to_remote output belongs to us.
1360
        remoteAuxLeaf := fn.FlatMapOption(
3✔
1361
                func(l CommitAuxLeaves) input.AuxTapLeaf {
3✔
1362
                        return l.RemoteAuxLeaf
×
1363
                },
×
1364
        )(auxResult.AuxLeaves)
1365
        ourScript, _, err := CommitScriptToRemote(
3✔
1366
                chanState.ChanType, isRemoteInitiator, keyRing.ToRemoteKey,
3✔
1367
                leaseExpiry, remoteAuxLeaf,
3✔
1368
        )
3✔
1369
        if err != nil {
3✔
1370
                return ourIndex, theirIndex, err
×
1371
        }
×
1372

1373
        // Now compare the scripts to find our/their output index.
1374
        for i, txOut := range chanCommit.CommitTx.TxOut {
6✔
1375
                switch {
3✔
1376
                case bytes.Equal(txOut.PkScript, ourScript.PkScript()):
3✔
1377
                        ourIndex = uint32(i)
3✔
1378
                case bytes.Equal(txOut.PkScript, theirScript.PkScript()):
3✔
1379
                        theirIndex = uint32(i)
3✔
1380
                }
1381
        }
1382

1383
        return ourIndex, theirIndex, nil
3✔
1384
}
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