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"
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/breez/lspd/basetypes"
|
"github.com/breez/lspd/basetypes"
|
||||||
|
"github.com/breez/lspd/lightning"
|
||||||
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
||||||
"github.com/btcsuite/btcd/wire"
|
"github.com/btcsuite/btcd/wire"
|
||||||
"github.com/niftynei/glightning/glightning"
|
"github.com/niftynei/glightning/glightning"
|
||||||
@@ -42,14 +43,14 @@ func NewClnClient(socketPath string) (*ClnClient, error) {
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ClnClient) GetInfo() (*GetInfoResult, error) {
|
func (c *ClnClient) GetInfo() (*lightning.GetInfoResult, error) {
|
||||||
info, err := c.client.GetInfo()
|
info, err := c.client.GetInfo()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("CLN: client.GetInfo() error: %v", err)
|
log.Printf("CLN: client.GetInfo() error: %v", err)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &GetInfoResult{
|
return &lightning.GetInfoResult{
|
||||||
Alias: info.Alias,
|
Alias: info.Alias,
|
||||||
Pubkey: info.Id,
|
Pubkey: info.Id,
|
||||||
}, nil
|
}, nil
|
||||||
@@ -74,7 +75,7 @@ func (c *ClnClient) IsConnected(destination []byte) (bool, error) {
|
|||||||
return false, nil
|
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)
|
pubkey := hex.EncodeToString(req.Destination)
|
||||||
var minConfs *uint16
|
var minConfs *uint16
|
||||||
if req.MinConfs != nil {
|
if req.MinConfs != nil {
|
||||||
@@ -140,7 +141,7 @@ func (c *ClnClient) OpenChannel(req *OpenChannelRequest) (*wire.OutPoint, error)
|
|||||||
return channelPoint, nil
|
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)
|
pubkey := hex.EncodeToString(peerID)
|
||||||
peer, err := c.client.GetPeer(pubkey)
|
peer, err := c.client.GetPeer(pubkey)
|
||||||
if err != nil {
|
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)
|
fmt.Printf("NewShortChannelIDFromString %v error: %v", c.Alias.Local, err)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &GetChannelResult{
|
return &lightning.GetChannelResult{
|
||||||
InitialChannelID: *initialChanID,
|
InitialChannelID: *initialChanID,
|
||||||
ConfirmedChannelID: *confirmedChanID,
|
ConfirmedChannelID: *confirmedChanID,
|
||||||
}, nil
|
}, nil
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import (
|
|||||||
|
|
||||||
"github.com/breez/lspd/chain"
|
"github.com/breez/lspd/chain"
|
||||||
"github.com/breez/lspd/config"
|
"github.com/breez/lspd/config"
|
||||||
|
"github.com/breez/lspd/lightning"
|
||||||
"github.com/btcsuite/btcd/btcec/v2"
|
"github.com/btcsuite/btcd/btcec/v2"
|
||||||
"github.com/btcsuite/btcd/wire"
|
"github.com/btcsuite/btcd/wire"
|
||||||
sphinx "github.com/lightningnetwork/lightning-onion"
|
sphinx "github.com/lightningnetwork/lightning-onion"
|
||||||
@@ -50,7 +51,7 @@ type interceptResult struct {
|
|||||||
onionBlob []byte
|
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)
|
reqPaymentHashStr := hex.EncodeToString(reqPaymentHash)
|
||||||
resp, _, _ := payHashGroup.Do(reqPaymentHashStr, func() (interface{}, error) {
|
resp, _, _ := payHashGroup.Do(reqPaymentHashStr, func() (interface{}, error) {
|
||||||
paymentHash, paymentSecret, destination, incomingAmountMsat, outgoingAmountMsat, channelPoint, err := paymentInfo(reqPaymentHash)
|
paymentHash, paymentSecret, destination, incomingAmountMsat, outgoingAmountMsat, channelPoint, err := paymentInfo(reqPaymentHash)
|
||||||
@@ -236,7 +237,7 @@ func checkPayment(config *config.NodeConfig, incomingAmountMsat, outgoingAmountM
|
|||||||
return nil
|
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
|
capacity := incomingAmountMsat/1000 + config.AdditionalChannelCapacity
|
||||||
if capacity == config.PublicChannelAmount {
|
if capacity == config.PublicChannelAmount {
|
||||||
capacity++
|
capacity++
|
||||||
@@ -268,7 +269,7 @@ func openChannel(client LightningClient, config *config.NodeConfig, paymentHash,
|
|||||||
feeStr,
|
feeStr,
|
||||||
confStr,
|
confStr,
|
||||||
)
|
)
|
||||||
channelPoint, err := client.OpenChannel(&OpenChannelRequest{
|
channelPoint, err := client.OpenChannel(&lightning.OpenChannelRequest{
|
||||||
Destination: destination,
|
Destination: destination,
|
||||||
CapacitySat: uint64(capacity),
|
CapacitySat: uint64(capacity),
|
||||||
MinConfs: config.MinConfs,
|
MinConfs: config.MinConfs,
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
package main
|
package lightning
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/breez/lspd/basetypes"
|
"github.com/breez/lspd/basetypes"
|
||||||
@@ -26,7 +26,7 @@ type OpenChannelRequest struct {
|
|||||||
TargetConf *uint32
|
TargetConf *uint32
|
||||||
}
|
}
|
||||||
|
|
||||||
type LightningClient interface {
|
type Client interface {
|
||||||
GetInfo() (*GetInfoResult, error)
|
GetInfo() (*GetInfoResult, error)
|
||||||
IsConnected(destination []byte) (bool, error)
|
IsConnected(destination []byte) (bool, error)
|
||||||
OpenChannel(req *OpenChannelRequest) (*wire.OutPoint, error)
|
OpenChannel(req *OpenChannelRequest) (*wire.OutPoint, error)
|
||||||
@@ -9,6 +9,7 @@ import (
|
|||||||
|
|
||||||
"github.com/breez/lspd/basetypes"
|
"github.com/breez/lspd/basetypes"
|
||||||
"github.com/breez/lspd/config"
|
"github.com/breez/lspd/config"
|
||||||
|
"github.com/breez/lspd/lightning"
|
||||||
"github.com/btcsuite/btcd/wire"
|
"github.com/btcsuite/btcd/wire"
|
||||||
"github.com/lightningnetwork/lnd/htlcswitch/hop"
|
"github.com/lightningnetwork/lnd/htlcswitch/hop"
|
||||||
"github.com/lightningnetwork/lnd/lnrpc"
|
"github.com/lightningnetwork/lnd/lnrpc"
|
||||||
@@ -64,14 +65,14 @@ func (c *LndClient) Close() {
|
|||||||
c.conn.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{})
|
info, err := c.client.GetInfo(context.Background(), &lnrpc.GetInfoRequest{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("LND: client.GetInfo() error: %v", err)
|
log.Printf("LND: client.GetInfo() error: %v", err)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &GetInfoResult{
|
return &lightning.GetInfoResult{
|
||||||
Alias: info.Alias,
|
Alias: info.Alias,
|
||||||
Pubkey: info.IdentityPubkey,
|
Pubkey: info.IdentityPubkey,
|
||||||
}, nil
|
}, nil
|
||||||
@@ -96,7 +97,7 @@ func (c *LndClient) IsConnected(destination []byte) (bool, error) {
|
|||||||
return false, nil
|
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{
|
lnReq := &lnrpc.OpenChannelRequest{
|
||||||
NodePubkey: req.Destination,
|
NodePubkey: req.Destination,
|
||||||
LocalFundingAmount: int64(req.CapacitySat),
|
LocalFundingAmount: int64(req.CapacitySat),
|
||||||
@@ -135,7 +136,7 @@ func (c *LndClient) OpenChannel(req *OpenChannelRequest) (*wire.OutPoint, error)
|
|||||||
return result, nil
|
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})
|
r, err := c.client.ListChannels(context.Background(), &lnrpc.ListChannelsRequest{Peer: peerID})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("client.ListChannels(%x) error: %v", peerID, err)
|
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
|
confirmedChanId = 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return &GetChannelResult{
|
return &lightning.GetChannelResult{
|
||||||
InitialChannelID: basetypes.ShortChannelID(c.ChanId),
|
InitialChannelID: basetypes.ShortChannelID(c.ChanId),
|
||||||
ConfirmedChannelID: basetypes.ShortChannelID(confirmedChanId),
|
ConfirmedChannelID: basetypes.ShortChannelID(confirmedChanId),
|
||||||
}, nil
|
}, nil
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import (
|
|||||||
|
|
||||||
"github.com/breez/lspd/btceclegacy"
|
"github.com/breez/lspd/btceclegacy"
|
||||||
"github.com/breez/lspd/config"
|
"github.com/breez/lspd/config"
|
||||||
|
"github.com/breez/lspd/lightning"
|
||||||
lspdrpc "github.com/breez/lspd/rpc"
|
lspdrpc "github.com/breez/lspd/rpc"
|
||||||
ecies "github.com/ecies/go/v2"
|
ecies "github.com/ecies/go/v2"
|
||||||
"github.com/golang/protobuf/proto"
|
"github.com/golang/protobuf/proto"
|
||||||
@@ -39,7 +40,7 @@ type server struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type node struct {
|
type node struct {
|
||||||
client LightningClient
|
client lightning.Client
|
||||||
nodeConfig *config.NodeConfig
|
nodeConfig *config.NodeConfig
|
||||||
privateKey *btcec.PrivateKey
|
privateKey *btcec.PrivateKey
|
||||||
publicKey *btcec.PublicKey
|
publicKey *btcec.PublicKey
|
||||||
@@ -140,7 +141,7 @@ func (s *server) OpenChannel(ctx context.Context, in *lspdrpc.OpenChannelRequest
|
|||||||
|
|
||||||
var outPoint *wire.OutPoint
|
var outPoint *wire.OutPoint
|
||||||
if channelCount == 0 {
|
if channelCount == 0 {
|
||||||
outPoint, err = node.client.OpenChannel(&OpenChannelRequest{
|
outPoint, err = node.client.OpenChannel(&lightning.OpenChannelRequest{
|
||||||
CapacitySat: node.nodeConfig.ChannelAmount,
|
CapacitySat: node.nodeConfig.ChannelAmount,
|
||||||
Destination: pubkey,
|
Destination: pubkey,
|
||||||
TargetConf: &node.nodeConfig.TargetConf,
|
TargetConf: &node.nodeConfig.TargetConf,
|
||||||
|
|||||||
Reference in New Issue
Block a user