From 5ca018613bbdfc375d1848d1dd303281aa717e5b Mon Sep 17 00:00:00 2001 From: carla Date: Wed, 17 Jun 2020 08:44:43 +0200 Subject: [PATCH] lndclient: add listinvoices to client --- lndclient/lightning_client.go | 88 ++++++++++++++++++++++++++++++++++- test/lightning_client_mock.go | 15 ++++++ 2 files changed, 102 insertions(+), 1 deletion(-) diff --git a/lndclient/lightning_client.go b/lndclient/lightning_client.go index b948b98..244a78d 100644 --- a/lndclient/lightning_client.go +++ b/lndclient/lightning_client.go @@ -58,6 +58,10 @@ type LightningClient interface { ForwardingHistory(ctx context.Context, req ForwardingHistoryRequest) (*ForwardingHistoryResponse, error) + // ListInvoices makes a paginated call to our list invoices endpoint. + ListInvoices(ctx context.Context, req ListInvoicesRequest) ( + *ListInvoicesResponse, 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) @@ -606,6 +610,21 @@ func (s *lightningClient) LookupInvoice(ctx context.Context, return nil, err } + invoice, err := unmarshalInvoice(resp) + if err != nil { + return nil, err + } + + return invoice, nil +} + +// unmarshalInvoice creates an invoice from the rpc response provided. +func unmarshalInvoice(resp *lnrpc.Invoice) (*Invoice, error) { + hash, err := lntypes.MakeHash(resp.RHash) + if err != nil { + return nil, err + } + invoice := &Invoice{ Preimage: nil, Hash: hash, @@ -638,7 +657,8 @@ func (s *lightningClient) LookupInvoice(ctx context.Context, invoice.State = channeldb.ContractCanceled default: - return nil, fmt.Errorf("unknown invoice state: %v", resp.State) + return nil, fmt.Errorf("unknown invoice state: %v", + resp.State) } // Only set settle date if it is non-zero, because 0 unix time is @@ -936,6 +956,72 @@ func (s *lightningClient) ForwardingHistory(ctx context.Context, }, nil } +// ListInvoicesRequest contains the request parameters for a paginated +// list invoices call. +type ListInvoicesRequest struct { + // MaxInvoices is the maximum number of invoices to return. + MaxInvoices uint64 + + // Offset is the index from which to start querying. + Offset uint64 + + // Reversed is set to query our invoices backwards. + Reversed bool + + // PendingOnly is set if we only want pending invoices. + PendingOnly bool +} + +// ListInvoicesResponse contains the response to a list invoices query, +// including the index offsets required for paginated queries. +type ListInvoicesResponse struct { + // FirstIndexOffset is the index offset of the first item in our set. + FirstIndexOffset uint64 + + // LastIndexOffset is the index offset of the last item in our set. + LastIndexOffset uint64 + + // Invoices is the set of invoices that were returned. + Invoices []Invoice +} + +// ListInvoices returns a list of invoices from our node. +func (s *lightningClient) ListInvoices(ctx context.Context, + req ListInvoicesRequest) (*ListInvoicesResponse, error) { + + rpcCtx, cancel := context.WithTimeout(ctx, rpcTimeout) + defer cancel() + + resp, err := s.client.ListInvoices( + s.adminMac.WithMacaroonAuth(rpcCtx), + &lnrpc.ListInvoiceRequest{ + PendingOnly: false, + IndexOffset: req.Offset, + NumMaxInvoices: req.MaxInvoices, + Reversed: req.Reversed, + }, + ) + if err != nil { + return nil, err + } + + invoices := make([]Invoice, len(resp.Invoices)) + for i, invoice := range resp.Invoices { + inv, err := unmarshalInvoice(invoice) + if err != nil { + return nil, err + } + + invoices[i] = *inv + } + + return &ListInvoicesResponse{ + FirstIndexOffset: resp.FirstIndexOffset, + LastIndexOffset: resp.LastIndexOffset, + Invoices: invoices, + }, 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, diff --git a/test/lightning_client_mock.go b/test/lightning_client_mock.go index 37c4a9d..d93359c 100644 --- a/test/lightning_client_mock.go +++ b/test/lightning_client_mock.go @@ -193,6 +193,21 @@ func (h *mockLightningClient) ForwardingHistory(_ context.Context, }, nil } +// ListInvoices returns our mock's invoices. +func (h *mockLightningClient) ListInvoices(_ context.Context, + _ lndclient.ListInvoicesRequest) (*lndclient.ListInvoicesResponse, + error) { + + invoices := make([]lndclient.Invoice, 0, len(h.lnd.Invoices)) + for _, invoice := range h.lnd.Invoices { + invoices = append(invoices, *invoice) + } + + return &lndclient.ListInvoicesResponse{ + Invoices: invoices, + }, 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) {