Skip to content

Commit 1e7c541

Browse files
committed
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.
1 parent af60fa0 commit 1e7c541

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

contractcourt/breach_arbitrator.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1160,6 +1160,11 @@ func (bo *breachedOutput) SignDesc() *input.SignDescriptor {
11601160
return &bo.signDesc
11611161
}
11621162

1163+
// Preimage returns the preimage that was used to create the breached output.
1164+
func (bo *breachedOutput) Preimage() fn.Option[lntypes.Preimage] {
1165+
return fn.None[lntypes.Preimage]()
1166+
}
1167+
11631168
// CraftInputScript computes a valid witness that allows us to spend from the
11641169
// breached output. It does so by first generating and memoizing the witness
11651170
// generation function, which parameterized primarily by the witness type and

input/input.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ type Input interface {
6969
// ResolutionBlob returns a special opaque blob to be used to
7070
// sweep/resolve this input.
7171
ResolutionBlob() fn.Option[tlv.Blob]
72+
73+
// Preimage returns the preimage for the input if it is an HTLC input.
74+
Preimage() fn.Option[lntypes.Preimage]
7275
}
7376

7477
// 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,
285288
return witnessFunc(txn, hashCache, txinIdx)
286289
}
287290

291+
// Preimage returns the preimage for the input if it is an HTLC input.
292+
func (bi *BaseInput) Preimage() fn.Option[lntypes.Preimage] {
293+
return fn.None[lntypes.Preimage]()
294+
}
295+
288296
// HtlcSucceedInput constitutes a sweep input that needs a pre-image. The input
289297
// is expected to reside on the commitment tx of the remote party and should
290298
// not be a second level tx output.
@@ -357,7 +365,6 @@ func (h *HtlcSucceedInput) CraftInputScript(signer Signer, txn *wire.MsgTx,
357365
}
358366

359367
desc.SignMethod = TaprootScriptSpendSignMethod
360-
361368
witness, err = SenderHTLCScriptTaprootRedeem(
362369
signer, &desc, txn, h.preimage, nil, nil,
363370
)
@@ -375,6 +382,15 @@ func (h *HtlcSucceedInput) CraftInputScript(signer Signer, txn *wire.MsgTx,
375382
}, nil
376383
}
377384

385+
// Preimage returns the preimage for the input if it is an HTLC input.
386+
func (h *HtlcSucceedInput) Preimage() fn.Option[lntypes.Preimage] {
387+
if len(h.preimage) == 0 {
388+
return fn.None[lntypes.Preimage]()
389+
}
390+
391+
return fn.Some(lntypes.Preimage(h.preimage))
392+
}
393+
378394
// HtlcSecondLevelAnchorInput is an input type used to spend HTLC outputs
379395
// using a re-signed second level transaction, either via the timeout or success
380396
// paths.
@@ -391,6 +407,8 @@ type HtlcSecondLevelAnchorInput struct {
391407
hashCache *txscript.TxSigHashes,
392408
prevOutputFetcher txscript.PrevOutputFetcher,
393409
txinIdx int) (wire.TxWitness, error)
410+
411+
preimage []byte
394412
}
395413

396414
// RequiredTxOut returns the tx out needed to be present on the sweep tx for
@@ -427,6 +445,15 @@ func (i *HtlcSecondLevelAnchorInput) CraftInputScript(signer Signer,
427445
}, nil
428446
}
429447

448+
// Preimage returns the preimage for the input if it is an HTLC input.
449+
func (i *HtlcSecondLevelAnchorInput) Preimage() fn.Option[lntypes.Preimage] {
450+
if len(i.preimage) == 0 {
451+
return fn.None[lntypes.Preimage]()
452+
}
453+
454+
return fn.Some(lntypes.Preimage(i.preimage))
455+
}
456+
430457
// MakeHtlcSecondLevelTimeoutAnchorInput creates an input allowing the sweeper
431458
// to spend the HTLC output on our commit using the second level timeout
432459
// transaction.
@@ -545,6 +572,7 @@ func MakeHtlcSecondLevelSuccessAnchorInput(signedTx *wire.MsgTx,
545572
SignedTx: signedTx,
546573
inputKit: input.inputKit,
547574
createWitness: createWitness,
575+
preimage: preimage[:],
548576
}
549577
}
550578

@@ -588,6 +616,7 @@ func MakeHtlcSecondLevelSuccessTaprootInput(signedTx *wire.MsgTx,
588616
inputKit: input.inputKit,
589617
SignedTx: signedTx,
590618
createWitness: createWitness,
619+
preimage: preimage[:],
591620
}
592621
}
593622

input/mocks.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,17 @@ func (m *MockInput) ResolutionBlob() fn.Option[tlv.Blob] {
140140
return info.(fn.Option[tlv.Blob])
141141
}
142142

143+
func (m *MockInput) Preimage() fn.Option[lntypes.Preimage] {
144+
args := m.Called()
145+
146+
info := args.Get(0)
147+
if info == nil {
148+
return fn.None[lntypes.Preimage]()
149+
}
150+
151+
return info.(fn.Option[lntypes.Preimage])
152+
}
153+
143154
// MockWitnessType implements the `WitnessType` interface and is used by other
144155
// packages for mock testing.
145156
type MockWitnessType struct {

0 commit comments

Comments
 (0)