Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chantools scbforceclose: extract close tx from SCB and sign it #95

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

starius
Copy link
Contributor

@starius starius commented Nov 16, 2023

This is part of lightningnetwork/lnd#7658 (comment) implementation.

This PR depends on lightningnetwork/lnd#8183
(Field chanbackup.Single.CloseTxInputs is needed.)

I added chantools scbforceclose command, which extracts closing tx from SCB and signs it and optionally broadcasts.

New command:

chantools scbforceclose --help
$ chantools scbforceclose --help

If you are certain that a node is offline for good (AFTER you've tried SCB!)
and a channel is still open, you can use this method to force-close your
latest state that you have in your channel.db.

!!! WARNING !!! DANGER !!! WARNING !!!

If you do this and the state that you publish is not the latest state, then
the remote node could punish you by taking the whole channel amount if they
come online before you can sweep the funds from the time locked (144 - 2000
blocks) transaction or they have a watch tower looking out for them.

This should absolutely be the last resort and you have been warned!

Usage:
chantools scbforceclose [flags]

Examples:
chantools scbforceclose --multi_file channel.backup

Flags:
--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
-h, --help help for scbforceclose
--multi_backup string a hex encoded multi-channel backup obtained from exportchanbackup for force-closing channels
--multi_file string the path to a single-channel backup file (channel.backup)
--publish publish force-closing TX to the chain API instead of just printing the TX
--rootkey string BIP32 HD root key of the wallet to use for decrypting the backup and signing tx; leave empty to prompt for lnd 24 word aezeed
--single_backup string a hex encoded single channel backup obtained from exportchanbackup for force-closing channels
--single_file string the path to a single-channel backup file

Global Flags:
-r, --regtest Indicates if regtest parameters should be used
-s, --signet Indicates if the public signet parameters should be used
-t, --testnet Indicates if testnet parameters should be used

The command extracts closing transactions from SCB, signs and prints them.

Example

Here is an example of using chantools scbforceclose in testnet to sign a force closing transaction and to publish it.

chantools --testnet scbforceclose --single_backup xxx --publish --apiurl https://blockstream.info/testnet/api
$ chantools --testnet scbforceclose --single_backup xxx --publish --apiurl https://blockstream.info/testnet/api
2023-12-31 18:06:04.121 [INF] CHAN: chantools version v0.12.1 commit 
Input your 24-word mnemonic separated by spaces: ***

Input your cipher seed passphrase (press enter if your seed doesn't have a passphrase):

Found 1 channel backups, 1 of them have close tx.

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
If you are certain that a node is offline for good (AFTER you've tried SCB!)
and a channel is still open, you can use this method to force-close your
latest state that you have in your channel.db.

!!! WARNING !!! DANGER !!! WARNING !!!

If you do this and the state that you publish is not the latest state, then
the remote node could punish you by taking the whole channel amount if they
come online before you can sweep the funds from the time locked (144 - 2000
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 YES to proceed: YES
Signed transactions will be broadcasted automatically.
Type YES again to proceed: YES
xxx:0
xxxxxxxx

2023-12-31 18:06:28.796 [INF] CHAN: Published TX xxx, response: xxx

(To use --publish option in testnet, --apiurl should be adjusted. Hopefully newExplorerAPI from #107 is fixing this.)

TODOs

Copy link
Member

@guggero guggero left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the two PRs!
I think both need some extra work but are on the right track.

cmd/chantools/forceclose.go Outdated Show resolved Hide resolved
cmd/chantools/forceclose.go Outdated Show resolved Hide resolved
cmd/chantools/forceclose.go Outdated Show resolved Hide resolved
@starius starius force-pushed the close-tx-in-static-backup branch 3 times, most recently from 74d418b to 3e8ea11 Compare December 31, 2023 19:13
@starius starius changed the title chantools forceclose: extract close tx from SCB chantools scbforceclose: extract close tx from SCB and sign it Dec 31, 2023
@starius
Copy link
Contributor Author

starius commented Dec 31, 2023

I updated both PRs. Signing now happens in chantools scbforceclose. I put an example into PR's description.

@guggero Please take another look!

@starius starius requested a review from guggero December 31, 2023 20:29
@guggero guggero removed their request for review April 22, 2024 12:36
@starius starius marked this pull request as draft June 3, 2024 01:56
@starius starius force-pushed the close-tx-in-static-backup branch 2 times, most recently from b78f22b to 4b79ab4 Compare June 18, 2024 23:01
@starius
Copy link
Contributor Author

starius commented Jun 18, 2024

Rebased for LND 0.18, fixed lint warnings.
Tested manually.

@starius starius marked this pull request as ready for review June 18, 2024 23:44
@starius starius requested a review from guggero June 18, 2024 23:45
Copy link
Member

@guggero guggero left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great PR, looks very good.

Will test once lnd side has been updated.


if s.CloseTxInputs == nil {
return nil, errors.New("channel backup does not have data " +
"needed to sign force sloe tx")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: s/sloe/close/

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed, moved to LND PR (commit chanbackup: add function SignCloseTx).

}

func createTaprootNonceProducer(
s chanbackup.Single,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: formatting.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed, moved to LND PR (commit chanbackup: add function SignCloseTx).

@starius starius force-pushed the close-tx-in-static-backup branch from 4b79ab4 to 521d4c8 Compare July 7, 2024 04:10
@starius
Copy link
Contributor Author

starius commented Jul 7, 2024

Thanks for the review! I updated LND PR and this PR. The code producing signed transaction was moved to LND to use it in itest.

@starius starius requested a review from guggero July 7, 2024 04:13
Copy link
Member

@guggero guggero left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love how minimal this now turned out 💯

LGTM pending some final manual tests (will attempt to do later today).

Copy link
Member

@guggero guggero left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tACK, LGTM 🎉

To make things easier to debug, can you please apply the following diff as another commit? That will add the new data to the output of chantools dumpbackup.

dumpchanbackup.diff.txt

Will merge once the lnd PR is in.

return errors.New("failed to read user input")
}
if strings.TrimSpace(userInput) != "YES" {
return errors.New("cancelled by user")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: improve error message slightly, perhaps something like "canceled by user, must type uppercase 'YES'"? Same below.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

s.FundingOutpoint, err)
}
txHex := hex.EncodeToString(buf.Bytes())
fmt.Println(s.FundingOutpoint)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should prefix these values, otherwise it looks a bit misleading on the command line.
Now:

Type YES to proceed: YES
8821015bb99036728b00f4b1f68ab4e41d8573947f3f94fe0d4ec2f38c1f92d4:0
02000000000101d4921f8cf3c24e0dfe943f7f9473851de4b48af6b1f4008b723690b95b01218800000000000508b280044a010000000000002......

Suggestion:

Type YES to proceed: YES

Channel point: 8821015bb99036728b00f4b1f68ab4e41d8573947f3f94fe0d4ec2f38c1f92d4:0
Raw transaction hex: 02000000000101d4921f8cf3c24e0dfe943f7f9473851de4b48af6b1f4008b723690b95b01218800000000000508b280044a01000000000.....

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implemented!

Remove MockSigner from Signer. MockSigner has MusigSessionManager is an embedded
field, and it is the only part of MockSigner needed in Signer.
It can now be used as keychain.ECDHRing.
It will be reused in scbforceclose.
starius and others added 2 commits July 17, 2024 11:17
That will add the new data to the output of chantools dumpbackup.
@starius
Copy link
Contributor Author

starius commented Jul 17, 2024

I rebased the PR, updated go replace to the latest lightningnetwork/lnd#8183 version.

Also included the suggested patch for dump.go. @guggero I put your name in Author field of the commit.

@lightninglabs-deploy
Copy link

@starius, remember to re-request review from reviewers when ready

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants