Skip to content

Commit 890a1ca

Browse files
committed
multi: use prev output fetcher everywhere
1 parent 6ba000e commit 890a1ca

File tree

7 files changed

+65
-46
lines changed

7 files changed

+65
-46
lines changed

cmd/chantools/closepoolaccount.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,11 @@ func closePoolAccount(extendedKey *hdkeychain.ExtendedKey, apiURL string,
241241
// Calculate the fee based on the given fee rate and our weight
242242
// estimation.
243243
var (
244-
estimator input.TxWeightEstimator
245-
signDesc = &input.SignDescriptor{
244+
estimator input.TxWeightEstimator
245+
prevOutFetcher = txscript.NewCannedPrevOutputFetcher(
246+
pkScript, sweepValue,
247+
)
248+
signDesc = &input.SignDescriptor{
246249
KeyDesc: keychain.KeyDescriptor{
247250
KeyLocator: keychain.KeyLocator{
248251
Family: poolscript.AccountKeyFamily,
@@ -255,10 +258,8 @@ func closePoolAccount(extendedKey *hdkeychain.ExtendedKey, apiURL string,
255258
PkScript: pkScript,
256259
Value: sweepValue,
257260
},
258-
InputIndex: 0,
259-
PrevOutputFetcher: txscript.NewCannedPrevOutputFetcher(
260-
pkScript, sweepValue,
261-
),
261+
InputIndex: 0,
262+
PrevOutputFetcher: prevOutFetcher,
262263
}
263264
)
264265

@@ -267,7 +268,9 @@ func closePoolAccount(extendedKey *hdkeychain.ExtendedKey, apiURL string,
267268
estimator.AddWitnessInput(poolscript.ExpiryWitnessSize)
268269
signDesc.HashType = txscript.SigHashAll
269270
signDesc.SignMethod = input.WitnessV0SignMethod
270-
signDesc.SigHashes = input.NewTxSigHashesV0Only(sweepTx)
271+
signDesc.SigHashes = txscript.NewTxSigHashes(
272+
sweepTx, prevOutFetcher,
273+
)
271274

272275
case account.VersionTaprootEnabled:
273276
estimator.AddWitnessInput(poolscript.TaprootExpiryWitnessSize)

cmd/chantools/recoverloopin.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -258,8 +258,12 @@ func getSignedTx(signer *lnd.Signer, loopIn *loopdb.LoopIn, sweepTx *wire.MsgTx,
258258
keyIndex uint32) ([]byte, error) {
259259

260260
// Create the sign descriptor.
261-
prevoutFetcher := txscript.NewCannedPrevOutputFetcher(
262-
htlc.PkScript, int64(loopIn.Contract.AmountRequested),
261+
prevTxOut := &wire.TxOut{
262+
PkScript: htlc.PkScript,
263+
Value: int64(loopIn.Contract.AmountRequested),
264+
}
265+
prevOutputFetcher := txscript.NewCannedPrevOutputFetcher(
266+
prevTxOut.PkScript, prevTxOut.Value,
263267
)
264268

265269
signDesc := &input.SignDescriptor{
@@ -272,11 +276,8 @@ func getSignedTx(signer *lnd.Signer, loopIn *loopdb.LoopIn, sweepTx *wire.MsgTx,
272276
WitnessScript: htlc.TimeoutScript(),
273277
HashType: htlc.SigHash(),
274278
InputIndex: 0,
275-
PrevOutputFetcher: prevoutFetcher,
276-
Output: &wire.TxOut{
277-
PkScript: htlc.PkScript,
278-
Value: int64(loopIn.Contract.AmountRequested),
279-
},
279+
PrevOutputFetcher: prevOutputFetcher,
280+
Output: prevTxOut,
280281
}
281282
switch htlc.Version {
282283
case swap.HtlcV2:
@@ -303,13 +304,13 @@ func getSignedTx(signer *lnd.Signer, loopIn *loopdb.LoopIn, sweepTx *wire.MsgTx,
303304
return nil, err
304305
}
305306

306-
sighashes := txscript.NewTxSigHashes(sweepTx, prevoutFetcher)
307+
sigHashes := txscript.NewTxSigHashes(sweepTx, prevOutputFetcher)
307308

308309
// Verify the signature. This will throw an error if the signature is
309310
// invalid and allows us to bruteforce the key index.
310311
vm, err := txscript.NewEngine(
311-
htlc.PkScript, sweepTx, 0, txscript.StandardVerifyFlags, nil,
312-
sighashes, int64(loopIn.Contract.AmountRequested), prevoutFetcher,
312+
prevTxOut.PkScript, sweepTx, 0, txscript.StandardVerifyFlags,
313+
nil, sigHashes, prevTxOut.Value, prevOutputFetcher,
313314
)
314315
if err != nil {
315316
return nil, err

cmd/chantools/sweepremoteclosed.go

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ func sweepRemoteClosed(extendedKey *hdkeychain.ExtendedKey, apiURL,
173173
signDescs []*input.SignDescriptor
174174
sweepTx = wire.NewMsgTx(2)
175175
totalOutputValue = uint64(0)
176+
prevOutFetcher = txscript.NewMultiPrevOutFetcher(nil)
176177
)
177178

178179
// Add all found target outputs.
@@ -207,22 +208,25 @@ func sweepRemoteClosed(extendedKey *hdkeychain.ExtendedKey, apiURL,
207208
sequence = 1
208209
}
209210

211+
prevOutPoint := wire.OutPoint{
212+
Hash: *txHash,
213+
Index: uint32(vout.Outspend.Vin),
214+
}
215+
prevTxOut := &wire.TxOut{
216+
PkScript: pkScript,
217+
Value: int64(vout.Value),
218+
}
219+
prevOutFetcher.AddPrevOut(prevOutPoint, prevTxOut)
210220
sweepTx.TxIn = append(sweepTx.TxIn, &wire.TxIn{
211-
PreviousOutPoint: wire.OutPoint{
212-
Hash: *txHash,
213-
Index: uint32(vout.Outspend.Vin),
214-
},
215-
Sequence: sequence,
221+
PreviousOutPoint: prevOutPoint,
222+
Sequence: sequence,
216223
})
217224

218225
signDescs = append(signDescs, &input.SignDescriptor{
219226
KeyDesc: *target.keyDesc,
220227
WitnessScript: target.script,
221-
Output: &wire.TxOut{
222-
PkScript: pkScript,
223-
Value: int64(vout.Value),
224-
},
225-
HashType: txscript.SigHashAll,
228+
Output: prevTxOut,
229+
HashType: txscript.SigHashAll,
226230
})
227231
}
228232
}
@@ -259,7 +263,7 @@ func sweepRemoteClosed(extendedKey *hdkeychain.ExtendedKey, apiURL,
259263
ExtendedKey: extendedKey,
260264
ChainParams: chainParams,
261265
}
262-
sigHashes = input.NewTxSigHashesV0Only(sweepTx)
266+
sigHashes = txscript.NewTxSigHashes(sweepTx, prevOutFetcher)
263267
)
264268
for idx, desc := range signDescs {
265269
desc.SigHashes = sigHashes

cmd/chantools/sweeptimelock.go

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -222,11 +222,13 @@ func sweepTimeLock(extendedKey *hdkeychain.ExtendedKey, apiURL string,
222222
}
223223
api := &btc.ExplorerAPI{BaseURL: apiURL}
224224

225-
sweepTx := wire.NewMsgTx(2)
226-
totalOutputValue := int64(0)
227-
signDescs := make([]*input.SignDescriptor, 0)
228-
var estimator input.TxWeightEstimator
229-
225+
var (
226+
sweepTx = wire.NewMsgTx(2)
227+
totalOutputValue = int64(0)
228+
signDescs = make([]*input.SignDescriptor, 0)
229+
prevOutFetcher = txscript.NewMultiPrevOutFetcher(nil)
230+
estimator input.TxWeightEstimator
231+
)
230232
for _, target := range targets {
231233
// We can't rely on the CSV delay of the channel DB to be
232234
// correct. But it doesn't cost us a lot to just brute force it.
@@ -246,11 +248,17 @@ func sweepTimeLock(extendedKey *hdkeychain.ExtendedKey, apiURL string,
246248
}
247249

248250
// Create the transaction input.
251+
prevOutPoint := wire.OutPoint{
252+
Hash: target.txid,
253+
Index: target.index,
254+
}
255+
prevTxOut := &wire.TxOut{
256+
PkScript: scriptHash,
257+
Value: target.value,
258+
}
259+
prevOutFetcher.AddPrevOut(prevOutPoint, prevTxOut)
249260
sweepTx.TxIn = append(sweepTx.TxIn, &wire.TxIn{
250-
PreviousOutPoint: wire.OutPoint{
251-
Hash: target.txid,
252-
Index: target.index,
253-
},
261+
PreviousOutPoint: prevOutPoint,
254262
Sequence: input.LockTimeToSequence(
255263
false, uint32(csvTimeout),
256264
),
@@ -264,11 +272,8 @@ func sweepTimeLock(extendedKey *hdkeychain.ExtendedKey, apiURL string,
264272
target.delayBasePointDesc.PubKey,
265273
),
266274
WitnessScript: script,
267-
Output: &wire.TxOut{
268-
PkScript: scriptHash,
269-
Value: target.value,
270-
},
271-
HashType: txscript.SigHashAll,
275+
Output: prevTxOut,
276+
HashType: txscript.SigHashAll,
272277
}
273278
totalOutputValue += target.value
274279
signDescs = append(signDescs, signDesc)
@@ -298,7 +303,7 @@ func sweepTimeLock(extendedKey *hdkeychain.ExtendedKey, apiURL string,
298303
}}
299304

300305
// Sign the transaction now.
301-
sigHashes := input.NewTxSigHashesV0Only(sweepTx)
306+
sigHashes := txscript.NewTxSigHashes(sweepTx, prevOutFetcher)
302307
for idx, desc := range signDescs {
303308
desc.SigHashes = sigHashes
304309
desc.InputIndex = idx

cmd/chantools/sweeptimelockmanual.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,10 @@ func sweepTimeLockManual(extendedKey *hdkeychain.ExtendedKey, apiURL string,
257257
totalFee, sweepValue, estimator.Weight())
258258

259259
// Create the sign descriptor for the input then sign the transaction.
260-
sigHashes := input.NewTxSigHashesV0Only(sweepTx)
260+
prevOutFetcher := txscript.NewCannedPrevOutputFetcher(
261+
scriptHash, sweepValue,
262+
)
263+
sigHashes := txscript.NewTxSigHashes(sweepTx, prevOutFetcher)
261264
signDesc := &input.SignDescriptor{
262265
KeyDesc: *delayDesc,
263266
SingleTweak: input.SingleTweakBytes(

lnd/channel.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ func (lc *LightningChannel) SignedCommitTx() (*wire.MsgTx, error) {
6767

6868
// With this, we then generate the full witness so the caller can
6969
// broadcast a fully signed transaction.
70-
lc.SignDesc.SigHashes = input.NewTxSigHashesV0Only(commitTx)
7170
ourSig, err := lc.TXSigner.SignOutputRaw(commitTx, lc.SignDesc)
7271
if err != nil {
7372
return nil, err

lnd/signer.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/btcsuite/btcd/chaincfg"
1313
"github.com/btcsuite/btcd/txscript"
1414
"github.com/btcsuite/btcd/wire"
15+
"github.com/btcsuite/btcwallet/wallet"
1516
"github.com/lightningnetwork/lnd/input"
1617
"github.com/lightningnetwork/lnd/keychain"
1718
)
@@ -132,13 +133,16 @@ func (s *Signer) AddPartialSignature(packet *psbt.Packet,
132133
inputIndex int) error {
133134

134135
// Now we add our partial signature.
136+
prevOutFetcher := wallet.PsbtPrevOutputFetcher(packet)
135137
signDesc := &input.SignDescriptor{
136138
KeyDesc: keyDesc,
137139
WitnessScript: witnessScript,
138140
Output: utxo,
139141
InputIndex: inputIndex,
140142
HashType: txscript.SigHashAll,
141-
SigHashes: input.NewTxSigHashesV0Only(packet.UnsignedTx),
143+
SigHashes: txscript.NewTxSigHashes(
144+
packet.UnsignedTx, prevOutFetcher,
145+
),
142146
}
143147
ourSigRaw, err := s.SignOutputRaw(packet.UnsignedTx, signDesc)
144148
if err != nil {

0 commit comments

Comments
 (0)