From 9781ac6bb0ede83d8f3cecbbb3ad7c38b62afedf Mon Sep 17 00:00:00 2001 From: Jesse de Wit Date: Fri, 24 Mar 2023 14:43:20 +0100 Subject: [PATCH] cleanup: move lightning client to seperate package --- cln_client.go | 11 ++++++----- intercept.go | 7 ++++--- lightning_client.go => lightning/client.go | 4 ++-- lnd_client.go | 11 ++++++----- server.go | 5 +++-- 5 files changed, 21 insertions(+), 17 deletions(-) rename lightning_client.go => lightning/client.go (94%) diff --git a/cln_client.go b/cln_client.go index a9ee908..0026151 100644 --- a/cln_client.go +++ b/cln_client.go @@ -7,6 +7,7 @@ import ( "path/filepath" "github.com/breez/lspd/basetypes" + "github.com/breez/lspd/lightning" "github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/btcsuite/btcd/wire" "github.com/niftynei/glightning/glightning" @@ -42,14 +43,14 @@ func NewClnClient(socketPath string) (*ClnClient, error) { }, nil } -func (c *ClnClient) GetInfo() (*GetInfoResult, error) { +func (c *ClnClient) GetInfo() (*lightning.GetInfoResult, error) { info, err := c.client.GetInfo() if err != nil { log.Printf("CLN: client.GetInfo() error: %v", err) return nil, err } - return &GetInfoResult{ + return &lightning.GetInfoResult{ Alias: info.Alias, Pubkey: info.Id, }, nil @@ -74,7 +75,7 @@ func (c *ClnClient) IsConnected(destination []byte) (bool, error) { return false, nil } -func (c *ClnClient) OpenChannel(req *OpenChannelRequest) (*wire.OutPoint, error) { +func (c *ClnClient) OpenChannel(req *lightning.OpenChannelRequest) (*wire.OutPoint, error) { pubkey := hex.EncodeToString(req.Destination) var minConfs *uint16 if req.MinConfs != nil { @@ -140,7 +141,7 @@ func (c *ClnClient) OpenChannel(req *OpenChannelRequest) (*wire.OutPoint, error) return channelPoint, nil } -func (c *ClnClient) GetChannel(peerID []byte, channelPoint wire.OutPoint) (*GetChannelResult, error) { +func (c *ClnClient) GetChannel(peerID []byte, channelPoint wire.OutPoint) (*lightning.GetChannelResult, error) { pubkey := hex.EncodeToString(peerID) peer, err := c.client.GetPeer(pubkey) if err != nil { @@ -162,7 +163,7 @@ func (c *ClnClient) GetChannel(peerID []byte, channelPoint wire.OutPoint) (*GetC fmt.Printf("NewShortChannelIDFromString %v error: %v", c.Alias.Local, err) return nil, err } - return &GetChannelResult{ + return &lightning.GetChannelResult{ InitialChannelID: *initialChanID, ConfirmedChannelID: *confirmedChanID, }, nil diff --git a/intercept.go b/intercept.go index 2683e21..52b6cce 100644 --- a/intercept.go +++ b/intercept.go @@ -11,6 +11,7 @@ import ( "github.com/breez/lspd/chain" "github.com/breez/lspd/config" + "github.com/breez/lspd/lightning" "github.com/btcsuite/btcd/btcec/v2" "github.com/btcsuite/btcd/wire" sphinx "github.com/lightningnetwork/lightning-onion" @@ -50,7 +51,7 @@ type interceptResult struct { onionBlob []byte } -func intercept(client LightningClient, config *config.NodeConfig, nextHop string, reqPaymentHash []byte, reqOutgoingAmountMsat uint64, reqOutgoingExpiry uint32, reqIncomingExpiry uint32) interceptResult { +func intercept(client lightning.Client, config *config.NodeConfig, nextHop string, reqPaymentHash []byte, reqOutgoingAmountMsat uint64, reqOutgoingExpiry uint32, reqIncomingExpiry uint32) interceptResult { reqPaymentHashStr := hex.EncodeToString(reqPaymentHash) resp, _, _ := payHashGroup.Do(reqPaymentHashStr, func() (interface{}, error) { paymentHash, paymentSecret, destination, incomingAmountMsat, outgoingAmountMsat, channelPoint, err := paymentInfo(reqPaymentHash) @@ -236,7 +237,7 @@ func checkPayment(config *config.NodeConfig, incomingAmountMsat, outgoingAmountM return nil } -func openChannel(client LightningClient, config *config.NodeConfig, paymentHash, destination []byte, incomingAmountMsat int64) (*wire.OutPoint, error) { +func openChannel(client lightning.Client, config *config.NodeConfig, paymentHash, destination []byte, incomingAmountMsat int64) (*wire.OutPoint, error) { capacity := incomingAmountMsat/1000 + config.AdditionalChannelCapacity if capacity == config.PublicChannelAmount { capacity++ @@ -268,7 +269,7 @@ func openChannel(client LightningClient, config *config.NodeConfig, paymentHash, feeStr, confStr, ) - channelPoint, err := client.OpenChannel(&OpenChannelRequest{ + channelPoint, err := client.OpenChannel(&lightning.OpenChannelRequest{ Destination: destination, CapacitySat: uint64(capacity), MinConfs: config.MinConfs, diff --git a/lightning_client.go b/lightning/client.go similarity index 94% rename from lightning_client.go rename to lightning/client.go index f026bfe..d414deb 100644 --- a/lightning_client.go +++ b/lightning/client.go @@ -1,4 +1,4 @@ -package main +package lightning import ( "github.com/breez/lspd/basetypes" @@ -26,7 +26,7 @@ type OpenChannelRequest struct { TargetConf *uint32 } -type LightningClient interface { +type Client interface { GetInfo() (*GetInfoResult, error) IsConnected(destination []byte) (bool, error) OpenChannel(req *OpenChannelRequest) (*wire.OutPoint, error) diff --git a/lnd_client.go b/lnd_client.go index 113cb6a..1ced705 100644 --- a/lnd_client.go +++ b/lnd_client.go @@ -9,6 +9,7 @@ import ( "github.com/breez/lspd/basetypes" "github.com/breez/lspd/config" + "github.com/breez/lspd/lightning" "github.com/btcsuite/btcd/wire" "github.com/lightningnetwork/lnd/htlcswitch/hop" "github.com/lightningnetwork/lnd/lnrpc" @@ -64,14 +65,14 @@ func (c *LndClient) Close() { c.conn.Close() } -func (c *LndClient) GetInfo() (*GetInfoResult, error) { +func (c *LndClient) GetInfo() (*lightning.GetInfoResult, error) { info, err := c.client.GetInfo(context.Background(), &lnrpc.GetInfoRequest{}) if err != nil { log.Printf("LND: client.GetInfo() error: %v", err) return nil, err } - return &GetInfoResult{ + return &lightning.GetInfoResult{ Alias: info.Alias, Pubkey: info.IdentityPubkey, }, nil @@ -96,7 +97,7 @@ func (c *LndClient) IsConnected(destination []byte) (bool, error) { return false, nil } -func (c *LndClient) OpenChannel(req *OpenChannelRequest) (*wire.OutPoint, error) { +func (c *LndClient) OpenChannel(req *lightning.OpenChannelRequest) (*wire.OutPoint, error) { lnReq := &lnrpc.OpenChannelRequest{ NodePubkey: req.Destination, LocalFundingAmount: int64(req.CapacitySat), @@ -135,7 +136,7 @@ func (c *LndClient) OpenChannel(req *OpenChannelRequest) (*wire.OutPoint, error) return result, nil } -func (c *LndClient) GetChannel(peerID []byte, channelPoint wire.OutPoint) (*GetChannelResult, error) { +func (c *LndClient) GetChannel(peerID []byte, channelPoint wire.OutPoint) (*lightning.GetChannelResult, error) { r, err := c.client.ListChannels(context.Background(), &lnrpc.ListChannelsRequest{Peer: peerID}) if err != nil { log.Printf("client.ListChannels(%x) error: %v", peerID, err) @@ -157,7 +158,7 @@ func (c *LndClient) GetChannel(peerID []byte, channelPoint wire.OutPoint) (*GetC confirmedChanId = 0 } } - return &GetChannelResult{ + return &lightning.GetChannelResult{ InitialChannelID: basetypes.ShortChannelID(c.ChanId), ConfirmedChannelID: basetypes.ShortChannelID(confirmedChanId), }, nil diff --git a/server.go b/server.go index 5d23c41..f14152f 100644 --- a/server.go +++ b/server.go @@ -12,6 +12,7 @@ import ( "github.com/breez/lspd/btceclegacy" "github.com/breez/lspd/config" + "github.com/breez/lspd/lightning" lspdrpc "github.com/breez/lspd/rpc" ecies "github.com/ecies/go/v2" "github.com/golang/protobuf/proto" @@ -39,7 +40,7 @@ type server struct { } type node struct { - client LightningClient + client lightning.Client nodeConfig *config.NodeConfig privateKey *btcec.PrivateKey publicKey *btcec.PublicKey @@ -140,7 +141,7 @@ func (s *server) OpenChannel(ctx context.Context, in *lspdrpc.OpenChannelRequest var outPoint *wire.OutPoint if channelCount == 0 { - outPoint, err = node.client.OpenChannel(&OpenChannelRequest{ + outPoint, err = node.client.OpenChannel(&lightning.OpenChannelRequest{ CapacitySat: node.nodeConfig.ChannelAmount, Destination: pubkey, TargetConf: &node.nodeConfig.TargetConf,