Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 75 additions & 8 deletions cmd/chantools/forceclose.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
"github.com/lightninglabs/chantools/lnd"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/input"
"github.com/lightningnetwork/lnd/lnwire"
"github.com/lightningnetwork/lnd/tlv"
"github.com/spf13/cobra"
)

Expand All @@ -34,9 +36,11 @@ blocks) transaction *or* they have a watch tower looking out for them.
**This should absolutely be the last resort and you have been warned!**`

type forceCloseCommand struct {
APIURL string
ChannelDB string
Publish bool
APIURL string
ChannelDB string
ChannelPoint string
Publish bool
StateIndex int32

rootKey *rootKey
inputs *inputFlags
Expand All @@ -53,7 +57,11 @@ func newForceCloseCommand() *cobra.Command {
Example: `chantools forceclose \
--fromsummary results/summary-xxxx-yyyy.json
--channeldb ~/.lnd/data/graph/mainnet/channel.db \
--publish`,
--publish

chantools forceclose \
--channelpoint abcdef01234...:x \
--channeldb ~/.lnd/data/graph/mainnet/channel.db`,
RunE: cc.Execute,
}
cc.cmd.Flags().StringVar(
Expand All @@ -64,6 +72,16 @@ func newForceCloseCommand() *cobra.Command {
&cc.ChannelDB, "channeldb", "", "lnd channel.db file to use "+
"for force-closing channels",
)
cc.cmd.Flags().StringVar(
&cc.ChannelPoint, "channelpoint", "", "the channel point of "+
"the channel to force-close",
)
cc.cmd.Flags().Int32Var(
&cc.StateIndex, "stateindex", -1, "the index of the channel "+
"state to publish; if not set, the latest state is "+
"used; not using the latest state comes with extreme "+
"risk, read the warning in the help of this command",
)
cc.cmd.Flags().BoolVar(
&cc.Publish, "publish", false, "publish force-closing TX to "+
"the chain API instead of just printing the TX",
Expand Down Expand Up @@ -91,18 +109,29 @@ func (c *forceCloseCommand) Execute(_ *cobra.Command, _ []string) error {
}

// Parse channel entries from any of the possible input files.
entries, err := c.inputs.parseInputType()
if err != nil {
return err
var entries []*dataformat.SummaryEntry
if c.ChannelPoint != "" {
entries = []*dataformat.SummaryEntry{
{
ChannelPoint: c.ChannelPoint,
},
}
} else {
entries, err = c.inputs.parseInputType()
if err != nil {
return err
}
}

return forceCloseChannels(
c.APIURL, extendedKey, entries, db.ChannelStateDB(), c.Publish,
c.StateIndex,
)
}

func forceCloseChannels(apiURL string, extendedKey *hdkeychain.ExtendedKey,
entries []*dataformat.SummaryEntry, chanDb *channeldb.ChannelStateDB,
publish bool) error {
publish bool, state int32) error {

channels, err := chanDb.FetchAllChannels()
if err != nil {
Expand Down Expand Up @@ -131,6 +160,42 @@ func forceCloseChannels(apiURL string, extendedKey *hdkeychain.ExtendedKey,
}

localCommit := channel.LocalCommitment

// Use the specified state index if given.
if state >= 0 {
revLog, commitment, err := channel.FindPreviousState(
uint64(state),
)
if err != nil {
return fmt.Errorf("unable to find state %d "+
"for channel %s: %w", state,
channelPoint, err)
}

if commitment == nil {
return fmt.Errorf("no commitment for state %d "+
"for channel %s", state, channelPoint)
}

if revLog != nil {
zero := tlv.NewBigSizeT[lnwire.MilliSatoshi](0)
ourBalanceOpt := revLog.OurBalance.ValOpt()
theirBalanceOpt := revLog.TheirBalance.ValOpt()
ourBalance := ourBalanceOpt.UnwrapOr(zero)
theirBalance := theirBalanceOpt.UnwrapOr(zero)
log.Warnf("Publishing state %d for channel "+
"%s: our_balance=%d, their_balance=%d",
state, channelPoint, ourBalance.Int(),
theirBalance.Int())
} else {
log.Warnf("Publishing state %d for channel %s "+
"but no revocation log found", state,
channelPoint)
}

localCommit = *commitment
}

localCommitTx := localCommit.CommitTx
if localCommitTx == nil {
log.Errorf("Cannot force-close, no local commit TX "+
Expand Down Expand Up @@ -221,6 +286,8 @@ func forceCloseChannels(apiURL string, extendedKey *hdkeychain.ExtendedKey,
}
log.Infof("Published TX %s, response: %s",
hash.String(), response)
} else {
log.Infof("Raw transaction hex: %s", serialized)
}
}

Expand Down
1 change: 1 addition & 0 deletions doc/chantools.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ https://github.com/lightninglabs/chantools/.
* [chantools chanbackup](chantools_chanbackup.md) - Create a channel.backup file from a channel database
* [chantools closepoolaccount](chantools_closepoolaccount.md) - Tries to close a Pool account that has expired
* [chantools compactdb](chantools_compactdb.md) - Create a copy of a channel.db file in safe/read-only mode
* [chantools completion](chantools_completion.md) - Generate the autocompletion script for the specified shell
* [chantools createwallet](chantools_createwallet.md) - Create a new lnd compatible wallet.db file from an existing seed or by generating a new one
* [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
Expand Down
34 changes: 34 additions & 0 deletions doc/chantools_completion.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
## chantools completion

Generate the autocompletion script for the specified shell

### Synopsis

Generate the autocompletion script for chantools for the specified shell.
See each sub-command's help for details on how to use the generated script.


### Options

```
-h, --help help for completion
```

### Options inherited from parent commands

```
--nologfile If set, no log file will be created. This is useful for testing purposes where we don't want to create a log file.
-r, --regtest Indicates if regtest parameters should be used
--resultsdir string Directory where results should be stored (default "./results")
-s, --signet Indicates if the public signet parameters should be used
-t, --testnet Indicates if testnet parameters should be used
```

### SEE ALSO

* [chantools](chantools.md) - Chantools helps recover funds from lightning channels
* [chantools completion bash](chantools_completion_bash.md) - Generate the autocompletion script for bash
* [chantools completion fish](chantools_completion_fish.md) - Generate the autocompletion script for fish
* [chantools completion powershell](chantools_completion_powershell.md) - Generate the autocompletion script for powershell
* [chantools completion zsh](chantools_completion_zsh.md) - Generate the autocompletion script for zsh

53 changes: 53 additions & 0 deletions doc/chantools_completion_bash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
## chantools completion bash

Generate the autocompletion script for bash

### Synopsis

Generate the autocompletion script for the bash shell.

This script depends on the 'bash-completion' package.
If it is not installed already, you can install it via your OS's package manager.

To load completions in your current shell session:

source <(chantools completion bash)

To load completions for every new session, execute once:

#### Linux:

chantools completion bash > /etc/bash_completion.d/chantools

#### macOS:

chantools completion bash > $(brew --prefix)/etc/bash_completion.d/chantools

You will need to start a new shell for this setup to take effect.


```
chantools completion bash
```

### Options

```
-h, --help help for bash
--no-descriptions disable completion descriptions
```

### Options inherited from parent commands

```
--nologfile If set, no log file will be created. This is useful for testing purposes where we don't want to create a log file.
-r, --regtest Indicates if regtest parameters should be used
--resultsdir string Directory where results should be stored (default "./results")
-s, --signet Indicates if the public signet parameters should be used
-t, --testnet Indicates if testnet parameters should be used
```

### SEE ALSO

* [chantools completion](chantools_completion.md) - Generate the autocompletion script for the specified shell

44 changes: 44 additions & 0 deletions doc/chantools_completion_fish.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
## chantools completion fish

Generate the autocompletion script for fish

### Synopsis

Generate the autocompletion script for the fish shell.

To load completions in your current shell session:

chantools completion fish | source

To load completions for every new session, execute once:

chantools completion fish > ~/.config/fish/completions/chantools.fish

You will need to start a new shell for this setup to take effect.


```
chantools completion fish [flags]
```

### Options

```
-h, --help help for fish
--no-descriptions disable completion descriptions
```

### Options inherited from parent commands

```
--nologfile If set, no log file will be created. This is useful for testing purposes where we don't want to create a log file.
-r, --regtest Indicates if regtest parameters should be used
--resultsdir string Directory where results should be stored (default "./results")
-s, --signet Indicates if the public signet parameters should be used
-t, --testnet Indicates if testnet parameters should be used
```

### SEE ALSO

* [chantools completion](chantools_completion.md) - Generate the autocompletion script for the specified shell

41 changes: 41 additions & 0 deletions doc/chantools_completion_powershell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
## chantools completion powershell

Generate the autocompletion script for powershell

### Synopsis

Generate the autocompletion script for powershell.

To load completions in your current shell session:

chantools completion powershell | Out-String | Invoke-Expression

To load completions for every new session, add the output of the above command
to your powershell profile.


```
chantools completion powershell [flags]
```

### Options

```
-h, --help help for powershell
--no-descriptions disable completion descriptions
```

### Options inherited from parent commands

```
--nologfile If set, no log file will be created. This is useful for testing purposes where we don't want to create a log file.
-r, --regtest Indicates if regtest parameters should be used
--resultsdir string Directory where results should be stored (default "./results")
-s, --signet Indicates if the public signet parameters should be used
-t, --testnet Indicates if testnet parameters should be used
```

### SEE ALSO

* [chantools completion](chantools_completion.md) - Generate the autocompletion script for the specified shell

55 changes: 55 additions & 0 deletions doc/chantools_completion_zsh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
## chantools completion zsh

Generate the autocompletion script for zsh

### Synopsis

Generate the autocompletion script for the zsh shell.

If shell completion is not already enabled in your environment you will need
to enable it. You can execute the following once:

echo "autoload -U compinit; compinit" >> ~/.zshrc

To load completions in your current shell session:

source <(chantools completion zsh)

To load completions for every new session, execute once:

#### Linux:

chantools completion zsh > "${fpath[1]}/_chantools"

#### macOS:

chantools completion zsh > $(brew --prefix)/share/zsh/site-functions/_chantools

You will need to start a new shell for this setup to take effect.


```
chantools completion zsh [flags]
```

### Options

```
-h, --help help for zsh
--no-descriptions disable completion descriptions
```

### Options inherited from parent commands

```
--nologfile If set, no log file will be created. This is useful for testing purposes where we don't want to create a log file.
-r, --regtest Indicates if regtest parameters should be used
--resultsdir string Directory where results should be stored (default "./results")
-s, --signet Indicates if the public signet parameters should be used
-t, --testnet Indicates if testnet parameters should be used
```

### SEE ALSO

* [chantools completion](chantools_completion.md) - Generate the autocompletion script for the specified shell

Loading