2017-05-07 14:04:28 +03:00
|
|
|
package lnwallet
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/roasbeef/btcd/txscript"
|
|
|
|
"github.com/roasbeef/btcd/wire"
|
|
|
|
)
|
|
|
|
|
|
|
|
// WitnessType determines how an output's witness will be generated. The
|
|
|
|
// default commitmentTimeLock type will generate a witness that will allow
|
|
|
|
// spending of a time-locked transaction enforced by CheckSequenceVerify.
|
|
|
|
type WitnessType uint16
|
|
|
|
|
|
|
|
const (
|
2017-05-08 00:58:53 +03:00
|
|
|
// CommitmentTimeLock is a witness that allows us to spend the output of a
|
|
|
|
// commitment transaction after a relative lock-time lockout.
|
2017-05-07 14:04:28 +03:00
|
|
|
CommitmentTimeLock WitnessType = 0
|
|
|
|
|
2017-05-08 00:58:53 +03:00
|
|
|
// CommitmentNoDelay is a witness that allows us to spend a settled no-delay
|
|
|
|
// output immediately on a counterparty's commitment transaction.
|
2017-05-07 14:04:28 +03:00
|
|
|
CommitmentNoDelay WitnessType = 1
|
|
|
|
|
2017-05-08 00:58:53 +03:00
|
|
|
// CommitmentRevoke is a witness that allows us to sweep the settled output
|
|
|
|
// of a malicious counterparty's who broadcasts a revoked commitment
|
|
|
|
// transaction.
|
2017-05-07 14:04:28 +03:00
|
|
|
CommitmentRevoke WitnessType = 2
|
2017-09-06 23:39:45 +03:00
|
|
|
|
|
|
|
// HtlcOfferedRevoke is a witness that allows us to sweep an HTLC
|
|
|
|
// output that we offered to the counterparty.
|
|
|
|
HtlcOfferedRevoke WitnessType = 3
|
|
|
|
|
|
|
|
// HtlcAcceptedRevoke is a witness that allows us to sweep an HTLC
|
|
|
|
// output that we accepted from the counterparty.
|
|
|
|
HtlcAcceptedRevoke WitnessType = 4
|
2017-05-07 14:04:28 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// WitnessGenerator represents a function which is able to generate the final
|
|
|
|
// witness for a particular public key script. This function acts as an
|
|
|
|
// abstraction layer, hiding the details of the underlying script.
|
|
|
|
type WitnessGenerator func(tx *wire.MsgTx, hc *txscript.TxSigHashes,
|
|
|
|
inputIndex int) ([][]byte, error)
|
|
|
|
|
|
|
|
// GenWitnessFunc will return a WitnessGenerator function that an output
|
|
|
|
// uses to generate the witness for a sweep transaction.
|
2017-08-22 03:03:16 +03:00
|
|
|
func (wt WitnessType) GenWitnessFunc(signer Signer,
|
2017-05-07 14:04:28 +03:00
|
|
|
descriptor *SignDescriptor) WitnessGenerator {
|
|
|
|
|
|
|
|
return func(tx *wire.MsgTx, hc *txscript.TxSigHashes,
|
|
|
|
inputIndex int) ([][]byte, error) {
|
|
|
|
|
|
|
|
desc := descriptor
|
|
|
|
desc.SigHashes = hc
|
|
|
|
desc.InputIndex = inputIndex
|
|
|
|
|
|
|
|
switch wt {
|
|
|
|
case CommitmentTimeLock:
|
2017-08-22 03:03:16 +03:00
|
|
|
return CommitSpendTimeout(signer, desc, tx)
|
2017-05-07 14:04:28 +03:00
|
|
|
case CommitmentNoDelay:
|
2017-08-22 03:03:16 +03:00
|
|
|
return CommitSpendNoDelay(signer, desc, tx)
|
2017-05-07 14:04:28 +03:00
|
|
|
case CommitmentRevoke:
|
2017-08-22 03:03:16 +03:00
|
|
|
return CommitSpendRevoke(signer, desc, tx)
|
2017-09-06 23:39:45 +03:00
|
|
|
case HtlcOfferedRevoke:
|
|
|
|
return ReceiverHtlcSpendRevoke(signer, desc, tx)
|
|
|
|
case HtlcAcceptedRevoke:
|
|
|
|
return SenderHtlcSpendRevoke(signer, desc, tx)
|
2017-05-07 14:04:28 +03:00
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unknown witness type: %v", wt)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|