@@ -15,8 +15,8 @@ import (
1515 proxy "github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
1616 "github.com/lightninglabs/lndclient"
1717 "github.com/lightninglabs/loop"
18+ "github.com/lightninglabs/loop/loopdb"
1819 "github.com/lightninglabs/loop/looprpc"
19- "github.com/lightningnetwork/lnd/kvdb"
2020 "github.com/lightningnetwork/lnd/lntypes"
2121 "github.com/lightningnetwork/lnd/macaroons"
2222 "google.golang.org/grpc"
@@ -85,8 +85,7 @@ type Daemon struct {
8585 restListener net.Listener
8686 restCtxCancel func ()
8787
88- macaroonService * macaroons.Service
89- macaroonDB kvdb.Backend
88+ macaroonService * lndclient.MacaroonService
9089}
9190
9291// New creates a new instance of the loop client daemon.
@@ -164,7 +163,7 @@ func (d *Daemon) Start() error {
164163// for REST (if enabled), instead of creating an own mux and HTTP server, we
165164// register to an existing one.
166165func (d * Daemon ) StartAsSubserver (lndGrpc * lndclient.GrpcLndServices ,
167- createDefaultMacaroonFile bool ) error {
166+ withMacaroonService bool ) error {
168167
169168 // There should be no reason to start the daemon twice. Therefore return
170169 // an error if that's tried. This is mostly to guard against Start and
@@ -181,7 +180,7 @@ func (d *Daemon) StartAsSubserver(lndGrpc *lndclient.GrpcLndServices,
181180 // the swap server client, the RPC server instance and our main swap
182181 // handlers. If this fails, then nothing has been started yet and we can
183182 // just return the error.
184- err := d .initialize (createDefaultMacaroonFile )
183+ err := d .initialize (withMacaroonService )
185184 if errors .Is (err , bbolt .ErrTimeout ) {
186185 // We're trying to be started inside LiT so there most likely is
187186 // another standalone Loop process blocking the DB.
@@ -200,6 +199,10 @@ func (d *Daemon) StartAsSubserver(lndGrpc *lndclient.GrpcLndServices,
200199func (d * Daemon ) ValidateMacaroon (ctx context.Context ,
201200 requiredPermissions []bakery.Op , fullMethod string ) error {
202201
202+ if d .macaroonService == nil {
203+ return fmt .Errorf ("macaroon service has not been initialised" )
204+ }
205+
203206 // Delegate the call to loop's own macaroon validator service.
204207 return d .macaroonService .ValidateMacaroon (
205208 ctx , requiredPermissions , fullMethod ,
@@ -213,11 +216,14 @@ func (d *Daemon) startWebServers() error {
213216 // With our client created, let's now finish setting up and start our
214217 // RPC server. First we add the security interceptor to our gRPC server
215218 // options that checks the macaroons for validity.
216- serverOpts , err := d .macaroonInterceptor ()
219+ unaryInterceptor , streamInterceptor , err := d .macaroonService . Interceptors ()
217220 if err != nil {
218221 return fmt .Errorf ("error with macaroon interceptor: %v" , err )
219222 }
220- d .grpcServer = grpc .NewServer (serverOpts ... )
223+ d .grpcServer = grpc .NewServer (
224+ grpc .UnaryInterceptor (unaryInterceptor ),
225+ grpc .StreamInterceptor (streamInterceptor ),
226+ )
221227 looprpc .RegisterSwapClientServer (d .grpcServer , d )
222228
223229 // Register our debug server if it is compiled in.
@@ -341,7 +347,7 @@ func (d *Daemon) startWebServers() error {
341347// the swap client RPC server instance and our main swap and error handlers. If
342348// this method fails with an error then no goroutine was started yet and no
343349// cleanup is necessary. If it succeeds, then goroutines have been spawned.
344- func (d * Daemon ) initialize (createDefaultMacaroonFile bool ) error {
350+ func (d * Daemon ) initialize (withMacaroonService bool ) error {
345351 // If no swap server is specified, use the default addresses for mainnet
346352 // and testnet.
347353 if d .cfg .Server .Host == "" {
@@ -370,15 +376,43 @@ func (d *Daemon) initialize(createDefaultMacaroonFile bool) error {
370376 // stop on main context cancel. So we create it early and pass it down.
371377 d .mainCtx , d .mainCtxCancel = context .WithCancel (context .Background ())
372378
373- // Start the macaroon service and let it create its default macaroon in
374- // case it doesn't exist yet.
375- err = d .startMacaroonService (createDefaultMacaroonFile )
376- if err != nil {
377- // The client is the only thing we started yet, so if we clean
378- // up its connection now, nothing else needs to be shut down at
379- // this point.
380- clientCleanup ()
381- return err
379+ // Add our debug permissions to our main set of required permissions
380+ // if compiled in.
381+ for endpoint , perm := range debugRequiredPermissions {
382+ RequiredPermissions [endpoint ] = perm
383+ }
384+
385+ if withMacaroonService {
386+ // Start the macaroon service and let it create its default
387+ // macaroon in case it doesn't exist yet.
388+ d .macaroonService , err = lndclient .NewMacaroonService (
389+ & lndclient.MacaroonServiceConfig {
390+ DBPath : d .cfg .DataDir ,
391+ DBFileName : "macaroons.db" ,
392+ DBTimeout : loopdb .DefaultLoopDBTimeout ,
393+ MacaroonLocation : loopMacaroonLocation ,
394+ MacaroonPath : d .cfg .MacaroonPath ,
395+ Checkers : []macaroons.Checker {
396+ macaroons .IPLockChecker ,
397+ },
398+ RequiredPerms : RequiredPermissions ,
399+ DBPassword : macDbDefaultPw ,
400+ LndClient : & d .lnd .LndServices ,
401+ EphemeralKey : lndclient .SharedKeyNUMS ,
402+ KeyLocator : lndclient .SharedKeyLocator ,
403+ },
404+ )
405+ if err != nil {
406+ return err
407+ }
408+
409+ if err = d .macaroonService .Start (); err != nil {
410+ // The client is the only thing we started yet, so if we
411+ // clean up its connection now, nothing else needs to be
412+ // shut down at this point.
413+ clientCleanup ()
414+ return err
415+ }
382416 }
383417
384418 // Now finally fully initialize the swap client RPC server instance.
@@ -396,10 +430,15 @@ func (d *Daemon) initialize(createDefaultMacaroonFile bool) error {
396430 // Retrieve all currently existing swaps from the database.
397431 swapsList , err := d .impl .FetchSwaps ()
398432 if err != nil {
433+ if d .macaroonService == nil {
434+ clientCleanup ()
435+ return err
436+ }
437+
399438 // The client and the macaroon service are the only things we
400439 // started yet, so if we clean that up now, nothing else needs
401440 // to be shut down at this point.
402- if err := d .StopMacaroonService (); err != nil {
441+ if err := d .macaroonService . Stop (); err != nil {
403442 log .Errorf ("Error shutting down macaroon service: %v" ,
404443 err )
405444 }
@@ -520,9 +559,11 @@ func (d *Daemon) stop() {
520559 d .restCtxCancel ()
521560 }
522561
523- err := d .StopMacaroonService ()
524- if err != nil {
525- log .Errorf ("Error stopping macaroon service: %v" , err )
562+ if d .macaroonService != nil {
563+ err := d .macaroonService .Stop ()
564+ if err != nil {
565+ log .Errorf ("Error stopping macaroon service: %v" , err )
566+ }
526567 }
527568
528569 // Next, shut down the connections to lnd and the swap server.
0 commit comments