input: add new Preimage method to input.Input

In this commit, we add a new method to obtain an option of a preimage to
the input.Input struct. This is useful for callers that have an Input,
and want to optionally obtain the preimage.
This commit is contained in:
Olaoluwa Osuntokun
2024-10-15 19:17:06 -07:00
parent af60fa0c3a
commit 1e7c5415ae
3 changed files with 46 additions and 1 deletions

View File

@@ -69,6 +69,9 @@ type Input interface {
// ResolutionBlob returns a special opaque blob to be used to
// sweep/resolve this input.
ResolutionBlob() fn.Option[tlv.Blob]
// Preimage returns the preimage for the input if it is an HTLC input.
Preimage() fn.Option[lntypes.Preimage]
}
// TxInfo describes properties of a parent tx that are relevant for CPFP.
@@ -285,6 +288,11 @@ func (bi *BaseInput) CraftInputScript(signer Signer, txn *wire.MsgTx,
return witnessFunc(txn, hashCache, txinIdx)
}
// Preimage returns the preimage for the input if it is an HTLC input.
func (bi *BaseInput) Preimage() fn.Option[lntypes.Preimage] {
return fn.None[lntypes.Preimage]()
}
// HtlcSucceedInput constitutes a sweep input that needs a pre-image. The input
// is expected to reside on the commitment tx of the remote party and should
// not be a second level tx output.
@@ -357,7 +365,6 @@ func (h *HtlcSucceedInput) CraftInputScript(signer Signer, txn *wire.MsgTx,
}
desc.SignMethod = TaprootScriptSpendSignMethod
witness, err = SenderHTLCScriptTaprootRedeem(
signer, &desc, txn, h.preimage, nil, nil,
)
@@ -375,6 +382,15 @@ func (h *HtlcSucceedInput) CraftInputScript(signer Signer, txn *wire.MsgTx,
}, nil
}
// Preimage returns the preimage for the input if it is an HTLC input.
func (h *HtlcSucceedInput) Preimage() fn.Option[lntypes.Preimage] {
if len(h.preimage) == 0 {
return fn.None[lntypes.Preimage]()
}
return fn.Some(lntypes.Preimage(h.preimage))
}
// HtlcSecondLevelAnchorInput is an input type used to spend HTLC outputs
// using a re-signed second level transaction, either via the timeout or success
// paths.
@@ -391,6 +407,8 @@ type HtlcSecondLevelAnchorInput struct {
hashCache *txscript.TxSigHashes,
prevOutputFetcher txscript.PrevOutputFetcher,
txinIdx int) (wire.TxWitness, error)
preimage []byte
}
// RequiredTxOut returns the tx out needed to be present on the sweep tx for
@@ -427,6 +445,15 @@ func (i *HtlcSecondLevelAnchorInput) CraftInputScript(signer Signer,
}, nil
}
// Preimage returns the preimage for the input if it is an HTLC input.
func (i *HtlcSecondLevelAnchorInput) Preimage() fn.Option[lntypes.Preimage] {
if len(i.preimage) == 0 {
return fn.None[lntypes.Preimage]()
}
return fn.Some(lntypes.Preimage(i.preimage))
}
// MakeHtlcSecondLevelTimeoutAnchorInput creates an input allowing the sweeper
// to spend the HTLC output on our commit using the second level timeout
// transaction.
@@ -545,6 +572,7 @@ func MakeHtlcSecondLevelSuccessAnchorInput(signedTx *wire.MsgTx,
SignedTx: signedTx,
inputKit: input.inputKit,
createWitness: createWitness,
preimage: preimage[:],
}
}
@@ -588,6 +616,7 @@ func MakeHtlcSecondLevelSuccessTaprootInput(signedTx *wire.MsgTx,
inputKit: input.inputKit,
SignedTx: signedTx,
createWitness: createWitness,
preimage: preimage[:],
}
}