Skip to content

Commit 5ca0186

Browse files
committed
lndclient: add listinvoices to client
1 parent e016d2e commit 5ca0186

File tree

2 files changed

+102
-1
lines changed

2 files changed

+102
-1
lines changed

lndclient/lightning_client.go

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ type LightningClient interface {
5858
ForwardingHistory(ctx context.Context,
5959
req ForwardingHistoryRequest) (*ForwardingHistoryResponse, error)
6060

61+
// ListInvoices makes a paginated call to our list invoices endpoint.
62+
ListInvoices(ctx context.Context, req ListInvoicesRequest) (
63+
*ListInvoicesResponse, error)
64+
6165
// ChannelBackup retrieves the backup for a particular channel. The
6266
// backup is returned as an encrypted chanbackup.Single payload.
6367
ChannelBackup(context.Context, wire.OutPoint) ([]byte, error)
@@ -606,6 +610,21 @@ func (s *lightningClient) LookupInvoice(ctx context.Context,
606610
return nil, err
607611
}
608612

613+
invoice, err := unmarshalInvoice(resp)
614+
if err != nil {
615+
return nil, err
616+
}
617+
618+
return invoice, nil
619+
}
620+
621+
// unmarshalInvoice creates an invoice from the rpc response provided.
622+
func unmarshalInvoice(resp *lnrpc.Invoice) (*Invoice, error) {
623+
hash, err := lntypes.MakeHash(resp.RHash)
624+
if err != nil {
625+
return nil, err
626+
}
627+
609628
invoice := &Invoice{
610629
Preimage: nil,
611630
Hash: hash,
@@ -638,7 +657,8 @@ func (s *lightningClient) LookupInvoice(ctx context.Context,
638657
invoice.State = channeldb.ContractCanceled
639658

640659
default:
641-
return nil, fmt.Errorf("unknown invoice state: %v", resp.State)
660+
return nil, fmt.Errorf("unknown invoice state: %v",
661+
resp.State)
642662
}
643663

644664
// 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,
936956
}, nil
937957
}
938958

959+
// ListInvoicesRequest contains the request parameters for a paginated
960+
// list invoices call.
961+
type ListInvoicesRequest struct {
962+
// MaxInvoices is the maximum number of invoices to return.
963+
MaxInvoices uint64
964+
965+
// Offset is the index from which to start querying.
966+
Offset uint64
967+
968+
// Reversed is set to query our invoices backwards.
969+
Reversed bool
970+
971+
// PendingOnly is set if we only want pending invoices.
972+
PendingOnly bool
973+
}
974+
975+
// ListInvoicesResponse contains the response to a list invoices query,
976+
// including the index offsets required for paginated queries.
977+
type ListInvoicesResponse struct {
978+
// FirstIndexOffset is the index offset of the first item in our set.
979+
FirstIndexOffset uint64
980+
981+
// LastIndexOffset is the index offset of the last item in our set.
982+
LastIndexOffset uint64
983+
984+
// Invoices is the set of invoices that were returned.
985+
Invoices []Invoice
986+
}
987+
988+
// ListInvoices returns a list of invoices from our node.
989+
func (s *lightningClient) ListInvoices(ctx context.Context,
990+
req ListInvoicesRequest) (*ListInvoicesResponse, error) {
991+
992+
rpcCtx, cancel := context.WithTimeout(ctx, rpcTimeout)
993+
defer cancel()
994+
995+
resp, err := s.client.ListInvoices(
996+
s.adminMac.WithMacaroonAuth(rpcCtx),
997+
&lnrpc.ListInvoiceRequest{
998+
PendingOnly: false,
999+
IndexOffset: req.Offset,
1000+
NumMaxInvoices: req.MaxInvoices,
1001+
Reversed: req.Reversed,
1002+
},
1003+
)
1004+
if err != nil {
1005+
return nil, err
1006+
}
1007+
1008+
invoices := make([]Invoice, len(resp.Invoices))
1009+
for i, invoice := range resp.Invoices {
1010+
inv, err := unmarshalInvoice(invoice)
1011+
if err != nil {
1012+
return nil, err
1013+
}
1014+
1015+
invoices[i] = *inv
1016+
}
1017+
1018+
return &ListInvoicesResponse{
1019+
FirstIndexOffset: resp.FirstIndexOffset,
1020+
LastIndexOffset: resp.LastIndexOffset,
1021+
Invoices: invoices,
1022+
}, nil
1023+
}
1024+
9391025
// ChannelBackup retrieves the backup for a particular channel. The backup is
9401026
// returned as an encrypted chanbackup.Single payload.
9411027
func (s *lightningClient) ChannelBackup(ctx context.Context,

test/lightning_client_mock.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,21 @@ func (h *mockLightningClient) ForwardingHistory(_ context.Context,
193193
}, nil
194194
}
195195

196+
// ListInvoices returns our mock's invoices.
197+
func (h *mockLightningClient) ListInvoices(_ context.Context,
198+
_ lndclient.ListInvoicesRequest) (*lndclient.ListInvoicesResponse,
199+
error) {
200+
201+
invoices := make([]lndclient.Invoice, 0, len(h.lnd.Invoices))
202+
for _, invoice := range h.lnd.Invoices {
203+
invoices = append(invoices, *invoice)
204+
}
205+
206+
return &lndclient.ListInvoicesResponse{
207+
Invoices: invoices,
208+
}, nil
209+
}
210+
196211
// ChannelBackup retrieves the backup for a particular channel. The
197212
// backup is returned as an encrypted chanbackup.Single payload.
198213
func (h *mockLightningClient) ChannelBackup(context.Context, wire.OutPoint) ([]byte, error) {

0 commit comments

Comments
 (0)