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

lightningnetwork / lnd / 12199391122

06 Dec 2024 01:10PM UTC coverage: 49.807% (-9.1%) from 58.933%
12199391122

push

github

web-flow
Merge pull request #9337 from Guayaba221/patch-1

chore: fix typo in ruby.md

100137 of 201051 relevant lines covered (49.81%)

2.07 hits per line

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

73.3
/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 {
4✔
264
        switch wt {
4✔
265
        case CommitmentTimeLock:
4✔
266
                return "CommitmentTimeLock"
4✔
267

268
        case CommitmentToRemoteConfirmed:
4✔
269
                return "CommitmentToRemoteConfirmed"
4✔
270

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

274
        case CommitmentNoDelay:
4✔
275
                return "CommitmentNoDelay"
4✔
276

277
        case CommitSpendNoDelayTweakless:
4✔
278
                return "CommitmentNoDelayTweakless"
4✔
279

280
        case CommitmentRevoke:
4✔
281
                return "CommitmentRevoke"
4✔
282

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

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

289
        case HtlcOfferedTimeoutSecondLevel:
4✔
290
                return "HtlcOfferedTimeoutSecondLevel"
4✔
291

292
        case HtlcOfferedTimeoutSecondLevelInputConfirmed:
4✔
293
                return "HtlcOfferedTimeoutSecondLevelInputConfirmed"
4✔
294

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

298
        case HtlcAcceptedSuccessSecondLevelInputConfirmed:
4✔
299
                return "HtlcAcceptedSuccessSecondLevelInputConfirmed"
4✔
300

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

304
        case HtlcAcceptedRemoteSuccess:
4✔
305
                return "HtlcAcceptedRemoteSuccess"
4✔
306

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

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

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

316
        case LeaseCommitmentTimeLock:
4✔
317
                return "LeaseCommitmentTimeLock"
4✔
318

319
        case LeaseCommitmentToRemoteConfirmed:
4✔
320
                return "LeaseCommitmentToRemoteConfirmed"
4✔
321

322
        case LeaseHtlcOfferedTimeoutSecondLevel:
4✔
323
                return "LeaseHtlcOfferedTimeoutSecondLevel"
4✔
324

325
        case LeaseHtlcAcceptedSuccessSecondLevel:
4✔
326
                return "LeaseHtlcAcceptedSuccessSecondLevel"
4✔
327

328
        case TaprootPubKeySpend:
4✔
329
                return "TaprootPubKeySpend"
4✔
330

331
        case TaprootLocalCommitSpend:
4✔
332
                return "TaprootLocalCommitSpend"
4✔
333

334
        case TaprootRemoteCommitSpend:
4✔
335
                return "TaprootRemoteCommitSpend"
4✔
336

337
        case TaprootAnchorSweepSpend:
4✔
338
                return "TaprootAnchorSweepSpend"
4✔
339

340
        case TaprootHtlcOfferedTimeoutSecondLevel:
4✔
341
                return "TaprootHtlcOfferedTimeoutSecondLevel"
4✔
342

343
        case TaprootHtlcAcceptedSuccessSecondLevel:
4✔
344
                return "TaprootHtlcAcceptedSuccessSecondLevel"
4✔
345

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

349
        case TaprootHtlcAcceptedRevoke:
4✔
350
                return "TaprootHtlcAcceptedRevoke"
4✔
351

352
        case TaprootHtlcOfferedRevoke:
4✔
353
                return "TaprootHtlcOfferedRevoke"
4✔
354

355
        case TaprootHtlcOfferedRemoteTimeout:
4✔
356
                return "TaprootHtlcOfferedRemoteTimeout"
4✔
357

358
        case TaprootHtlcLocalOfferedTimeout:
4✔
359
                return "TaprootHtlcLocalOfferedTimeout"
4✔
360

361
        case TaprootHtlcAcceptedRemoteSuccess:
4✔
362
                return "TaprootHtlcAcceptedRemoteSuccess"
4✔
363

364
        case TaprootHtlcAcceptedLocalSuccess:
4✔
365
                return "TaprootHtlcAcceptedLocalSuccess"
4✔
366

367
        case TaprootCommitmentRevoke:
4✔
368
                return "TaprootCommitmentRevoke"
4✔
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 {
4✔
383

4✔
384
        return func(tx *wire.MsgTx, hc *txscript.TxSigHashes,
4✔
385
                inputIndex int) (*Script, error) {
8✔
386

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

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

399
                        return &Script{
4✔
400
                                Witness: witness,
4✔
401
                        }, nil
4✔
402

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

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

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

421
                        return &Script{
4✔
422
                                Witness: witness,
4✔
423
                        }, nil
4✔
424

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

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

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

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

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

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

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

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

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

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

475
                case HtlcOfferedTimeoutSecondLevel,
476
                        LeaseHtlcOfferedTimeoutSecondLevel,
477
                        HtlcAcceptedSuccessSecondLevel,
478
                        LeaseHtlcAcceptedSuccessSecondLevel:
4✔
479

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

485
                        return &Script{
4✔
486
                                Witness: witness,
4✔
487
                        }, nil
4✔
488

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

498
                        return &Script{
4✔
499
                                Witness: witness,
4✔
500
                        }, nil
4✔
501

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

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

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

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

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

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

537
                        return &Script{
4✔
538
                                Witness: witness,
4✔
539
                        }, nil
4✔
540

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

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

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

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

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

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

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

581
                        return &Script{
4✔
582
                                Witness: witness,
4✔
583
                        }, nil
4✔
584

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

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

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

604
                        return &Script{
4✔
605
                                Witness: witness,
4✔
606
                        }, nil
4✔
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

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

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

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

648
                        return &Script{
4✔
649
                                Witness: witness,
4✔
650
                        }, nil
4✔
651

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

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

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

670
                        return &Script{
4✔
671
                                Witness: witness,
4✔
672
                        }, nil
4✔
673

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

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

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

692
                        return &Script{
4✔
693
                                Witness: witness,
4✔
694
                        }, nil
4✔
695

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

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

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

714
                        return &Script{
4✔
715
                                Witness: witness,
4✔
716
                        }, nil
4✔
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) {
4✔
732

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

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

4✔
750
                return lntypes.WeightUnit(size), false, nil
4✔
751

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

4✔
759
                return lntypes.WeightUnit(size), false, nil
4✔
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.
768
        case HtlcOfferedTimeoutSecondLevel:
4✔
769
                return ToLocalTimeoutWitnessSize, false, nil
4✔
770
        case LeaseHtlcOfferedTimeoutSecondLevel:
4✔
771
                size := ToLocalTimeoutWitnessSize +
4✔
772
                        LeaseWitnessScriptSizeOverhead
4✔
773

4✔
774
                return lntypes.WeightUnit(size), false, nil
4✔
775

776
        // Input to the outgoing HTLC second layer timeout transaction.
777
        case HtlcOfferedTimeoutSecondLevelInputConfirmed:
4✔
778
                return OfferedHtlcTimeoutWitnessSizeConfirmed, false, nil
4✔
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:
4✔
784
                return ToLocalTimeoutWitnessSize, false, nil
4✔
785
        case LeaseHtlcAcceptedSuccessSecondLevel:
4✔
786
                size := ToLocalTimeoutWitnessSize +
4✔
787
                        LeaseWitnessScriptSizeOverhead
4✔
788

4✔
789
                return lntypes.WeightUnit(size), false, nil
4✔
790

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

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

800
        // An HTLC on the commitment transaction of the remote party,
801
        // that can be swept with the preimage.
802
        case HtlcAcceptedRemoteSuccess:
4✔
803
                return OfferedHtlcSuccessWitnessSize, false, nil
4✔
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:
4✔
809
                return P2WKHWitnessSize, true, nil
4✔
810

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

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

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

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

828
        case TaprootPubKeySpend:
4✔
829
                return TaprootKeyPathCustomSighashWitnessSize, false, nil
4✔
830

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

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

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

844
        case TaprootHtlcOfferedTimeoutSecondLevel,
845
                TaprootHtlcAcceptedSuccessSecondLevel:
4✔
846

4✔
847
                return TaprootSecondLevelHtlcWitnessSize, false, nil
4✔
848

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

852
        case TaprootHtlcAcceptedRevoke:
4✔
853
                return TaprootAcceptedRevokeWitnessSize, false, nil
4✔
854

855
        case TaprootHtlcOfferedRevoke:
4✔
856
                return TaprootOfferedRevokeWitnessSize, false, nil
4✔
857

858
        case TaprootHtlcOfferedRemoteTimeout:
4✔
859
                return TaprootHtlcOfferedRemoteTimeoutWitnessSize, false, nil
4✔
860

861
        case TaprootHtlcLocalOfferedTimeout:
4✔
862
                return TaprootOfferedLocalTimeoutWitnessSize, false, nil
4✔
863

864
        case TaprootHtlcAcceptedRemoteSuccess:
4✔
865
                return TaprootHtlcAcceptedRemoteSuccessWitnessSize, false, nil
4✔
866

867
        case TaprootHtlcAcceptedLocalSuccess:
4✔
868
                return TaprootHtlcAcceptedLocalSuccessWitnessSize, false, nil
4✔
869

870
        case TaprootCommitmentRevoke:
4✔
871
                return TaprootToLocalRevokeWitnessSize, false, nil
4✔
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 {
4✔
882
        // For fee estimation purposes, we'll now attempt to obtain an
4✔
883
        // upper bound on the weight this input will add when fully
4✔
884
        // populated.
4✔
885
        size, isNestedP2SH, err := wt.SizeUpperBound()
4✔
886
        if err != nil {
4✔
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 {
8✔
893
                e.AddNestedP2WSHInput(size)
4✔
894
        } else {
8✔
895
                e.AddWitnessInput(size)
4✔
896
        }
4✔
897

898
        return nil
4✔
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