github.com/decred/dcrlnd@v0.7.6/input/witnessgen.go (about) 1 package input 2 3 import ( 4 "fmt" 5 6 "github.com/decred/dcrd/wire" 7 ) 8 9 // WitnessGenerator represents a function that is able to generate the final 10 // witness for a particular public key script. Additionally, if required, this 11 // function will also return the sigScript for spending nested P2SH witness 12 // outputs. This function acts as an abstraction layer, hiding the details of 13 // the underlying script. 14 type WitnessGenerator func(tx *wire.MsgTx, inputIndex int) (*Script, error) 15 16 // WitnessType determines how an output's witness will be generated. This 17 // interface can be implemented to be used for custom sweep scripts if the 18 // pre-defined StandardWitnessType list doesn't provide a suitable one. 19 // 20 // NOTE(decred): This would ordinarily be called 'SigScriptType'. 21 type WitnessType interface { 22 // String returns a human readable version of the WitnessType. 23 String() string 24 25 // WitnessGenerator will return a WitnessGenerator function that an 26 // output uses to generate the witness and optionally the sigScript for 27 // a sweep transaction. 28 WitnessGenerator(signer Signer, 29 descriptor *SignDescriptor) WitnessGenerator 30 31 // SizeUpperBound returns the maximum length of the SigScript of this 32 // WitnessType if it would be included in a tx. It also returns if the 33 // output itself is a nested p2sh output, if so then we need to take 34 // into account the extra sigScript data size. 35 SizeUpperBound() (int64, bool, error) 36 37 // AddSizeEstimation adds the estimated size of the witness in bytes to 38 // the given weight estimator. 39 AddSizeEstimation(e *TxSizeEstimator) error 40 } 41 42 // StandardWitnessType is a numeric representation of standard pre-defined types 43 // of witness configurations. 44 type StandardWitnessType uint16 45 46 // A compile time check to ensure StandardWitnessType implements the 47 // WitnessType interface. 48 var _ WitnessType = (StandardWitnessType)(0) 49 50 const ( 51 // CommitmentTimeLock is a witness that allows us to spend our output 52 // on our local commitment transaction after a relative lock-time 53 // lockout. 54 CommitmentTimeLock StandardWitnessType = 0 55 56 // CommitmentNoDelay is a witness that allows us to spend a settled 57 // no-delay output immediately on a counterparty's commitment 58 // transaction. 59 CommitmentNoDelay StandardWitnessType = 1 60 61 // CommitmentRevoke is a witness that allows us to sweep the settled 62 // output of a malicious counterparty's who broadcasts a revoked 63 // commitment transaction. 64 CommitmentRevoke StandardWitnessType = 2 65 66 // HtlcOfferedRevoke is a witness that allows us to sweep an HTLC which 67 // we offered to the remote party in the case that they broadcast a 68 // revoked commitment state. 69 HtlcOfferedRevoke StandardWitnessType = 3 70 71 // HtlcAcceptedRevoke is a witness that allows us to sweep an HTLC 72 // output sent to us in the case that the remote party broadcasts a 73 // revoked commitment state. 74 HtlcAcceptedRevoke StandardWitnessType = 4 75 76 // HtlcOfferedTimeoutSecondLevel is a witness that allows us to sweep 77 // an HTLC output that we extended to a party, but was never fulfilled. 78 // This HTLC output isn't directly on the commitment transaction, but 79 // is the result of a confirmed second-level HTLC transaction. As a 80 // result, we can only spend this after a CSV delay. 81 HtlcOfferedTimeoutSecondLevel StandardWitnessType = 5 82 83 // HtlcOfferedTimeoutSecondLevelInputConfirmed is a witness that allows 84 // us to sweep an HTLC output that we extended to a party, but was 85 // never fulfilled. This _is_ the HTLC output directly on our 86 // commitment transaction, and the input to the second-level HTLC 87 // tiemout transaction. It can only be spent after CLTV expiry, and 88 // commitment confirmation. 89 HtlcOfferedTimeoutSecondLevelInputConfirmed StandardWitnessType = 15 90 91 // HtlcAcceptedSuccessSecondLevel is a witness that allows us to sweep 92 // an HTLC output that was offered to us, and for which we have a 93 // payment preimage. This HTLC output isn't directly on our commitment 94 // transaction, but is the result of confirmed second-level HTLC 95 // transaction. As a result, we can only spend this after a CSV delay. 96 HtlcAcceptedSuccessSecondLevel StandardWitnessType = 6 97 98 // HtlcAcceptedSuccessSecondLevelInputConfirmed is a witness that 99 // allows us to sweep an HTLC output that was offered to us, and for 100 // which we have a payment preimage. This _is_ the HTLC output directly 101 // on our commitment transaction, and the input to the second-level 102 // HTLC success transaction. It can only be spent after the commitment 103 // has confirmed. 104 HtlcAcceptedSuccessSecondLevelInputConfirmed StandardWitnessType = 16 105 106 // HtlcOfferedRemoteTimeout is a witness that allows us to sweep an 107 // HTLC that we offered to the remote party which lies in the 108 // commitment transaction of the remote party. We can spend this output 109 // after the absolute CLTV timeout of the HTLC as passed. 110 HtlcOfferedRemoteTimeout StandardWitnessType = 7 111 112 // HtlcAcceptedRemoteSuccess is a witness that allows us to sweep an 113 // HTLC that was offered to us by the remote party. We use this witness 114 // in the case that the remote party goes to chain, and we know the 115 // pre-image to the HTLC. We can sweep this without any additional 116 // timeout. 117 HtlcAcceptedRemoteSuccess StandardWitnessType = 8 118 119 // HtlcSecondLevelRevoke is a witness that allows us to sweep an HTLC 120 // from the remote party's commitment transaction in the case that the 121 // broadcast a revoked commitment, but then also immediately attempt to 122 // go to the second level to claim the HTLC. 123 HtlcSecondLevelRevoke StandardWitnessType = 9 124 125 // WitnessKeyHash is a witness type that allows us to spend a regular 126 // p2wkh output that's sent to an output which is under complete 127 // control of the backing wallet. 128 WitnessKeyHash StandardWitnessType = 10 129 130 // NestedWitnessKeyHash is a witness type that allows us to sweep an 131 // output that sends to a nested P2SH script that pays to a key solely 132 // under our control. The witness generated needs to include the 133 NestedWitnessKeyHash StandardWitnessType = 11 134 135 // PublicKeyHash is a witness type that allows us to sweep an output 136 // that sends to a standard p2pkh script that pays to a key solely 137 // under the control of the backing wallet. 138 // 139 // NOTE(decred): The value was chosen so that it won't conflict with 140 // future new types added to the upstream lnd project. 141 PublicKeyHash StandardWitnessType = 901 142 143 // CommitSpendNoDelayTweakless is similar to the CommitSpendNoDelay 144 // type, but it omits the tweak that randomizes the key we need to 145 // spend with a channel peer supplied set of randomness. 146 CommitSpendNoDelayTweakless StandardWitnessType = 12 147 148 // CommitmentToRemoteConfirmed is a witness that allows us to spend our 149 // output on the counterparty's commitment transaction after a 150 // confirmation. 151 CommitmentToRemoteConfirmed StandardWitnessType = 13 152 153 // CommitmentAnchor is a witness that allows us to spend our anchor on 154 // the commitment transaction. 155 CommitmentAnchor StandardWitnessType = 14 156 157 // LeaseCommitmentTimeLock is a witness that allows us to spend our 158 // output on our local commitment transaction after a relative and 159 // absolute lock-time lockout as part of the script enforced lease 160 // commitment type. 161 LeaseCommitmentTimeLock StandardWitnessType = 17 162 163 // LeaseCommitmentToRemoteConfirmed is a witness that allows us to spend 164 // our output on the counterparty's commitment transaction after a 165 // confirmation and absolute locktime as part of the script enforced 166 // lease commitment type. 167 LeaseCommitmentToRemoteConfirmed StandardWitnessType = 18 168 169 // LeaseHtlcOfferedTimeoutSecondLevel is a witness that allows us to 170 // sweep an HTLC output that we extended to a party, but was never 171 // fulfilled. This HTLC output isn't directly on the commitment 172 // transaction, but is the result of a confirmed second-level HTLC 173 // transaction. As a result, we can only spend this after a CSV delay 174 // and CLTV locktime as part of the script enforced lease commitment 175 // type. 176 LeaseHtlcOfferedTimeoutSecondLevel StandardWitnessType = 19 177 178 // LeaseHtlcAcceptedSuccessSecondLevel is a witness that allows us to 179 // sweep an HTLC output that was offered to us, and for which we have a 180 // payment preimage. This HTLC output isn't directly on our commitment 181 // transaction, but is the result of confirmed second-level HTLC 182 // transaction. As a result, we can only spend this after a CSV delay 183 // and CLTV locktime as part of the script enforced lease commitment 184 // type. 185 LeaseHtlcAcceptedSuccessSecondLevel StandardWitnessType = 20 186 ) 187 188 // String returns a human readable version of the target WitnessType. 189 // 190 // NOTE: This is part of the WitnessType interface. 191 func (wt StandardWitnessType) String() string { 192 switch wt { 193 case CommitmentTimeLock: 194 return "CommitmentTimeLock" 195 196 case CommitmentToRemoteConfirmed: 197 return "CommitmentToRemoteConfirmed" 198 199 case CommitmentAnchor: 200 return "CommitmentAnchor" 201 202 case CommitmentNoDelay: 203 return "CommitmentNoDelay" 204 205 case CommitSpendNoDelayTweakless: 206 return "CommitmentNoDelayTweakless" 207 208 case CommitmentRevoke: 209 return "CommitmentRevoke" 210 211 case HtlcOfferedRevoke: 212 return "HtlcOfferedRevoke" 213 214 case HtlcAcceptedRevoke: 215 return "HtlcAcceptedRevoke" 216 217 case HtlcOfferedTimeoutSecondLevel: 218 return "HtlcOfferedTimeoutSecondLevel" 219 220 case HtlcOfferedTimeoutSecondLevelInputConfirmed: 221 return "HtlcOfferedTimeoutSecondLevelInputConfirmed" 222 223 case HtlcAcceptedSuccessSecondLevel: 224 return "HtlcAcceptedSuccessSecondLevel" 225 226 case HtlcAcceptedSuccessSecondLevelInputConfirmed: 227 return "HtlcAcceptedSuccessSecondLevelInputConfirmed" 228 229 case HtlcOfferedRemoteTimeout: 230 return "HtlcOfferedRemoteTimeout" 231 232 case HtlcAcceptedRemoteSuccess: 233 return "HtlcAcceptedRemoteSuccess" 234 235 case HtlcSecondLevelRevoke: 236 return "HtlcSecondLevelRevoke" 237 238 case PublicKeyHash: 239 return "PublicKeyHash" 240 241 case WitnessKeyHash: 242 return "WitnessKeyHash" 243 244 case NestedWitnessKeyHash: 245 return "NestedWitnessKeyHash" 246 247 case LeaseCommitmentTimeLock: 248 return "LeaseCommitmentTimeLock" 249 250 case LeaseCommitmentToRemoteConfirmed: 251 return "LeaseCommitmentToRemoteConfirmed" 252 253 case LeaseHtlcOfferedTimeoutSecondLevel: 254 return "LeaseHtlcOfferedTimeoutSecondLevel" 255 256 case LeaseHtlcAcceptedSuccessSecondLevel: 257 return "LeaseHtlcAcceptedSuccessSecondLevel" 258 259 default: 260 return fmt.Sprintf("Unknown WitnessType: %v", uint32(wt)) 261 } 262 } 263 264 // WitnessGenerator will return a WitnessGenerator function that an output uses 265 // to generate the witness and optionally the sigScript for a sweep 266 // transaction. The sigScript will be generated if the witness type warrants 267 // one for spending, such as the NestedWitnessKeyHash witness type. 268 // 269 // NOTE: This is part of the WitnessType interface. 270 func (wt StandardWitnessType) WitnessGenerator(signer Signer, 271 descriptor *SignDescriptor) WitnessGenerator { 272 273 return func(tx *wire.MsgTx, inputIndex int) (*Script, error) { 274 desc := descriptor 275 desc.InputIndex = inputIndex 276 277 switch wt { 278 case CommitmentTimeLock, LeaseCommitmentTimeLock: 279 witness, err := CommitSpendTimeout(signer, desc, tx) 280 if err != nil { 281 return nil, err 282 } 283 284 return &Script{ 285 Witness: witness, 286 }, nil 287 288 case CommitmentToRemoteConfirmed, LeaseCommitmentToRemoteConfirmed: 289 witness, err := CommitSpendToRemoteConfirmed( 290 signer, desc, tx, 291 ) 292 if err != nil { 293 return nil, err 294 } 295 296 return &Script{ 297 Witness: witness, 298 }, nil 299 300 case CommitmentAnchor: 301 witness, err := CommitSpendAnchor(signer, desc, tx) 302 if err != nil { 303 return nil, err 304 } 305 306 return &Script{ 307 Witness: witness, 308 }, nil 309 310 case CommitmentNoDelay: 311 witness, err := CommitSpendNoDelay(signer, desc, tx, false) 312 if err != nil { 313 return nil, err 314 } 315 316 return &Script{ 317 Witness: witness, 318 }, nil 319 320 case CommitSpendNoDelayTweakless: 321 witness, err := CommitSpendNoDelay(signer, desc, tx, true) 322 if err != nil { 323 return nil, err 324 } 325 326 return &Script{ 327 Witness: witness, 328 }, nil 329 330 case CommitmentRevoke: 331 witness, err := CommitSpendRevoke(signer, desc, tx) 332 if err != nil { 333 return nil, err 334 } 335 336 return &Script{ 337 Witness: witness, 338 }, nil 339 340 case HtlcOfferedRevoke: 341 witness, err := ReceiverHtlcSpendRevoke(signer, desc, tx) 342 if err != nil { 343 return nil, err 344 } 345 346 return &Script{ 347 Witness: witness, 348 }, nil 349 350 case HtlcAcceptedRevoke: 351 witness, err := SenderHtlcSpendRevoke(signer, desc, tx) 352 if err != nil { 353 return nil, err 354 } 355 356 return &Script{ 357 Witness: witness, 358 }, nil 359 360 case HtlcOfferedTimeoutSecondLevel, 361 LeaseHtlcOfferedTimeoutSecondLevel, 362 HtlcAcceptedSuccessSecondLevel, 363 LeaseHtlcAcceptedSuccessSecondLevel: 364 365 witness, err := HtlcSecondLevelSpend(signer, desc, tx) 366 if err != nil { 367 return nil, err 368 } 369 370 return &Script{ 371 Witness: witness, 372 }, nil 373 374 case HtlcOfferedRemoteTimeout: 375 // We pass in a value of -1 for the timeout, as we 376 // expect the caller to have already set the lock time 377 // value. 378 witness, err := ReceiverHtlcSpendTimeout(signer, desc, tx, -1) 379 if err != nil { 380 return nil, err 381 } 382 383 return &Script{ 384 Witness: witness, 385 }, nil 386 387 case HtlcSecondLevelRevoke: 388 witness, err := HtlcSpendRevoke(signer, desc, tx) 389 if err != nil { 390 return nil, err 391 } 392 393 return &Script{ 394 Witness: witness, 395 }, nil 396 397 case WitnessKeyHash: 398 fallthrough 399 400 case NestedWitnessKeyHash: 401 return signer.ComputeInputScript(tx, desc) 402 403 case PublicKeyHash: 404 return signer.ComputeInputScript(tx, desc) 405 406 default: 407 return nil, fmt.Errorf("unknown witness type: %v", wt) 408 } 409 } 410 } 411 412 // SizeUpperBound returns the maximum length of the witness of this witness 413 // type if it would be included in a tx. We also return if the output itself is 414 // a nested p2sh output, if so then we need to take into account the extra 415 // sigScript data size. 416 // 417 // NOTE: This is part of the WitnessType interface. 418 func (wt StandardWitnessType) SizeUpperBound() (int64, bool, error) { 419 switch wt { 420 421 // Outputs on a remote commitment transaction that pay directly to us. 422 case CommitSpendNoDelayTweakless: 423 fallthrough 424 case WitnessKeyHash: 425 fallthrough 426 case CommitmentNoDelay: 427 return P2PKHSigScriptSize, false, nil 428 429 // Outputs on a past commitment transaction that pay directly 430 // to us. 431 case CommitmentTimeLock: 432 return ToLocalTimeoutSigScriptSize, false, nil 433 case LeaseCommitmentTimeLock: 434 size := ToLocalTimeoutSigScriptSize + 435 LeaseRedeemScriptSizeOverhead 436 return size, false, nil 437 438 // 1 CSV time locked output to us on remote commitment. 439 case CommitmentToRemoteConfirmed: 440 return ToRemoteConfirmedWitnessSize, false, nil 441 case LeaseCommitmentToRemoteConfirmed: 442 size := ToRemoteConfirmedWitnessSize + 443 LeaseRedeemScriptSizeOverhead 444 return int64(size), false, nil 445 446 // Anchor output on the commitment transaction. 447 case CommitmentAnchor: 448 return AnchorSigScriptSize, false, nil 449 450 // Outgoing second layer HTLC's that have confirmed within the 451 // chain, and the output they produced is now mature enough to 452 // sweep. 453 case HtlcOfferedTimeoutSecondLevel: 454 return ToLocalTimeoutSigScriptSize, false, nil 455 case LeaseHtlcOfferedTimeoutSecondLevel: 456 size := ToLocalTimeoutSigScriptSize + 457 LeaseRedeemScriptSizeOverhead 458 return size, false, nil 459 460 // Input to the outgoing HTLC second layer timeout transaction. 461 case HtlcOfferedTimeoutSecondLevelInputConfirmed: 462 return OfferedHtlcTimeoutSigScriptSizeConfirmed, false, nil 463 464 // Incoming second layer HTLC's that have confirmed within the 465 // chain, and the output they produced is now mature enough to 466 // sweep. 467 case HtlcAcceptedSuccessSecondLevel: 468 return ToLocalTimeoutSigScriptSize, false, nil 469 case LeaseHtlcAcceptedSuccessSecondLevel: 470 size := ToLocalTimeoutSigScriptSize + 471 LeaseRedeemScriptSizeOverhead 472 return size, false, nil 473 474 // Input to the incoming second-layer HTLC success transaction. 475 case HtlcAcceptedSuccessSecondLevelInputConfirmed: 476 return AcceptedHtlcSuccessSigScriptSizeConfirmed, false, nil 477 478 // An HTLC on the commitment transaction of the remote party, 479 // that has had its absolute timelock expire. 480 case HtlcOfferedRemoteTimeout: 481 return AcceptedHtlcTimeoutSigScriptSize, false, nil 482 483 // An HTLC on the commitment transaction of the remote party, 484 // that can be swept with the preimage. 485 case HtlcAcceptedRemoteSuccess: 486 return OfferedHtlcSuccessSigScriptSize, false, nil 487 488 // The revocation output on a revoked commitment transaction. 489 case CommitmentRevoke: 490 return ToLocalPenaltySigScriptSize, false, nil 491 492 // The revocation output on a revoked HTLC that we offered to the remote 493 // party. 494 case HtlcOfferedRevoke: 495 return OfferedHtlcPenaltySigScriptSize, false, nil 496 497 // The revocation output on a revoked HTLC that was sent to us. 498 case HtlcAcceptedRevoke: 499 return AcceptedHtlcPenaltySigScriptSize, false, nil 500 501 // The revocation output of a second level output of an HTLC. 502 case HtlcSecondLevelRevoke: 503 return ToLocalPenaltySigScriptSize, false, nil 504 505 // A standard P2PKH output. 506 case PublicKeyHash: 507 return P2PKHSigScriptSize, false, nil 508 } 509 510 return 0, false, fmt.Errorf("unexpected witness type: %v", wt) 511 } 512 513 // AddSizeEstimation adds the estimated size of the witness in bytes to the 514 // given weight estimator. 515 // 516 // NOTE: This is part of the WitnessType interface. 517 func (wt StandardWitnessType) AddSizeEstimation(e *TxSizeEstimator) error { 518 // For fee estimation purposes, we'll now attempt to obtain an upper 519 // bound on the weight this input will add when fully populated. 520 size, isNestedP2SH, err := wt.SizeUpperBound() 521 if err != nil { 522 return err 523 } 524 525 // If this is a nested P2SH input, then we'll need to factor in 526 // the additional data push within the sigScript. 527 if isNestedP2SH { 528 return fmt.Errorf("nested p2sh are not supported in decred") 529 } else { 530 e.AddCustomInput(size) 531 } 532 533 return nil 534 }