2019-01-16 15:47:43 +01:00
|
|
|
package input
|
2018-09-21 19:49:58 +02:00
|
|
|
|
|
|
|
import (
|
2023-08-09 11:30:52 +02:00
|
|
|
"fmt"
|
|
|
|
|
2022-02-23 14:48:00 +01:00
|
|
|
"github.com/btcsuite/btcd/btcutil"
|
2018-09-21 19:49:58 +02:00
|
|
|
"github.com/btcsuite/btcd/txscript"
|
|
|
|
"github.com/btcsuite/btcd/wire"
|
2020-12-09 12:24:03 +01:00
|
|
|
"github.com/lightningnetwork/lnd/lntypes"
|
2018-09-21 19:49:58 +02:00
|
|
|
)
|
|
|
|
|
2018-11-18 05:48:41 +01: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 17:59:30 +02:00
|
|
|
type Input interface {
|
2018-09-21 19:49:58 +02:00
|
|
|
// Outpoint returns the reference to the output being spent, used to
|
|
|
|
// construct the corresponding transaction input.
|
|
|
|
OutPoint() *wire.OutPoint
|
|
|
|
|
2020-11-06 19:59:11 +01: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 19:35:01 +01: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 19:49:58 +02:00
|
|
|
// WitnessType returns an enum specifying the type of witness that must
|
|
|
|
// be generated in order to spend this output.
|
2019-01-16 15:47:43 +01:00
|
|
|
WitnessType() WitnessType
|
2018-09-21 19:49:58 +02:00
|
|
|
|
2018-10-18 01:27:11 +02: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 15:47:43 +01:00
|
|
|
SignDesc() *SignDescriptor
|
2018-09-21 19:49:58 +02:00
|
|
|
|
2018-11-18 05:48:41 +01: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 15:47:43 +01:00
|
|
|
CraftInputScript(signer Signer, txn *wire.MsgTx,
|
2018-09-21 19:49:58 +02:00
|
|
|
hashCache *txscript.TxSigHashes,
|
2022-03-18 18:37:44 +01:00
|
|
|
prevOutputFetcher txscript.PrevOutputFetcher,
|
2019-01-16 15:47:43 +01:00
|
|
|
txinIdx int) (*Script, error)
|
2018-09-26 17:59:30 +02: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 16:35:54 +01:00
|
|
|
|
|
|
|
// HeightHint returns the minimum height at which a confirmed spending
|
|
|
|
// tx can occur.
|
|
|
|
HeightHint() uint32
|
2020-09-04 11:28:17 +02: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 19:49:58 +02:00
|
|
|
}
|
|
|
|
|
2023-08-09 11:30:52 +02:00
|
|
|
// String returns a human readable version of the tx info.
|
|
|
|
func (t *TxInfo) String() string {
|
|
|
|
return fmt.Sprintf("fee=%v, weight=%v", t.Fee, t.Weight)
|
|
|
|
}
|
|
|
|
|
2020-12-09 12:24:01 +01: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 20:00:10 +02:00
|
|
|
type inputKit struct {
|
2019-10-29 17:17:45 +01:00
|
|
|
outpoint wire.OutPoint
|
|
|
|
witnessType WitnessType
|
|
|
|
signDesc SignDescriptor
|
|
|
|
heightHint uint32
|
|
|
|
blockToMaturity uint32
|
2021-07-14 01:19:26 +02:00
|
|
|
cltvExpiry uint32
|
2020-09-04 11:28:17 +02:00
|
|
|
|
|
|
|
// unconfParent contains information about a potential unconfirmed
|
|
|
|
// parent transaction.
|
|
|
|
unconfParent *TxInfo
|
2018-09-26 17:59:30 +02:00
|
|
|
}
|
2018-09-21 19:49:58 +02:00
|
|
|
|
2018-10-18 01:27:11 +02:00
|
|
|
// OutPoint returns the breached output's identifier that is to be included as
|
|
|
|
// a transaction input.
|
2018-09-26 20:00:10 +02:00
|
|
|
func (i *inputKit) OutPoint() *wire.OutPoint {
|
|
|
|
return &i.outpoint
|
2018-09-26 17:59:30 +02:00
|
|
|
}
|
|
|
|
|
2020-11-06 19:59:11 +01:00
|
|
|
// RequiredTxOut returns a nil for the base input type.
|
|
|
|
func (i *inputKit) RequiredTxOut() *wire.TxOut {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-06 19:35:01 +01: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) {
|
2021-07-14 01:19:26 +02:00
|
|
|
return i.cltvExpiry, i.cltvExpiry > 0
|
2020-11-06 19:35:01 +01:00
|
|
|
}
|
|
|
|
|
2018-09-26 17:59:30 +02:00
|
|
|
// WitnessType returns the type of witness that must be generated to spend the
|
|
|
|
// breached output.
|
2019-01-16 15:47:43 +01:00
|
|
|
func (i *inputKit) WitnessType() WitnessType {
|
2018-09-26 20:00:10 +02:00
|
|
|
return i.witnessType
|
2018-09-26 17:59:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// SignDesc returns the breached output's SignDescriptor, which is used during
|
|
|
|
// signing to compute the witness.
|
2019-01-16 15:47:43 +01:00
|
|
|
func (i *inputKit) SignDesc() *SignDescriptor {
|
2018-09-26 20:00:10 +02:00
|
|
|
return &i.signDesc
|
|
|
|
}
|
|
|
|
|
2018-11-07 16:35:54 +01: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 17:17:45 +01: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 11:28:17 +02:00
|
|
|
// Cpfp returns information about a possibly unconfirmed parent tx.
|
|
|
|
func (i *inputKit) UnconfParent() *TxInfo {
|
|
|
|
return i.unconfParent
|
|
|
|
}
|
|
|
|
|
2023-03-02 06:50:55 +01:00
|
|
|
// BaseInput contains all the information needed to sweep a basic
|
|
|
|
// output (CSV/CLTV/no time lock).
|
2018-09-26 20:00:10 +02:00
|
|
|
type BaseInput struct {
|
|
|
|
inputKit
|
|
|
|
}
|
|
|
|
|
|
|
|
// MakeBaseInput assembles a new BaseInput that can be used to construct a
|
|
|
|
// sweep transaction.
|
2019-01-16 15:47:43 +01:00
|
|
|
func MakeBaseInput(outpoint *wire.OutPoint, witnessType WitnessType,
|
2020-09-04 11:28:17 +02:00
|
|
|
signDescriptor *SignDescriptor, heightHint uint32,
|
|
|
|
unconfParent *TxInfo) BaseInput {
|
2018-09-26 20:00:10 +02:00
|
|
|
|
|
|
|
return BaseInput{
|
|
|
|
inputKit{
|
2020-09-04 11:28:17 +02:00
|
|
|
outpoint: *outpoint,
|
|
|
|
witnessType: witnessType,
|
|
|
|
signDesc: *signDescriptor,
|
|
|
|
heightHint: heightHint,
|
|
|
|
unconfParent: unconfParent,
|
2018-09-26 20:00:10 +02:00
|
|
|
},
|
|
|
|
}
|
2018-09-26 17:59:30 +02:00
|
|
|
}
|
2018-09-21 19:49:58 +02:00
|
|
|
|
2019-02-06 03:27:07 +01: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 11:28:17 +02:00
|
|
|
outpoint, witnessType, signDescriptor, heightHint, nil,
|
2019-02-06 03:27:07 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
return &input
|
|
|
|
}
|
|
|
|
|
2019-10-29 17:17:45 +01: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,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-14 01:19:26 +02:00
|
|
|
// NewCsvInputWithCltv assembles a new csv and cltv locked input that can be
|
|
|
|
// used to construct a sweep transaction.
|
|
|
|
func NewCsvInputWithCltv(outpoint *wire.OutPoint, witnessType WitnessType,
|
|
|
|
signDescriptor *SignDescriptor, heightHint uint32,
|
|
|
|
csvDelay uint32, cltvExpiry uint32) *BaseInput {
|
|
|
|
|
|
|
|
return &BaseInput{
|
|
|
|
inputKit{
|
|
|
|
outpoint: *outpoint,
|
|
|
|
witnessType: witnessType,
|
|
|
|
signDesc: *signDescriptor,
|
|
|
|
heightHint: heightHint,
|
|
|
|
blockToMaturity: csvDelay,
|
|
|
|
cltvExpiry: cltvExpiry,
|
|
|
|
unconfParent: nil,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-18 05:48:41 +01:00
|
|
|
// CraftInputScript returns a valid set of input scripts allowing this output
|
2019-05-10 17:14:19 +02:00
|
|
|
// to be spent. The returned input scripts should target the input at location
|
2018-11-18 05:48:41 +01: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 15:47:43 +01:00
|
|
|
func (bi *BaseInput) CraftInputScript(signer Signer, txn *wire.MsgTx,
|
2022-03-18 18:37:44 +01:00
|
|
|
hashCache *txscript.TxSigHashes,
|
|
|
|
prevOutputFetcher txscript.PrevOutputFetcher, txinIdx int) (*Script,
|
|
|
|
error) {
|
2018-09-26 17:59:30 +02:00
|
|
|
|
2022-03-18 18:37:44 +01:00
|
|
|
signDesc := bi.SignDesc()
|
|
|
|
signDesc.PrevOutputFetcher = prevOutputFetcher
|
|
|
|
witnessFunc := bi.witnessType.WitnessGenerator(signer, signDesc)
|
2018-09-26 17:59:30 +02:00
|
|
|
|
|
|
|
return witnessFunc(txn, hashCache, txinIdx)
|
2018-09-21 19:49:58 +02:00
|
|
|
}
|
2018-09-26 17:59:30 +02:00
|
|
|
|
2018-09-26 20:00:10 +02:00
|
|
|
// HtlcSucceedInput constitutes a sweep input that needs a pre-image. The input
|
2018-10-18 01:27:11 +02: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 20:00:10 +02: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 16:11:47 +01:00
|
|
|
signDescriptor *SignDescriptor, preimage []byte, heightHint,
|
|
|
|
blocksToMaturity uint32) HtlcSucceedInput {
|
2018-09-26 20:00:10 +02:00
|
|
|
|
|
|
|
return HtlcSucceedInput{
|
|
|
|
inputKit: inputKit{
|
2020-03-06 16:11:47 +01:00
|
|
|
outpoint: *outpoint,
|
|
|
|
witnessType: HtlcAcceptedRemoteSuccess,
|
|
|
|
signDesc: *signDescriptor,
|
|
|
|
heightHint: heightHint,
|
|
|
|
blockToMaturity: blocksToMaturity,
|
2018-09-26 20:00:10 +02:00
|
|
|
},
|
|
|
|
preimage: preimage,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-02 06:50:55 +01:00
|
|
|
// MakeTaprootHtlcSucceedInput creates a new HtlcSucceedInput that can be used
|
|
|
|
// to spend an HTLC output for a taproot channel on the remote party's
|
|
|
|
// commitment transaction.
|
|
|
|
func MakeTaprootHtlcSucceedInput(op *wire.OutPoint, signDesc *SignDescriptor,
|
|
|
|
preimage []byte, heightHint, blocksToMaturity uint32) HtlcSucceedInput {
|
|
|
|
|
|
|
|
return HtlcSucceedInput{
|
|
|
|
inputKit: inputKit{
|
|
|
|
outpoint: *op,
|
|
|
|
witnessType: TaprootHtlcAcceptedRemoteSuccess,
|
|
|
|
signDesc: *signDesc,
|
|
|
|
heightHint: heightHint,
|
|
|
|
blockToMaturity: blocksToMaturity,
|
|
|
|
},
|
|
|
|
preimage: preimage,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-18 05:48:41 +01: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 15:47:43 +01:00
|
|
|
func (h *HtlcSucceedInput) CraftInputScript(signer Signer, txn *wire.MsgTx,
|
2022-03-18 18:37:44 +01:00
|
|
|
hashCache *txscript.TxSigHashes,
|
|
|
|
prevOutputFetcher txscript.PrevOutputFetcher, txinIdx int) (*Script,
|
|
|
|
error) {
|
2018-09-26 20:00:10 +02:00
|
|
|
|
|
|
|
desc := h.signDesc
|
|
|
|
desc.SigHashes = hashCache
|
|
|
|
desc.InputIndex = txinIdx
|
2022-03-18 18:37:44 +01:00
|
|
|
desc.PrevOutputFetcher = prevOutputFetcher
|
2018-09-26 20:00:10 +02:00
|
|
|
|
2023-03-02 06:50:55 +01:00
|
|
|
isTaproot := txscript.IsPayToTaproot(desc.Output.PkScript)
|
|
|
|
|
|
|
|
var (
|
|
|
|
witness wire.TxWitness
|
|
|
|
err error
|
2018-09-26 20:00:10 +02:00
|
|
|
)
|
2023-03-02 06:50:55 +01:00
|
|
|
if isTaproot {
|
|
|
|
if desc.ControlBlock == nil {
|
|
|
|
return nil, fmt.Errorf("ctrl block must be set")
|
|
|
|
}
|
|
|
|
|
|
|
|
desc.SignMethod = TaprootScriptSpendSignMethod
|
|
|
|
|
|
|
|
witness, err = SenderHTLCScriptTaprootRedeem(
|
|
|
|
signer, &desc, txn, h.preimage, nil, nil,
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
witness, err = SenderHtlcSpendRedeem(
|
|
|
|
signer, &desc, txn, h.preimage,
|
|
|
|
)
|
|
|
|
}
|
2018-11-18 05:48:41 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-01-16 15:47:43 +01:00
|
|
|
return &Script{
|
2018-11-18 05:48:41 +01:00
|
|
|
Witness: witness,
|
|
|
|
}, nil
|
2018-09-26 20:00:10 +02:00
|
|
|
}
|
|
|
|
|
2020-12-09 12:24:03 +01: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,
|
2022-03-18 18:37:44 +01:00
|
|
|
hashCache *txscript.TxSigHashes,
|
|
|
|
prevOutputFetcher txscript.PrevOutputFetcher,
|
|
|
|
txinIdx int) (wire.TxWitness, error)
|
2020-12-09 12:24:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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,
|
2022-03-18 18:37:44 +01:00
|
|
|
prevOutputFetcher txscript.PrevOutputFetcher, txinIdx int) (*Script,
|
|
|
|
error) {
|
2020-12-09 12:24:03 +01:00
|
|
|
|
2022-03-18 18:37:44 +01:00
|
|
|
witness, err := i.createWitness(
|
|
|
|
signer, txn, hashCache, prevOutputFetcher, txinIdx,
|
|
|
|
)
|
2020-12-09 12:24:03 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Script{
|
|
|
|
Witness: witness,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-12-09 12:24:03 +01: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,
|
2022-03-18 18:37:44 +01:00
|
|
|
prevOutputFetcher txscript.PrevOutputFetcher,
|
2020-12-09 12:24:03 +01:00
|
|
|
txinIdx int) (wire.TxWitness, error) {
|
|
|
|
|
|
|
|
desc := signDetails.SignDesc
|
2022-03-18 18:37:44 +01:00
|
|
|
desc.SigHashes = txscript.NewTxSigHashes(txn, prevOutputFetcher)
|
2020-12-09 12:24:03 +01:00
|
|
|
desc.InputIndex = txinIdx
|
2022-03-18 18:37:44 +01:00
|
|
|
desc.PrevOutputFetcher = prevOutputFetcher
|
2020-12-09 12:24:03 +01:00
|
|
|
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-02 06:50:36 +01:00
|
|
|
// MakeHtlcSecondLevelTimeoutTaprootInput creates an input that allows the
|
|
|
|
// sweeper to spend an HTLC output to the second level on our commitment
|
|
|
|
// transaction. The sweeper is also able to generate witnesses on demand to
|
|
|
|
// sweep the second level HTLC aggregated with other transactions.
|
|
|
|
func MakeHtlcSecondLevelTimeoutTaprootInput(signedTx *wire.MsgTx,
|
|
|
|
signDetails *SignDetails, heightHint uint32) HtlcSecondLevelAnchorInput {
|
|
|
|
|
|
|
|
createWitness := func(signer Signer, txn *wire.MsgTx,
|
|
|
|
hashCache *txscript.TxSigHashes,
|
|
|
|
prevOutputFetcher txscript.PrevOutputFetcher,
|
|
|
|
txinIdx int) (wire.TxWitness, error) {
|
|
|
|
|
|
|
|
desc := signDetails.SignDesc
|
|
|
|
if desc.ControlBlock == nil {
|
|
|
|
return nil, fmt.Errorf("ctrl block must be set")
|
|
|
|
}
|
|
|
|
|
|
|
|
desc.SigHashes = txscript.NewTxSigHashes(txn, prevOutputFetcher)
|
|
|
|
desc.InputIndex = txinIdx
|
|
|
|
desc.PrevOutputFetcher = prevOutputFetcher
|
|
|
|
|
|
|
|
desc.SignMethod = TaprootScriptSpendSignMethod
|
|
|
|
|
|
|
|
return SenderHTLCScriptTaprootTimeout(
|
|
|
|
signDetails.PeerSig, signDetails.SigHashType, signer,
|
|
|
|
&desc, txn, nil, nil,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return HtlcSecondLevelAnchorInput{
|
|
|
|
inputKit: inputKit{
|
|
|
|
outpoint: signedTx.TxIn[0].PreviousOutPoint,
|
|
|
|
witnessType: TaprootHtlcLocalOfferedTimeout,
|
|
|
|
signDesc: signDetails.SignDesc,
|
|
|
|
heightHint: heightHint,
|
|
|
|
|
|
|
|
// CSV delay is always 1 for these inputs.
|
|
|
|
blockToMaturity: 1,
|
|
|
|
},
|
|
|
|
SignedTx: signedTx,
|
|
|
|
createWitness: createWitness,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-09 12:24:03 +01: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,
|
2022-03-18 18:37:44 +01:00
|
|
|
prevOutputFetcher txscript.PrevOutputFetcher,
|
2020-12-09 12:24:03 +01:00
|
|
|
txinIdx int) (wire.TxWitness, error) {
|
|
|
|
|
|
|
|
desc := signDetails.SignDesc
|
|
|
|
desc.SigHashes = hashCache
|
|
|
|
desc.InputIndex = txinIdx
|
2022-03-18 18:37:44 +01:00
|
|
|
desc.PrevOutputFetcher = prevOutputFetcher
|
2020-12-09 12:24:03 +01:00
|
|
|
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-02 06:50:36 +01:00
|
|
|
// MakeHtlcSecondLevelSuccessTaprootInput creates an input that allows the
|
|
|
|
// sweeper to spend an HTLC output to the second level on our taproot
|
|
|
|
// commitment transaction.
|
|
|
|
func MakeHtlcSecondLevelSuccessTaprootInput(signedTx *wire.MsgTx,
|
|
|
|
signDetails *SignDetails, preimage lntypes.Preimage,
|
|
|
|
heightHint uint32) HtlcSecondLevelAnchorInput {
|
|
|
|
|
|
|
|
createWitness := func(signer Signer, txn *wire.MsgTx,
|
|
|
|
hashCache *txscript.TxSigHashes,
|
|
|
|
prevOutputFetcher txscript.PrevOutputFetcher,
|
|
|
|
txinIdx int) (wire.TxWitness, error) {
|
|
|
|
|
|
|
|
desc := signDetails.SignDesc
|
|
|
|
if desc.ControlBlock == nil {
|
|
|
|
return nil, fmt.Errorf("ctrl block must be set")
|
|
|
|
}
|
|
|
|
|
|
|
|
desc.SigHashes = txscript.NewTxSigHashes(txn, prevOutputFetcher)
|
|
|
|
desc.InputIndex = txinIdx
|
|
|
|
desc.PrevOutputFetcher = prevOutputFetcher
|
|
|
|
|
|
|
|
desc.SignMethod = TaprootScriptSpendSignMethod
|
|
|
|
|
|
|
|
return ReceiverHTLCScriptTaprootRedeem(
|
|
|
|
signDetails.PeerSig, signDetails.SigHashType,
|
|
|
|
preimage[:], signer, &desc, txn, nil, nil,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return HtlcSecondLevelAnchorInput{
|
|
|
|
inputKit: inputKit{
|
|
|
|
outpoint: signedTx.TxIn[0].PreviousOutPoint,
|
|
|
|
witnessType: TaprootHtlcAcceptedLocalSuccess,
|
|
|
|
signDesc: signDetails.SignDesc,
|
|
|
|
heightHint: heightHint,
|
|
|
|
|
|
|
|
// CSV delay is always 1 for these inputs.
|
|
|
|
blockToMaturity: 1,
|
|
|
|
},
|
|
|
|
SignedTx: signedTx,
|
|
|
|
createWitness: createWitness,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-18 01:27:11 +02:00
|
|
|
// Compile-time constraints to ensure each input struct implement the Input
|
|
|
|
// interface.
|
2018-09-26 17:59:30 +02:00
|
|
|
var _ Input = (*BaseInput)(nil)
|
2018-09-26 20:00:10 +02:00
|
|
|
var _ Input = (*HtlcSucceedInput)(nil)
|
2020-12-09 12:24:03 +01:00
|
|
|
var _ Input = (*HtlcSecondLevelAnchorInput)(nil)
|