2019-01-16 17:47:43 +03:00
|
|
|
package input
|
2018-09-21 20:49:58 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/btcsuite/btcd/txscript"
|
|
|
|
"github.com/btcsuite/btcd/wire"
|
2020-09-04 12:28:17 +03:00
|
|
|
"github.com/btcsuite/btcutil"
|
2020-12-09 14:24:03 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lntypes"
|
2018-09-21 20:49:58 +03:00
|
|
|
)
|
|
|
|
|
2018-11-18 07:48:41 +03:00
|
|
|
// Input represents an abstract UTXO which is to be spent using a sweeping
|
|
|
|
// transaction. The method provided give the caller all information needed to
|
|
|
|
// construct a valid input within a sweeping transaction to sweep this
|
|
|
|
// lingering UTXO.
|
2018-09-26 18:59:30 +03:00
|
|
|
type Input interface {
|
2018-09-21 20:49:58 +03:00
|
|
|
// Outpoint returns the reference to the output being spent, used to
|
|
|
|
// construct the corresponding transaction input.
|
|
|
|
OutPoint() *wire.OutPoint
|
|
|
|
|
2020-11-06 21:59:11 +03:00
|
|
|
// RequiredTxOut returns a non-nil TxOut if input commits to a certain
|
|
|
|
// transaction output. This is used in the SINGLE|ANYONECANPAY case to
|
|
|
|
// make sure any presigned input is still valid by including the
|
|
|
|
// output.
|
|
|
|
RequiredTxOut() *wire.TxOut
|
|
|
|
|
2020-11-06 21:35:01 +03:00
|
|
|
// RequiredLockTime returns whether this input commits to a tx locktime
|
|
|
|
// that must be used in the transaction including it.
|
|
|
|
RequiredLockTime() (uint32, bool)
|
|
|
|
|
2018-09-21 20:49:58 +03:00
|
|
|
// WitnessType returns an enum specifying the type of witness that must
|
|
|
|
// be generated in order to spend this output.
|
2019-01-16 17:47:43 +03:00
|
|
|
WitnessType() WitnessType
|
2018-09-21 20:49:58 +03:00
|
|
|
|
2018-10-18 02:27:11 +03:00
|
|
|
// SignDesc returns a reference to a spendable output's sign
|
|
|
|
// descriptor, which is used during signing to compute a valid witness
|
|
|
|
// that spends this output.
|
2019-01-16 17:47:43 +03:00
|
|
|
SignDesc() *SignDescriptor
|
2018-09-21 20:49:58 +03:00
|
|
|
|
2018-11-18 07:48:41 +03:00
|
|
|
// CraftInputScript returns a valid set of input scripts allowing this
|
|
|
|
// output to be spent. The returns input scripts should target the
|
|
|
|
// input at location txIndex within the passed transaction. The input
|
|
|
|
// scripts generated by this method support spending p2wkh, p2wsh, and
|
|
|
|
// also nested p2sh outputs.
|
2019-01-16 17:47:43 +03:00
|
|
|
CraftInputScript(signer Signer, txn *wire.MsgTx,
|
2018-09-21 20:49:58 +03:00
|
|
|
hashCache *txscript.TxSigHashes,
|
2019-01-16 17:47:43 +03:00
|
|
|
txinIdx int) (*Script, error)
|
2018-09-26 18:59:30 +03:00
|
|
|
|
|
|
|
// BlocksToMaturity returns the relative timelock, as a number of
|
|
|
|
// blocks, that must be built on top of the confirmation height before
|
|
|
|
// the output can be spent. For non-CSV locked inputs this is always
|
|
|
|
// zero.
|
|
|
|
BlocksToMaturity() uint32
|
2018-11-07 18:35:54 +03:00
|
|
|
|
|
|
|
// HeightHint returns the minimum height at which a confirmed spending
|
|
|
|
// tx can occur.
|
|
|
|
HeightHint() uint32
|
2020-09-04 12:28:17 +03:00
|
|
|
|
|
|
|
// UnconfParent returns information about a possibly unconfirmed parent
|
|
|
|
// tx.
|
|
|
|
UnconfParent() *TxInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
// TxInfo describes properties of a parent tx that are relevant for CPFP.
|
|
|
|
type TxInfo struct {
|
|
|
|
// Fee is the fee of the tx.
|
|
|
|
Fee btcutil.Amount
|
|
|
|
|
|
|
|
// Weight is the weight of the tx.
|
|
|
|
Weight int64
|
2018-09-21 20:49:58 +03:00
|
|
|
}
|
|
|
|
|
2020-12-09 14:24:01 +03:00
|
|
|
// SignDetails is a struct containing information needed to resign certain
|
|
|
|
// inputs. It is used to re-sign 2nd level HTLC transactions that uses the
|
|
|
|
// SINGLE|ANYONECANPAY sighash type, as we have a signature provided by our
|
|
|
|
// peer, but we can aggregate multiple of these 2nd level transactions into a
|
|
|
|
// new transaction, that needs to be signed by us.
|
|
|
|
type SignDetails struct {
|
|
|
|
// SignDesc is the sign descriptor needed for us to sign the input.
|
|
|
|
SignDesc SignDescriptor
|
|
|
|
|
|
|
|
// PeerSig is the peer's signature for this input.
|
|
|
|
PeerSig Signature
|
|
|
|
|
|
|
|
// SigHashType is the sighash signed by the peer.
|
|
|
|
SigHashType txscript.SigHashType
|
|
|
|
}
|
|
|
|
|
2018-09-26 21:00:10 +03:00
|
|
|
type inputKit struct {
|
2019-10-29 19:17:45 +03:00
|
|
|
outpoint wire.OutPoint
|
|
|
|
witnessType WitnessType
|
|
|
|
signDesc SignDescriptor
|
|
|
|
heightHint uint32
|
|
|
|
blockToMaturity uint32
|
2020-09-04 12:28:17 +03:00
|
|
|
|
|
|
|
// unconfParent contains information about a potential unconfirmed
|
|
|
|
// parent transaction.
|
|
|
|
unconfParent *TxInfo
|
2018-09-26 18:59:30 +03:00
|
|
|
}
|
2018-09-21 20:49:58 +03:00
|
|
|
|
2018-10-18 02:27:11 +03:00
|
|
|
// OutPoint returns the breached output's identifier that is to be included as
|
|
|
|
// a transaction input.
|
2018-09-26 21:00:10 +03:00
|
|
|
func (i *inputKit) OutPoint() *wire.OutPoint {
|
|
|
|
return &i.outpoint
|
2018-09-26 18:59:30 +03:00
|
|
|
}
|
|
|
|
|
2020-11-06 21:59:11 +03:00
|
|
|
// RequiredTxOut returns a nil for the base input type.
|
|
|
|
func (i *inputKit) RequiredTxOut() *wire.TxOut {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-06 21:35:01 +03:00
|
|
|
// RequiredLockTime returns whether this input commits to a tx locktime that
|
|
|
|
// must be used in the transaction including it. This will be false for the
|
|
|
|
// base input type since we can re-sign for any lock time.
|
|
|
|
func (i *inputKit) RequiredLockTime() (uint32, bool) {
|
|
|
|
return 0, false
|
|
|
|
}
|
|
|
|
|
2018-09-26 18:59:30 +03:00
|
|
|
// WitnessType returns the type of witness that must be generated to spend the
|
|
|
|
// breached output.
|
2019-01-16 17:47:43 +03:00
|
|
|
func (i *inputKit) WitnessType() WitnessType {
|
2018-09-26 21:00:10 +03:00
|
|
|
return i.witnessType
|
2018-09-26 18:59:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// SignDesc returns the breached output's SignDescriptor, which is used during
|
|
|
|
// signing to compute the witness.
|
2019-01-16 17:47:43 +03:00
|
|
|
func (i *inputKit) SignDesc() *SignDescriptor {
|
2018-09-26 21:00:10 +03:00
|
|
|
return &i.signDesc
|
|
|
|
}
|
|
|
|
|
2018-11-07 18:35:54 +03:00
|
|
|
// HeightHint returns the minimum height at which a confirmed spending
|
|
|
|
// tx can occur.
|
|
|
|
func (i *inputKit) HeightHint() uint32 {
|
|
|
|
return i.heightHint
|
|
|
|
}
|
|
|
|
|
2019-10-29 19:17:45 +03:00
|
|
|
// BlocksToMaturity returns the relative timelock, as a number of blocks, that
|
|
|
|
// must be built on top of the confirmation height before the output can be
|
|
|
|
// spent. For non-CSV locked inputs this is always zero.
|
|
|
|
func (i *inputKit) BlocksToMaturity() uint32 {
|
|
|
|
return i.blockToMaturity
|
|
|
|
}
|
|
|
|
|
2020-09-04 12:28:17 +03:00
|
|
|
// Cpfp returns information about a possibly unconfirmed parent tx.
|
|
|
|
func (i *inputKit) UnconfParent() *TxInfo {
|
|
|
|
return i.unconfParent
|
|
|
|
}
|
|
|
|
|
2018-09-26 21:00:10 +03:00
|
|
|
// BaseInput contains all the information needed to sweep a basic output
|
|
|
|
// (CSV/CLTV/no time lock)
|
|
|
|
type BaseInput struct {
|
|
|
|
inputKit
|
|
|
|
}
|
|
|
|
|
|
|
|
// MakeBaseInput assembles a new BaseInput that can be used to construct a
|
|
|
|
// sweep transaction.
|
2019-01-16 17:47:43 +03:00
|
|
|
func MakeBaseInput(outpoint *wire.OutPoint, witnessType WitnessType,
|
2020-09-04 12:28:17 +03:00
|
|
|
signDescriptor *SignDescriptor, heightHint uint32,
|
|
|
|
unconfParent *TxInfo) BaseInput {
|
2018-09-26 21:00:10 +03:00
|
|
|
|
|
|
|
return BaseInput{
|
|
|
|
inputKit{
|
2020-09-04 12:28:17 +03:00
|
|
|
outpoint: *outpoint,
|
|
|
|
witnessType: witnessType,
|
|
|
|
signDesc: *signDescriptor,
|
|
|
|
heightHint: heightHint,
|
|
|
|
unconfParent: unconfParent,
|
2018-09-26 21:00:10 +03:00
|
|
|
},
|
|
|
|
}
|
2018-09-26 18:59:30 +03:00
|
|
|
}
|
2018-09-21 20:49:58 +03:00
|
|
|
|
2019-02-06 05:27:07 +03:00
|
|
|
// NewBaseInput allocates and assembles a new *BaseInput that can be used to
|
|
|
|
// construct a sweep transaction.
|
|
|
|
func NewBaseInput(outpoint *wire.OutPoint, witnessType WitnessType,
|
|
|
|
signDescriptor *SignDescriptor, heightHint uint32) *BaseInput {
|
|
|
|
|
|
|
|
input := MakeBaseInput(
|
2020-09-04 12:28:17 +03:00
|
|
|
outpoint, witnessType, signDescriptor, heightHint, nil,
|
2019-02-06 05:27:07 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
return &input
|
|
|
|
}
|
|
|
|
|
2019-10-29 19:17:45 +03:00
|
|
|
// NewCsvInput assembles a new csv-locked input that can be used to
|
|
|
|
// construct a sweep transaction.
|
|
|
|
func NewCsvInput(outpoint *wire.OutPoint, witnessType WitnessType,
|
|
|
|
signDescriptor *SignDescriptor, heightHint uint32,
|
|
|
|
blockToMaturity uint32) *BaseInput {
|
|
|
|
|
|
|
|
return &BaseInput{
|
|
|
|
inputKit{
|
|
|
|
outpoint: *outpoint,
|
|
|
|
witnessType: witnessType,
|
|
|
|
signDesc: *signDescriptor,
|
|
|
|
heightHint: heightHint,
|
|
|
|
blockToMaturity: blockToMaturity,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-18 07:48:41 +03:00
|
|
|
// CraftInputScript returns a valid set of input scripts allowing this output
|
2019-05-10 18:14:19 +03:00
|
|
|
// to be spent. The returned input scripts should target the input at location
|
2018-11-18 07:48:41 +03:00
|
|
|
// txIndex within the passed transaction. The input scripts generated by this
|
|
|
|
// method support spending p2wkh, p2wsh, and also nested p2sh outputs.
|
2019-01-16 17:47:43 +03:00
|
|
|
func (bi *BaseInput) CraftInputScript(signer Signer, txn *wire.MsgTx,
|
|
|
|
hashCache *txscript.TxSigHashes, txinIdx int) (*Script, error) {
|
2018-09-26 18:59:30 +03:00
|
|
|
|
2019-10-07 14:41:46 +03:00
|
|
|
witnessFunc := bi.witnessType.WitnessGenerator(signer, bi.SignDesc())
|
2018-09-26 18:59:30 +03:00
|
|
|
|
|
|
|
return witnessFunc(txn, hashCache, txinIdx)
|
2018-09-21 20:49:58 +03:00
|
|
|
}
|
2018-09-26 18:59:30 +03:00
|
|
|
|
2018-09-26 21:00:10 +03:00
|
|
|
// HtlcSucceedInput constitutes a sweep input that needs a pre-image. The input
|
2018-10-18 02:27:11 +03:00
|
|
|
// is expected to reside on the commitment tx of the remote party and should
|
|
|
|
// not be a second level tx output.
|
2018-09-26 21:00:10 +03:00
|
|
|
type HtlcSucceedInput struct {
|
|
|
|
inputKit
|
|
|
|
|
|
|
|
preimage []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
// MakeHtlcSucceedInput assembles a new redeem input that can be used to
|
|
|
|
// construct a sweep transaction.
|
|
|
|
func MakeHtlcSucceedInput(outpoint *wire.OutPoint,
|
2020-03-06 18:11:47 +03:00
|
|
|
signDescriptor *SignDescriptor, preimage []byte, heightHint,
|
|
|
|
blocksToMaturity uint32) HtlcSucceedInput {
|
2018-09-26 21:00:10 +03:00
|
|
|
|
|
|
|
return HtlcSucceedInput{
|
|
|
|
inputKit: inputKit{
|
2020-03-06 18:11:47 +03:00
|
|
|
outpoint: *outpoint,
|
|
|
|
witnessType: HtlcAcceptedRemoteSuccess,
|
|
|
|
signDesc: *signDescriptor,
|
|
|
|
heightHint: heightHint,
|
|
|
|
blockToMaturity: blocksToMaturity,
|
2018-09-26 21:00:10 +03:00
|
|
|
},
|
|
|
|
preimage: preimage,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-18 07:48:41 +03:00
|
|
|
// CraftInputScript returns a valid set of input scripts allowing this output
|
|
|
|
// to be spent. The returns input scripts should target the input at location
|
|
|
|
// txIndex within the passed transaction. The input scripts generated by this
|
|
|
|
// method support spending p2wkh, p2wsh, and also nested p2sh outputs.
|
2019-01-16 17:47:43 +03:00
|
|
|
func (h *HtlcSucceedInput) CraftInputScript(signer Signer, txn *wire.MsgTx,
|
|
|
|
hashCache *txscript.TxSigHashes, txinIdx int) (*Script, error) {
|
2018-09-26 21:00:10 +03:00
|
|
|
|
|
|
|
desc := h.signDesc
|
|
|
|
desc.SigHashes = hashCache
|
|
|
|
desc.InputIndex = txinIdx
|
|
|
|
|
2019-01-16 17:47:43 +03:00
|
|
|
witness, err := SenderHtlcSpendRedeem(
|
2018-11-18 07:48:41 +03:00
|
|
|
signer, &desc, txn, h.preimage,
|
2018-09-26 21:00:10 +03:00
|
|
|
)
|
2018-11-18 07:48:41 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-01-16 17:47:43 +03:00
|
|
|
return &Script{
|
2018-11-18 07:48:41 +03:00
|
|
|
Witness: witness,
|
|
|
|
}, nil
|
2018-09-26 21:00:10 +03:00
|
|
|
}
|
|
|
|
|
2020-12-09 14:24:03 +03:00
|
|
|
// HtlcsSecondLevelAnchorInput is an input type used to spend HTLC outputs
|
|
|
|
// using a re-signed second level transaction, either via the timeout or success
|
|
|
|
// paths.
|
|
|
|
type HtlcSecondLevelAnchorInput struct {
|
|
|
|
inputKit
|
|
|
|
|
|
|
|
// SignedTx is the original second level transaction signed by the
|
|
|
|
// channel peer.
|
|
|
|
SignedTx *wire.MsgTx
|
|
|
|
|
|
|
|
// createWitness creates a witness allowing the passed transaction to
|
|
|
|
// spend the input.
|
|
|
|
createWitness func(signer Signer, txn *wire.MsgTx,
|
|
|
|
hashCache *txscript.TxSigHashes, txinIdx int) (wire.TxWitness, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RequiredTxOut returns the tx out needed to be present on the sweep tx for
|
|
|
|
// the spend of the input to be valid.
|
|
|
|
func (i *HtlcSecondLevelAnchorInput) RequiredTxOut() *wire.TxOut {
|
|
|
|
return i.SignedTx.TxOut[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
// RequiredLockTime returns the locktime needed for the sweep tx for the spend
|
|
|
|
// of the input to be valid. For a second level HTLC timeout this will be the
|
|
|
|
// CLTV expiry, for HTLC success it will be zero.
|
|
|
|
func (i *HtlcSecondLevelAnchorInput) RequiredLockTime() (uint32, bool) {
|
|
|
|
return i.SignedTx.LockTime, true
|
|
|
|
}
|
|
|
|
|
|
|
|
// CraftInputScript returns a valid set of input scripts allowing this output
|
|
|
|
// to be spent. The returns input scripts should target the input at location
|
|
|
|
// txIndex within the passed transaction. The input scripts generated by this
|
|
|
|
// method support spending p2wkh, p2wsh, and also nested p2sh outputs.
|
|
|
|
func (i *HtlcSecondLevelAnchorInput) CraftInputScript(signer Signer,
|
|
|
|
txn *wire.MsgTx, hashCache *txscript.TxSigHashes,
|
|
|
|
txinIdx int) (*Script, error) {
|
|
|
|
|
|
|
|
witness, err := i.createWitness(signer, txn, hashCache, txinIdx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Script{
|
|
|
|
Witness: witness,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-12-09 14:24:03 +03:00
|
|
|
// MakeHtlcSecondLevelTimeoutAnchorInput creates an input allowing the sweeper
|
|
|
|
// to spend the HTLC output on our commit using the second level timeout
|
|
|
|
// transaction.
|
|
|
|
func MakeHtlcSecondLevelTimeoutAnchorInput(signedTx *wire.MsgTx,
|
|
|
|
signDetails *SignDetails, heightHint uint32) HtlcSecondLevelAnchorInput {
|
|
|
|
|
|
|
|
// Spend an HTLC output on our local commitment tx using the
|
|
|
|
// 2nd timeout transaction.
|
|
|
|
createWitness := func(signer Signer, txn *wire.MsgTx,
|
|
|
|
hashCache *txscript.TxSigHashes,
|
|
|
|
txinIdx int) (wire.TxWitness, error) {
|
|
|
|
|
|
|
|
desc := signDetails.SignDesc
|
|
|
|
desc.SigHashes = txscript.NewTxSigHashes(txn)
|
|
|
|
desc.InputIndex = txinIdx
|
|
|
|
|
|
|
|
return SenderHtlcSpendTimeout(
|
|
|
|
signDetails.PeerSig, signDetails.SigHashType, signer,
|
|
|
|
&desc, txn,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return HtlcSecondLevelAnchorInput{
|
|
|
|
inputKit: inputKit{
|
|
|
|
outpoint: signedTx.TxIn[0].PreviousOutPoint,
|
|
|
|
witnessType: HtlcOfferedTimeoutSecondLevelInputConfirmed,
|
|
|
|
signDesc: signDetails.SignDesc,
|
|
|
|
heightHint: heightHint,
|
|
|
|
|
|
|
|
// CSV delay is always 1 for these inputs.
|
|
|
|
blockToMaturity: 1,
|
|
|
|
},
|
|
|
|
SignedTx: signedTx,
|
|
|
|
createWitness: createWitness,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-09 14:24:03 +03:00
|
|
|
// MakeHtlcSecondLevelSuccessAnchorInput creates an input allowing the sweeper
|
|
|
|
// to spend the HTLC output on our commit using the second level success
|
|
|
|
// transaction.
|
|
|
|
func MakeHtlcSecondLevelSuccessAnchorInput(signedTx *wire.MsgTx,
|
|
|
|
signDetails *SignDetails, preimage lntypes.Preimage,
|
|
|
|
heightHint uint32) HtlcSecondLevelAnchorInput {
|
|
|
|
|
|
|
|
// Spend an HTLC output on our local commitment tx using the 2nd
|
|
|
|
// success transaction.
|
|
|
|
createWitness := func(signer Signer, txn *wire.MsgTx,
|
|
|
|
hashCache *txscript.TxSigHashes,
|
|
|
|
txinIdx int) (wire.TxWitness, error) {
|
|
|
|
|
|
|
|
desc := signDetails.SignDesc
|
|
|
|
desc.SigHashes = hashCache
|
|
|
|
desc.InputIndex = txinIdx
|
|
|
|
|
|
|
|
return ReceiverHtlcSpendRedeem(
|
|
|
|
signDetails.PeerSig, signDetails.SigHashType,
|
|
|
|
preimage[:], signer, &desc, txn,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return HtlcSecondLevelAnchorInput{
|
|
|
|
inputKit: inputKit{
|
|
|
|
outpoint: signedTx.TxIn[0].PreviousOutPoint,
|
|
|
|
witnessType: HtlcAcceptedSuccessSecondLevelInputConfirmed,
|
|
|
|
signDesc: signDetails.SignDesc,
|
|
|
|
heightHint: heightHint,
|
|
|
|
|
|
|
|
// CSV delay is always 1 for these inputs.
|
|
|
|
blockToMaturity: 1,
|
|
|
|
},
|
|
|
|
SignedTx: signedTx,
|
|
|
|
createWitness: createWitness,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-18 02:27:11 +03:00
|
|
|
// Compile-time constraints to ensure each input struct implement the Input
|
|
|
|
// interface.
|
2018-09-26 18:59:30 +03:00
|
|
|
var _ Input = (*BaseInput)(nil)
|
2018-09-26 21:00:10 +03:00
|
|
|
var _ Input = (*HtlcSucceedInput)(nil)
|
2020-12-09 14:24:03 +03:00
|
|
|
var _ Input = (*HtlcSecondLevelAnchorInput)(nil)
|