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

lightningnetwork / lnd / 12986279612

27 Jan 2025 09:51AM UTC coverage: 57.652% (-1.1%) from 58.788%
12986279612

Pull #9447

github

yyforyongyu
sweep: rename methods for clarity

We now rename "third party" to "unknown" as the inputs can be spent via
an older sweeping tx, a third party (anchor), or a remote party (pin).
In fee bumper we don't have the info to distinguish the above cases, and
leave them to be further handled by the sweeper as it has more context.
Pull Request #9447: sweep: start tracking input spending status in the fee bumper

83 of 87 new or added lines in 2 files covered. (95.4%)

19578 existing lines in 256 files now uncovered.

103448 of 179434 relevant lines covered (57.65%)

24884.58 hits per line

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

31.51
/input/witnessgen.go
1
package input
2

3
import (
4
        "fmt"
5

6
        "github.com/btcsuite/btcd/txscript"
7
        "github.com/btcsuite/btcd/wire"
8
        "github.com/lightningnetwork/lnd/lntypes"
9
)
10

11
// WitnessGenerator represents a function that is able to generate the final
12
// witness for a particular public key script. Additionally, if required, this
13
// function will also return the sigScript for spending nested P2SH witness
14
// outputs. This function acts as an abstraction layer, hiding the details of
15
// the underlying script.
16
type WitnessGenerator func(tx *wire.MsgTx, hc *txscript.TxSigHashes,
17
        inputIndex int) (*Script, error)
18

19
// WitnessType determines how an output's witness will be generated. This
20
// interface can be implemented to be used for custom sweep scripts if the
21
// pre-defined StandardWitnessType list doesn't provide a suitable one.
22
type WitnessType interface {
23
        // String returns a human readable version of the WitnessType.
24
        String() string
25

26
        // WitnessGenerator will return a WitnessGenerator function that an
27
        // output uses to generate the witness and optionally the sigScript for
28
        // a sweep transaction.
29
        WitnessGenerator(signer Signer,
30
                descriptor *SignDescriptor) WitnessGenerator
31

32
        // SizeUpperBound returns the maximum length of the witness of this
33
        // WitnessType if it would be included in a tx. It also returns if the
34
        // output itself is a nested p2sh output, if so then we need to take
35
        // into account the extra sigScript data size.
36
        SizeUpperBound() (lntypes.WeightUnit, bool, error)
37

38
        // AddWeightEstimation adds the estimated size of the witness in bytes
39
        // to the given weight estimator.
40
        AddWeightEstimation(e *TxWeightEstimator) error
41
}
42

43
// StandardWitnessType is a numeric representation of standard pre-defined types
44
// of witness configurations.
45
type StandardWitnessType uint16
46

47
// A compile time check to ensure StandardWitnessType implements the
48
// WitnessType interface.
49
var _ WitnessType = (StandardWitnessType)(0)
50

51
// NOTE: When adding a new `StandardWitnessType`, also update the `WitnessType`
52
// protobuf enum and the `allWitnessTypes` map in the `walletrpc` package.
53
const (
54
        // CommitmentTimeLock is a witness that allows us to spend our output
55
        // on our local commitment transaction after a relative lock-time
56
        // lockout.
57
        CommitmentTimeLock StandardWitnessType = 0
58

59
        // CommitmentNoDelay is a witness that allows us to spend a settled
60
        // no-delay output immediately on a counterparty's commitment
61
        // transaction.
62
        CommitmentNoDelay StandardWitnessType = 1
63

64
        // CommitmentRevoke is a witness that allows us to sweep the settled
65
        // output of a malicious counterparty's who broadcasts a revoked
66
        // commitment transaction.
67
        CommitmentRevoke StandardWitnessType = 2
68

69
        // HtlcOfferedRevoke is a witness that allows us to sweep an HTLC which
70
        // we offered to the remote party in the case that they broadcast a
71
        // revoked commitment state.
72
        HtlcOfferedRevoke StandardWitnessType = 3
73

74
        // HtlcAcceptedRevoke is a witness that allows us to sweep an HTLC
75
        // output sent to us in the case that the remote party broadcasts a
76
        // revoked commitment state.
77
        HtlcAcceptedRevoke StandardWitnessType = 4
78

79
        // HtlcOfferedTimeoutSecondLevel is a witness that allows us to sweep
80
        // an HTLC output that we extended to a party, but was never fulfilled.
81
        // This HTLC output isn't directly on the commitment transaction, but
82
        // is the result of a confirmed second-level HTLC transaction. As a
83
        // result, we can only spend this after a CSV delay.
84
        HtlcOfferedTimeoutSecondLevel StandardWitnessType = 5
85

86
        // HtlcOfferedTimeoutSecondLevelInputConfirmed is a witness that allows
87
        // us to sweep an HTLC output that we extended to a party, but was
88
        // never fulfilled. This _is_ the HTLC output directly on our
89
        // commitment transaction, and the input to the second-level HTLC
90
        // timeout transaction. It can only be spent after CLTV expiry, and
91
        // commitment confirmation.
92
        HtlcOfferedTimeoutSecondLevelInputConfirmed StandardWitnessType = 15
93

94
        // HtlcAcceptedSuccessSecondLevel is a witness that allows us to sweep
95
        // an HTLC output that was offered to us, and for which we have a
96
        // payment preimage. This HTLC output isn't directly on our commitment
97
        // transaction, but is the result of confirmed second-level HTLC
98
        // transaction. As a result, we can only spend this after a CSV delay.
99
        HtlcAcceptedSuccessSecondLevel StandardWitnessType = 6
100

101
        // HtlcAcceptedSuccessSecondLevelInputConfirmed is a witness that
102
        // allows us to sweep an HTLC output that was offered to us, and for
103
        // which we have a payment preimage. This _is_ the HTLC output directly
104
        // on our commitment transaction, and the input to the second-level
105
        // HTLC success transaction.  It can only be spent after the commitment
106
        // has confirmed.
107
        HtlcAcceptedSuccessSecondLevelInputConfirmed StandardWitnessType = 16
108

109
        // HtlcOfferedRemoteTimeout is a witness that allows us to sweep an
110
        // HTLC that we offered to the remote party which lies in the
111
        // commitment transaction of the remote party. We can spend this output
112
        // after the absolute CLTV timeout of the HTLC as passed.
113
        HtlcOfferedRemoteTimeout StandardWitnessType = 7
114

115
        // HtlcAcceptedRemoteSuccess is a witness that allows us to sweep an
116
        // HTLC that was offered to us by the remote party. We use this witness
117
        // in the case that the remote party goes to chain, and we know the
118
        // pre-image to the HTLC. We can sweep this without any additional
119
        // timeout.
120
        HtlcAcceptedRemoteSuccess StandardWitnessType = 8
121

122
        // HtlcSecondLevelRevoke is a witness that allows us to sweep an HTLC
123
        // from the remote party's commitment transaction in the case that the
124
        // broadcast a revoked commitment, but then also immediately attempt to
125
        // go to the second level to claim the HTLC.
126
        HtlcSecondLevelRevoke StandardWitnessType = 9
127

128
        // WitnessKeyHash is a witness type that allows us to spend a regular
129
        // p2wkh output that's sent to an output which is under complete
130
        // control of the backing wallet.
131
        WitnessKeyHash StandardWitnessType = 10
132

133
        // NestedWitnessKeyHash is a witness type that allows us to sweep an
134
        // output that sends to a nested P2SH script that pays to a key solely
135
        // under our control. The witness generated needs to include the
136
        NestedWitnessKeyHash StandardWitnessType = 11
137

138
        // CommitSpendNoDelayTweakless is similar to the CommitSpendNoDelay
139
        // type, but it omits the tweak that randomizes the key we need to
140
        // spend with a channel peer supplied set of randomness.
141
        CommitSpendNoDelayTweakless StandardWitnessType = 12
142

143
        // CommitmentToRemoteConfirmed is a witness that allows us to spend our
144
        // output on the counterparty's commitment transaction after a
145
        // confirmation.
146
        CommitmentToRemoteConfirmed StandardWitnessType = 13
147

148
        // CommitmentAnchor is a witness that allows us to spend our anchor on
149
        // the commitment transaction.
150
        CommitmentAnchor StandardWitnessType = 14
151

152
        // LeaseCommitmentTimeLock is a witness that allows us to spend our
153
        // output on our local commitment transaction after a relative and
154
        // absolute lock-time lockout as part of the script enforced lease
155
        // commitment type.
156
        LeaseCommitmentTimeLock StandardWitnessType = 17
157

158
        // LeaseCommitmentToRemoteConfirmed is a witness that allows us to spend
159
        // our output on the counterparty's commitment transaction after a
160
        // confirmation and absolute locktime as part of the script enforced
161
        // lease commitment type.
162
        LeaseCommitmentToRemoteConfirmed StandardWitnessType = 18
163

164
        // LeaseHtlcOfferedTimeoutSecondLevel is a witness that allows us to
165
        // sweep an HTLC output that we extended to a party, but was never
166
        // fulfilled. This HTLC output isn't directly on the commitment
167
        // transaction, but is the result of a confirmed second-level HTLC
168
        // transaction. As a result, we can only spend this after a CSV delay
169
        // and CLTV locktime as part of the script enforced lease commitment
170
        // type.
171
        LeaseHtlcOfferedTimeoutSecondLevel StandardWitnessType = 19
172

173
        // LeaseHtlcAcceptedSuccessSecondLevel is a witness that allows us to
174
        // sweep an HTLC output that was offered to us, and for which we have a
175
        // payment preimage. This HTLC output isn't directly on our commitment
176
        // transaction, but is the result of confirmed second-level HTLC
177
        // transaction. As a result, we can only spend this after a CSV delay
178
        // and CLTV locktime as part of the script enforced lease commitment
179
        // type.
180
        LeaseHtlcAcceptedSuccessSecondLevel StandardWitnessType = 20
181

182
        // TaprootPubKeySpend is a witness type that allows us to spend a
183
        // regular p2tr output that's sent to an output which is under complete
184
        // control of the backing wallet.
185
        TaprootPubKeySpend StandardWitnessType = 21
186

187
        // TaprootLocalCommitSpend is a witness type that allows us to spend
188
        // our settled local commitment after a CSV delay when we force close
189
        // the channel.
190
        TaprootLocalCommitSpend StandardWitnessType = 22
191

192
        // TaprootRemoteCommitSpend is a witness type that allows us to spend
193
        // our settled local commitment after a CSV delay when the remote party
194
        // has force closed the channel.
195
        TaprootRemoteCommitSpend StandardWitnessType = 23
196

197
        // TaprootAnchorSweepSpend is the witness type we'll use for spending
198
        // our own anchor output.
199
        TaprootAnchorSweepSpend StandardWitnessType = 24
200

201
        // TaprootHtlcOfferedTimeoutSecondLevel is a witness that allows us to
202
        // timeout an HTLC we offered to the remote party on our commitment
203
        // transaction. We use this when we need to go on chain to time out an
204
        // HTLC.
205
        TaprootHtlcOfferedTimeoutSecondLevel StandardWitnessType = 25
206

207
        // TaprootHtlcAcceptedSuccessSecondLevel is a witness that allows us to
208
        // sweep an HTLC we accepted on our commitment transaction after we go
209
        // to the second level on chain.
210
        TaprootHtlcAcceptedSuccessSecondLevel StandardWitnessType = 26
211

212
        // TaprootHtlcSecondLevelRevoke is a witness that allows us to sweep an
213
        // HTLC on the revoked transaction of the remote party that goes to the
214
        // second level.
215
        TaprootHtlcSecondLevelRevoke StandardWitnessType = 27
216

217
        // TaprootHtlcAcceptedRevoke is a witness that allows us to sweep an
218
        // HTLC sent to us by the remote party in the event that they broadcast
219
        // a revoked state.
220
        TaprootHtlcAcceptedRevoke StandardWitnessType = 28
221

222
        // TaprootHtlcOfferedRevoke is a witness that allows us to sweep an
223
        // HTLC we offered to the remote party if they broadcast a revoked
224
        // commitment.
225
        TaprootHtlcOfferedRevoke StandardWitnessType = 29
226

227
        // TaprootHtlcOfferedRemoteTimeout is a witness that allows us to sweep
228
        // an HTLC we offered to the remote party that lies on the commitment
229
        // transaction for the remote party. We can spend this output after the
230
        // absolute CLTV timeout of the HTLC as passed.
231
        TaprootHtlcOfferedRemoteTimeout StandardWitnessType = 30
232

233
        // TaprootHtlcLocalOfferedTimeout is a witness type that allows us to
234
        // sign the second level HTLC timeout transaction when spending from an
235
        // HTLC residing on our local commitment transaction.
236
        //
237
        // This is used by the sweeper to re-sign inputs if it needs to
238
        // aggregate several second level HTLCs.
239
        TaprootHtlcLocalOfferedTimeout StandardWitnessType = 31
240

241
        // TaprootHtlcAcceptedRemoteSuccess is a witness that allows us to
242
        // sweep an HTLC that was offered to us by the remote party for a
243
        // taproot channels. We use this witness in the case that the remote
244
        // party goes to chain, and we know the pre-image to the HTLC. We can
245
        // sweep this without any additional timeout.
246
        TaprootHtlcAcceptedRemoteSuccess StandardWitnessType = 32
247

248
        // TaprootHtlcAcceptedLocalSuccess is a witness type that allows us to
249
        // sweep the HTLC offered to us on our local commitment transaction.
250
        // We'll use this when we need to go on chain to sweep the HTLC. In
251
        // this case, this is the second level HTLC success transaction.
252
        TaprootHtlcAcceptedLocalSuccess StandardWitnessType = 33
253

254
        // TaprootCommitmentRevoke is a witness that allows us to sweep the
255
        // settled output of a malicious counterparty's who broadcasts a
256
        // revoked taproot commitment transaction.
257
        TaprootCommitmentRevoke StandardWitnessType = 34
258
)
259

260
// String returns a human readable version of the target WitnessType.
261
//
262
// NOTE: This is part of the WitnessType interface.
263
func (wt StandardWitnessType) String() string {
84✔
264
        switch wt {
84✔
265
        case CommitmentTimeLock:
2✔
266
                return "CommitmentTimeLock"
2✔
267

UNCOV
268
        case CommitmentToRemoteConfirmed:
×
UNCOV
269
                return "CommitmentToRemoteConfirmed"
×
270

271
        case CommitmentAnchor:
11✔
272
                return "CommitmentAnchor"
11✔
273

UNCOV
274
        case CommitmentNoDelay:
×
UNCOV
275
                return "CommitmentNoDelay"
×
276

UNCOV
277
        case CommitSpendNoDelayTweakless:
×
UNCOV
278
                return "CommitmentNoDelayTweakless"
×
279

UNCOV
280
        case CommitmentRevoke:
×
UNCOV
281
                return "CommitmentRevoke"
×
282

283
        case HtlcOfferedRevoke:
×
284
                return "HtlcOfferedRevoke"
×
285

286
        case HtlcAcceptedRevoke:
×
287
                return "HtlcAcceptedRevoke"
×
288

UNCOV
289
        case HtlcOfferedTimeoutSecondLevel:
×
UNCOV
290
                return "HtlcOfferedTimeoutSecondLevel"
×
291

UNCOV
292
        case HtlcOfferedTimeoutSecondLevelInputConfirmed:
×
UNCOV
293
                return "HtlcOfferedTimeoutSecondLevelInputConfirmed"
×
294

295
        case HtlcAcceptedSuccessSecondLevel:
3✔
296
                return "HtlcAcceptedSuccessSecondLevel"
3✔
297

UNCOV
298
        case HtlcAcceptedSuccessSecondLevelInputConfirmed:
×
UNCOV
299
                return "HtlcAcceptedSuccessSecondLevelInputConfirmed"
×
300

301
        case HtlcOfferedRemoteTimeout:
3✔
302
                return "HtlcOfferedRemoteTimeout"
3✔
303

UNCOV
304
        case HtlcAcceptedRemoteSuccess:
×
UNCOV
305
                return "HtlcAcceptedRemoteSuccess"
×
306

307
        case HtlcSecondLevelRevoke:
×
308
                return "HtlcSecondLevelRevoke"
×
309

310
        case WitnessKeyHash:
63✔
311
                return "WitnessKeyHash"
63✔
312

313
        case NestedWitnessKeyHash:
2✔
314
                return "NestedWitnessKeyHash"
2✔
315

UNCOV
316
        case LeaseCommitmentTimeLock:
×
UNCOV
317
                return "LeaseCommitmentTimeLock"
×
318

UNCOV
319
        case LeaseCommitmentToRemoteConfirmed:
×
UNCOV
320
                return "LeaseCommitmentToRemoteConfirmed"
×
321

UNCOV
322
        case LeaseHtlcOfferedTimeoutSecondLevel:
×
UNCOV
323
                return "LeaseHtlcOfferedTimeoutSecondLevel"
×
324

UNCOV
325
        case LeaseHtlcAcceptedSuccessSecondLevel:
×
UNCOV
326
                return "LeaseHtlcAcceptedSuccessSecondLevel"
×
327

UNCOV
328
        case TaprootPubKeySpend:
×
UNCOV
329
                return "TaprootPubKeySpend"
×
330

UNCOV
331
        case TaprootLocalCommitSpend:
×
UNCOV
332
                return "TaprootLocalCommitSpend"
×
333

UNCOV
334
        case TaprootRemoteCommitSpend:
×
UNCOV
335
                return "TaprootRemoteCommitSpend"
×
336

UNCOV
337
        case TaprootAnchorSweepSpend:
×
UNCOV
338
                return "TaprootAnchorSweepSpend"
×
339

UNCOV
340
        case TaprootHtlcOfferedTimeoutSecondLevel:
×
UNCOV
341
                return "TaprootHtlcOfferedTimeoutSecondLevel"
×
342

UNCOV
343
        case TaprootHtlcAcceptedSuccessSecondLevel:
×
UNCOV
344
                return "TaprootHtlcAcceptedSuccessSecondLevel"
×
345

346
        case TaprootHtlcSecondLevelRevoke:
×
347
                return "TaprootHtlcSecondLevelRevoke"
×
348

UNCOV
349
        case TaprootHtlcAcceptedRevoke:
×
UNCOV
350
                return "TaprootHtlcAcceptedRevoke"
×
351

UNCOV
352
        case TaprootHtlcOfferedRevoke:
×
UNCOV
353
                return "TaprootHtlcOfferedRevoke"
×
354

UNCOV
355
        case TaprootHtlcOfferedRemoteTimeout:
×
UNCOV
356
                return "TaprootHtlcOfferedRemoteTimeout"
×
357

UNCOV
358
        case TaprootHtlcLocalOfferedTimeout:
×
UNCOV
359
                return "TaprootHtlcLocalOfferedTimeout"
×
360

UNCOV
361
        case TaprootHtlcAcceptedRemoteSuccess:
×
UNCOV
362
                return "TaprootHtlcAcceptedRemoteSuccess"
×
363

UNCOV
364
        case TaprootHtlcAcceptedLocalSuccess:
×
UNCOV
365
                return "TaprootHtlcAcceptedLocalSuccess"
×
366

UNCOV
367
        case TaprootCommitmentRevoke:
×
UNCOV
368
                return "TaprootCommitmentRevoke"
×
369

370
        default:
×
371
                return fmt.Sprintf("Unknown WitnessType: %v", uint32(wt))
×
372
        }
373
}
374

375
// WitnessGenerator will return a WitnessGenerator function that an output uses
376
// to generate the witness and optionally the sigScript for a sweep
377
// transaction. The sigScript will be generated if the witness type warrants
378
// one for spending, such as the NestedWitnessKeyHash witness type.
379
//
380
// NOTE: This is part of the WitnessType interface.
381
func (wt StandardWitnessType) WitnessGenerator(signer Signer,
382
        descriptor *SignDescriptor) WitnessGenerator {
1,490✔
383

1,490✔
384
        return func(tx *wire.MsgTx, hc *txscript.TxSigHashes,
1,490✔
385
                inputIndex int) (*Script, error) {
2,980✔
386

1,490✔
387
                // TODO(roasbeef): copy the desc?
1,490✔
388
                desc := descriptor
1,490✔
389
                desc.SigHashes = hc
1,490✔
390
                desc.InputIndex = inputIndex
1,490✔
391

1,490✔
392
                switch wt {
1,490✔
UNCOV
393
                case CommitmentTimeLock, LeaseCommitmentTimeLock:
×
UNCOV
394
                        witness, err := CommitSpendTimeout(signer, desc, tx)
×
UNCOV
395
                        if err != nil {
×
396
                                return nil, err
×
397
                        }
×
398

UNCOV
399
                        return &Script{
×
UNCOV
400
                                Witness: witness,
×
UNCOV
401
                        }, nil
×
402

403
                case CommitmentToRemoteConfirmed, LeaseCommitmentToRemoteConfirmed:
6✔
404
                        witness, err := CommitSpendToRemoteConfirmed(
6✔
405
                                signer, desc, tx,
6✔
406
                        )
6✔
407
                        if err != nil {
6✔
408
                                return nil, err
×
409
                        }
×
410

411
                        return &Script{
6✔
412
                                Witness: witness,
6✔
413
                        }, nil
6✔
414

UNCOV
415
                case CommitmentAnchor:
×
UNCOV
416
                        witness, err := CommitSpendAnchor(signer, desc, tx)
×
UNCOV
417
                        if err != nil {
×
418
                                return nil, err
×
419
                        }
×
420

UNCOV
421
                        return &Script{
×
UNCOV
422
                                Witness: witness,
×
UNCOV
423
                        }, nil
×
424

425
                case CommitmentNoDelay:
6✔
426
                        witness, err := CommitSpendNoDelay(signer, desc, tx, false)
6✔
427
                        if err != nil {
6✔
428
                                return nil, err
×
429
                        }
×
430

431
                        return &Script{
6✔
432
                                Witness: witness,
6✔
433
                        }, nil
6✔
434

435
                case CommitSpendNoDelayTweakless:
22✔
436
                        witness, err := CommitSpendNoDelay(signer, desc, tx, true)
22✔
437
                        if err != nil {
22✔
438
                                return nil, err
×
439
                        }
×
440

441
                        return &Script{
22✔
442
                                Witness: witness,
22✔
443
                        }, nil
22✔
444

445
                case CommitmentRevoke:
36✔
446
                        witness, err := CommitSpendRevoke(signer, desc, tx)
36✔
447
                        if err != nil {
36✔
448
                                return nil, err
×
449
                        }
×
450

451
                        return &Script{
36✔
452
                                Witness: witness,
36✔
453
                        }, nil
36✔
454

455
                case HtlcOfferedRevoke:
15✔
456
                        witness, err := ReceiverHtlcSpendRevoke(signer, desc, tx)
15✔
457
                        if err != nil {
15✔
458
                                return nil, err
×
459
                        }
×
460

461
                        return &Script{
15✔
462
                                Witness: witness,
15✔
463
                        }, nil
15✔
464

465
                case HtlcAcceptedRevoke:
2✔
466
                        witness, err := SenderHtlcSpendRevoke(signer, desc, tx)
2✔
467
                        if err != nil {
2✔
468
                                return nil, err
×
469
                        }
×
470

471
                        return &Script{
2✔
472
                                Witness: witness,
2✔
473
                        }, nil
2✔
474

475
                case HtlcOfferedTimeoutSecondLevel,
476
                        LeaseHtlcOfferedTimeoutSecondLevel,
477
                        HtlcAcceptedSuccessSecondLevel,
UNCOV
478
                        LeaseHtlcAcceptedSuccessSecondLevel:
×
UNCOV
479

×
UNCOV
480
                        witness, err := HtlcSecondLevelSpend(signer, desc, tx)
×
UNCOV
481
                        if err != nil {
×
482
                                return nil, err
×
483
                        }
×
484

UNCOV
485
                        return &Script{
×
UNCOV
486
                                Witness: witness,
×
UNCOV
487
                        }, nil
×
488

UNCOV
489
                case HtlcOfferedRemoteTimeout:
×
UNCOV
490
                        // We pass in a value of -1 for the timeout, as we
×
UNCOV
491
                        // expect the caller to have already set the lock time
×
UNCOV
492
                        // value.
×
UNCOV
493
                        witness, err := ReceiverHtlcSpendTimeout(signer, desc, tx, -1)
×
UNCOV
494
                        if err != nil {
×
495
                                return nil, err
×
496
                        }
×
497

UNCOV
498
                        return &Script{
×
UNCOV
499
                                Witness: witness,
×
UNCOV
500
                        }, nil
×
501

502
                case HtlcSecondLevelRevoke:
10✔
503
                        witness, err := HtlcSpendRevoke(signer, desc, tx)
10✔
504
                        if err != nil {
10✔
505
                                return nil, err
×
506
                        }
×
507

508
                        return &Script{
10✔
509
                                Witness: witness,
10✔
510
                        }, nil
10✔
511

512
                case WitnessKeyHash:
25✔
513
                        fallthrough
25✔
514
                case TaprootPubKeySpend:
25✔
515
                        fallthrough
25✔
516
                case NestedWitnessKeyHash:
26✔
517
                        return signer.ComputeInputScript(tx, desc)
26✔
518

UNCOV
519
                case TaprootLocalCommitSpend:
×
UNCOV
520
                        // Ensure that the sign desc has the proper sign method
×
UNCOV
521
                        // set, and a valid prev output fetcher.
×
UNCOV
522
                        desc.SignMethod = TaprootScriptSpendSignMethod
×
UNCOV
523

×
UNCOV
524
                        // The control block bytes must be set at this point.
×
UNCOV
525
                        if desc.ControlBlock == nil {
×
526
                                return nil, fmt.Errorf("control block must " +
×
527
                                        "be set for taproot spend")
×
528
                        }
×
529

UNCOV
530
                        witness, err := TaprootCommitSpendSuccess(
×
UNCOV
531
                                signer, desc, tx, nil,
×
UNCOV
532
                        )
×
UNCOV
533
                        if err != nil {
×
534
                                return nil, err
×
535
                        }
×
536

UNCOV
537
                        return &Script{
×
UNCOV
538
                                Witness: witness,
×
UNCOV
539
                        }, nil
×
540

541
                case TaprootRemoteCommitSpend:
683✔
542
                        // Ensure that the sign desc has the proper sign method
683✔
543
                        // set, and a valid prev output fetcher.
683✔
544
                        desc.SignMethod = TaprootScriptSpendSignMethod
683✔
545

683✔
546
                        // The control block bytes must be set at this point.
683✔
547
                        if desc.ControlBlock == nil {
683✔
548
                                return nil, fmt.Errorf("control block must " +
×
549
                                        "be set for taproot spend")
×
550
                        }
×
551

552
                        witness, err := TaprootCommitRemoteSpend(
683✔
553
                                signer, desc, tx, nil,
683✔
554
                        )
683✔
555
                        if err != nil {
683✔
556
                                return nil, err
×
557
                        }
×
558

559
                        return &Script{
683✔
560
                                Witness: witness,
683✔
561
                        }, nil
683✔
562

UNCOV
563
                case TaprootAnchorSweepSpend:
×
UNCOV
564
                        // Ensure that the sign desc has the proper sign method
×
UNCOV
565
                        // set, and a valid prev output fetcher.
×
UNCOV
566
                        desc.SignMethod = TaprootKeySpendSignMethod
×
UNCOV
567

×
UNCOV
568
                        // The tap tweak must be set at this point.
×
UNCOV
569
                        if desc.TapTweak == nil {
×
570
                                return nil, fmt.Errorf("tap tweak must be " +
×
571
                                        "set for keyspend")
×
572
                        }
×
573

UNCOV
574
                        witness, err := TaprootAnchorSpend(
×
UNCOV
575
                                signer, desc, tx,
×
UNCOV
576
                        )
×
UNCOV
577
                        if err != nil {
×
578
                                return nil, err
×
579
                        }
×
580

UNCOV
581
                        return &Script{
×
UNCOV
582
                                Witness: witness,
×
UNCOV
583
                        }, nil
×
584

585
                case TaprootHtlcOfferedTimeoutSecondLevel,
UNCOV
586
                        TaprootHtlcAcceptedSuccessSecondLevel:
×
UNCOV
587
                        // Ensure that the sign desc has the proper sign method
×
UNCOV
588
                        // set, and a valid prev output fetcher.
×
UNCOV
589
                        desc.SignMethod = TaprootScriptSpendSignMethod
×
UNCOV
590

×
UNCOV
591
                        // The control block bytes must be set at this point.
×
UNCOV
592
                        if desc.ControlBlock == nil {
×
593
                                return nil, fmt.Errorf("control block must " +
×
594
                                        "be set for taproot spend")
×
595
                        }
×
596

UNCOV
597
                        witness, err := TaprootHtlcSpendSuccess(
×
UNCOV
598
                                signer, desc, tx, nil, nil,
×
UNCOV
599
                        )
×
UNCOV
600
                        if err != nil {
×
601
                                return nil, err
×
602
                        }
×
603

UNCOV
604
                        return &Script{
×
UNCOV
605
                                Witness: witness,
×
UNCOV
606
                        }, nil
×
607

608
                case TaprootHtlcSecondLevelRevoke:
×
609
                        // Ensure that the sign desc has the proper sign method
×
610
                        // set, and a valid prev output fetcher.
×
611
                        desc.SignMethod = TaprootKeySpendSignMethod
×
612

×
613
                        // The tap tweak must be set at this point.
×
614
                        if desc.TapTweak == nil {
×
615
                                return nil, fmt.Errorf("tap tweak must be " +
×
616
                                        "set for keyspend")
×
617
                        }
×
618

619
                        witness, err := TaprootHtlcSpendRevoke(
×
620
                                signer, desc, tx,
×
621
                        )
×
622
                        if err != nil {
×
623
                                return nil, err
×
624
                        }
×
625

626
                        return &Script{
×
627
                                Witness: witness,
×
628
                        }, nil
×
629

UNCOV
630
                case TaprootHtlcOfferedRevoke:
×
UNCOV
631
                        // Ensure that the sign desc has the proper sign method
×
UNCOV
632
                        // set, and a valid prev output fetcher.
×
UNCOV
633
                        desc.SignMethod = TaprootKeySpendSignMethod
×
UNCOV
634

×
UNCOV
635
                        // The tap tweak must be set at this point.
×
UNCOV
636
                        if desc.TapTweak == nil {
×
637
                                return nil, fmt.Errorf("tap tweak must be " +
×
638
                                        "set for keyspend")
×
639
                        }
×
640

UNCOV
641
                        witness, err := SenderHTLCScriptTaprootRevoke(
×
UNCOV
642
                                signer, desc, tx,
×
UNCOV
643
                        )
×
UNCOV
644
                        if err != nil {
×
645
                                return nil, err
×
646
                        }
×
647

UNCOV
648
                        return &Script{
×
UNCOV
649
                                Witness: witness,
×
UNCOV
650
                        }, nil
×
651

UNCOV
652
                case TaprootHtlcAcceptedRevoke:
×
UNCOV
653
                        // Ensure that the sign desc has the proper sign method
×
UNCOV
654
                        // set, and a valid prev output fetcher.
×
UNCOV
655
                        desc.SignMethod = TaprootKeySpendSignMethod
×
UNCOV
656

×
UNCOV
657
                        // The tap tweak must be set at this point.
×
UNCOV
658
                        if desc.TapTweak == nil {
×
659
                                return nil, fmt.Errorf("tap tweak must be " +
×
660
                                        "set for keyspend")
×
661
                        }
×
662

UNCOV
663
                        witness, err := ReceiverHTLCScriptTaprootRevoke(
×
UNCOV
664
                                signer, desc, tx,
×
UNCOV
665
                        )
×
UNCOV
666
                        if err != nil {
×
667
                                return nil, err
×
668
                        }
×
669

UNCOV
670
                        return &Script{
×
UNCOV
671
                                Witness: witness,
×
UNCOV
672
                        }, nil
×
673

UNCOV
674
                case TaprootHtlcOfferedRemoteTimeout:
×
UNCOV
675
                        // Ensure that the sign desc has the proper sign method
×
UNCOV
676
                        // set, and a valid prev output fetcher.
×
UNCOV
677
                        desc.SignMethod = TaprootScriptSpendSignMethod
×
UNCOV
678

×
UNCOV
679
                        // The control block bytes must be set at this point.
×
UNCOV
680
                        if desc.ControlBlock == nil {
×
681
                                return nil, fmt.Errorf("control block " +
×
682
                                        "must be set for taproot spend")
×
683
                        }
×
684

UNCOV
685
                        witness, err := ReceiverHTLCScriptTaprootTimeout(
×
UNCOV
686
                                signer, desc, tx, -1, nil, nil,
×
UNCOV
687
                        )
×
UNCOV
688
                        if err != nil {
×
689
                                return nil, err
×
690
                        }
×
691

UNCOV
692
                        return &Script{
×
UNCOV
693
                                Witness: witness,
×
UNCOV
694
                        }, nil
×
695

696
                case TaprootCommitmentRevoke:
684✔
697
                        // Ensure that the sign desc has the proper sign method
684✔
698
                        // set, and a valid prev output fetcher.
684✔
699
                        desc.SignMethod = TaprootScriptSpendSignMethod
684✔
700

684✔
701
                        // The control block bytes must be set at this point.
684✔
702
                        if desc.ControlBlock == nil {
684✔
703
                                return nil, fmt.Errorf("control block " +
×
704
                                        "must be set for taproot spend")
×
705
                        }
×
706

707
                        witness, err := TaprootCommitSpendRevoke(
684✔
708
                                signer, desc, tx, nil,
684✔
709
                        )
684✔
710
                        if err != nil {
684✔
711
                                return nil, err
×
712
                        }
×
713

714
                        return &Script{
684✔
715
                                Witness: witness,
684✔
716
                        }, nil
684✔
717

718
                default:
×
719
                        return nil, fmt.Errorf("unknown witness type: %v", wt)
×
720
                }
721
        }
722
}
723

724
// SizeUpperBound returns the maximum length of the witness of this witness
725
// type if it would be included in a tx. We also return if the output itself is
726
// a nested p2sh output, if so then we need to take into account the extra
727
// sigScript data size.
728
//
729
// NOTE: This is part of the WitnessType interface.
730
func (wt StandardWitnessType) SizeUpperBound() (lntypes.WeightUnit,
731
        bool, error) {
135✔
732

135✔
733
        switch wt {
135✔
734
        // Outputs on a remote commitment transaction that pay directly to us.
735
        case CommitSpendNoDelayTweakless:
18✔
736
                fallthrough
18✔
737
        case WitnessKeyHash:
60✔
738
                fallthrough
60✔
739
        case CommitmentNoDelay:
62✔
740
                return P2WKHWitnessSize, false, nil
62✔
741

742
        // Outputs on a past commitment transaction that pay directly
743
        // to us.
744
        case CommitmentTimeLock:
6✔
745
                return ToLocalTimeoutWitnessSize, false, nil
6✔
UNCOV
746
        case LeaseCommitmentTimeLock:
×
UNCOV
747
                size := ToLocalTimeoutWitnessSize +
×
UNCOV
748
                        LeaseWitnessScriptSizeOverhead
×
UNCOV
749

×
UNCOV
750
                return lntypes.WeightUnit(size), false, nil
×
751

752
        // 1 CSV time locked output to us on remote commitment.
753
        case CommitmentToRemoteConfirmed:
2✔
754
                return ToRemoteConfirmedWitnessSize, false, nil
2✔
UNCOV
755
        case LeaseCommitmentToRemoteConfirmed:
×
UNCOV
756
                size := ToRemoteConfirmedWitnessSize +
×
UNCOV
757
                        LeaseWitnessScriptSizeOverhead
×
UNCOV
758

×
UNCOV
759
                return lntypes.WeightUnit(size), false, nil
×
760

761
        // Anchor output on the commitment transaction.
762
        case CommitmentAnchor:
4✔
763
                return AnchorWitnessSize, false, nil
4✔
764

765
        // Outgoing second layer HTLC's that have confirmed within the
766
        // chain, and the output they produced is now mature enough to
767
        // sweep.
UNCOV
768
        case HtlcOfferedTimeoutSecondLevel:
×
UNCOV
769
                return ToLocalTimeoutWitnessSize, false, nil
×
UNCOV
770
        case LeaseHtlcOfferedTimeoutSecondLevel:
×
UNCOV
771
                size := ToLocalTimeoutWitnessSize +
×
UNCOV
772
                        LeaseWitnessScriptSizeOverhead
×
UNCOV
773

×
UNCOV
774
                return lntypes.WeightUnit(size), false, nil
×
775

776
        // Input to the outgoing HTLC second layer timeout transaction.
UNCOV
777
        case HtlcOfferedTimeoutSecondLevelInputConfirmed:
×
UNCOV
778
                return OfferedHtlcTimeoutWitnessSizeConfirmed, false, nil
×
779

780
        // Incoming second layer HTLC's that have confirmed within the
781
        // chain, and the output they produced is now mature enough to
782
        // sweep.
783
        case HtlcAcceptedSuccessSecondLevel:
6✔
784
                return ToLocalTimeoutWitnessSize, false, nil
6✔
UNCOV
785
        case LeaseHtlcAcceptedSuccessSecondLevel:
×
UNCOV
786
                size := ToLocalTimeoutWitnessSize +
×
UNCOV
787
                        LeaseWitnessScriptSizeOverhead
×
UNCOV
788

×
UNCOV
789
                return lntypes.WeightUnit(size), false, nil
×
790

791
        // Input to the incoming second-layer HTLC success transaction.
UNCOV
792
        case HtlcAcceptedSuccessSecondLevelInputConfirmed:
×
UNCOV
793
                return AcceptedHtlcSuccessWitnessSizeConfirmed, false, nil
×
794

795
        // An HTLC on the commitment transaction of the remote party,
796
        // that has had its absolute timelock expire.
797
        case HtlcOfferedRemoteTimeout:
6✔
798
                return AcceptedHtlcTimeoutWitnessSize, false, nil
6✔
799

800
        // An HTLC on the commitment transaction of the remote party,
801
        // that can be swept with the preimage.
UNCOV
802
        case HtlcAcceptedRemoteSuccess:
×
UNCOV
803
                return OfferedHtlcSuccessWitnessSize, false, nil
×
804

805
        // A nested P2SH input that has a p2wkh witness script. We'll mark this
806
        // as nested P2SH so the caller can estimate the weight properly
807
        // including the sigScript.
808
        case NestedWitnessKeyHash:
1✔
809
                return P2WKHWitnessSize, true, nil
1✔
810

811
        // The revocation output on a revoked commitment transaction.
812
        case CommitmentRevoke:
24✔
813
                return ToLocalPenaltyWitnessSize, false, nil
24✔
814

815
        // The revocation output on a revoked HTLC that we offered to the remote
816
        // party.
817
        case HtlcOfferedRevoke:
12✔
818
                return OfferedHtlcPenaltyWitnessSize, false, nil
12✔
819

820
        // The revocation output on a revoked HTLC that was sent to us.
821
        case HtlcAcceptedRevoke:
2✔
822
                return AcceptedHtlcPenaltyWitnessSize, false, nil
2✔
823

824
        // The revocation output of a second level output of an HTLC.
825
        case HtlcSecondLevelRevoke:
10✔
826
                return ToLocalPenaltyWitnessSize, false, nil
10✔
827

UNCOV
828
        case TaprootPubKeySpend:
×
UNCOV
829
                return TaprootKeyPathCustomSighashWitnessSize, false, nil
×
830

831
        // Sweeping a self output after a delay for taproot channels.
UNCOV
832
        case TaprootLocalCommitSpend:
×
UNCOV
833
                return TaprootToLocalWitnessSize, false, nil
×
834

835
        // Sweeping a self output after the remote party fro ce closes. Must
836
        // wait 1 CSV.
UNCOV
837
        case TaprootRemoteCommitSpend:
×
UNCOV
838
                return TaprootToRemoteWitnessSize, false, nil
×
839

840
        // Sweeping our anchor output with a key spend witness.
UNCOV
841
        case TaprootAnchorSweepSpend:
×
UNCOV
842
                return TaprootAnchorWitnessSize, false, nil
×
843

844
        case TaprootHtlcOfferedTimeoutSecondLevel,
UNCOV
845
                TaprootHtlcAcceptedSuccessSecondLevel:
×
UNCOV
846

×
UNCOV
847
                return TaprootSecondLevelHtlcWitnessSize, false, nil
×
848

849
        case TaprootHtlcSecondLevelRevoke:
×
850
                return TaprootSecondLevelRevokeWitnessSize, false, nil
×
851

UNCOV
852
        case TaprootHtlcAcceptedRevoke:
×
UNCOV
853
                return TaprootAcceptedRevokeWitnessSize, false, nil
×
854

UNCOV
855
        case TaprootHtlcOfferedRevoke:
×
UNCOV
856
                return TaprootOfferedRevokeWitnessSize, false, nil
×
857

UNCOV
858
        case TaprootHtlcOfferedRemoteTimeout:
×
UNCOV
859
                return TaprootHtlcOfferedRemoteTimeoutWitnessSize, false, nil
×
860

UNCOV
861
        case TaprootHtlcLocalOfferedTimeout:
×
UNCOV
862
                return TaprootOfferedLocalTimeoutWitnessSize, false, nil
×
863

UNCOV
864
        case TaprootHtlcAcceptedRemoteSuccess:
×
UNCOV
865
                return TaprootHtlcAcceptedRemoteSuccessWitnessSize, false, nil
×
866

UNCOV
867
        case TaprootHtlcAcceptedLocalSuccess:
×
UNCOV
868
                return TaprootHtlcAcceptedLocalSuccessWitnessSize, false, nil
×
869

UNCOV
870
        case TaprootCommitmentRevoke:
×
UNCOV
871
                return TaprootToLocalRevokeWitnessSize, false, nil
×
872
        }
873

874
        return 0, false, fmt.Errorf("unexpected witness type: %v", wt)
×
875
}
876

877
// AddWeightEstimation adds the estimated size of the witness in bytes to the
878
// given weight estimator.
879
//
880
// NOTE: This is part of the WitnessType interface.
881
func (wt StandardWitnessType) AddWeightEstimation(e *TxWeightEstimator) error {
65✔
882
        // For fee estimation purposes, we'll now attempt to obtain an
65✔
883
        // upper bound on the weight this input will add when fully
65✔
884
        // populated.
65✔
885
        size, isNestedP2SH, err := wt.SizeUpperBound()
65✔
886
        if err != nil {
65✔
887
                return err
×
888
        }
×
889

890
        // If this is a nested P2SH input, then we'll need to factor in
891
        // the additional data push within the sigScript.
892
        if isNestedP2SH {
66✔
893
                e.AddNestedP2WSHInput(size)
1✔
894
        } else {
65✔
895
                e.AddWitnessInput(size)
64✔
896
        }
64✔
897

898
        return nil
65✔
899
}
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