2018-12-20 13:42:28 +03:00
|
|
|
// +build invoicesrpc
|
|
|
|
|
|
|
|
package invoicesrpc
|
|
|
|
|
|
|
|
import (
|
2019-01-03 21:15:14 +03:00
|
|
|
"context"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2019-02-02 17:46:23 +03:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
"gopkg.in/macaroon-bakery.v2/bakery"
|
2019-01-11 13:19:16 +03:00
|
|
|
|
2020-04-02 16:12:28 +03:00
|
|
|
"github.com/grpc-ecosystem/grpc-gateway/runtime"
|
2019-02-11 14:02:39 +03:00
|
|
|
"github.com/lightningnetwork/lnd/channeldb"
|
2019-01-11 13:19:16 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnrpc"
|
|
|
|
"github.com/lightningnetwork/lnd/lntypes"
|
2020-07-23 19:36:42 +03:00
|
|
|
"github.com/lightningnetwork/lnd/macaroons"
|
2018-12-20 13:42:28 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// subServerName is the name of the sub rpc server. We'll use this name
|
|
|
|
// to register ourselves, and we also require that the main
|
|
|
|
// SubServerConfigDispatcher instance recognize it as the name of our
|
|
|
|
// RPC service.
|
|
|
|
subServerName = "InvoicesRPC"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2019-01-03 21:15:14 +03:00
|
|
|
// macaroonOps are the set of capabilities that our minted macaroon (if
|
|
|
|
// it doesn't already exist) will have.
|
|
|
|
macaroonOps = []bakery.Op{
|
2019-01-11 13:19:16 +03:00
|
|
|
{
|
|
|
|
Entity: "invoices",
|
|
|
|
Action: "write",
|
|
|
|
},
|
2019-01-03 21:15:14 +03:00
|
|
|
{
|
|
|
|
Entity: "invoices",
|
|
|
|
Action: "read",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-12-20 13:42:28 +03:00
|
|
|
// macPermissions maps RPC calls to the permissions they require.
|
2019-01-03 21:15:14 +03:00
|
|
|
macPermissions = map[string][]bakery.Op{
|
|
|
|
"/invoicesrpc.Invoices/SubscribeSingleInvoice": {{
|
|
|
|
Entity: "invoices",
|
|
|
|
Action: "read",
|
|
|
|
}},
|
2019-02-11 14:02:39 +03:00
|
|
|
"/invoicesrpc.Invoices/SettleInvoice": {{
|
|
|
|
Entity: "invoices",
|
|
|
|
Action: "write",
|
|
|
|
}},
|
2019-01-11 13:19:16 +03:00
|
|
|
"/invoicesrpc.Invoices/CancelInvoice": {{
|
|
|
|
Entity: "invoices",
|
|
|
|
Action: "write",
|
|
|
|
}},
|
2019-01-15 14:11:22 +03:00
|
|
|
"/invoicesrpc.Invoices/AddHoldInvoice": {{
|
|
|
|
Entity: "invoices",
|
|
|
|
Action: "write",
|
|
|
|
}},
|
2019-01-03 21:15:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultInvoicesMacFilename is the default name of the invoices
|
|
|
|
// macaroon that we expect to find via a file handle within the main
|
|
|
|
// configuration file in this package.
|
|
|
|
DefaultInvoicesMacFilename = "invoices.macaroon"
|
2018-12-20 13:42:28 +03:00
|
|
|
)
|
|
|
|
|
2021-01-29 17:49:48 +03:00
|
|
|
// ServerShell is a shell struct holding a reference to the actual sub-server.
|
|
|
|
// It is used to register the gRPC sub-server with the root server before we
|
|
|
|
// have the necessary dependencies to populate the actual sub-server.
|
|
|
|
type ServerShell struct {
|
|
|
|
InvoicesServer
|
|
|
|
}
|
|
|
|
|
2018-12-20 13:42:28 +03:00
|
|
|
// Server is a sub-server of the main RPC server: the invoices RPC. This sub
|
|
|
|
// RPC server allows external callers to access the status of the invoices
|
|
|
|
// currently active within lnd, as well as configuring it at runtime.
|
|
|
|
type Server struct {
|
2019-01-03 21:15:14 +03:00
|
|
|
quit chan struct{}
|
|
|
|
|
2018-12-20 13:42:28 +03:00
|
|
|
cfg *Config
|
|
|
|
}
|
|
|
|
|
|
|
|
// A compile time check to ensure that Server fully implements the
|
|
|
|
// InvoicesServer gRPC service.
|
|
|
|
var _ InvoicesServer = (*Server)(nil)
|
|
|
|
|
|
|
|
// New returns a new instance of the invoicesrpc Invoices sub-server. We also
|
|
|
|
// return the set of permissions for the macaroons that we may create within
|
|
|
|
// this method. If the macaroons we need aren't found in the filepath, then
|
|
|
|
// we'll create them on start up. If we're unable to locate, or create the
|
|
|
|
// macaroons we need, then we'll return with an error.
|
|
|
|
func New(cfg *Config) (*Server, lnrpc.MacaroonPerms, error) {
|
2019-01-03 21:15:14 +03:00
|
|
|
// If the path of the invoices macaroon wasn't specified, then we'll
|
|
|
|
// assume that it's found at the default network directory.
|
|
|
|
macFilePath := filepath.Join(
|
|
|
|
cfg.NetworkDir, DefaultInvoicesMacFilename,
|
|
|
|
)
|
|
|
|
|
|
|
|
// Now that we know the full path of the invoices macaroon, we can
|
2020-10-06 18:23:42 +03:00
|
|
|
// check to see if we need to create it or not. If stateless_init is set
|
|
|
|
// then we don't write the macaroons.
|
|
|
|
if cfg.MacService != nil && !cfg.MacService.StatelessInit &&
|
|
|
|
!lnrpc.FileExists(macFilePath) {
|
|
|
|
|
2019-01-03 21:15:14 +03:00
|
|
|
log.Infof("Baking macaroons for invoices RPC Server at: %v",
|
|
|
|
macFilePath)
|
|
|
|
|
|
|
|
// At this point, we know that the invoices macaroon doesn't
|
|
|
|
// yet, exist, so we need to create it with the help of the
|
|
|
|
// main macaroon service.
|
2020-07-23 19:36:42 +03:00
|
|
|
invoicesMac, err := cfg.MacService.NewMacaroon(
|
2020-07-23 19:37:13 +03:00
|
|
|
context.Background(), macaroons.DefaultRootKeyID,
|
|
|
|
macaroonOps...,
|
2019-01-03 21:15:14 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
invoicesMacBytes, err := invoicesMac.M().MarshalBinary()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
err = ioutil.WriteFile(macFilePath, invoicesMacBytes, 0644)
|
|
|
|
if err != nil {
|
2020-10-06 18:23:42 +03:00
|
|
|
_ = os.Remove(macFilePath)
|
2019-01-03 21:15:14 +03:00
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-20 13:42:28 +03:00
|
|
|
server := &Server{
|
2019-01-03 21:15:14 +03:00
|
|
|
cfg: cfg,
|
|
|
|
quit: make(chan struct{}, 1),
|
2018-12-20 13:42:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return server, macPermissions, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start launches any helper goroutines required for the Server to function.
|
|
|
|
//
|
|
|
|
// NOTE: This is part of the lnrpc.SubServer interface.
|
|
|
|
func (s *Server) Start() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop signals any active goroutines for a graceful closure.
|
|
|
|
//
|
|
|
|
// NOTE: This is part of the lnrpc.SubServer interface.
|
|
|
|
func (s *Server) Stop() error {
|
2019-01-03 21:15:14 +03:00
|
|
|
close(s.quit)
|
|
|
|
|
2018-12-20 13:42:28 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name returns a unique string representation of the sub-server. This can be
|
|
|
|
// used to identify the sub-server and also de-duplicate them.
|
|
|
|
//
|
|
|
|
// NOTE: This is part of the lnrpc.SubServer interface.
|
|
|
|
func (s *Server) Name() string {
|
|
|
|
return subServerName
|
|
|
|
}
|
|
|
|
|
|
|
|
// RegisterWithRootServer will be called by the root gRPC server to direct a sub
|
|
|
|
// RPC server to register itself with the main gRPC root server. Until this is
|
|
|
|
// called, each sub-server won't be able to have requests routed towards it.
|
|
|
|
//
|
2021-01-29 17:49:48 +03:00
|
|
|
// NOTE: This is part of the lnrpc.GrpcHandler interface.
|
|
|
|
func (r *ServerShell) RegisterWithRootServer(grpcServer *grpc.Server) error {
|
2018-12-20 13:42:28 +03:00
|
|
|
// We make sure that we register it with the main gRPC server to ensure
|
|
|
|
// all our methods are routed properly.
|
2021-01-29 17:49:48 +03:00
|
|
|
RegisterInvoicesServer(grpcServer, r)
|
2018-12-20 13:42:28 +03:00
|
|
|
|
2019-01-03 21:15:14 +03:00
|
|
|
log.Debugf("Invoices RPC server successfully registered with root " +
|
2018-12-20 13:42:28 +03:00
|
|
|
"gRPC server")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2019-01-03 21:15:14 +03:00
|
|
|
|
2020-04-02 16:12:28 +03:00
|
|
|
// RegisterWithRestServer will be called by the root REST mux to direct a sub
|
|
|
|
// RPC server to register itself with the main REST mux server. Until this is
|
|
|
|
// called, each sub-server won't be able to have requests routed towards it.
|
|
|
|
//
|
2021-01-29 17:49:48 +03:00
|
|
|
// NOTE: This is part of the lnrpc.GrpcHandler interface.
|
|
|
|
func (r *ServerShell) RegisterWithRestServer(ctx context.Context,
|
2020-04-02 16:12:28 +03:00
|
|
|
mux *runtime.ServeMux, dest string, opts []grpc.DialOption) error {
|
|
|
|
|
|
|
|
// We make sure that we register it with the main REST server to ensure
|
|
|
|
// all our methods are routed properly.
|
|
|
|
err := RegisterInvoicesHandlerFromEndpoint(ctx, mux, dest, opts)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Could not register Invoices REST server "+
|
|
|
|
"with root REST server: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debugf("Invoices REST server successfully registered with " +
|
|
|
|
"root REST server")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-29 17:49:48 +03:00
|
|
|
// CreateSubServer populates the subserver's dependencies using the passed
|
|
|
|
// SubServerConfigDispatcher. This method should fully initialize the
|
|
|
|
// sub-server instance, making it ready for action. It returns the macaroon
|
|
|
|
// permissions that the sub-server wishes to pass on to the root server for all
|
|
|
|
// methods routed towards it.
|
|
|
|
//
|
|
|
|
// NOTE: This is part of the lnrpc.GrpcHandler interface.
|
|
|
|
func (r *ServerShell) CreateSubServer(configRegistry lnrpc.SubServerConfigDispatcher) (
|
|
|
|
lnrpc.SubServer, lnrpc.MacaroonPerms, error) {
|
|
|
|
|
|
|
|
subServer, macPermissions, err := createNewSubServer(configRegistry)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
r.InvoicesServer = subServer
|
|
|
|
return subServer, macPermissions, nil
|
|
|
|
}
|
|
|
|
|
2019-04-27 12:30:26 +03:00
|
|
|
// SubscribeSingleInvoice returns a uni-directional stream (server -> client)
|
|
|
|
// for notifying the client of state changes for a specified invoice.
|
|
|
|
func (s *Server) SubscribeSingleInvoice(req *SubscribeSingleInvoiceRequest,
|
2019-01-03 21:15:14 +03:00
|
|
|
updateStream Invoices_SubscribeSingleInvoiceServer) error {
|
|
|
|
|
2019-02-09 22:39:43 +03:00
|
|
|
hash, err := lntypes.MakeHash(req.RHash)
|
2019-01-03 21:15:14 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-08-09 13:22:53 +03:00
|
|
|
invoiceClient, err := s.cfg.InvoiceRegistry.SubscribeSingleInvoice(hash)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-01-03 21:15:14 +03:00
|
|
|
defer invoiceClient.Cancel()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case newInvoice := <-invoiceClient.Updates:
|
|
|
|
rpcInvoice, err := CreateRPCInvoice(
|
|
|
|
newInvoice, s.cfg.ChainParams,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := updateStream.Send(rpcInvoice); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-04-14 10:19:23 +03:00
|
|
|
// If we have reached a terminal state, close the
|
|
|
|
// stream with no error.
|
|
|
|
if newInvoice.State.IsFinal() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-18 23:41:11 +03:00
|
|
|
case <-updateStream.Context().Done():
|
|
|
|
return updateStream.Context().Err()
|
|
|
|
|
2019-01-03 21:15:14 +03:00
|
|
|
case <-s.quit:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-11 13:19:16 +03:00
|
|
|
|
2019-02-11 14:02:39 +03:00
|
|
|
// SettleInvoice settles an accepted invoice. If the invoice is already settled,
|
|
|
|
// this call will succeed.
|
|
|
|
func (s *Server) SettleInvoice(ctx context.Context,
|
|
|
|
in *SettleInvoiceMsg) (*SettleInvoiceResp, error) {
|
|
|
|
|
|
|
|
preimage, err := lntypes.MakePreimage(in.Preimage)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = s.cfg.InvoiceRegistry.SettleHodlInvoice(preimage)
|
|
|
|
if err != nil && err != channeldb.ErrInvoiceAlreadySettled {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &SettleInvoiceResp{}, nil
|
|
|
|
}
|
|
|
|
|
2019-01-11 13:19:16 +03:00
|
|
|
// CancelInvoice cancels a currently open invoice. If the invoice is already
|
|
|
|
// canceled, this call will succeed. If the invoice is already settled, it will
|
|
|
|
// fail.
|
|
|
|
func (s *Server) CancelInvoice(ctx context.Context,
|
|
|
|
in *CancelInvoiceMsg) (*CancelInvoiceResp, error) {
|
|
|
|
|
2019-02-09 22:39:43 +03:00
|
|
|
paymentHash, err := lntypes.MakeHash(in.PaymentHash)
|
2019-01-11 13:19:16 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-02-09 22:39:43 +03:00
|
|
|
err = s.cfg.InvoiceRegistry.CancelInvoice(paymentHash)
|
2019-01-11 13:19:16 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("Canceled invoice %v", paymentHash)
|
|
|
|
|
|
|
|
return &CancelInvoiceResp{}, nil
|
|
|
|
}
|
2019-01-15 14:11:22 +03:00
|
|
|
|
|
|
|
// AddHoldInvoice attempts to add a new hold invoice to the invoice database.
|
|
|
|
// Any duplicated invoices are rejected, therefore all invoices *must* have a
|
|
|
|
// unique payment hash.
|
|
|
|
func (s *Server) AddHoldInvoice(ctx context.Context,
|
|
|
|
invoice *AddHoldInvoiceRequest) (*AddHoldInvoiceResp, error) {
|
|
|
|
|
|
|
|
addInvoiceCfg := &AddInvoiceConfig{
|
2021-05-06 19:15:01 +03:00
|
|
|
AddInvoice: s.cfg.InvoiceRegistry.AddInvoice,
|
|
|
|
IsChannelActive: s.cfg.IsChannelActive,
|
|
|
|
ChainParams: s.cfg.ChainParams,
|
|
|
|
NodeSigner: s.cfg.NodeSigner,
|
|
|
|
DefaultCLTVExpiry: s.cfg.DefaultCLTVExpiry,
|
|
|
|
ChanDB: s.cfg.RemoteChanDB,
|
|
|
|
Graph: s.cfg.LocalChanDB.ChannelGraph(),
|
|
|
|
GenInvoiceFeatures: s.cfg.GenInvoiceFeatures,
|
|
|
|
GenAmpInvoiceFeatures: s.cfg.GenAmpInvoiceFeatures,
|
2019-01-15 14:11:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
hash, err := lntypes.MakeHash(invoice.Hash)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-11-15 10:59:14 +03:00
|
|
|
value, err := lnrpc.UnmarshallAmt(invoice.Value, invoice.ValueMsat)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-11-03 16:00:27 +03:00
|
|
|
// Convert the passed routing hints to the required format.
|
|
|
|
routeHints, err := CreateZpay32HopHints(invoice.RouteHints)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-01-15 14:11:22 +03:00
|
|
|
addInvoiceData := &AddInvoiceData{
|
|
|
|
Memo: invoice.Memo,
|
|
|
|
Hash: &hash,
|
2019-11-15 10:59:14 +03:00
|
|
|
Value: value,
|
2019-01-15 14:11:22 +03:00
|
|
|
DescriptionHash: invoice.DescriptionHash,
|
|
|
|
Expiry: invoice.Expiry,
|
|
|
|
FallbackAddr: invoice.FallbackAddr,
|
|
|
|
CltvExpiry: invoice.CltvExpiry,
|
|
|
|
Private: invoice.Private,
|
2020-04-08 14:47:10 +03:00
|
|
|
HodlInvoice: true,
|
|
|
|
Preimage: nil,
|
2019-11-03 16:00:27 +03:00
|
|
|
RouteHints: routeHints,
|
2019-01-15 14:11:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
_, dbInvoice, err := AddInvoice(ctx, addInvoiceCfg, addInvoiceData)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &AddHoldInvoiceResp{
|
|
|
|
PaymentRequest: string(dbInvoice.PaymentRequest),
|
|
|
|
}, nil
|
|
|
|
}
|