Skip to content

Commit 53b5b6c

Browse files
committed
triggerforceclose: make command CLN compatible
1 parent 5810cce commit 53b5b6c

File tree

2 files changed

+49
-15
lines changed

2 files changed

+49
-15
lines changed

cmd/chantools/triggerforceclose.go

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"encoding/hex"
45
"fmt"
56
"strconv"
67
"strings"
@@ -10,6 +11,7 @@ import (
1011
"github.com/btcsuite/btcd/chaincfg/chainhash"
1112
"github.com/btcsuite/btcd/connmgr"
1213
"github.com/btcsuite/btcd/wire"
14+
"github.com/lightninglabs/chantools/cln"
1315
"github.com/lightninglabs/chantools/lnd"
1416
"github.com/lightningnetwork/lnd/brontide"
1517
"github.com/lightningnetwork/lnd/keychain"
@@ -34,6 +36,8 @@ type triggerForceCloseCommand struct {
3436

3537
TorProxy string
3638

39+
HsmSecret string
40+
3741
rootKey *rootKey
3842
cmd *cobra.Command
3943
}
@@ -71,28 +75,56 @@ does not properly respond to a Data Loss Protection re-establish message).'`,
7175
&cc.TorProxy, "torproxy", "", "SOCKS5 proxy to use for Tor "+
7276
"connections (to .onion addresses)",
7377
)
78+
cc.cmd.Flags().StringVar(
79+
&cc.HsmSecret, "hsm_secret", "", "the hex encoded HSM secret "+
80+
"to use for deriving the node key for a CLN "+
81+
"node; obtain by running 'xxd -p -c32 "+
82+
"~/.lightning/bitcoin/hsm_secret'",
83+
)
7484
cc.rootKey = newRootKey(cc.cmd, "deriving the identity key")
7585

7686
return cc.cmd
7787
}
7888

7989
func (c *triggerForceCloseCommand) Execute(_ *cobra.Command, _ []string) error {
80-
extendedKey, err := c.rootKey.read()
81-
if err != nil {
82-
return fmt.Errorf("error reading root key: %w", err)
83-
}
90+
var identityPriv *btcec.PrivateKey
91+
switch {
92+
case c.HsmSecret != "":
93+
secretBytes, err := hex.DecodeString(c.HsmSecret)
94+
if err != nil {
95+
return fmt.Errorf("error decoding HSM secret: %w", err)
96+
}
8497

85-
identityPath := lnd.IdentityPath(chainParams)
86-
child, pubKey, _, err := lnd.DeriveKey(
87-
extendedKey, identityPath, chainParams,
88-
)
89-
if err != nil {
90-
return fmt.Errorf("could not derive identity key: %w", err)
91-
}
92-
identityPriv, err := child.ECPrivKey()
93-
if err != nil {
94-
return fmt.Errorf("could not get identity private key: %w", err)
98+
var hsmSecret [32]byte
99+
copy(hsmSecret[:], secretBytes)
100+
101+
_, identityPriv, err = cln.NodeKey(hsmSecret)
102+
if err != nil {
103+
return fmt.Errorf("error deriving identity key: %w",
104+
err)
105+
}
106+
107+
default:
108+
extendedKey, err := c.rootKey.read()
109+
if err != nil {
110+
return fmt.Errorf("error reading root key: %w", err)
111+
}
112+
113+
identityPath := lnd.IdentityPath(chainParams)
114+
child, _, _, err := lnd.DeriveKey(
115+
extendedKey, identityPath, chainParams,
116+
)
117+
if err != nil {
118+
return fmt.Errorf("could not derive identity key: %w",
119+
err)
120+
}
121+
identityPriv, err = child.ECPrivKey()
122+
if err != nil {
123+
return fmt.Errorf("could not get identity private "+
124+
"key: %w", err)
125+
}
95126
}
127+
96128
identityECDH := &keychain.PrivKeyECDH{
97129
PrivKey: identityPriv,
98130
}
@@ -103,7 +135,8 @@ func (c *triggerForceCloseCommand) Execute(_ *cobra.Command, _ []string) error {
103135
}
104136

105137
err = requestForceClose(
106-
c.Peer, c.TorProxy, pubKey, *outPoint, identityECDH,
138+
c.Peer, c.TorProxy, identityPriv.PubKey(), *outPoint,
139+
identityECDH,
107140
)
108141
if err != nil {
109142
return fmt.Errorf("error requesting force close: %w", err)

doc/chantools_triggerforceclose.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ chantools triggerforceclose \
2828
--bip39 read a classic BIP39 seed and passphrase from the terminal instead of asking for lnd seed format or providing the --rootkey flag
2929
--channel_point string funding transaction outpoint of the channel to trigger the force close of (<txid>:<txindex>)
3030
-h, --help help for triggerforceclose
31+
--hsm_secret string the hex encoded HSM secret to use for deriving the node key for a CLN node; obtain by running 'xxd -p -c32 ~/.lightning/bitcoin/hsm_secret'
3132
--peer string remote peer address (<pubkey>@<host>[:<port>])
3233
--rootkey string BIP32 HD root key of the wallet to use for deriving the identity key; leave empty to prompt for lnd 24 word aezeed
3334
--torproxy string SOCKS5 proxy to use for Tor connections (to .onion addresses)

0 commit comments

Comments
 (0)