Skip to content

Commit

Permalink
multi: standardize address checks
Browse files Browse the repository at this point in the history
  • Loading branch information
guggero committed Jan 4, 2024
1 parent 53085d3 commit 7e3ea44
Show file tree
Hide file tree
Showing 18 changed files with 206 additions and 52 deletions.
12 changes: 9 additions & 3 deletions cmd/chantools/closepoolaccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 "+
Expand Down Expand Up @@ -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.
Expand Down
30 changes: 18 additions & 12 deletions cmd/chantools/doublespendinputs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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 "+
Expand Down Expand Up @@ -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.
Expand Down
21 changes: 18 additions & 3 deletions cmd/chantools/pullanchor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 "+
Expand All @@ -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)
Expand Down
14 changes: 9 additions & 5 deletions cmd/chantools/recoverloopin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 "+
Expand Down Expand Up @@ -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,
Expand Down
12 changes: 11 additions & 1 deletion cmd/chantools/rescuefunding.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 "+
Expand Down Expand Up @@ -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)
Expand Down
14 changes: 10 additions & 4 deletions cmd/chantools/sweepremoteclosed.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 "+
Expand All @@ -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.
Expand Down Expand Up @@ -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
}
Expand Down
12 changes: 9 additions & 3 deletions cmd/chantools/sweeptimelock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 "+
Expand All @@ -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.
Expand Down
21 changes: 16 additions & 5 deletions cmd/chantools/sweeptimelockmanual.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 "+
Expand Down Expand Up @@ -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 (
Expand Down
5 changes: 3 additions & 2 deletions doc/chantools.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion doc/chantools_closepoolaccount.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ chantools closepoolaccount \
--outpoint string last account outpoint of the account to close (<txid>:<txindex>)
--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
Expand Down
18 changes: 12 additions & 6 deletions doc/chantools_doublespendinputs.md
Original file line number Diff line number Diff line change
@@ -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]
Expand All @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion doc/chantools_pullanchor.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion doc/chantools_recoverloopin.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand Down
2 changes: 1 addition & 1 deletion doc/chantools_rescuefunding.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion doc/chantools_sweepremoteclosed.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion doc/chantools_sweeptimelock.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion doc/chantools_sweeptimelockmanual.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

Expand Down
Loading

0 comments on commit 7e3ea44

Please sign in to comment.