diff --git a/cmd/chantools/closepoolaccount.go b/cmd/chantools/closepoolaccount.go index fa8cf0a..0503539 100644 --- a/cmd/chantools/closepoolaccount.go +++ b/cmd/chantools/closepoolaccount.go @@ -88,7 +88,9 @@ obtained by running 'pool accounts list' `, "API instead of just printing the TX", ) cc.cmd.Flags().StringVar( - &cc.SweepAddr, "sweepaddr", "", "address to sweep the funds to", + &cc.SweepAddr, "sweepaddr", "", "address to recover the funds "+ + "to; specify '"+lnd.AddressDeriveFromWallet+"' to "+ + "derive a new address from the seed automatically", ) cc.cmd.Flags().Uint32Var( &cc.FeeRate, "feerate", defaultFeeSatPerVByte, "fee rate to "+ @@ -124,8 +126,12 @@ func (c *closePoolAccountCommand) Execute(_ *cobra.Command, _ []string) error { } // Make sure sweep addr is set. - if c.SweepAddr == "" { - return fmt.Errorf("sweep addr is required") + err = lnd.CheckAddress( + c.SweepAddr, chainParams, true, "sweep", lnd.AddrTypeP2WKH, + lnd.AddrTypeP2TR, + ) + if err != nil { + return err } // Parse account outpoint and auctioneer key. diff --git a/cmd/chantools/doublespendinputs.go b/cmd/chantools/doublespendinputs.go index ece5d1c..c36c5a6 100644 --- a/cmd/chantools/doublespendinputs.go +++ b/cmd/chantools/doublespendinputs.go @@ -34,16 +34,16 @@ type doubleSpendInputs struct { func newDoubleSpendInputsCommand() *cobra.Command { cc := &doubleSpendInputs{} cc.cmd = &cobra.Command{ - Use: "doublespendinputs", - Short: "Tries to double spend the given inputs by deriving the " + - "private for the address and sweeping the funds to the given " + - "address. This can only be used with inputs that belong to " + - "an lnd wallet.", + Use: "doublespendinputs", + Short: "Replace a transaction by double spending its input", + Long: `Tries to double spend the given inputs by deriving the +private for the address and sweeping the funds to the given address. This can +only be used with inputs that belong to an lnd wallet.`, Example: `chantools doublespendinputs \ - --inputoutpoints xxxxxxxxx:y,xxxxxxxxx:y \ - --sweepaddr bc1q..... \ - --feerate 10 \ - --publish`, + --inputoutpoints xxxxxxxxx:y,xxxxxxxxx:y \ + --sweepaddr bc1q..... \ + --feerate 10 \ + --publish`, RunE: cc.Execute, } cc.cmd.Flags().StringVar( @@ -55,7 +55,9 @@ func newDoubleSpendInputsCommand() *cobra.Command { "list of outpoints to double spend in the format txid:vout", ) cc.cmd.Flags().StringVar( - &cc.SweepAddr, "sweepaddr", "", "address to sweep the funds to", + &cc.SweepAddr, "sweepaddr", "", "address to recover the funds "+ + "to; specify '"+lnd.AddressDeriveFromWallet+"' to "+ + "derive a new address from the seed automatically", ) cc.cmd.Flags().Uint32Var( &cc.FeeRate, "feerate", defaultFeeSatPerVByte, "fee rate to "+ @@ -83,8 +85,12 @@ func (c *doubleSpendInputs) Execute(_ *cobra.Command, _ []string) error { } // Make sure sweep addr is set. - if c.SweepAddr == "" { - return fmt.Errorf("sweep addr is required") + err = lnd.CheckAddress( + c.SweepAddr, chainParams, true, "sweep", lnd.AddrTypeP2WKH, + lnd.AddrTypeP2TR, + ) + if err != nil { + return err } // Make sure we have at least one input. diff --git a/cmd/chantools/pullanchor.go b/cmd/chantools/pullanchor.go index ed438ba..2fa32da 100644 --- a/cmd/chantools/pullanchor.go +++ b/cmd/chantools/pullanchor.go @@ -65,7 +65,9 @@ transaction of an anchor output channel type. This will attempt to CPFP the ) cc.cmd.Flags().StringVar( &cc.ChangeAddr, "changeaddr", "", "the change address to "+ - "send the remaining funds to", + "send the remaining funds back to; specify '"+ + lnd.AddressDeriveFromWallet+"' to derive a new "+ + "address from the seed automatically", ) cc.cmd.Flags().Uint32Var( &cc.FeeRate, "feerate", defaultFeeSatPerVByte, "fee rate to "+ @@ -90,8 +92,21 @@ func (c *pullAnchorCommand) Execute(_ *cobra.Command, _ []string) error { if len(c.AnchorAddrs) == 0 { return fmt.Errorf("at least one anchor addr is required") } - if c.ChangeAddr == "" { - return fmt.Errorf("change addr is required") + for _, anchorAddr := range c.AnchorAddrs { + err = lnd.CheckAddress( + anchorAddr, chainParams, true, "anchor", + lnd.AddrTypeP2WSH, lnd.AddrTypeP2TR, + ) + if err != nil { + return err + } + } + err = lnd.CheckAddress( + c.ChangeAddr, chainParams, true, "change", lnd.AddrTypeP2WKH, + lnd.AddrTypeP2TR, + ) + if err != nil { + return err } outpoint, err := lnd.ParseOutpoint(c.SponsorInput) diff --git a/cmd/chantools/recoverloopin.go b/cmd/chantools/recoverloopin.go index 21654ce..18dfbdf 100644 --- a/cmd/chantools/recoverloopin.go +++ b/cmd/chantools/recoverloopin.go @@ -69,8 +69,9 @@ func newRecoverLoopInCommand() *cobra.Command { "database directory, where the loop.db file is located", ) cc.cmd.Flags().StringVar( - &cc.SweepAddr, "sweep_addr", "", "address to recover "+ - "the funds to", + &cc.SweepAddr, "sweepaddr", "", "address to recover the funds "+ + "to; specify '"+lnd.AddressDeriveFromWallet+"' to "+ + "derive a new address from the seed automatically", ) cc.cmd.Flags().Uint32Var( &cc.FeeRate, "feerate", 0, "fee rate to "+ @@ -116,12 +117,15 @@ func (c *recoverLoopInCommand) Execute(_ *cobra.Command, _ []string) error { return fmt.Errorf("loop_db_dir is required") } - if c.SweepAddr == "" { - return fmt.Errorf("sweep_addr is required") + err = lnd.CheckAddress( + c.SweepAddr, chainParams, true, "sweep", lnd.AddrTypeP2WKH, + lnd.AddrTypeP2TR, + ) + if err != nil { + return err } api := newExplorerAPI(c.APIURL) - signer := &lnd.Signer{ ExtendedKey: extendedKey, ChainParams: chainParams, diff --git a/cmd/chantools/rescuefunding.go b/cmd/chantools/rescuefunding.go index cc59219..a5e92cb 100644 --- a/cmd/chantools/rescuefunding.go +++ b/cmd/chantools/rescuefunding.go @@ -112,7 +112,9 @@ chantools rescuefunding \ "specified manually", ) cc.cmd.Flags().StringVar( - &cc.SweepAddr, "sweepaddr", "", "address to sweep the funds to", + &cc.SweepAddr, "sweepaddr", "", "address to recover the funds "+ + "to; specify '"+lnd.AddressDeriveFromWallet+"' to "+ + "derive a new address from the seed automatically", ) cc.cmd.Flags().Uint32Var( &cc.FeeRate, "feerate", defaultFeeSatPerVByte, "fee rate to "+ @@ -226,6 +228,14 @@ func (c *rescueFundingCommand) Execute(_ *cobra.Command, _ []string) error { } } + err = lnd.CheckAddress( + c.SweepAddr, chainParams, true, "sweep", lnd.AddrTypeP2WKH, + lnd.AddrTypeP2TR, + ) + if err != nil { + return err + } + // Make sure the sweep addr is a P2WKH address so we can do accurate // fee estimation. sweepScript, err := lnd.GetP2WPKHScript(c.SweepAddr, chainParams) diff --git a/cmd/chantools/sweepremoteclosed.go b/cmd/chantools/sweepremoteclosed.go index 6ed9227..f8cd6ef 100644 --- a/cmd/chantools/sweepremoteclosed.go +++ b/cmd/chantools/sweepremoteclosed.go @@ -76,7 +76,9 @@ Supported remote force-closed channel types are: "API instead of just printing the TX", ) cc.cmd.Flags().StringVar( - &cc.SweepAddr, "sweepaddr", "", "address to sweep the funds to", + &cc.SweepAddr, "sweepaddr", "", "address to recover the funds "+ + "to; specify '"+lnd.AddressDeriveFromWallet+"' to "+ + "derive a new address from the seed automatically", ) cc.cmd.Flags().Uint32Var( &cc.FeeRate, "feerate", defaultFeeSatPerVByte, "fee rate to "+ @@ -95,8 +97,12 @@ func (c *sweepRemoteClosedCommand) Execute(_ *cobra.Command, _ []string) error { } // Make sure sweep addr is set. - if c.SweepAddr == "" { - return fmt.Errorf("sweep addr is required") + err = lnd.CheckAddress( + c.SweepAddr, chainParams, true, "sweep", lnd.AddrTypeP2WKH, + lnd.AddrTypeP2TR, + ) + if err != nil { + return err } // Set default values. @@ -423,7 +429,7 @@ func queryAddressBalances(pubKey *btcec.PublicKey, path string, return nil, err } - p2tr, scriptTree, err := lnd.P2TaprootStaticRemove(pubKey, chainParams) + p2tr, scriptTree, err := lnd.P2TaprootStaticRemote(pubKey, chainParams) if err != nil { return nil, err } diff --git a/cmd/chantools/sweeptimelock.go b/cmd/chantools/sweeptimelock.go index a1ce477..d6571ef 100644 --- a/cmd/chantools/sweeptimelock.go +++ b/cmd/chantools/sweeptimelock.go @@ -64,7 +64,9 @@ parameter to 144.`, "API instead of just printing the TX", ) cc.cmd.Flags().StringVar( - &cc.SweepAddr, "sweepaddr", "", "address to sweep the funds to", + &cc.SweepAddr, "sweepaddr", "", "address to recover the funds "+ + "to; specify '"+lnd.AddressDeriveFromWallet+"' to "+ + "derive a new address from the seed automatically", ) cc.cmd.Flags().Uint16Var( &cc.MaxCsvLimit, "maxcsvlimit", defaultCsvLimit, "maximum CSV "+ @@ -88,8 +90,12 @@ func (c *sweepTimeLockCommand) Execute(_ *cobra.Command, _ []string) error { } // Make sure sweep addr is set. - if c.SweepAddr == "" { - return fmt.Errorf("sweep addr is required") + err = lnd.CheckAddress( + c.SweepAddr, chainParams, true, "sweep", lnd.AddrTypeP2WKH, + lnd.AddrTypeP2TR, + ) + if err != nil { + return err } // Parse channel entries from any of the possible input files. diff --git a/cmd/chantools/sweeptimelockmanual.go b/cmd/chantools/sweeptimelockmanual.go index 04cde72..7bdf748 100644 --- a/cmd/chantools/sweeptimelockmanual.go +++ b/cmd/chantools/sweeptimelockmanual.go @@ -89,7 +89,9 @@ chantools sweeptimelockmanual \ "API instead of just printing the TX", ) cc.cmd.Flags().StringVar( - &cc.SweepAddr, "sweepaddr", "", "address to sweep the funds to", + &cc.SweepAddr, "sweepaddr", "", "address to recover the funds "+ + "to; specify '"+lnd.AddressDeriveFromWallet+"' to "+ + "derive a new address from the seed automatically", ) cc.cmd.Flags().Uint16Var( &cc.MaxCsvLimit, "maxcsvlimit", defaultCsvLimit, "maximum CSV "+ @@ -142,11 +144,20 @@ func (c *sweepTimeLockManualCommand) Execute(_ *cobra.Command, _ []string) error } // Make sure the sweep and time lock addrs are set. - if c.SweepAddr == "" { - return fmt.Errorf("sweep addr is required") + err = lnd.CheckAddress( + c.SweepAddr, chainParams, true, "sweep", lnd.AddrTypeP2WKH, + lnd.AddrTypeP2TR, + ) + if err != nil { + return err } - if c.TimeLockAddr == "" { - return fmt.Errorf("time lock addr is required") + + err = lnd.CheckAddress( + c.TimeLockAddr, chainParams, true, "time lock", + lnd.AddrTypeP2WSH, + ) + if err != nil { + return err } var ( diff --git a/doc/chantools.md b/doc/chantools.md index fb07e6d..cca764f 100644 --- a/doc/chantools.md +++ b/doc/chantools.md @@ -6,7 +6,8 @@ Chantools helps recover funds from lightning channels This tool provides helper functions that can be used rescue funds locked in lnd channels in case lnd itself cannot run properly anymore. -Complete documentation is available at https://github.com/lightninglabs/chantools/. +Complete documentation is available at +https://github.com/lightninglabs/chantools/. ### Options @@ -24,7 +25,7 @@ Complete documentation is available at https://github.com/lightninglabs/chantool * [chantools compactdb](chantools_compactdb.md) - Create a copy of a channel.db file in safe/read-only mode * [chantools deletepayments](chantools_deletepayments.md) - Remove all (failed) payments from a channel DB * [chantools derivekey](chantools_derivekey.md) - Derive a key with a specific derivation path -* [chantools doublespendinputs](chantools_doublespendinputs.md) - Tries to double spend the given inputs by deriving the private for the address and sweeping the funds to the given address. This can only be used with inputs that belong to an lnd wallet. +* [chantools doublespendinputs](chantools_doublespendinputs.md) - Replace a transaction by double spending its input * [chantools dropchannelgraph](chantools_dropchannelgraph.md) - Remove all graph related data from a channel DB * [chantools dropgraphzombies](chantools_dropgraphzombies.md) - Remove all channels identified as zombies from the graph to force a re-sync of the graph * [chantools dumpbackup](chantools_dumpbackup.md) - Dump the content of a channel.backup file diff --git a/doc/chantools_closepoolaccount.md b/doc/chantools_closepoolaccount.md index f2f739a..da9d900 100644 --- a/doc/chantools_closepoolaccount.md +++ b/doc/chantools_closepoolaccount.md @@ -41,7 +41,7 @@ chantools closepoolaccount \ --outpoint string last account outpoint of the account to close (:) --publish publish sweep TX to the chain API instead of just printing the TX --rootkey string BIP32 HD root key of the wallet to use for deriving keys; leave empty to prompt for lnd 24 word aezeed - --sweepaddr string address to sweep the funds to + --sweepaddr string address to recover the funds to; specify 'fromseed' to derive a new address from the seed automatically ``` ### Options inherited from parent commands diff --git a/doc/chantools_doublespendinputs.md b/doc/chantools_doublespendinputs.md index 79d1602..fe6b5a6 100644 --- a/doc/chantools_doublespendinputs.md +++ b/doc/chantools_doublespendinputs.md @@ -1,6 +1,12 @@ ## chantools doublespendinputs -Tries to double spend the given inputs by deriving the private for the address and sweeping the funds to the given address. This can only be used with inputs that belong to an lnd wallet. +Replace a transaction by double spending its input + +### Synopsis + +Tries to double spend the given inputs by deriving the +private for the address and sweeping the funds to the given address. This can +only be used with inputs that belong to an lnd wallet. ``` chantools doublespendinputs [flags] @@ -10,10 +16,10 @@ chantools doublespendinputs [flags] ``` chantools doublespendinputs \ - --inputoutpoints xxxxxxxxx:y,xxxxxxxxx:y \ - --sweepaddr bc1q..... \ - --feerate 10 \ - --publish + --inputoutpoints xxxxxxxxx:y,xxxxxxxxx:y \ + --sweepaddr bc1q..... \ + --feerate 10 \ + --publish ``` ### Options @@ -27,7 +33,7 @@ chantools doublespendinputs \ --publish publish replacement TX to the chain API instead of just printing the TX --recoverywindow uint32 number of keys to scan per internal/external branch; output will consist of double this amount of keys (default 2500) --rootkey string BIP32 HD root key of the wallet to use for deriving the input keys; leave empty to prompt for lnd 24 word aezeed - --sweepaddr string address to sweep the funds to + --sweepaddr string address to recover the funds to; specify 'fromseed' to derive a new address from the seed automatically ``` ### Options inherited from parent commands diff --git a/doc/chantools_pullanchor.md b/doc/chantools_pullanchor.md index eb14045..523a6c7 100644 --- a/doc/chantools_pullanchor.md +++ b/doc/chantools_pullanchor.md @@ -28,7 +28,7 @@ chantools pullanchor \ --anchoraddr stringArray the address of the anchor output (p2wsh or p2tr output with 330 satoshis) that should be pulled; can be specified multiple times per command to pull multiple anchors with a single transaction --apiurl string API URL to use (must be esplora compatible) (default "https://blockstream.info/api") --bip39 read a classic BIP39 seed and passphrase from the terminal instead of asking for lnd seed format or providing the --rootkey flag - --changeaddr string the change address to send the remaining funds to + --changeaddr string the change address to send the remaining funds back to; specify 'fromseed' to derive a new address from the seed automatically --feerate uint32 fee rate to use for the sweep transaction in sat/vByte (default 30) -h, --help help for pullanchor --rootkey string BIP32 HD root key of the wallet to use for deriving keys; leave empty to prompt for lnd 24 word aezeed diff --git a/doc/chantools_recoverloopin.md b/doc/chantools_recoverloopin.md index 307e555..b3d9cd2 100644 --- a/doc/chantools_recoverloopin.md +++ b/doc/chantools_recoverloopin.md @@ -31,7 +31,7 @@ chantools recoverloopin \ --rootkey string BIP32 HD root key of the wallet to use for deriving starting key; leave empty to prompt for lnd 24 word aezeed --start_key_index int start key index to try to find the correct key index --swap_hash string swap hash of the loop in swap - --sweep_addr string address to recover the funds to + --sweepaddr string address to recover the funds to; specify 'fromseed' to derive a new address from the seed automatically --txid string transaction id of the on-chain transaction that created the HTLC --vout uint32 output index of the on-chain transaction that created the HTLC ``` diff --git a/doc/chantools_rescuefunding.md b/doc/chantools_rescuefunding.md index 3cde047..4a730b2 100644 --- a/doc/chantools_rescuefunding.md +++ b/doc/chantools_rescuefunding.md @@ -49,7 +49,7 @@ chantools rescuefunding \ --localkeyindex uint32 in case a channel DB is not available (but perhaps a channel backup file), the derivation index of the local multisig public key can be specified manually --remotepubkey string in case a channel DB is not available (but perhaps a channel backup file), the remote multisig public key can be specified manually --rootkey string BIP32 HD root key of the wallet to use for deriving keys; leave empty to prompt for lnd 24 word aezeed - --sweepaddr string address to sweep the funds to + --sweepaddr string address to recover the funds to; specify 'fromseed' to derive a new address from the seed automatically ``` ### Options inherited from parent commands diff --git a/doc/chantools_sweepremoteclosed.md b/doc/chantools_sweepremoteclosed.md index 105a2f9..f254b19 100644 --- a/doc/chantools_sweepremoteclosed.md +++ b/doc/chantools_sweepremoteclosed.md @@ -40,7 +40,7 @@ chantools sweepremoteclosed \ --publish publish sweep TX to the chain API instead of just printing the TX --recoverywindow uint32 number of keys to scan per derivation path (default 200) --rootkey string BIP32 HD root key of the wallet to use for sweeping the wallet; leave empty to prompt for lnd 24 word aezeed - --sweepaddr string address to sweep the funds to + --sweepaddr string address to recover the funds to; specify 'fromseed' to derive a new address from the seed automatically ``` ### Options inherited from parent commands diff --git a/doc/chantools_sweeptimelock.md b/doc/chantools_sweeptimelock.md index 06e3708..5284585 100644 --- a/doc/chantools_sweeptimelock.md +++ b/doc/chantools_sweeptimelock.md @@ -40,7 +40,7 @@ chantools sweeptimelock \ --pendingchannels string channel input is in the format of lncli's pendingchannels format; specify '-' to read from stdin --publish publish sweep TX to the chain API instead of just printing the TX --rootkey string BIP32 HD root key of the wallet to use for deriving keys; leave empty to prompt for lnd 24 word aezeed - --sweepaddr string address to sweep the funds to + --sweepaddr string address to recover the funds to; specify 'fromseed' to derive a new address from the seed automatically ``` ### Options inherited from parent commands diff --git a/doc/chantools_sweeptimelockmanual.md b/doc/chantools_sweeptimelockmanual.md index 0aec8d6..7c5b9ff 100644 --- a/doc/chantools_sweeptimelockmanual.md +++ b/doc/chantools_sweeptimelockmanual.md @@ -61,7 +61,7 @@ chantools sweeptimelockmanual \ --publish publish sweep TX to the chain API instead of just printing the TX --remoterevbasepoint string remote node's revocation base point, can be found in a channel.backup file --rootkey string BIP32 HD root key of the wallet to use for deriving keys; leave empty to prompt for lnd 24 word aezeed - --sweepaddr string address to sweep the funds to + --sweepaddr string address to recover the funds to; specify 'fromseed' to derive a new address from the seed automatically --timelockaddr string address of the time locked commitment output where the funds are stuck in ``` diff --git a/lnd/hdkeychain.go b/lnd/hdkeychain.go index 838fa74..f67fea8 100644 --- a/lnd/hdkeychain.go +++ b/lnd/hdkeychain.go @@ -24,6 +24,16 @@ const ( WalletBIP49DerivationPath = "m/49'/0'/0'" WalletBIP86DerivationPath = "m/86'/0'/0'" LndDerivationPath = "m/1017'/%d'/%d'" + + AddressDeriveFromWallet = "fromseed" +) + +type AddrType int + +const ( + AddrTypeP2WKH AddrType = iota + AddrTypeP2WSH + AddrTypeP2TR ) func DeriveChildren(key *hdkeychain.ExtendedKey, path []uint32) ( @@ -383,7 +393,7 @@ func P2AnchorStaticRemote(pubKey *btcec.PublicKey, return p2wsh, commitScript, err } -func P2TaprootStaticRemove(pubKey *btcec.PublicKey, +func P2TaprootStaticRemote(pubKey *btcec.PublicKey, params *chaincfg.Params) (*btcutil.AddressTaproot, *input.CommitScriptTree, error) { @@ -398,6 +408,79 @@ func P2TaprootStaticRemove(pubKey *btcec.PublicKey, return addr, scriptTree, err } +func CheckAddress(addr string, chainParams *chaincfg.Params, allowDerive bool, + hint string, allowedTypes ...AddrType) error { + + // We generally always want an address to be specified. If one should + // be derived from the wallet automatically, the user should specify + // "derive" as the address. + if addr == "" { + return fmt.Errorf("%s address cannot be empty", hint) + } + + // If we're allowed to derive an address from the wallet, we can skip + // the rest of the checks. + if allowDerive && addr == AddressDeriveFromWallet { + return nil + } + + parsedAddr, err := ParseAddress(addr, chainParams) + if err != nil { + return fmt.Errorf("%s address is invalid: %w", hint, err) + } + + if !matchAddrType(parsedAddr, allowedTypes...) { + return fmt.Errorf("%s address is of wrong type, allowed "+ + "types: %s", hint, addrTypesToString(allowedTypes)) + } + + return nil +} + +func matchAddrType(addr btcutil.Address, allowedTypes ...AddrType) bool { + contains := func(allowedTypes []AddrType, addrType AddrType) bool { + for _, allowedType := range allowedTypes { + if allowedType == addrType { + return true + } + } + + return false + } + + switch addr.(type) { + case *btcutil.AddressWitnessPubKeyHash: + return contains(allowedTypes, AddrTypeP2WKH) + + case *btcutil.AddressWitnessScriptHash: + return contains(allowedTypes, AddrTypeP2WSH) + + case *btcutil.AddressTaproot: + return contains(allowedTypes, AddrTypeP2TR) + + default: + return false + } +} + +func addrTypesToString(allowedTypes []AddrType) string { + var types []string + for _, allowedType := range allowedTypes { + switch allowedType { + case AddrTypeP2WKH: + types = append(types, "P2WKH") + + case AddrTypeP2WSH: + types = append(types, "P2WSH") + + case AddrTypeP2TR: + types = append(types, "P2TR") + } + } + + return strings.Join(types, ", ") +} + type HDKeyRing struct { ExtendedKey *hdkeychain.ExtendedKey ChainParams *chaincfg.Params