Skip to content

Commit 50cfd3b

Browse files
committed
multi: add macroon service to litd
Add a macaroonService to the main LightningTerminal struct.
1 parent 1aee594 commit 50cfd3b

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

config.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ const (
7373
// certificate. The value corresponds to 14 months
7474
// (14 months * 30 days * 24 hours).
7575
DefaultAutogenValidity = 14 * 30 * 24 * time.Hour
76+
77+
// DefaultMacaroonFilename is the default file name for the
78+
// autogenerated lit macaroon.
79+
DefaultMacaroonFilename = "lit.macaroon"
7680
)
7781

7882
var (
@@ -119,6 +123,12 @@ var (
119123
lndDefaultConfig.DataDir, defaultLndChainSubDir,
120124
defaultLndChain, DefaultNetwork, defaultLndMacaroon,
121125
)
126+
127+
// DefaultMacaroonPath is the default full path of the base lit
128+
// macaroon.
129+
DefaultMacaroonPath = filepath.Join(
130+
DefaultLitDir, DefaultNetwork, DefaultMacaroonFilename,
131+
)
122132
)
123133

124134
// Config is the main configuration struct of lightning-terminal. It contains
@@ -141,6 +151,8 @@ type Config struct {
141151
LitDir string `long:"lit-dir" description:"The main directory where LiT looks for its configuration file. If LiT is running in 'remote' lnd mode, this is also the directory where the TLS certificates and log files are stored by default."`
142152
ConfigFile string `long:"configfile" description:"Path to LiT's configuration file."`
143153

154+
MacaroonPath string `long:"macaroonpath" description:"Path to write the macaroon for litd's RPC and REST services if it doesn't exist."`
155+
144156
// Network is the Bitcoin network we're running on. This will be parsed
145157
// before the configuration is loaded and will set the correct flag on
146158
// `lnd.bitcoin.mainnet|testnet|regtest` and also for the other daemons.
@@ -296,6 +308,7 @@ func defaultConfig() *Config {
296308
LitDir: DefaultLitDir,
297309
LetsEncryptListen: defaultLetsEncryptListen,
298310
LetsEncryptDir: defaultLetsEncryptDir,
311+
MacaroonPath: DefaultMacaroonPath,
299312
ConfigFile: defaultConfigFile,
300313
FaradayMode: defaultFaradayMode,
301314
Faraday: &faradayDefaultConfig,
@@ -394,6 +407,14 @@ func loadAndValidateConfig(interceptor signal.Interceptor) (*Config, error) {
394407
"UI, at least %d characters long", uiPasswordMinLength)
395408
}
396409

410+
if cfg.Network != DefaultNetwork {
411+
if cfg.MacaroonPath == DefaultMacaroonPath {
412+
cfg.MacaroonPath = filepath.Join(
413+
litDir, cfg.Network, DefaultMacaroonFilename,
414+
)
415+
}
416+
}
417+
397418
// Initiate our listeners. For now, we only support listening on one
398419
// port at a time because we can only pass in one pre-configured RPC
399420
// listener into lnd.

terminal.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,9 @@ type LightningTerminal struct {
159159
sessionRpcServer *sessionRpcServer
160160
sessionRpcServerStarted bool
161161

162+
macaroonService *lndclient.MacaroonService
163+
macaroonServiceStarted bool
164+
162165
restHandler http.Handler
163166
restCancel func()
164167
}
@@ -548,6 +551,28 @@ func (g *LightningTerminal) startSubservers() error {
548551
g.poolStarted = true
549552
}
550553

554+
g.macaroonService, err = lndclient.NewMacaroonService(
555+
&lndclient.MacaroonServiceConfig{
556+
DBPath: path.Join(g.cfg.LitDir, g.cfg.Network),
557+
MacaroonLocation: "litd",
558+
StatelessInit: !createDefaultMacaroons,
559+
RequiredPerms: litPermissions,
560+
LndClient: &g.lndClient.LndServices,
561+
EphemeralKey: lndclient.SharedKeyNUMS,
562+
KeyLocator: lndclient.SharedKeyLocator,
563+
MacaroonPath: g.cfg.MacaroonPath,
564+
},
565+
)
566+
if err != nil {
567+
log.Errorf("Could not create a new macaroon service: %v", err)
568+
return err
569+
}
570+
571+
if err := g.macaroonService.Start(); err != nil {
572+
return fmt.Errorf("could not start macaroon service: %v", err)
573+
}
574+
g.macaroonServiceStarted = true
575+
551576
if err = g.sessionRpcServer.start(); err != nil {
552577
return err
553578
}
@@ -829,6 +854,13 @@ func (g *LightningTerminal) shutdown() error {
829854
}
830855
}
831856

857+
if g.macaroonServiceStarted {
858+
if err := g.macaroonService.Stop(); err != nil {
859+
log.Errorf("Error stopping macaroon service: %v", err)
860+
returnErr = err
861+
}
862+
}
863+
832864
if g.lndClient != nil {
833865
g.lndClient.Close()
834866
}

0 commit comments

Comments
 (0)