@@ -17,6 +17,7 @@ import (
1717 "github.com/lightningnetwork/lnd/lnrpc/invoicesrpc"
1818 "github.com/lightningnetwork/lnd/lntypes"
1919 "github.com/lightningnetwork/lnd/lnwire"
20+ "github.com/lightningnetwork/lnd/routing/route"
2021 "github.com/lightningnetwork/lnd/zpay32"
2122 "google.golang.org/grpc"
2223 "google.golang.org/grpc/codes"
@@ -46,6 +47,9 @@ type LightningClient interface {
4647 // node.
4748 ListTransactions (ctx context.Context ) ([]* wire.MsgTx , error )
4849
50+ // ListChannels retrieves all channels of the backing lnd node.
51+ ListChannels (ctx context.Context ) ([]ChannelInfo , error )
52+
4953 // ChannelBackup retrieves the backup for a particular channel. The
5054 // backup is returned as an encrypted chanbackup.Single payload.
5155 ChannelBackup (context.Context , wire.OutPoint ) ([]byte , error )
@@ -65,6 +69,29 @@ type Info struct {
6569 Uris []string
6670}
6771
72+ // ChannelInfo stores unpacked per-channel info.
73+ type ChannelInfo struct {
74+ // Active indecates whether the channel is active.
75+ Active bool
76+
77+ // ChannelID holds the unique channel ID for the channel. The first 3 bytes
78+ // are the block height, the next 3 the index within the block, and the last
79+ // 2 bytes are the /output index for the channel.
80+ ChannelID uint64
81+
82+ // PubKeyBytes is the raw bytes of the public key of the remote node.
83+ PubKeyBytes route.Vertex
84+
85+ // Capacity is the total amount of funds held in this channel.
86+ Capacity btcutil.Amount
87+
88+ // LocalBalance is the current balance of this node in this channel.
89+ LocalBalance btcutil.Amount
90+
91+ // RemoteBalance is the counterparty's current balance in this channel.
92+ RemoteBalance btcutil.Amount
93+ }
94+
6895var (
6996 // ErrMalformedServerResponse is returned when the swap and/or prepay
7097 // invoice is malformed.
@@ -495,6 +522,41 @@ func (s *lightningClient) ListTransactions(ctx context.Context) ([]*wire.MsgTx,
495522 return txs , nil
496523}
497524
525+ // ListChannels retrieves all channels of the backing lnd node.
526+ func (s * lightningClient ) ListChannels (ctx context.Context ) (
527+ []ChannelInfo , error ) {
528+
529+ rpcCtx , cancel := context .WithTimeout (ctx , rpcTimeout )
530+ defer cancel ()
531+
532+ response , err := s .client .ListChannels (
533+ s .adminMac .WithMacaroonAuth (rpcCtx ),
534+ & lnrpc.ListChannelsRequest {},
535+ )
536+ if err != nil {
537+ return nil , err
538+ }
539+
540+ result := make ([]ChannelInfo , len (response .Channels ))
541+ for i , channel := range response .Channels {
542+ remoteVertex , err := route .NewVertexFromStr (channel .RemotePubkey )
543+ if err != nil {
544+ return nil , err
545+ }
546+
547+ result [i ] = ChannelInfo {
548+ Active : channel .Active ,
549+ ChannelID : channel .ChanId ,
550+ PubKeyBytes : remoteVertex ,
551+ Capacity : btcutil .Amount (channel .Capacity ),
552+ LocalBalance : btcutil .Amount (channel .LocalBalance ),
553+ RemoteBalance : btcutil .Amount (channel .RemoteBalance ),
554+ }
555+ }
556+
557+ return result , nil
558+ }
559+
498560// ChannelBackup retrieves the backup for a particular channel. The backup is
499561// returned as an encrypted chanbackup.Single payload.
500562func (s * lightningClient ) ChannelBackup (ctx context.Context ,
0 commit comments