mirror of https://github.com/guggero/chantools
parent
a01dd32e55
commit
083453ef5d
@ -0,0 +1,202 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
||||
"github.com/btcsuite/btcd/connmgr"
|
||||
"github.com/btcsuite/btcd/wire"
|
||||
"github.com/guggero/chantools/btc"
|
||||
"github.com/guggero/chantools/lnd"
|
||||
"github.com/lightningnetwork/lnd/brontide"
|
||||
"github.com/lightningnetwork/lnd/keychain"
|
||||
"github.com/lightningnetwork/lnd/lncfg"
|
||||
"github.com/lightningnetwork/lnd/lnwire"
|
||||
"github.com/lightningnetwork/lnd/tor"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var (
|
||||
dialTimeout = time.Minute
|
||||
)
|
||||
|
||||
type triggerForceCloseCommand struct {
|
||||
Peer string
|
||||
ChannelPoint string
|
||||
|
||||
APIURL string
|
||||
|
||||
rootKey *rootKey
|
||||
cmd *cobra.Command
|
||||
}
|
||||
|
||||
func newTriggerForceCloseCommand() *cobra.Command {
|
||||
cc := &triggerForceCloseCommand{}
|
||||
cc.cmd = &cobra.Command{
|
||||
Use: "triggerforceclose",
|
||||
Short: "Connect to a peer and send a custom message to " +
|
||||
"trigger a force close of the specified channel",
|
||||
Example: `chantools triggerforceclose \
|
||||
--peer 03abce...@xx.yy.zz.aa:9735 \
|
||||
--channel_point abcdef01234...:x`,
|
||||
RunE: cc.Execute,
|
||||
}
|
||||
cc.cmd.Flags().StringVar(
|
||||
&cc.Peer, "peer", "", "remote peer address "+
|
||||
"(<pubkey>@<host>[:<port>])",
|
||||
)
|
||||
cc.cmd.Flags().StringVar(
|
||||
&cc.ChannelPoint, "channel_point", "", "funding transaction "+
|
||||
"outpoint of the channel to trigger the force close "+
|
||||
"of (<txid>:<txindex>)",
|
||||
)
|
||||
cc.cmd.Flags().StringVar(
|
||||
&cc.APIURL, "apiurl", defaultAPIURL, "API URL to use (must "+
|
||||
"be esplora compatible)",
|
||||
)
|
||||
cc.rootKey = newRootKey(cc.cmd, "deriving the identity key")
|
||||
|
||||
return cc.cmd
|
||||
}
|
||||
|
||||
func (c *triggerForceCloseCommand) Execute(_ *cobra.Command, _ []string) error {
|
||||
extendedKey, err := c.rootKey.read()
|
||||
if err != nil {
|
||||
return fmt.Errorf("error reading root key: %w", err)
|
||||
}
|
||||
|
||||
identityPath := lnd.IdentityPath(chainParams)
|
||||
child, pubKey, _, err := lnd.DeriveKey(
|
||||
extendedKey, identityPath, chainParams,
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not derive identity key: %w", err)
|
||||
}
|
||||
identityPriv, err := child.ECPrivKey()
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not get identity private key: %w", err)
|
||||
}
|
||||
identityECDH := &keychain.PrivKeyECDH{
|
||||
PrivKey: identityPriv,
|
||||
}
|
||||
|
||||
peerAddr, err := lncfg.ParseLNAddressString(
|
||||
c.Peer, "9735", net.ResolveTCPAddr,
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error parsing peer address: %w", err)
|
||||
}
|
||||
|
||||
outPoint, err := parseOutPoint(c.ChannelPoint)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error parsing channel point: %w", err)
|
||||
}
|
||||
channelID := lnwire.NewChanIDFromOutPoint(outPoint)
|
||||
|
||||
conn, err := noiseDial(
|
||||
identityECDH, peerAddr, &tor.ClearNet{}, dialTimeout,
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error dialing peer: %w", err)
|
||||
}
|
||||
|
||||
log.Infof("Attempting to connect to peer %x, dial timeout is %v",
|
||||
pubKey.SerializeCompressed(), dialTimeout)
|
||||
req := &connmgr.ConnReq{
|
||||
Addr: peerAddr,
|
||||
Permanent: false,
|
||||
}
|
||||
p, err := lnd.ConnectPeer(conn, req, chainParams, identityECDH)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error connecting to peer: %w", err)
|
||||
}
|
||||
|
||||
log.Infof("Connection established to peer %x",
|
||||
pubKey.SerializeCompressed())
|
||||
|
||||
// We'll wait until the peer is active.
|
||||
select {
|
||||
case <-p.ActiveSignal():
|
||||
case <-p.QuitSignal():
|
||||
return fmt.Errorf("peer %x disconnected",
|
||||
pubKey.SerializeCompressed())
|
||||
}
|
||||
|
||||
// Channel ID (32 byte) + u16 for the data length (which will be 0).
|
||||
data := make([]byte, 34)
|
||||
copy(data[:32], channelID[:])
|
||||
|
||||
log.Infof("Sending channel error message to peer to trigger force "+
|
||||
"close of channel %v", c.ChannelPoint)
|
||||
|
||||
_ = lnwire.SetCustomOverrides([]uint16{lnwire.MsgError})
|
||||
msg, err := lnwire.NewCustom(lnwire.MsgError, data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = p.SendMessageLazy(true, msg)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error sending message: %w", err)
|
||||
}
|
||||
|
||||
log.Infof("Message sent, waiting for force close transaction to " +
|
||||
"appear in mempool")
|
||||
|
||||
api := &btc.ExplorerAPI{BaseURL: c.APIURL}
|
||||
channelAddress, err := api.Address(c.ChannelPoint)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error getting channel address: %w", err)
|
||||
}
|
||||
|
||||
spends, err := api.Spends(channelAddress)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error getting spends: %w", err)
|
||||
}
|
||||
for len(spends) == 0 {
|
||||
log.Infof("No spends found yet, waiting 5 seconds...")
|
||||
time.Sleep(5 * time.Second)
|
||||
spends, err = api.Spends(channelAddress)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error getting spends: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
log.Infof("Found force close transaction %v", spends[0].TXID)
|
||||
log.Infof("You can now use the sweepremoteclosed command to sweep " +
|
||||
"the funds from the channel")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func noiseDial(idKey keychain.SingleKeyECDH, lnAddr *lnwire.NetAddress,
|
||||
netCfg tor.Net, timeout time.Duration) (*brontide.Conn, error) {
|
||||
|
||||
return brontide.Dial(idKey, lnAddr, timeout, netCfg.Dial)
|
||||
}
|
||||
|
||||
func parseOutPoint(s string) (*wire.OutPoint, error) {
|
||||
split := strings.Split(s, ":")
|
||||
if len(split) != 2 || len(split[0]) == 0 || len(split[1]) == 0 {
|
||||
return nil, fmt.Errorf("invalid channel point format: %v", s)
|
||||
}
|
||||
|
||||
index, err := strconv.ParseInt(split[1], 10, 64)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to decode output index: %v", err)
|
||||
}
|
||||
|
||||
txid, err := chainhash.NewHashFromStr(split[0])
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to parse hex string: %v", err)
|
||||
}
|
||||
|
||||
return &wire.OutPoint{
|
||||
Hash: *txid,
|
||||
Index: uint32(index),
|
||||
}, nil
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
## chantools triggerforceclose
|
||||
|
||||
Connect to a peer and send a custom message to trigger a force close of the specified channel
|
||||
|
||||
```
|
||||
chantools triggerforceclose [flags]
|
||||
```
|
||||
|
||||
### Examples
|
||||
|
||||
```
|
||||
chantools triggerforceclose \
|
||||
--peer 03abce...@xx.yy.zz.aa:9735 \
|
||||
--channel_point abcdef01234...:x
|
||||
```
|
||||
|
||||
### Options
|
||||
|
||||
```
|
||||
--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
|
||||
--channel_point string funding transaction outpoint of the channel to trigger the force close of (<txid>:<txindex>)
|
||||
-h, --help help for triggerforceclose
|
||||
--peer string remote peer address (<pubkey>@<host>[:<port>])
|
||||
--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
|
||||
```
|
||||
|
||||
### Options inherited from parent commands
|
||||
|
||||
```
|
||||
-r, --regtest Indicates if regtest 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
|
||||
|
@ -0,0 +1,235 @@
|
||||
package lnd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/btcsuite/btcd/btcec/v2"
|
||||
"github.com/btcsuite/btcd/btcec/v2/ecdsa"
|
||||
"github.com/btcsuite/btcd/chaincfg"
|
||||
"github.com/btcsuite/btcd/connmgr"
|
||||
"github.com/lightningnetwork/lnd/aliasmgr"
|
||||
"github.com/lightningnetwork/lnd/brontide"
|
||||
"github.com/lightningnetwork/lnd/channeldb"
|
||||
"github.com/lightningnetwork/lnd/discovery"
|
||||
"github.com/lightningnetwork/lnd/feature"
|
||||
"github.com/lightningnetwork/lnd/htlcswitch"
|
||||
"github.com/lightningnetwork/lnd/htlcswitch/hodl"
|
||||
"github.com/lightningnetwork/lnd/keychain"
|
||||
"github.com/lightningnetwork/lnd/lncfg"
|
||||
"github.com/lightningnetwork/lnd/lnpeer"
|
||||
"github.com/lightningnetwork/lnd/lntest/mock"
|
||||
"github.com/lightningnetwork/lnd/lnwallet"
|
||||
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
|
||||
"github.com/lightningnetwork/lnd/lnwire"
|
||||
"github.com/lightningnetwork/lnd/netann"
|
||||
"github.com/lightningnetwork/lnd/peer"
|
||||
"github.com/lightningnetwork/lnd/pool"
|
||||
"github.com/lightningnetwork/lnd/queue"
|
||||
"github.com/lightningnetwork/lnd/routing/route"
|
||||
"github.com/lightningnetwork/lnd/ticker"
|
||||
)
|
||||
|
||||
const (
|
||||
defaultChannelCommitBatchSize = 10
|
||||
defaultCoopCloseTargetConfs = 6
|
||||
)
|
||||
|
||||
var (
|
||||
chanEnableTimeout = 19 * time.Minute
|
||||
defaultChannelCommitInterval = 50 * time.Millisecond
|
||||
defaultPendingCommitInterval = 1 * time.Minute
|
||||
)
|
||||
|
||||
func ConnectPeer(conn *brontide.Conn, connReq *connmgr.ConnReq,
|
||||
netParams *chaincfg.Params,
|
||||
identityECDH keychain.SingleKeyECDH) (*peer.Brontide, error) {
|
||||
|
||||
featureMgr, err := feature.NewManager(feature.Config{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
initFeatures := featureMgr.Get(feature.SetInit)
|
||||
legacyFeatures := featureMgr.Get(feature.SetLegacyGlobal)
|
||||
|
||||
addr := conn.RemoteAddr()
|
||||
pubKey := conn.RemotePub()
|
||||
peerAddr := &lnwire.NetAddress{
|
||||
IdentityKey: pubKey,
|
||||
Address: addr,
|
||||
ChainNet: netParams.Net,
|
||||
}
|
||||
errBuffer, err := queue.NewCircularBuffer(500)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
pongBuf := make([]byte, lnwire.MaxPongBytes)
|
||||
|
||||
writeBufferPool := pool.NewWriteBuffer(
|
||||
pool.DefaultWriteBufferGCInterval,
|
||||
pool.DefaultWriteBufferExpiryInterval,
|
||||
)
|
||||
writePool := pool.NewWrite(
|
||||
writeBufferPool, lncfg.DefaultWriteWorkers,
|
||||
pool.DefaultWorkerTimeout,
|
||||
)
|
||||
|
||||
readBufferPool := pool.NewReadBuffer(
|
||||
pool.DefaultReadBufferGCInterval,
|
||||
pool.DefaultReadBufferExpiryInterval,
|
||||
)
|
||||
readPool := pool.NewRead(
|
||||
readBufferPool, lncfg.DefaultWriteWorkers,
|
||||
pool.DefaultWorkerTimeout,
|
||||
)
|
||||
commitFee := chainfee.SatPerKVByte(
|
||||
lnwallet.DefaultAnchorsCommitMaxFeeRateSatPerVByte * 1000,
|
||||
)
|
||||
|
||||
if err := writePool.Start(); err != nil {
|
||||
return nil, fmt.Errorf("unable to start write pool: %v", err)
|
||||
}
|
||||
if err := readPool.Start(); err != nil {
|
||||
return nil, fmt.Errorf("unable to start read pool: %v", err)
|
||||
}
|
||||
|
||||
channelDB, err := channeldb.Open(os.TempDir())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
gossiper := discovery.New(discovery.Config{
|
||||
ChainHash: *netParams.GenesisHash,
|
||||
Broadcast: func(skips map[route.Vertex]struct{},
|
||||
msg ...lnwire.Message) error {
|
||||
|
||||
return nil
|
||||
},
|
||||
NotifyWhenOnline: func(peerPubKey [33]byte,
|
||||
peerChan chan<- lnpeer.Peer) {
|
||||
|
||||
},
|
||||
NotifyWhenOffline: func(peerPubKey [33]byte) <-chan struct{} {
|
||||
return make(chan struct{})
|
||||
},
|
||||
SelfNodeAnnouncement: func(
|
||||
refresh bool) (lnwire.NodeAnnouncement, error) {
|
||||
|
||||
return lnwire.NodeAnnouncement{}, nil
|
||||
},
|
||||
ProofMatureDelta: 0,
|
||||
TrickleDelay: time.Millisecond * 50,
|
||||
RetransmitTicker: ticker.New(time.Minute * 30),
|
||||
RebroadcastInterval: time.Hour * 24,
|
||||
RotateTicker: ticker.New(discovery.DefaultSyncerRotationInterval),
|
||||
HistoricalSyncTicker: ticker.New(discovery.DefaultHistoricalSyncInterval),
|
||||
NumActiveSyncers: 0,
|
||||
MinimumBatchSize: 10,
|
||||
SubBatchDelay: discovery.DefaultSubBatchDelay,
|
||||
IgnoreHistoricalFilters: true,
|
||||
PinnedSyncers: make(map[route.Vertex]struct{}),
|
||||
MaxChannelUpdateBurst: discovery.DefaultMaxChannelUpdateBurst,
|
||||
ChannelUpdateInterval: discovery.DefaultChannelUpdateInterval,
|
||||
IsAlias: aliasmgr.IsAlias,
|
||||
SignAliasUpdate: func(u *lnwire.ChannelUpdate) (*ecdsa.Signature, error) {
|
||||
return nil, fmt.Errorf("unimplemented")
|
||||
},
|
||||
FindBaseByAlias: func(alias lnwire.ShortChannelID) (lnwire.ShortChannelID, error) {
|
||||
return lnwire.ShortChannelID{}, fmt.Errorf("unimplemented")
|
||||
},
|
||||
GetAlias: func(id lnwire.ChannelID) (lnwire.ShortChannelID, error) {
|
||||
return lnwire.ShortChannelID{}, fmt.Errorf("unimplemented")
|
||||
},
|
||||
FindChannel: func(node *btcec.PublicKey,
|
||||
chanID lnwire.ChannelID) (*channeldb.OpenChannel, error) {
|
||||
|
||||
return nil, fmt.Errorf("unimplemented")
|
||||
},
|
||||
}, &keychain.KeyDescriptor{
|
||||
KeyLocator: keychain.KeyLocator{},
|
||||
PubKey: identityECDH.PubKey(),
|
||||
})
|
||||
|
||||
pCfg := peer.Config{
|
||||
Conn: conn,
|
||||
ConnReq: connReq,
|
||||
Addr: peerAddr,
|
||||
Inbound: false,
|
||||
Features: initFeatures,
|
||||
LegacyFeatures: legacyFeatures,
|
||||
OutgoingCltvRejectDelta: lncfg.DefaultOutgoingCltvRejectDelta,
|
||||
ChanActiveTimeout: chanEnableTimeout,
|
||||
ErrorBuffer: errBuffer,
|
||||
WritePool: writePool,
|
||||
ReadPool: readPool,
|
||||
ChannelDB: channelDB.ChannelStateDB(),
|
||||
AuthGossiper: gossiper,
|
||||
ChainNotifier: &mock.ChainNotifier{},
|
||||
DisconnectPeer: func(key *btcec.PublicKey) error {
|
||||
fmt.Printf("Peer %x disconnected\n",
|
||||
key.SerializeCompressed())
|
||||
return nil
|
||||
},
|
||||
GenNodeAnnouncement: func(b bool,
|
||||
modifier ...netann.NodeAnnModifier) (
|
||||
lnwire.NodeAnnouncement, error) {
|
||||
|
||||
return lnwire.NodeAnnouncement{},
|
||||
fmt.Errorf("unimplemented")
|
||||
},
|
||||
|
||||
PongBuf: pongBuf,
|
||||
|
||||
PrunePersistentPeerConnection: func(bytes [33]byte) {},
|
||||
|
||||
FetchLastChanUpdate: func(id lnwire.ShortChannelID) (
|
||||
*lnwire.ChannelUpdate, error) {
|
||||
|
||||
return nil, fmt.Errorf("unimplemented")
|
||||
},
|
||||
|
||||
Hodl: &hodl.Config{},
|
||||
UnsafeReplay: false,
|
||||
MaxOutgoingCltvExpiry: htlcswitch.DefaultMaxOutgoingCltvExpiry,
|
||||
MaxChannelFeeAllocation: htlcswitch.DefaultMaxLinkFeeAllocation,
|
||||
CoopCloseTargetConfs: defaultCoopCloseTargetConfs,
|
||||
MaxAnchorsCommitFeeRate: commitFee.FeePerKWeight(),
|
||||
ChannelCommitInterval: defaultChannelCommitInterval,
|
||||
PendingCommitInterval: defaultPendingCommitInterval,
|
||||
ChannelCommitBatchSize: defaultChannelCommitBatchSize,
|
||||
HandleCustomMessage: func(peer [33]byte,
|
||||
msg *lnwire.Custom) error {
|
||||
|
||||
fmt.Printf("Received custom message from %x: %v\n",
|
||||
peer[:], msg)
|
||||
return nil
|
||||
},
|
||||
GetAliases: func(
|
||||
base lnwire.ShortChannelID) []lnwire.ShortChannelID {
|
||||
|
||||
return nil
|
||||
},
|
||||
RequestAlias: func() (lnwire.ShortChannelID, error) {
|
||||
return lnwire.ShortChannelID{}, nil
|
||||
},
|
||||
AddLocalAlias: func(alias, base lnwire.ShortChannelID,
|
||||
gossip bool) error {
|
||||
|
||||
return nil
|
||||
},
|
||||
Quit: make(chan struct{}),
|
||||
}
|
||||
|
||||
copy(pCfg.PubKeyBytes[:], peerAddr.IdentityKey.SerializeCompressed())
|
||||
copy(pCfg.ServerPubKey[:], identityECDH.PubKey().SerializeCompressed())
|
||||
|
||||
p := peer.NewBrontide(pCfg)
|
||||
if err := p.Start(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return p, nil
|
||||
}
|
Loading…
Reference in New Issue