Skip to content

Commit 2199e58

Browse files
authored
Merge pull request #203 from RocketNodeLN/patch-2
Fix multisig path for sweeptimelockmanual
2 parents d7d5ad9 + 813e498 commit 2199e58

File tree

2 files changed

+28
-13
lines changed

2 files changed

+28
-13
lines changed

cmd/chantools/sweeptimelockmanual.go

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ func (c *sweepTimeLockManualCommand) Execute(_ *cobra.Command, _ []string) error
167167
startNumChannelsTotal uint16
168168
maxNumChannelsTotal = c.MaxNumChannelsTotal
169169
remoteRevocationBasePoint = c.RemoteRevocationBasePoint
170+
multiSigIdx uint32
170171
)
171172

172173
// We either support specifying the remote revocation base point
@@ -193,14 +194,13 @@ func (c *sweepTimeLockManualCommand) Execute(_ *cobra.Command, _ []string) error
193194
}
194195

195196
remoteCfg := backupChan.RemoteChanCfg
197+
localCfg := backupChan.LocalChanCfg
196198
remoteRevocationBasePoint = remoteCfg.RevocationBasePoint.PubKey
197199

198200
startCsvLimit = remoteCfg.CsvDelay
199201
maxCsvLimit = startCsvLimit + 1
200202

201-
delayPath, err := lnd.ParsePath(
202-
backupChan.LocalChanCfg.DelayBasePoint.Path,
203-
)
203+
delayPath, err := lnd.ParsePath(localCfg.DelayBasePoint.Path)
204204
if err != nil {
205205
return fmt.Errorf("error parsing delay path: %w", err)
206206
}
@@ -210,6 +210,16 @@ func (c *sweepTimeLockManualCommand) Execute(_ *cobra.Command, _ []string) error
210210

211211
startNumChannelsTotal = uint16(delayPath[4])
212212
maxNumChannelsTotal = startNumChannelsTotal + 1
213+
multiSigKeyPath, err := lnd.ParsePath(localCfg.MultiSigKey.Path)
214+
if err != nil {
215+
return fmt.Errorf("error parsing multisigkey path: %w",
216+
err)
217+
}
218+
if len(multiSigKeyPath) != 5 {
219+
return fmt.Errorf("invalid multisig path '%v'",
220+
multiSigKeyPath)
221+
}
222+
multiSigIdx = multiSigKeyPath[4]
213223

214224
case c.ChannelBackup != "" && c.RemoteRevocationBasePoint != "":
215225
return errors.New("cannot use both --frombackup and " +
@@ -230,16 +240,17 @@ func (c *sweepTimeLockManualCommand) Execute(_ *cobra.Command, _ []string) error
230240

231241
return sweepTimeLockManual(
232242
extendedKey, c.APIURL, c.SweepAddr, c.TimeLockAddr,
233-
remoteRevPoint, startCsvLimit, maxCsvLimit,
243+
remoteRevPoint, multiSigIdx, startCsvLimit, maxCsvLimit,
234244
startNumChannelsTotal, maxNumChannelsTotal,
235245
c.MaxNumChanUpdates, c.Publish, c.FeeRate,
236246
)
237247
}
238248

239249
func sweepTimeLockManual(extendedKey *hdkeychain.ExtendedKey, apiURL string,
240250
sweepAddr, timeLockAddr string, remoteRevPoint *btcec.PublicKey,
241-
startCsvTimeout, maxCsvTimeout, startNumChannels, maxNumChannels uint16,
242-
maxNumChanUpdates uint64, publish bool, feeRate uint32) error {
251+
multiSigIdx uint32, startCsvTimeout, maxCsvTimeout, startNumChannels,
252+
maxNumChannels uint16, maxNumChanUpdates uint64, publish bool,
253+
feeRate uint32) error {
243254

244255
log.Debugf("Starting to brute force the time lock script, using: "+
245256
"remote_rev_base_point=%x, start_csv_limit=%d, "+
@@ -301,9 +312,13 @@ func sweepTimeLockManual(extendedKey *hdkeychain.ExtendedKey, apiURL string,
301312
commitPoint *btcec.PublicKey
302313
)
303314
for i := startNumChannels; i < maxNumChannels; i++ {
315+
if multiSigIdx == 0 {
316+
multiSigIdx = uint32(i)
317+
}
318+
304319
csvTimeout, script, scriptHash, commitPoint, delayDesc, err = tryKey(
305320
baseKey, remoteRevPoint, startCsvTimeout, maxCsvTimeout,
306-
lockScript, uint32(i), maxNumChanUpdates,
321+
lockScript, uint32(i), multiSigIdx, maxNumChanUpdates,
307322
)
308323

309324
if err == nil {
@@ -413,9 +428,9 @@ func sweepTimeLockManual(extendedKey *hdkeychain.ExtendedKey, apiURL string,
413428
}
414429

415430
func tryKey(baseKey *hdkeychain.ExtendedKey, remoteRevPoint *btcec.PublicKey,
416-
startCsvTimeout, maxCsvTimeout uint16, lockScript []byte, idx uint32,
417-
maxNumChanUpdates uint64) (int32, []byte, []byte, *btcec.PublicKey,
418-
*keychain.KeyDescriptor, error) {
431+
startCsvTimeout, maxCsvTimeout uint16, lockScript []byte, idx,
432+
multiSigIdx uint32, maxNumChanUpdates uint64) (int32, []byte,
433+
[]byte, *btcec.PublicKey, *keychain.KeyDescriptor, error) {
419434

420435
// The easy part first, let's derive the delay base point.
421436
delayPath := []uint32{
@@ -495,7 +510,7 @@ func tryKey(baseKey *hdkeychain.ExtendedKey, remoteRevPoint *btcec.PublicKey,
495510
// Now we try the same with the new revocation producer format.
496511
multiSigPath := []uint32{
497512
lnd.HardenedKey(uint32(keychain.KeyFamilyMultiSig)),
498-
0, idx,
513+
0, multiSigIdx,
499514
}
500515
multiSigPrivKey, err := lnd.PrivKeyFromPath(baseKey, multiSigPath)
501516
if err != nil {

cmd/chantools/sweeptimelockmanual_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ var sweepTimeLockManualCases = []struct {
9191
func TestSweepTimeLockManual(t *testing.T) {
9292
for _, tc := range sweepTimeLockManualCases {
9393
// First, we need to parse the lock addr and make sure we can
94-
// brute force the script with the information we have. If not,
94+
// brute-force the script with the information we have. If not,
9595
// we can't continue anyway.
9696
lockScript, err := lnd.GetP2WSHScript(
9797
tc.timeLockAddr, tc.params,
@@ -115,7 +115,7 @@ func TestSweepTimeLockManual(t *testing.T) {
115115

116116
_, _, _, _, _, err = tryKey(
117117
baseKey, revPubKey, 0, defaultCsvLimit, lockScript,
118-
tc.keyIndex, 1000,
118+
tc.keyIndex, tc.keyIndex, 1000,
119119
)
120120
require.NoError(t, err)
121121
}

0 commit comments

Comments
 (0)