Merge pull request #203 from bhandras/lndclient_extension

lndclient: extend LightningClient with ListChannels and RouterClient with keysend
pull/209/head
András Bánki-Horváth 4 years ago committed by GitHub
commit bd2d39bd10
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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"
@ -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)
// 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)
@ -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.
@ -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)
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,

@ -2,6 +2,7 @@ package lndclient
import (
"context"
"crypto/rand"
"encoding/hex"
"fmt"
"time"
@ -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"
@ -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
@ -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.
@ -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.

@ -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) {

@ -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

Loading…
Cancel
Save