Skip to content

Commit b084b4f

Browse files
committed
sweeptimelockmanual: add flags for num keys and num updates
1 parent 351aa98 commit b084b4f

File tree

1 file changed

+29
-11
lines changed

1 file changed

+29
-11
lines changed

cmd/chantools/sweeptimelockmanual.go

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ type sweepTimeLockManualCommand struct {
3434
TimeLockAddr string
3535
RemoteRevocationBasePoint string
3636

37+
MaxNumChansTotal uint16
38+
MaxNumChanUpdates uint64
39+
3740
rootKey *rootKey
3841
inputs *inputFlags
3942
cmd *cobra.Command
@@ -79,6 +82,16 @@ address is always the one that's longer (because it's P2WSH and not P2PKH).`,
7982
&cc.MaxCsvLimit, "maxcsvlimit", defaultCsvLimit, "maximum CSV "+
8083
"limit to use",
8184
)
85+
cc.cmd.Flags().Uint16Var(
86+
&cc.MaxNumChansTotal, "maxnumchanstotal", maxKeys, "maximum "+
87+
"number of keys to try, set to maximum number of "+
88+
"channels the local node potentially has or had",
89+
)
90+
cc.cmd.Flags().Uint64Var(
91+
&cc.MaxNumChanUpdates, "maxnumchanupdates", maxPoints,
92+
"maximum number of channel updates to try, set to maximum "+
93+
"number of times the channel was used",
94+
)
8295
cc.cmd.Flags().Uint16Var(
8396
&cc.FeeRate, "feerate", defaultFeeSatPerVByte, "fee rate to "+
8497
"use for the sweep transaction in sat/vByte",
@@ -123,13 +136,15 @@ func (c *sweepTimeLockManualCommand) Execute(_ *cobra.Command, _ []string) error
123136

124137
return sweepTimeLockManual(
125138
extendedKey, c.APIURL, c.SweepAddr, c.TimeLockAddr,
126-
remoteRevPoint, c.MaxCsvLimit, c.Publish, c.FeeRate,
139+
remoteRevPoint, c.MaxCsvLimit, c.MaxNumChansTotal,
140+
c.MaxNumChanUpdates, c.Publish, c.FeeRate,
127141
)
128142
}
129143

130144
func sweepTimeLockManual(extendedKey *hdkeychain.ExtendedKey, apiURL string,
131145
sweepAddr, timeLockAddr string, remoteRevPoint *btcec.PublicKey,
132-
maxCsvTimeout uint16, publish bool, feeRate uint16) error {
146+
maxCsvTimeout, maxNumChannels uint16, maxNumChanUpdates uint64,
147+
publish bool, feeRate uint16) error {
133148

134149
// First of all, we need to parse the lock addr and make sure we can
135150
// brute force the script with the information we have. If not, we can't
@@ -164,9 +179,10 @@ func sweepTimeLockManual(extendedKey *hdkeychain.ExtendedKey, apiURL string,
164179
delayDesc *keychain.KeyDescriptor
165180
commitPoint *btcec.PublicKey
166181
)
167-
for i := uint32(0); i < maxKeys; i++ {
182+
for i := uint16(0); i < maxNumChannels; i++ {
168183
csvTimeout, script, scriptHash, commitPoint, delayDesc, err = tryKey(
169-
baseKey, remoteRevPoint, maxCsvTimeout, lockScript, i,
184+
baseKey, remoteRevPoint, maxCsvTimeout, lockScript,
185+
uint32(i), maxNumChanUpdates,
170186
)
171187

172188
if err == nil {
@@ -285,8 +301,9 @@ func sweepTimeLockManual(extendedKey *hdkeychain.ExtendedKey, apiURL string,
285301
}
286302

287303
func tryKey(baseKey *hdkeychain.ExtendedKey, remoteRevPoint *btcec.PublicKey,
288-
maxCsvTimeout uint16, lockScript []byte, idx uint32) (int32, []byte,
289-
[]byte, *btcec.PublicKey, *keychain.KeyDescriptor, error) {
304+
maxCsvTimeout uint16, lockScript []byte, idx uint32,
305+
maxNumChanUpdates uint64) (int32, []byte, []byte, *btcec.PublicKey,
306+
*keychain.KeyDescriptor, error) {
290307

291308
// The easy part first, let's derive the delay base point.
292309
delayPath := []uint32{
@@ -316,7 +333,7 @@ func tryKey(baseKey *hdkeychain.ExtendedKey, remoteRevPoint *btcec.PublicKey,
316333
// points and CSV values.
317334
csvTimeout, script, scriptHash, commitPoint, err := bruteForceDelayPoint(
318335
delayPrivKey.PubKey(), remoteRevPoint, revRoot, lockScript,
319-
maxCsvTimeout,
336+
maxCsvTimeout, maxNumChanUpdates,
320337
)
321338
if err == nil {
322339
return csvTimeout, script, scriptHash, commitPoint,
@@ -346,7 +363,7 @@ func tryKey(baseKey *hdkeychain.ExtendedKey, remoteRevPoint *btcec.PublicKey,
346363
// points and CSV values.
347364
csvTimeout, script, scriptHash, commitPoint, err = bruteForceDelayPoint(
348365
delayPrivKey.PubKey(), remoteRevPoint, revRoot2, lockScript,
349-
maxCsvTimeout,
366+
maxCsvTimeout, maxNumChanUpdates,
350367
)
351368
if err == nil {
352369
return csvTimeout, script, scriptHash, commitPoint,
@@ -378,7 +395,7 @@ func tryKey(baseKey *hdkeychain.ExtendedKey, remoteRevPoint *btcec.PublicKey,
378395

379396
csvTimeout, script, scriptHash, commitPoint, err = bruteForceDelayPoint(
380397
delayPrivKey.PubKey(), remoteRevPoint, revRoot3, lockScript,
381-
maxCsvTimeout,
398+
maxCsvTimeout, maxNumChanUpdates,
382399
)
383400
if err == nil {
384401
return csvTimeout, script, scriptHash, commitPoint,
@@ -396,9 +413,10 @@ func tryKey(baseKey *hdkeychain.ExtendedKey, remoteRevPoint *btcec.PublicKey,
396413

397414
func bruteForceDelayPoint(delayBase, revBase *btcec.PublicKey,
398415
revRoot *shachain.RevocationProducer, lockScript []byte,
399-
maxCsvTimeout uint16) (int32, []byte, []byte, *btcec.PublicKey, error) {
416+
maxCsvTimeout uint16, maxChanUpdates uint64) (int32, []byte, []byte,
417+
*btcec.PublicKey, error) {
400418

401-
for i := uint64(0); i < maxPoints; i++ {
419+
for i := uint64(0); i < maxChanUpdates; i++ {
402420
revPreimage, err := revRoot.AtIndex(i)
403421
if err != nil {
404422
return 0, nil, nil, nil, err

0 commit comments

Comments
 (0)