Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions lndclient/lightning_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/lightningnetwork/lnd/lnrpc/invoicesrpc"
"github.com/lightningnetwork/lnd/lntypes"
"github.com/lightningnetwork/lnd/lnwire"
"github.com/lightningnetwork/lnd/routing/route"
"github.com/lightningnetwork/lnd/zpay32"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
Expand Down Expand Up @@ -46,6 +47,9 @@ type LightningClient interface {
// node.
ListTransactions(ctx context.Context) ([]*wire.MsgTx, error)

// ListChannels retrieves all channels of the backing lnd node.
ListChannels(ctx context.Context) ([]ChannelInfo, error)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[]*ChannelInfo?


// ChannelBackup retrieves the backup for a particular channel. The
// backup is returned as an encrypted chanbackup.Single payload.
ChannelBackup(context.Context, wire.OutPoint) ([]byte, error)
Expand All @@ -65,6 +69,29 @@ type Info struct {
Uris []string
}

// ChannelInfo stores unpacked per-channel info.
type ChannelInfo struct {
// Active indecates whether the channel is active.
Active bool

// ChannelID holds the unique channel ID for the channel. The first 3 bytes
// are the block height, the next 3 the index within the block, and the last
// 2 bytes are the /output index for the channel.
ChannelID uint64

// PubKeyBytes is the raw bytes of the public key of the remote node.
PubKeyBytes route.Vertex

// Capacity is the total amount of funds held in this channel.
Capacity btcutil.Amount

// LocalBalance is the current balance of this node in this channel.
LocalBalance btcutil.Amount

// RemoteBalance is the counterparty's current balance in this channel.
RemoteBalance btcutil.Amount
}

var (
// ErrMalformedServerResponse is returned when the swap and/or prepay
// invoice is malformed.
Expand Down Expand Up @@ -495,6 +522,41 @@ func (s *lightningClient) ListTransactions(ctx context.Context) ([]*wire.MsgTx,
return txs, nil
}

// ListChannels retrieves all channels of the backing lnd node.
func (s *lightningClient) ListChannels(ctx context.Context) (
[]ChannelInfo, error) {

rpcCtx, cancel := context.WithTimeout(ctx, rpcTimeout)
defer cancel()

response, err := s.client.ListChannels(
s.adminMac.WithMacaroonAuth(rpcCtx),
&lnrpc.ListChannelsRequest{},
)
if err != nil {
return nil, err
}

result := make([]ChannelInfo, len(response.Channels))
for i, channel := range response.Channels {
remoteVertex, err := route.NewVertexFromStr(channel.RemotePubkey)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Future devs will thank you for the luxury of not having to parse everywhere :)

if err != nil {
return nil, err
}

result[i] = ChannelInfo{
Active: channel.Active,
ChannelID: channel.ChanId,
PubKeyBytes: remoteVertex,
Capacity: btcutil.Amount(channel.Capacity),
LocalBalance: btcutil.Amount(channel.LocalBalance),
RemoteBalance: btcutil.Amount(channel.RemoteBalance),
}
}

return result, nil
}

// ChannelBackup retrieves the backup for a particular channel. The backup is
// returned as an encrypted chanbackup.Single payload.
func (s *lightningClient) ChannelBackup(ctx context.Context,
Expand Down
25 changes: 24 additions & 1 deletion lndclient/router_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package lndclient

import (
"context"
"crypto/rand"
"encoding/hex"
"fmt"
"time"
Expand All @@ -12,6 +13,7 @@ import (
"github.com/lightningnetwork/lnd/lnrpc/routerrpc"
"github.com/lightningnetwork/lnd/lntypes"
"github.com/lightningnetwork/lnd/lnwire"
"github.com/lightningnetwork/lnd/record"
"github.com/lightningnetwork/lnd/routing/route"
"github.com/lightningnetwork/lnd/zpay32"
"google.golang.org/grpc"
Expand Down Expand Up @@ -88,7 +90,7 @@ type SendPaymentRequest struct {

// PaymentHash is the r-hash value to use within the HTLC extended to
// the first hop.
PaymentHash [32]byte
PaymentHash *lntypes.Hash

// FinalCLTVDelta is the CTLV expiry delta to use for the _final_ hop
// in the route. This means that the final hop will have a CLTV delta
Expand All @@ -112,6 +114,9 @@ type SendPaymentRequest struct {
// MaxParts is the maximum number of partial payments that may be used
// to complete the full amount.
MaxParts uint32

// KeySend is set to true if the tlv payload will include the preimage.
KeySend bool
}

// routerClient is a wrapper around the generated routerrpc proxy.
Expand Down Expand Up @@ -149,6 +154,24 @@ func (r *routerClient) SendPayment(ctx context.Context,
if request.LastHopPubkey != nil {
rpcReq.LastHopPubkey = request.LastHopPubkey[:]
}
if request.KeySend {
if request.PaymentHash != nil {
return nil, nil, fmt.Errorf(
"keysend payment must not include a preset payment hash")
}

var preimage lntypes.Preimage
if _, err := rand.Read(preimage[:]); err != nil {
return nil, nil, err
}

// Override the payment hash.
rpcReq.DestCustomRecords = map[uint64][]byte{
record.KeySendType: preimage[:],
}
hash := preimage.Hash()
request.PaymentHash = &hash
}

// Only if there is no payment request set, we will parse the individual
// payment parameters.
Expand Down
7 changes: 7 additions & 0 deletions test/lightning_client_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,13 @@ func (h *mockLightningClient) ListTransactions(
return txs, nil
}

// ListChannels retrieves all channels of the backing lnd node.
func (h *mockLightningClient) ListChannels(ctx context.Context) (
[]lndclient.ChannelInfo, error) {

return h.lnd.Channels, nil
}

// ChannelBackup retrieves the backup for a particular channel. The
// backup is returned as an encrypted chanbackup.Single payload.
func (h *mockLightningClient) ChannelBackup(context.Context, wire.OutPoint) ([]byte, error) {
Expand Down
2 changes: 2 additions & 0 deletions test/lnd_services_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ type LndMockServices struct {
// keyed by hash string.
Invoices map[lntypes.Hash]*lndclient.Invoice

Channels []lndclient.ChannelInfo

WaitForFinished func()

lock sync.Mutex
Expand Down