@@ -15,8 +15,8 @@ import (
15
15
proxy "github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
16
16
"github.com/lightninglabs/lndclient"
17
17
"github.com/lightninglabs/loop"
18
+ "github.com/lightninglabs/loop/loopdb"
18
19
"github.com/lightninglabs/loop/looprpc"
19
- "github.com/lightningnetwork/lnd/kvdb"
20
20
"github.com/lightningnetwork/lnd/lntypes"
21
21
"github.com/lightningnetwork/lnd/macaroons"
22
22
"google.golang.org/grpc"
@@ -85,8 +85,7 @@ type Daemon struct {
85
85
restListener net.Listener
86
86
restCtxCancel func ()
87
87
88
- macaroonService * macaroons.Service
89
- macaroonDB kvdb.Backend
88
+ macaroonService * lndclient.MacaroonService
90
89
}
91
90
92
91
// New creates a new instance of the loop client daemon.
@@ -164,7 +163,7 @@ func (d *Daemon) Start() error {
164
163
// for REST (if enabled), instead of creating an own mux and HTTP server, we
165
164
// register to an existing one.
166
165
func (d * Daemon ) StartAsSubserver (lndGrpc * lndclient.GrpcLndServices ,
167
- createDefaultMacaroonFile bool ) error {
166
+ withMacaroonService bool ) error {
168
167
169
168
// There should be no reason to start the daemon twice. Therefore return
170
169
// 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,
181
180
// the swap server client, the RPC server instance and our main swap
182
181
// handlers. If this fails, then nothing has been started yet and we can
183
182
// just return the error.
184
- err := d .initialize (createDefaultMacaroonFile )
183
+ err := d .initialize (withMacaroonService )
185
184
if errors .Is (err , bbolt .ErrTimeout ) {
186
185
// We're trying to be started inside LiT so there most likely is
187
186
// another standalone Loop process blocking the DB.
@@ -200,6 +199,10 @@ func (d *Daemon) StartAsSubserver(lndGrpc *lndclient.GrpcLndServices,
200
199
func (d * Daemon ) ValidateMacaroon (ctx context.Context ,
201
200
requiredPermissions []bakery.Op , fullMethod string ) error {
202
201
202
+ if d .macaroonService == nil {
203
+ return fmt .Errorf ("macaroon service has not been initialised" )
204
+ }
205
+
203
206
// Delegate the call to loop's own macaroon validator service.
204
207
return d .macaroonService .ValidateMacaroon (
205
208
ctx , requiredPermissions , fullMethod ,
@@ -213,11 +216,14 @@ func (d *Daemon) startWebServers() error {
213
216
// With our client created, let's now finish setting up and start our
214
217
// RPC server. First we add the security interceptor to our gRPC server
215
218
// options that checks the macaroons for validity.
216
- serverOpts , err := d .macaroonInterceptor ()
219
+ unaryInterceptor , streamInterceptor , err := d .macaroonService . Interceptors ()
217
220
if err != nil {
218
221
return fmt .Errorf ("error with macaroon interceptor: %v" , err )
219
222
}
220
- d .grpcServer = grpc .NewServer (serverOpts ... )
223
+ d .grpcServer = grpc .NewServer (
224
+ grpc .UnaryInterceptor (unaryInterceptor ),
225
+ grpc .StreamInterceptor (streamInterceptor ),
226
+ )
221
227
looprpc .RegisterSwapClientServer (d .grpcServer , d )
222
228
223
229
// Register our debug server if it is compiled in.
@@ -341,7 +347,7 @@ func (d *Daemon) startWebServers() error {
341
347
// the swap client RPC server instance and our main swap and error handlers. If
342
348
// this method fails with an error then no goroutine was started yet and no
343
349
// 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 {
345
351
// If no swap server is specified, use the default addresses for mainnet
346
352
// and testnet.
347
353
if d .cfg .Server .Host == "" {
@@ -370,15 +376,43 @@ func (d *Daemon) initialize(createDefaultMacaroonFile bool) error {
370
376
// stop on main context cancel. So we create it early and pass it down.
371
377
d .mainCtx , d .mainCtxCancel = context .WithCancel (context .Background ())
372
378
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
+ }
382
416
}
383
417
384
418
// Now finally fully initialize the swap client RPC server instance.
@@ -396,10 +430,15 @@ func (d *Daemon) initialize(createDefaultMacaroonFile bool) error {
396
430
// Retrieve all currently existing swaps from the database.
397
431
swapsList , err := d .impl .FetchSwaps ()
398
432
if err != nil {
433
+ if d .macaroonService == nil {
434
+ clientCleanup ()
435
+ return err
436
+ }
437
+
399
438
// The client and the macaroon service are the only things we
400
439
// started yet, so if we clean that up now, nothing else needs
401
440
// to be shut down at this point.
402
- if err := d .StopMacaroonService (); err != nil {
441
+ if err := d .macaroonService . Stop (); err != nil {
403
442
log .Errorf ("Error shutting down macaroon service: %v" ,
404
443
err )
405
444
}
@@ -520,9 +559,11 @@ func (d *Daemon) stop() {
520
559
d .restCtxCancel ()
521
560
}
522
561
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
+ }
526
567
}
527
568
528
569
// Next, shut down the connections to lnd and the swap server.
0 commit comments