From 5bc49376a330ea72a5c033c555a4c6c0d6a03d44 Mon Sep 17 00:00:00 2001 From: Oliver Gugger Date: Tue, 7 Nov 2023 20:24:56 -0600 Subject: [PATCH] sweeptimelock: make start CSV timeout+channels configurable --- cmd/chantools/sweeptimelock.go | 8 +++---- cmd/chantools/sweeptimelockmanual.go | 26 +++++++++++------------ cmd/chantools/sweeptimelockmanual_test.go | 2 +- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/cmd/chantools/sweeptimelock.go b/cmd/chantools/sweeptimelock.go index d4cd932..8a1c98d 100644 --- a/cmd/chantools/sweeptimelock.go +++ b/cmd/chantools/sweeptimelock.go @@ -239,7 +239,7 @@ func sweepTimeLock(extendedKey *hdkeychain.ExtendedKey, apiURL string, ), input.DeriveRevocationPubkey( target.revocationBasePoint, target.commitPoint, - ), target.lockScript, maxCsvTimeout, + ), target.lockScript, 0, maxCsvTimeout, ) if err != nil { log.Errorf("Could not create matching script for %s "+ @@ -346,14 +346,14 @@ func pubKeyFromHex(pubKeyHex string) (*btcec.PublicKey, error) { } func bruteForceDelay(delayPubkey, revocationPubkey *btcec.PublicKey, - targetScript []byte, maxCsvTimeout uint16) (int32, []byte, []byte, - error) { + targetScript []byte, startCsvTimeout, maxCsvTimeout uint16) (int32, + []byte, []byte, error) { if len(targetScript) != 34 { return 0, nil, nil, fmt.Errorf("invalid target script: %s", targetScript) } - for i := uint16(0); i <= maxCsvTimeout; i++ { + for i := startCsvTimeout; i <= maxCsvTimeout; i++ { s, err := input.CommitScriptToSelf( uint32(i), delayPubkey, revocationPubkey, ) diff --git a/cmd/chantools/sweeptimelockmanual.go b/cmd/chantools/sweeptimelockmanual.go index ca6935c..72d1564 100644 --- a/cmd/chantools/sweeptimelockmanual.go +++ b/cmd/chantools/sweeptimelockmanual.go @@ -136,15 +136,15 @@ func (c *sweepTimeLockManualCommand) Execute(_ *cobra.Command, _ []string) error return sweepTimeLockManual( extendedKey, c.APIURL, c.SweepAddr, c.TimeLockAddr, - remoteRevPoint, c.MaxCsvLimit, c.MaxNumChansTotal, + remoteRevPoint, 0, c.MaxCsvLimit, 0, c.MaxNumChansTotal, c.MaxNumChanUpdates, c.Publish, c.FeeRate, ) } func sweepTimeLockManual(extendedKey *hdkeychain.ExtendedKey, apiURL string, sweepAddr, timeLockAddr string, remoteRevPoint *btcec.PublicKey, - maxCsvTimeout, maxNumChannels uint16, maxNumChanUpdates uint64, - publish bool, feeRate uint32) error { + startCsvTimeout, maxCsvTimeout, startNumChannels, maxNumChannels uint16, + maxNumChanUpdates uint64, publish bool, feeRate uint32) error { // First of all, we need to parse the lock addr and make sure we can // brute force the script with the information we have. If not, we can't @@ -179,10 +179,10 @@ func sweepTimeLockManual(extendedKey *hdkeychain.ExtendedKey, apiURL string, delayDesc *keychain.KeyDescriptor commitPoint *btcec.PublicKey ) - for i := uint16(0); i < maxNumChannels; i++ { + for i := startNumChannels; i < maxNumChannels; i++ { csvTimeout, script, scriptHash, commitPoint, delayDesc, err = tryKey( - baseKey, remoteRevPoint, maxCsvTimeout, lockScript, - uint32(i), maxNumChanUpdates, + baseKey, remoteRevPoint, startCsvTimeout, maxCsvTimeout, + lockScript, uint32(i), maxNumChanUpdates, ) if err == nil { @@ -305,7 +305,7 @@ func sweepTimeLockManual(extendedKey *hdkeychain.ExtendedKey, apiURL string, } func tryKey(baseKey *hdkeychain.ExtendedKey, remoteRevPoint *btcec.PublicKey, - maxCsvTimeout uint16, lockScript []byte, idx uint32, + startCsvTimeout, maxCsvTimeout uint16, lockScript []byte, idx uint32, maxNumChanUpdates uint64) (int32, []byte, []byte, *btcec.PublicKey, *keychain.KeyDescriptor, error) { @@ -338,7 +338,7 @@ func tryKey(baseKey *hdkeychain.ExtendedKey, remoteRevPoint *btcec.PublicKey, // points and CSV values. csvTimeout, script, scriptHash, commitPoint, err := bruteForceDelayPoint( delayPrivKey.PubKey(), remoteRevPoint, revRoot, lockScript, - maxCsvTimeout, maxNumChanUpdates, + startCsvTimeout, maxCsvTimeout, maxNumChanUpdates, ) if err == nil { return csvTimeout, script, scriptHash, commitPoint, @@ -403,7 +403,7 @@ func tryKey(baseKey *hdkeychain.ExtendedKey, remoteRevPoint *btcec.PublicKey, csvTimeout, script, scriptHash, commitPoint, err = bruteForceDelayPoint( delayPrivKey.PubKey(), remoteRevPoint, revRoot2, lockScript, - maxCsvTimeout, maxNumChanUpdates, + startCsvTimeout, maxCsvTimeout, maxNumChanUpdates, ) if err == nil { return csvTimeout, script, scriptHash, commitPoint, @@ -444,7 +444,7 @@ func tryKey(baseKey *hdkeychain.ExtendedKey, remoteRevPoint *btcec.PublicKey, csvTimeout, script, scriptHash, commitPoint, err = bruteForceDelayPoint( delayPrivKey.PubKey(), remoteRevPoint, revRoot3, lockScript, - maxCsvTimeout, maxNumChanUpdates, + startCsvTimeout, maxCsvTimeout, maxNumChanUpdates, ) if err == nil { return csvTimeout, script, scriptHash, commitPoint, @@ -462,8 +462,8 @@ func tryKey(baseKey *hdkeychain.ExtendedKey, remoteRevPoint *btcec.PublicKey, func bruteForceDelayPoint(delayBase, revBase *btcec.PublicKey, revRoot *shachain.RevocationProducer, lockScript []byte, - maxCsvTimeout uint16, maxChanUpdates uint64) (int32, []byte, []byte, - *btcec.PublicKey, error) { + startCsvTimeout, maxCsvTimeout uint16, maxChanUpdates uint64) (int32, + []byte, []byte, *btcec.PublicKey, error) { for i := uint64(0); i < maxChanUpdates; i++ { revPreimage, err := revRoot.AtIndex(i) @@ -475,7 +475,7 @@ func bruteForceDelayPoint(delayBase, revBase *btcec.PublicKey, csvTimeout, script, scriptHash, err := bruteForceDelay( input.TweakPubKey(delayBase, commitPoint), input.DeriveRevocationPubkey(revBase, commitPoint), - lockScript, maxCsvTimeout, + lockScript, startCsvTimeout, maxCsvTimeout, ) if err != nil { diff --git a/cmd/chantools/sweeptimelockmanual_test.go b/cmd/chantools/sweeptimelockmanual_test.go index 9c0323c..b22bec6 100644 --- a/cmd/chantools/sweeptimelockmanual_test.go +++ b/cmd/chantools/sweeptimelockmanual_test.go @@ -86,7 +86,7 @@ func TestSweepTimeLockManual(t *testing.T) { revPubKey, _ := btcec.ParsePubKey(revPubKeyBytes) _, _, _, _, _, err = tryKey( - baseKey, revPubKey, defaultCsvLimit, lockScript, + baseKey, revPubKey, 0, defaultCsvLimit, lockScript, tc.keyIndex, 500, ) require.NoError(t, err)