mirror of
https://github.com/aljazceru/lspd.git
synced 2025-12-20 15:24:23 +01:00
cleanup: move lightning client to seperate package
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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)
|
||||
@@ -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
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user