Skip to content

Commit c557234

Browse files
authored
Merge pull request #45 from Roasbeef/macaroon-pouch
lndclient+cmd/loopd: use unique macaroon per sub-server to avoid users having to delete admin.macaroon
2 parents 3d0d733 + 6037d01 commit c557234

File tree

11 files changed

+247
-86
lines changed

11 files changed

+247
-86
lines changed

cmd/loopd/config.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package main
22

33
type lndConfig struct {
4-
Host string `long:"host" description:"lnd instance rpc address"`
5-
MacaroonPath string `long:"macaroonpath" description:"Path to lnd macaroon"`
6-
TLSPath string `long:"tlspath" description:"Path to lnd tls certificate"`
4+
Host string `long:"host" description:"lnd instance rpc address"`
5+
MacaroonDir string `long:"macaroondir" description:"Path to the directory containing all the required lnd macaroons"`
6+
TLSPath string `long:"tlspath" description:"Path to lnd tls certificate"`
77
}
88

99
type viewParameters struct{}

cmd/loopd/daemon.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ func daemon(config *config) error {
3434

3535
// Create an instance of the loop client library.
3636
swapClient, cleanup, err := getClient(
37-
config.Network, config.SwapServer, config.Insecure, &lnd.LndServices,
37+
config.Network, config.SwapServer, config.Insecure,
38+
&lnd.LndServices,
3839
)
3940
if err != nil {
4041
return err

cmd/loopd/utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
// getLnd returns an instance of the lnd services proxy.
1212
func getLnd(network string, cfg *lndConfig) (*lndclient.GrpcLndServices, error) {
1313
return lndclient.NewLndServices(
14-
cfg.Host, "client", network, cfg.MacaroonPath, cfg.TLSPath,
14+
cfg.Host, "client", network, cfg.MacaroonDir, cfg.TLSPath,
1515
)
1616
}
1717

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ require (
1515
golang.org/x/net v0.0.0-20190313220215-9f648a60d977
1616
google.golang.org/genproto v0.0.0-20190307195333-5fe7a883aa19
1717
google.golang.org/grpc v1.19.0
18-
gopkg.in/macaroon.v2 v2.1.0
18+
gopkg.in/macaroon.v2 v2.1.0 // indirect
1919
)

lndclient/chainnotifier_client.go

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,16 @@ type ChainNotifierClient interface {
2828
}
2929

3030
type chainNotifierClient struct {
31-
client chainrpc.ChainNotifierClient
32-
wg sync.WaitGroup
31+
client chainrpc.ChainNotifierClient
32+
chainMac serializedMacaroon
33+
34+
wg sync.WaitGroup
3335
}
3436

35-
func newChainNotifierClient(conn *grpc.ClientConn) *chainNotifierClient {
37+
func newChainNotifierClient(conn *grpc.ClientConn, chainMac serializedMacaroon) *chainNotifierClient {
3638
return &chainNotifierClient{
37-
client: chainrpc.NewChainNotifierClient(conn),
39+
client: chainrpc.NewChainNotifierClient(conn),
40+
chainMac: chainMac,
3841
}
3942
}
4043

@@ -54,7 +57,8 @@ func (s *chainNotifierClient) RegisterSpendNtfn(ctx context.Context,
5457
}
5558
}
5659

57-
resp, err := s.client.RegisterSpendNtfn(ctx, &chainrpc.SpendRequest{
60+
macaroonAuth := s.chainMac.WithMacaroonAuth(ctx)
61+
resp, err := s.client.RegisterSpendNtfn(macaroonAuth, &chainrpc.SpendRequest{
5862
HeightHint: uint32(heightHint),
5963
Outpoint: rpcOutpoint,
6064
Script: pkScript,
@@ -125,16 +129,15 @@ func (s *chainNotifierClient) RegisterConfirmationsNtfn(ctx context.Context,
125129
if txid != nil {
126130
txidSlice = txid[:]
127131
}
128-
confStream, err := s.client.
129-
RegisterConfirmationsNtfn(
130-
ctx,
131-
&chainrpc.ConfRequest{
132-
Script: pkScript,
133-
NumConfs: uint32(numConfs),
134-
HeightHint: uint32(heightHint),
135-
Txid: txidSlice,
136-
},
137-
)
132+
confStream, err := s.client.RegisterConfirmationsNtfn(
133+
s.chainMac.WithMacaroonAuth(ctx),
134+
&chainrpc.ConfRequest{
135+
Script: pkScript,
136+
NumConfs: uint32(numConfs),
137+
HeightHint: uint32(heightHint),
138+
Txid: txidSlice,
139+
},
140+
)
138141
if err != nil {
139142
return nil, nil, err
140143
}
@@ -203,8 +206,9 @@ func (s *chainNotifierClient) RegisterConfirmationsNtfn(ctx context.Context,
203206
func (s *chainNotifierClient) RegisterBlockEpochNtfn(ctx context.Context) (
204207
chan int32, chan error, error) {
205208

206-
blockEpochClient, err := s.client.
207-
RegisterBlockEpochNtfn(ctx, &chainrpc.BlockEpoch{})
209+
blockEpochClient, err := s.client.RegisterBlockEpochNtfn(
210+
s.chainMac.WithMacaroonAuth(ctx), &chainrpc.BlockEpoch{},
211+
)
208212
if err != nil {
209213
return nil, nil, err
210214
}

lndclient/invoices_client.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,15 @@ type InvoiceUpdate struct {
3333
}
3434

3535
type invoicesClient struct {
36-
client invoicesrpc.InvoicesClient
37-
wg sync.WaitGroup
36+
client invoicesrpc.InvoicesClient
37+
invoiceMac serializedMacaroon
38+
wg sync.WaitGroup
3839
}
3940

40-
func newInvoicesClient(conn *grpc.ClientConn) *invoicesClient {
41+
func newInvoicesClient(conn *grpc.ClientConn, invoiceMac serializedMacaroon) *invoicesClient {
4142
return &invoicesClient{
42-
client: invoicesrpc.NewInvoicesClient(conn),
43+
client: invoicesrpc.NewInvoicesClient(conn),
44+
invoiceMac: invoiceMac,
4345
}
4446
}
4547

@@ -53,6 +55,7 @@ func (s *invoicesClient) SettleInvoice(ctx context.Context,
5355
rpcCtx, cancel := context.WithTimeout(ctx, rpcTimeout)
5456
defer cancel()
5557

58+
rpcCtx = s.invoiceMac.WithMacaroonAuth(ctx)
5659
_, err := s.client.SettleInvoice(rpcCtx, &invoicesrpc.SettleInvoiceMsg{
5760
Preimage: preimage[:],
5861
})
@@ -66,6 +69,7 @@ func (s *invoicesClient) CancelInvoice(ctx context.Context,
6669
rpcCtx, cancel := context.WithTimeout(ctx, rpcTimeout)
6770
defer cancel()
6871

72+
rpcCtx = s.invoiceMac.WithMacaroonAuth(rpcCtx)
6973
_, err := s.client.CancelInvoice(rpcCtx, &invoicesrpc.CancelInvoiceMsg{
7074
PaymentHash: hash[:],
7175
})
@@ -77,11 +81,12 @@ func (s *invoicesClient) SubscribeSingleInvoice(ctx context.Context,
7781
hash lntypes.Hash) (<-chan InvoiceUpdate,
7882
<-chan error, error) {
7983

80-
invoiceStream, err := s.client.
81-
SubscribeSingleInvoice(ctx,
82-
&lnrpc.PaymentHash{
83-
RHash: hash[:],
84-
})
84+
invoiceStream, err := s.client.SubscribeSingleInvoice(
85+
s.invoiceMac.WithMacaroonAuth(ctx),
86+
&lnrpc.PaymentHash{
87+
RHash: hash[:],
88+
},
89+
)
8590
if err != nil {
8691
return nil, nil, err
8792
}
@@ -135,6 +140,7 @@ func (s *invoicesClient) AddHoldInvoice(ctx context.Context,
135140
Private: true,
136141
}
137142

143+
rpcCtx = s.invoiceMac.WithMacaroonAuth(rpcCtx)
138144
resp, err := s.client.AddHoldInvoice(rpcCtx, rpcIn)
139145
if err != nil {
140146
return "", err

lndclient/lightning_client.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,17 +78,19 @@ var (
7878
)
7979

8080
type lightningClient struct {
81-
client lnrpc.LightningClient
82-
wg sync.WaitGroup
83-
params *chaincfg.Params
81+
client lnrpc.LightningClient
82+
wg sync.WaitGroup
83+
params *chaincfg.Params
84+
adminMac serializedMacaroon
8485
}
8586

8687
func newLightningClient(conn *grpc.ClientConn,
87-
params *chaincfg.Params) *lightningClient {
88+
params *chaincfg.Params, adminMac serializedMacaroon) *lightningClient {
8889

8990
return &lightningClient{
90-
client: lnrpc.NewLightningClient(conn),
91-
params: params,
91+
client: lnrpc.NewLightningClient(conn),
92+
params: params,
93+
adminMac: adminMac,
9294
}
9395
}
9496

@@ -110,6 +112,7 @@ func (s *lightningClient) ConfirmedWalletBalance(ctx context.Context) (
110112
rpcCtx, cancel := context.WithTimeout(ctx, rpcTimeout)
111113
defer cancel()
112114

115+
rpcCtx = s.adminMac.WithMacaroonAuth(rpcCtx)
113116
resp, err := s.client.WalletBalance(rpcCtx, &lnrpc.WalletBalanceRequest{})
114117
if err != nil {
115118
return 0, err
@@ -122,6 +125,7 @@ func (s *lightningClient) GetInfo(ctx context.Context) (*Info, error) {
122125
rpcCtx, cancel := context.WithTimeout(ctx, rpcTimeout)
123126
defer cancel()
124127

128+
rpcCtx = s.adminMac.WithMacaroonAuth(rpcCtx)
125129
resp, err := s.client.GetInfo(rpcCtx, &lnrpc.GetInfoRequest{})
126130
if err != nil {
127131
return nil, err
@@ -159,6 +163,7 @@ func (s *lightningClient) EstimateFeeToP2WSH(ctx context.Context,
159163
return 0, err
160164
}
161165

166+
rpcCtx = s.adminMac.WithMacaroonAuth(rpcCtx)
162167
resp, err := s.client.EstimateFee(
163168
rpcCtx,
164169
&lnrpc.EstimateFeeRequest{
@@ -216,6 +221,7 @@ func (s *lightningClient) payInvoice(ctx context.Context, invoice string,
216221

217222
hash := lntypes.Hash(*payReq.PaymentHash)
218223

224+
ctx = s.adminMac.WithMacaroonAuth(ctx)
219225
for {
220226
// Create no timeout context as this call can block for a long
221227
// time.
@@ -329,6 +335,7 @@ func (s *lightningClient) AddInvoice(ctx context.Context,
329335
rpcIn.RHash = in.Hash[:]
330336
}
331337

338+
rpcCtx = s.adminMac.WithMacaroonAuth(rpcCtx)
332339
resp, err := s.client.AddInvoice(rpcCtx, rpcIn)
333340
if err != nil {
334341
return lntypes.Hash{}, "", err

0 commit comments

Comments
 (0)