mirror of
https://github.com/lightninglabs/loop.git
synced 2026-08-13 12:33:03 +02:00
lndclient: add listinvoices to client
This commit is contained in:
parent
e016d2ebf4
commit
5ca018613b
2 changed files with 102 additions and 1 deletions
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue