Skip to content

Commit e84267c

Browse files
committed
terminal: detect stateless init, don't create default macaroons
In case the wallet was initialized in a stateless way, we don't want to create the default macaroons for the faraday/loop/pool daemons. In any other case we do so we can interact with the daemons through the CLI.
1 parent a25426d commit e84267c

File tree

1 file changed

+41
-4
lines changed

1 file changed

+41
-4
lines changed

terminal.go

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import (
77
"encoding/hex"
88
"errors"
99
"fmt"
10+
"github.com/lightningnetwork/lnd/chainreg"
11+
"github.com/lightningnetwork/lnd/lnwallet/btcwallet"
12+
"github.com/lightningnetwork/lnd/rpcperms"
1013
"io/fs"
1114
"net"
1215
"net/http"
@@ -127,6 +130,10 @@ type LightningTerminal struct {
127130

128131
defaultImplCfg *lnd.ImplementationCfg
129132

133+
// lndInterceptorChain is a reference to lnd's interceptor chain that
134+
// guards all incoming calls. This is only set in integrated mode!
135+
lndInterceptorChain *rpcperms.InterceptorChain
136+
130137
wg sync.WaitGroup
131138
lndErrChan chan error
132139

@@ -212,7 +219,7 @@ func (g *LightningTerminal) Run() error {
212219
RestRegistrar: g,
213220
ExternalValidator: g,
214221
DatabaseBuilder: g.defaultImplCfg.DatabaseBuilder,
215-
WalletConfigBuilder: g.defaultImplCfg.WalletConfigBuilder,
222+
WalletConfigBuilder: g,
216223
ChainControlBuilder: g.defaultImplCfg.ChainControlBuilder,
217224
}
218225

@@ -458,9 +465,20 @@ func (g *LightningTerminal) startSubservers() error {
458465
g.rpcProxy.superMacaroon = res.Macaroon
459466
}
460467

461-
// If we're in integrated mode, we won't create macaroon files in any
462-
// of the subserver daemons.
463-
createDefaultMacaroons := g.cfg.LndMode != ModeIntegrated
468+
// If we're in integrated and stateless init mode, we won't create
469+
// macaroon files in any of the subserver daemons.
470+
createDefaultMacaroons := true
471+
if g.cfg.LndMode == ModeIntegrated && g.lndInterceptorChain != nil &&
472+
g.lndInterceptorChain.MacaroonService() != nil {
473+
474+
// If the wallet was initialized in stateless mode, we don't
475+
// want any macaroons lying around on the filesystem. In that
476+
// case only the UI will be able to access any of the integrated
477+
// daemons. In all other cases we want default macaroons so we
478+
// can use the CLI tools to interact with loop/pool/faraday.
479+
macService := g.lndInterceptorChain.MacaroonService()
480+
createDefaultMacaroons = !macService.StatelessInit
481+
}
464482

465483
// Both connection types are ready now, let's start our subservers if
466484
// they should be started locally as an integrated service.
@@ -689,6 +707,25 @@ func (g *LightningTerminal) Permissions() map[string][]bakery.Op {
689707
return getSubserverPermissions()
690708
}
691709

710+
// BuildWalletConfig is responsible for creating or unlocking and then
711+
// fully initializing a wallet.
712+
//
713+
// NOTE: This is only implemented in order for us to intercept the setup call
714+
// and store a reference to the interceptor chain.
715+
//
716+
// NOTE: This is part of the lnd.WalletConfigBuilder interface.
717+
func (g *LightningTerminal) BuildWalletConfig(ctx context.Context,
718+
dbs *lnd.DatabaseInstances, interceptorChain *rpcperms.InterceptorChain,
719+
grpcListeners []*lnd.ListenerWithSignal) (*chainreg.PartialChainControl,
720+
*btcwallet.Config, func(), error) {
721+
722+
g.lndInterceptorChain = interceptorChain
723+
724+
return g.defaultImplCfg.WalletConfigBuilder.BuildWalletConfig(
725+
ctx, dbs, interceptorChain, grpcListeners,
726+
)
727+
}
728+
692729
// shutdown stops all subservers that were started and attached to lnd.
693730
func (g *LightningTerminal) shutdown() error {
694731
var returnErr error

0 commit comments

Comments
 (0)