lnrpc/invoicesrpc: add new invoice ref LookupInvoiceV2 method
In this commit, we add a new `LookupInvoiceV2` method attached to the invoice RPC sub-server. Compared to the existing version, this version allows an invoice to be looked up by an invoice ref. This enables users to query an AMP invoice based on a specific _set ID_, so they can get the information related to the set of cumulative settles to that invoices.
This commit is contained in:
9 files changed
+441
-35
No files matched your search
@@ -5,11 +5,14 @@ package invoicesrpc
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
"gopkg.in/macaroon-bakery.v2/bakery"
|
||||
|
||||
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
|
||||
@@ -59,6 +62,10 @@ var (
|
||||
Entity: "invoices",
|
||||
Action: "write",
|
||||
}},
|
||||
"/invoicesrpc.Invoices/LookupInvoiceV2": {{
|
||||
Entity: "invoices",
|
||||
Action: "write",
|
||||
}},
|
||||
}
|
||||
|
||||
// DefaultInvoicesMacFilename is the default name of the invoices
|
||||
@@ -363,3 +370,55 @@ func (s *Server) AddHoldInvoice(ctx context.Context,
|
||||
PaymentAddr: dbInvoice.Terms.PaymentAddr[:],
|
||||
}, nil
|
||||
}
|
||||
|
||||
// LookupInvoiceV2 attempts to look up at invoice. An invoice can be referenced
|
||||
// using either its payment hash, payment address, or set ID.
|
||||
func (s *Server) LookupInvoiceV2(ctx context.Context,
|
||||
req *LookupInvoiceMsg) (*lnrpc.Invoice, error) {
|
||||
|
||||
var invoiceRef channeldb.InvoiceRef
|
||||
|
||||
// First, we'll attempt to parse out the invoice ref from the proto
|
||||
// oneof. If none of the three currently supported types was
|
||||
// specified, then we'll exit with an error.
|
||||
switch {
|
||||
case req.GetPaymentHash() != nil:
|
||||
payHash, err := lntypes.MakeHash(req.GetPaymentHash())
|
||||
if err != nil {
|
||||
return nil, status.Error(
|
||||
codes.InvalidArgument,
|
||||
fmt.Sprintf("unable to parse pay hash: %v", err),
|
||||
)
|
||||
}
|
||||
|
||||
invoiceRef = channeldb.InvoiceRefByHash(payHash)
|
||||
|
||||
case req.GetPaymentAddr() != nil:
|
||||
var payAddr [32]byte
|
||||
copy(payAddr[:], req.GetPaymentAddr())
|
||||
|
||||
invoiceRef = channeldb.InvoiceRefByAddr(payAddr)
|
||||
|
||||
case req.GetSetId() != nil:
|
||||
var setID [32]byte
|
||||
copy(setID[:], req.GetSetId())
|
||||
|
||||
invoiceRef = channeldb.InvoiceRefBySetID(setID)
|
||||
|
||||
default:
|
||||
return nil, status.Error(codes.InvalidArgument,
|
||||
"invoice ref must be set")
|
||||
}
|
||||
|
||||
// Attempt to locate the invoice, returning a nice "not found" error if
|
||||
// we can't find it in the database.
|
||||
invoice, err := s.cfg.InvoiceRegistry.LookupInvoiceByRef(invoiceRef)
|
||||
switch {
|
||||
case err == channeldb.ErrInvoiceNotFound:
|
||||
return nil, status.Error(codes.NotFound, err.Error())
|
||||
case err != nil:
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return CreateRPCInvoice(&invoice, s.cfg.ChainParams)
|
||||
}
|
||||
Reference in new issue
Block a user