mirror of
https://github.com/aljazceru/lspd.git
synced 2025-12-18 14:24:21 +01:00
add listchannels to lightning clients
This commit is contained in:
@@ -320,6 +320,44 @@ func (c *ClnClient) WaitChannelActive(peerID []byte, deadline time.Time) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *ClnClient) ListChannels() ([]*lightning.Channel, error) {
|
||||||
|
channels, err := c.client.ListPeerChannels()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
result := make([]*lightning.Channel, len(channels))
|
||||||
|
for i, channel := range channels {
|
||||||
|
peerId, err := hex.DecodeString(channel.PeerId)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("cln.ListChannels returned channel without peer id: %+v", channel)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
aliasScid, confirmedScid, err := mapScidsFromChannel(channel)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var outpoint *wire.OutPoint
|
||||||
|
fundingTxId, err := hex.DecodeString(channel.FundingTxId)
|
||||||
|
if err == nil && fundingTxId != nil && len(fundingTxId) > 0 {
|
||||||
|
outpoint, _ = lightning.NewOutPoint(fundingTxId, channel.FundingOutnum)
|
||||||
|
}
|
||||||
|
if outpoint == nil {
|
||||||
|
log.Printf("cln.ListChannels returned channel without outpoint: %+v", channel)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
result[i] = &lightning.Channel{
|
||||||
|
AliasScid: aliasScid,
|
||||||
|
ConfirmedScid: confirmedScid,
|
||||||
|
ChannelPoint: outpoint,
|
||||||
|
PeerId: peerId,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
func mapScidsFromChannel(c *glightning.PeerChannel) (*lightning.ShortChannelID, *lightning.ShortChannelID, error) {
|
func mapScidsFromChannel(c *glightning.PeerChannel) (*lightning.ShortChannelID, *lightning.ShortChannelID, error) {
|
||||||
var confirmedScid *lightning.ShortChannelID
|
var confirmedScid *lightning.ShortChannelID
|
||||||
var aliasScid *lightning.ShortChannelID
|
var aliasScid *lightning.ShortChannelID
|
||||||
|
|||||||
@@ -25,6 +25,13 @@ type OpenChannelRequest struct {
|
|||||||
TargetConf *uint32
|
TargetConf *uint32
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Channel struct {
|
||||||
|
AliasScid *ShortChannelID
|
||||||
|
ConfirmedScid *ShortChannelID
|
||||||
|
ChannelPoint *wire.OutPoint
|
||||||
|
PeerId []byte
|
||||||
|
}
|
||||||
|
|
||||||
type Client interface {
|
type Client interface {
|
||||||
GetInfo() (*GetInfoResult, error)
|
GetInfo() (*GetInfoResult, error)
|
||||||
IsConnected(destination []byte) (bool, error)
|
IsConnected(destination []byte) (bool, error)
|
||||||
@@ -34,4 +41,5 @@ type Client interface {
|
|||||||
GetClosedChannels(nodeID string, channelPoints map[string]uint64) (map[string]uint64, error)
|
GetClosedChannels(nodeID string, channelPoints map[string]uint64) (map[string]uint64, error)
|
||||||
WaitOnline(peerID []byte, deadline time.Time) error
|
WaitOnline(peerID []byte, deadline time.Time) error
|
||||||
WaitChannelActive(peerID []byte, deadline time.Time) error
|
WaitChannelActive(peerID []byte, deadline time.Time) error
|
||||||
|
ListChannels() ([]*Channel, error)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,11 @@
|
|||||||
package lightning
|
package lightning
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/hex"
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
||||||
"github.com/btcsuite/btcd/wire"
|
"github.com/btcsuite/btcd/wire"
|
||||||
@@ -17,3 +21,22 @@ func NewOutPoint(fundingTxID []byte, index uint32) (*wire.OutPoint, error) {
|
|||||||
|
|
||||||
return wire.NewOutPoint(&h, index), nil
|
return wire.NewOutPoint(&h, index), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NewOutPointFromString(outpoint string) (*wire.OutPoint, error) {
|
||||||
|
split := strings.Split(outpoint, ":")
|
||||||
|
if len(split) != 2 {
|
||||||
|
return nil, fmt.Errorf("invalid outpoint")
|
||||||
|
}
|
||||||
|
|
||||||
|
fundingTxId, err := hex.DecodeString(split[0])
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("invalid outpoint")
|
||||||
|
}
|
||||||
|
|
||||||
|
outnum, err := strconv.ParseUint(split[1], 10, 32)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("invalid outpoint")
|
||||||
|
}
|
||||||
|
|
||||||
|
return NewOutPoint(fundingTxId, uint32(outnum))
|
||||||
|
}
|
||||||
|
|||||||
@@ -492,6 +492,65 @@ func (c *LndClient) WaitChannelActive(peerID []byte, deadline time.Time) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *LndClient) ListChannels() ([]*lightning.Channel, error) {
|
||||||
|
channels, err := c.client.ListChannels(
|
||||||
|
context.TODO(),
|
||||||
|
&lnrpc.ListChannelsRequest{},
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
pendingChannels, err := c.client.PendingChannels(
|
||||||
|
context.TODO(),
|
||||||
|
&lnrpc.PendingChannelsRequest{},
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
result := make([]*lightning.Channel, len(channels.Channels))
|
||||||
|
for i, c := range channels.Channels {
|
||||||
|
peerId, err := hex.DecodeString(c.RemotePubkey)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("hex.DecodeString in LndClient.ListChannels error: %v", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
alias, confirmedScid := mapScidsFromChannel(c)
|
||||||
|
outpoint, err := lightning.NewOutPointFromString(c.ChannelPoint)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("lightning.NewOutPointFromString(%s) in LndClient.ListChannels error: %v", c.ChannelPoint, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
result[i] = &lightning.Channel{
|
||||||
|
AliasScid: alias,
|
||||||
|
ConfirmedScid: confirmedScid,
|
||||||
|
ChannelPoint: outpoint,
|
||||||
|
PeerId: peerId,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, c := range pendingChannels.PendingOpenChannels {
|
||||||
|
peerId, err := hex.DecodeString(c.Channel.RemoteNodePub)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("hex.DecodeString in LndClient.ListChannels error: %v", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
outpoint, err := lightning.NewOutPointFromString(c.Channel.ChannelPoint)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("lightning.NewOutPointFromString(%s) in LndClient.ListChannels error: %v", c.Channel.ChannelPoint, err)
|
||||||
|
}
|
||||||
|
result = append(result, &lightning.Channel{
|
||||||
|
AliasScid: nil,
|
||||||
|
ConfirmedScid: nil,
|
||||||
|
ChannelPoint: outpoint,
|
||||||
|
PeerId: peerId,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
func mapScidsFromChannel(c *lnrpc.Channel) (*lightning.ShortChannelID, *lightning.ShortChannelID) {
|
func mapScidsFromChannel(c *lnrpc.Channel) (*lightning.ShortChannelID, *lightning.ShortChannelID) {
|
||||||
var alias *lightning.ShortChannelID
|
var alias *lightning.ShortChannelID
|
||||||
var confirmedScid *lightning.ShortChannelID
|
var confirmedScid *lightning.ShortChannelID
|
||||||
|
|||||||
@@ -147,6 +147,9 @@ func (c *mockLightningClient) WaitOnline(peerID []byte, deadline time.Time) erro
|
|||||||
func (c *mockLightningClient) WaitChannelActive(peerID []byte, deadline time.Time) error {
|
func (c *mockLightningClient) WaitChannelActive(peerID []byte, deadline time.Time) error {
|
||||||
return ErrNotImplemented
|
return ErrNotImplemented
|
||||||
}
|
}
|
||||||
|
func (c *mockLightningClient) ListChannels() ([]*lightning.Channel, error) {
|
||||||
|
return nil, ErrNotImplemented
|
||||||
|
}
|
||||||
|
|
||||||
type mockFeeEstimator struct {
|
type mockFeeEstimator struct {
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user