mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-08-30 23:53:41 +02:00
lnwallet+sweep: extend the WitnessGenerator type to return an InputScript
In this commit, we extend the WitnessGenerator type to now return an InputScript. This allows it to be more encompassing, as now callers can expect a sigScript to be populated if the input being swept requires a sigScript field. Along the way, we've also renamed input.BuildWitness to input.CraftInputScript. We also take a step towards allowing the sweeper to sweep transactions for n2pwkh outputs. We do so by modifying the BuiltWitness method to instead return an InputScript. Additionally, when populating inputs if a sigScript is present, it will now be populated.
This commit is contained in:
@@ -6,7 +6,10 @@ import (
|
||||
"github.com/lightningnetwork/lnd/lnwallet"
|
||||
)
|
||||
|
||||
// Input contains all data needed to construct a sweep tx input.
|
||||
// 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.
|
||||
type Input interface {
|
||||
// Outpoint returns the reference to the output being spent, used to
|
||||
// construct the corresponding transaction input.
|
||||
@@ -21,12 +24,14 @@ type Input interface {
|
||||
// that spends this output.
|
||||
SignDesc() *lnwallet.SignDescriptor
|
||||
|
||||
// BuildWitness returns a valid witness allowing this output to be
|
||||
// spent, the witness should be attached to the transaction at the
|
||||
// location determined by the given `txinIdx`.
|
||||
BuildWitness(signer lnwallet.Signer, txn *wire.MsgTx,
|
||||
// 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.
|
||||
CraftInputScript(signer lnwallet.Signer, txn *wire.MsgTx,
|
||||
hashCache *txscript.TxSigHashes,
|
||||
txinIdx int) ([][]byte, error)
|
||||
txinIdx int) (*lnwallet.InputScript, error)
|
||||
|
||||
// BlocksToMaturity returns the relative timelock, as a number of
|
||||
// blocks, that must be built on top of the confirmation height before
|
||||
@@ -91,12 +96,12 @@ func MakeBaseInput(outpoint *wire.OutPoint, witnessType lnwallet.WitnessType,
|
||||
}
|
||||
}
|
||||
|
||||
// BuildWitness computes a valid witness that allows us to spend from the
|
||||
// breached output. It does so by generating the witness generation function,
|
||||
// which is parameterized primarily by the witness type and sign descriptor.
|
||||
// The method then returns the witness computed by invoking this function.
|
||||
func (bi *BaseInput) BuildWitness(signer lnwallet.Signer, txn *wire.MsgTx,
|
||||
hashCache *txscript.TxSigHashes, txinIdx int) ([][]byte, error) {
|
||||
// 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 (bi *BaseInput) CraftInputScript(signer lnwallet.Signer, txn *wire.MsgTx,
|
||||
hashCache *txscript.TxSigHashes, txinIdx int) (*lnwallet.InputScript, error) {
|
||||
|
||||
witnessFunc := bi.witnessType.GenWitnessFunc(
|
||||
signer, bi.SignDesc(),
|
||||
@@ -138,20 +143,27 @@ func MakeHtlcSucceedInput(outpoint *wire.OutPoint,
|
||||
}
|
||||
}
|
||||
|
||||
// BuildWitness computes a valid witness that allows us to spend from the
|
||||
// breached output. For HtlcSpendInput it will need to make the preimage part
|
||||
// of the witness.
|
||||
func (h *HtlcSucceedInput) BuildWitness(signer lnwallet.Signer, txn *wire.MsgTx,
|
||||
hashCache *txscript.TxSigHashes, txinIdx int) ([][]byte, error) {
|
||||
// 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 (h *HtlcSucceedInput) CraftInputScript(signer lnwallet.Signer, txn *wire.MsgTx,
|
||||
hashCache *txscript.TxSigHashes, txinIdx int) (*lnwallet.InputScript, error) {
|
||||
|
||||
desc := h.signDesc
|
||||
desc.SigHashes = hashCache
|
||||
desc.InputIndex = txinIdx
|
||||
|
||||
return lnwallet.SenderHtlcSpendRedeem(
|
||||
signer, &desc, txn,
|
||||
h.preimage,
|
||||
witness, err := lnwallet.SenderHtlcSpendRedeem(
|
||||
signer, &desc, txn, h.preimage,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &lnwallet.InputScript{
|
||||
Witness: witness,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// BlocksToMaturity returns the relative timelock, as a number of blocks, that
|
||||
|
@@ -216,25 +216,29 @@ func createSweepTx(inputs []Input, outputPkScript []byte,
|
||||
|
||||
hashCache := txscript.NewTxSigHashes(sweepTx)
|
||||
|
||||
// With all the inputs in place, use each output's unique witness
|
||||
// With all the inputs in place, use each output's unique input script
|
||||
// function to generate the final witness required for spending.
|
||||
addWitness := func(idx int, tso Input) error {
|
||||
witness, err := tso.BuildWitness(
|
||||
addInputScript := func(idx int, tso Input) error {
|
||||
inputScript, err := tso.CraftInputScript(
|
||||
signer, sweepTx, hashCache, idx,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
sweepTx.TxIn[idx].Witness = witness
|
||||
sweepTx.TxIn[idx].Witness = inputScript.Witness
|
||||
|
||||
if len(inputScript.SigScript) != 0 {
|
||||
sweepTx.TxIn[idx].SignatureScript = inputScript.SigScript
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Finally we'll attach a valid witness to each csv and cltv input
|
||||
// Finally we'll attach a valid input script to each csv and cltv input
|
||||
// within the sweeping transaction.
|
||||
for i, input := range inputs {
|
||||
if err := addWitness(i, input); err != nil {
|
||||
if err := addInputScript(i, input); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user