|
7 | 7 | "strings" |
8 | 8 | "time" |
9 | 9 |
|
| 10 | + "github.com/btcsuite/btcd/btcec/v2" |
10 | 11 | "github.com/btcsuite/btcd/chaincfg/chainhash" |
11 | 12 | "github.com/btcsuite/btcd/connmgr" |
12 | 13 | "github.com/btcsuite/btcd/wire" |
@@ -37,13 +38,13 @@ func newTriggerForceCloseCommand() *cobra.Command { |
37 | 38 | cc := &triggerForceCloseCommand{} |
38 | 39 | cc.cmd = &cobra.Command{ |
39 | 40 | Use: "triggerforceclose", |
40 | | - Short: "Connect to a CLN peer and send a custom message to " + |
41 | | - "trigger a force close of the specified channel", |
42 | | - Long: `Certain versions of CLN didn't properly react to error |
43 | | -messages sent by peers and therefore didn't follow the DLP protocol to recover |
44 | | -channel funds using SCB. This command can be used to trigger a force close with |
45 | | -those earlier versions of CLN (this command will not work for lnd peers or CLN |
46 | | -peers of a different version).`, |
| 41 | + Short: "Connect to a Lightning Network peer and send " + |
| 42 | + "specific messages to trigger a force close of the " + |
| 43 | + "specified channel", |
| 44 | + Long: `Asks the specified remote peer to force close a specific |
| 45 | +channel by first sending a channel re-establish message, and if that doesn't |
| 46 | +work, a custom error message (in case the peer is a specific version of CLN that |
| 47 | +does not properly respond to a Data Loss Protection re-establish message).'`, |
47 | 48 | Example: `chantools triggerforceclose \ |
48 | 49 | |
49 | 50 | --channel_point abcdef01234...:x`, |
@@ -88,101 +89,125 @@ func (c *triggerForceCloseCommand) Execute(_ *cobra.Command, _ []string) error { |
88 | 89 | PrivKey: identityPriv, |
89 | 90 | } |
90 | 91 |
|
| 92 | + outPoint, err := parseOutPoint(c.ChannelPoint) |
| 93 | + if err != nil { |
| 94 | + return fmt.Errorf("error parsing channel point: %w", err) |
| 95 | + } |
| 96 | + |
| 97 | + err = requestForceClose(c.Peer, pubKey, outPoint, identityECDH) |
| 98 | + if err != nil { |
| 99 | + return fmt.Errorf("error requesting force close: %w", err) |
| 100 | + } |
| 101 | + |
| 102 | + log.Infof("Message sent, waiting for force close transaction to " + |
| 103 | + "appear in mempool") |
| 104 | + |
| 105 | + api := newExplorerAPI(c.APIURL) |
| 106 | + channelAddress, err := api.Address(c.ChannelPoint) |
| 107 | + if err != nil { |
| 108 | + return fmt.Errorf("error getting channel address: %w", err) |
| 109 | + } |
| 110 | + |
| 111 | + spends, err := api.Spends(channelAddress) |
| 112 | + if err != nil { |
| 113 | + return fmt.Errorf("error getting spends: %w", err) |
| 114 | + } |
| 115 | + for len(spends) == 0 { |
| 116 | + log.Infof("No spends found yet, waiting 5 seconds...") |
| 117 | + time.Sleep(5 * time.Second) |
| 118 | + spends, err = api.Spends(channelAddress) |
| 119 | + if err != nil { |
| 120 | + return fmt.Errorf("error getting spends: %w", err) |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + log.Infof("Found force close transaction %v", spends[0].TXID) |
| 125 | + log.Infof("You can now use the sweepremoteclosed command to sweep " + |
| 126 | + "the funds from the channel") |
| 127 | + |
| 128 | + return nil |
| 129 | +} |
| 130 | + |
| 131 | +func noiseDial(idKey keychain.SingleKeyECDH, lnAddr *lnwire.NetAddress, |
| 132 | + netCfg tor.Net, timeout time.Duration) (*brontide.Conn, error) { |
| 133 | + |
| 134 | + return brontide.Dial(idKey, lnAddr, timeout, netCfg.Dial) |
| 135 | +} |
| 136 | + |
| 137 | +func requestForceClose(peerHost string, peerPubKey *btcec.PublicKey, |
| 138 | + channelPoint *wire.OutPoint, identity keychain.SingleKeyECDH) error { |
| 139 | + |
91 | 140 | peerAddr, err := lncfg.ParseLNAddressString( |
92 | | - c.Peer, "9735", net.ResolveTCPAddr, |
| 141 | + peerHost, "9735", net.ResolveTCPAddr, |
93 | 142 | ) |
94 | 143 | if err != nil { |
95 | 144 | return fmt.Errorf("error parsing peer address: %w", err) |
96 | 145 | } |
97 | 146 |
|
98 | | - outPoint, err := parseOutPoint(c.ChannelPoint) |
99 | | - if err != nil { |
100 | | - return fmt.Errorf("error parsing channel point: %w", err) |
101 | | - } |
102 | | - channelID := lnwire.NewChanIDFromOutPoint(outPoint) |
| 147 | + channelID := lnwire.NewChanIDFromOutPoint(channelPoint) |
103 | 148 |
|
104 | 149 | conn, err := noiseDial( |
105 | | - identityECDH, peerAddr, &tor.ClearNet{}, dialTimeout, |
| 150 | + identity, peerAddr, &tor.ClearNet{}, dialTimeout, |
106 | 151 | ) |
107 | 152 | if err != nil { |
108 | 153 | return fmt.Errorf("error dialing peer: %w", err) |
109 | 154 | } |
110 | 155 |
|
111 | 156 | log.Infof("Attempting to connect to peer %x, dial timeout is %v", |
112 | | - pubKey.SerializeCompressed(), dialTimeout) |
| 157 | + peerPubKey.SerializeCompressed(), dialTimeout) |
113 | 158 | req := &connmgr.ConnReq{ |
114 | 159 | Addr: peerAddr, |
115 | 160 | Permanent: false, |
116 | 161 | } |
117 | | - p, err := lnd.ConnectPeer(conn, req, chainParams, identityECDH) |
| 162 | + p, err := lnd.ConnectPeer(conn, req, chainParams, identity) |
118 | 163 | if err != nil { |
119 | 164 | return fmt.Errorf("error connecting to peer: %w", err) |
120 | 165 | } |
121 | 166 |
|
122 | 167 | log.Infof("Connection established to peer %x", |
123 | | - pubKey.SerializeCompressed()) |
| 168 | + peerPubKey.SerializeCompressed()) |
124 | 169 |
|
125 | 170 | // We'll wait until the peer is active. |
126 | 171 | select { |
127 | 172 | case <-p.ActiveSignal(): |
128 | 173 | case <-p.QuitSignal(): |
129 | 174 | return fmt.Errorf("peer %x disconnected", |
130 | | - pubKey.SerializeCompressed()) |
| 175 | + peerPubKey.SerializeCompressed()) |
131 | 176 | } |
132 | 177 |
|
133 | 178 | // Channel ID (32 byte) + u16 for the data length (which will be 0). |
134 | 179 | data := make([]byte, 34) |
135 | 180 | copy(data[:32], channelID[:]) |
136 | 181 |
|
137 | | - log.Infof("Sending channel error message to peer to trigger force "+ |
138 | | - "close of channel %v", c.ChannelPoint) |
| 182 | + log.Infof("Sending channel re-establish to peer to trigger force "+ |
| 183 | + "close of channel %v", channelPoint) |
139 | 184 |
|
140 | | - _ = lnwire.SetCustomOverrides([]uint16{lnwire.MsgError}) |
141 | | - msg, err := lnwire.NewCustom(lnwire.MsgError, data) |
| 185 | + err = p.SendMessageLazy(true, &lnwire.ChannelReestablish{ |
| 186 | + ChanID: channelID, |
| 187 | + }) |
142 | 188 | if err != nil { |
143 | 189 | return err |
144 | 190 | } |
145 | 191 |
|
146 | | - err = p.SendMessageLazy(true, msg) |
147 | | - if err != nil { |
148 | | - return fmt.Errorf("error sending message: %w", err) |
149 | | - } |
150 | | - |
151 | | - log.Infof("Message sent, waiting for force close transaction to " + |
152 | | - "appear in mempool") |
| 192 | + log.Infof("Sending channel error message to peer to trigger force "+ |
| 193 | + "close of channel %v", channelPoint) |
153 | 194 |
|
154 | | - api := newExplorerAPI(c.APIURL) |
155 | | - channelAddress, err := api.Address(c.ChannelPoint) |
| 195 | + _ = lnwire.SetCustomOverrides([]uint16{ |
| 196 | + lnwire.MsgError, lnwire.MsgChannelReestablish, |
| 197 | + }) |
| 198 | + msg, err := lnwire.NewCustom(lnwire.MsgError, data) |
156 | 199 | if err != nil { |
157 | | - return fmt.Errorf("error getting channel address: %w", err) |
| 200 | + return err |
158 | 201 | } |
159 | 202 |
|
160 | | - spends, err := api.Spends(channelAddress) |
| 203 | + err = p.SendMessageLazy(true, msg) |
161 | 204 | if err != nil { |
162 | | - return fmt.Errorf("error getting spends: %w", err) |
163 | | - } |
164 | | - for len(spends) == 0 { |
165 | | - log.Infof("No spends found yet, waiting 5 seconds...") |
166 | | - time.Sleep(5 * time.Second) |
167 | | - spends, err = api.Spends(channelAddress) |
168 | | - if err != nil { |
169 | | - return fmt.Errorf("error getting spends: %w", err) |
170 | | - } |
| 205 | + return fmt.Errorf("error sending message: %w", err) |
171 | 206 | } |
172 | 207 |
|
173 | | - log.Infof("Found force close transaction %v", spends[0].TXID) |
174 | | - log.Infof("You can now use the sweepremoteclosed command to sweep " + |
175 | | - "the funds from the channel") |
176 | | - |
177 | 208 | return nil |
178 | 209 | } |
179 | 210 |
|
180 | | -func noiseDial(idKey keychain.SingleKeyECDH, lnAddr *lnwire.NetAddress, |
181 | | - netCfg tor.Net, timeout time.Duration) (*brontide.Conn, error) { |
182 | | - |
183 | | - return brontide.Dial(idKey, lnAddr, timeout, netCfg.Dial) |
184 | | -} |
185 | | - |
186 | 211 | func parseOutPoint(s string) (*wire.OutPoint, error) { |
187 | 212 | split := strings.Split(s, ":") |
188 | 213 | if len(split) != 2 || len(split[0]) == 0 || len(split[1]) == 0 { |
|
0 commit comments