Skip to content

Commit e31e52f

Browse files
committed
multi: add RawClientWithMacAuth to every client
1 parent 58bbe5c commit e31e52f

12 files changed

+163
-3
lines changed

chainkit_client.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import (
1414

1515
// ChainKitClient exposes chain functionality.
1616
type ChainKitClient interface {
17+
ServiceClient[chainrpc.ChainKitClient]
18+
1719
// GetBlock returns a block given the corresponding block hash.
1820
GetBlock(ctx context.Context, hash chainhash.Hash) (*wire.MsgBlock,
1921
error)
@@ -41,6 +43,10 @@ type chainKitClient struct {
4143
wg sync.WaitGroup
4244
}
4345

46+
// A compile time check to ensure that chainKitClient implements the
47+
// ChainKitClient interface.
48+
var _ ChainKitClient = (*chainKitClient)(nil)
49+
4450
func newChainKitClient(conn grpc.ClientConnInterface,
4551
chainMac serializedMacaroon, timeout time.Duration) *chainKitClient {
4652

@@ -55,6 +61,15 @@ func (s *chainKitClient) WaitForFinished() {
5561
s.wg.Wait()
5662
}
5763

64+
// RawClientWithMacAuth returns a context with the proper macaroon
65+
// authentication, the default RPC timeout, and the raw client.
66+
func (s *chainKitClient) RawClientWithMacAuth(
67+
parentCtx context.Context) (context.Context, time.Duration,
68+
chainrpc.ChainKitClient) {
69+
70+
return s.chainMac.WithMacaroonAuth(parentCtx), s.timeout, s.client
71+
}
72+
5873
// GetBlock returns a block given the corresponding block hash.
5974
func (s *chainKitClient) GetBlock(ctxParent context.Context,
6075
hash chainhash.Hash) (*wire.MsgBlock, error) {

chainnotifier_client.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ func WithReOrgChan(reOrgChan chan struct{}) NotifierOption {
6060

6161
// ChainNotifierClient exposes base lightning functionality.
6262
type ChainNotifierClient interface {
63+
ServiceClient[chainrpc.ChainNotifierClient]
64+
6365
RegisterBlockEpochNtfn(ctx context.Context) (
6466
chan int32, chan error, error)
6567

@@ -81,6 +83,10 @@ type chainNotifierClient struct {
8183
wg sync.WaitGroup
8284
}
8385

86+
// A compile time check to ensure that chainNotifierClient implements the
87+
// ChainNotifierClient interface.
88+
var _ ChainNotifierClient = (*chainNotifierClient)(nil)
89+
8490
func newChainNotifierClient(conn grpc.ClientConnInterface,
8591
chainMac serializedMacaroon, timeout time.Duration) *chainNotifierClient {
8692

@@ -95,6 +101,15 @@ func (s *chainNotifierClient) WaitForFinished() {
95101
s.wg.Wait()
96102
}
97103

104+
// RawClientWithMacAuth returns a context with the proper macaroon
105+
// authentication, the default RPC timeout, and the raw client.
106+
func (s *chainNotifierClient) RawClientWithMacAuth(
107+
parentCtx context.Context) (context.Context, time.Duration,
108+
chainrpc.ChainNotifierClient) {
109+
110+
return s.chainMac.WithMacaroonAuth(parentCtx), s.timeout, s.client
111+
}
112+
98113
func (s *chainNotifierClient) RegisterSpendNtfn(ctx context.Context,
99114
outpoint *wire.OutPoint, pkScript []byte, heightHint int32) (
100115
chan *chainntnfs.SpendDetail, chan error, error) {

invoices_client.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ type InvoiceHtlcModifyHandler func(context.Context,
4141

4242
// InvoicesClient exposes invoice functionality.
4343
type InvoicesClient interface {
44+
ServiceClient[invoicesrpc.InvoicesClient]
45+
4446
SubscribeSingleInvoice(ctx context.Context, hash lntypes.Hash) (
4547
<-chan InvoiceUpdate, <-chan error, error)
4648

@@ -75,6 +77,10 @@ type invoicesClient struct {
7577
wg sync.WaitGroup
7678
}
7779

80+
// A compile time check to ensure that invoicesClient implements the
81+
// InvoicesClient interface.
82+
var _ InvoicesClient = (*invoicesClient)(nil)
83+
7884
func newInvoicesClient(conn grpc.ClientConnInterface,
7985
invoiceMac serializedMacaroon, timeout time.Duration) *invoicesClient {
8086

@@ -94,6 +100,15 @@ func (s *invoicesClient) WaitForFinished() {
94100
s.wg.Wait()
95101
}
96102

103+
// RawClientWithMacAuth returns a context with the proper macaroon
104+
// authentication, the default RPC timeout, and the raw client.
105+
func (s *invoicesClient) RawClientWithMacAuth(
106+
parentCtx context.Context) (context.Context, time.Duration,
107+
invoicesrpc.InvoicesClient) {
108+
109+
return s.invoiceMac.WithMacaroonAuth(parentCtx), s.timeout, s.client
110+
}
111+
97112
func (s *invoicesClient) SettleInvoice(ctx context.Context,
98113
preimage lntypes.Preimage) error {
99114

lightning_client.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ func WithRemoteReserve(reserve uint64) OpenChannelOption {
7474

7575
// LightningClient exposes base lightning functionality.
7676
type LightningClient interface {
77+
ServiceClient[lnrpc.LightningClient]
78+
7779
PayInvoice(ctx context.Context, invoice string,
7880
maxFee btcutil.Amount,
7981
outgoingChannel *uint64) chan PaymentResult
@@ -1323,6 +1325,10 @@ type lightningClient struct {
13231325
adminMac serializedMacaroon
13241326
}
13251327

1328+
// A compile time check to ensure that lightningClient implements the
1329+
// LightningClient interface.
1330+
var _ LightningClient = (*lightningClient)(nil)
1331+
13261332
func newLightningClient(conn grpc.ClientConnInterface, timeout time.Duration,
13271333
params *chaincfg.Params, adminMac serializedMacaroon) *lightningClient {
13281334

@@ -1346,6 +1352,15 @@ func (s *lightningClient) WaitForFinished() {
13461352
s.wg.Wait()
13471353
}
13481354

1355+
// RawClientWithMacAuth returns a context with the proper macaroon
1356+
// authentication, the default RPC timeout, and the raw client.
1357+
func (s *lightningClient) RawClientWithMacAuth(
1358+
parentCtx context.Context) (context.Context, time.Duration,
1359+
lnrpc.LightningClient) {
1360+
1361+
return s.adminMac.WithMacaroonAuth(parentCtx), s.timeout, s.client
1362+
}
1363+
13491364
// WalletBalance returns a summary of the node's wallet balance.
13501365
func (s *lightningClient) WalletBalance(ctx context.Context) (
13511366
*WalletBalance, error) {

lnd_services.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@ var (
7878
}
7979
)
8080

81+
// ServiceClient is an interface that all lnd service clients need to implement.
82+
type ServiceClient[T any] interface {
83+
// RawClientWithMacAuth returns a context with the proper macaroon
84+
// authentication, the default RPC timeout, and the raw client.
85+
RawClientWithMacAuth(parentCtx context.Context) (context.Context,
86+
time.Duration, T)
87+
}
88+
8189
// LndServicesConfig holds all configuration settings that are needed to connect
8290
// to an lnd node.
8391
type LndServicesConfig struct {

lnd_services_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"errors"
66
"testing"
7+
"time"
78

89
"github.com/lightningnetwork/lnd/lnrpc"
910
"github.com/lightningnetwork/lnd/lnrpc/verrpc"
@@ -18,6 +19,15 @@ type mockVersioner struct {
1819
err error
1920
}
2021

22+
// RawClientWithMacAuth returns a context with the proper macaroon
23+
// authentication, the default RPC timeout, and the raw client.
24+
func (m *mockVersioner) RawClientWithMacAuth(
25+
parentCtx context.Context) (context.Context, time.Duration,
26+
verrpc.VersionerClient) {
27+
28+
return parentCtx, 0, nil
29+
}
30+
2131
func (m *mockVersioner) GetVersion(_ context.Context) (*verrpc.Version, error) {
2232
return m.version, m.err
2333
}

macaroon_recipes.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"reflect"
7+
"slices"
78
"strings"
89
)
910

@@ -43,6 +44,12 @@ var (
4344
"OpenChannelStream": "OpenChannel",
4445
"ListSweepsVerbose": "ListSweeps",
4546
}
47+
48+
// ignores is a list of method names on the client implementations that
49+
// we don't need to check macaroon permissions for.
50+
ignores = []string{
51+
"RawClientWithMacAuth",
52+
}
4653
)
4754

4855
// MacaroonRecipe returns a list of macaroon permissions that is required to use
@@ -79,6 +86,10 @@ func MacaroonRecipe(c LightningClient, packages []string) ([]MacaroonPermission,
7986
methodName = rename
8087
}
8188

89+
if slices.Contains(ignores, methodName) {
90+
continue
91+
}
92+
8293
// The full RPC URI is /package.Service/MethodName.
8394
rpcURI := fmt.Sprintf(
8495
"/%s.%s/%s", pkg, serverName, methodName,

router_client.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ var ErrRouterShuttingDown = errors.New("router shutting down")
3131

3232
// RouterClient exposes payment functionality.
3333
type RouterClient interface {
34+
ServiceClient[routerrpc.RouterClient]
35+
3436
// SendPayment attempts to route a payment to the final destination. The
3537
// call returns a payment update stream and an error stream.
3638
SendPayment(ctx context.Context, request SendPaymentRequest) (
@@ -405,6 +407,10 @@ type routerClient struct {
405407
wg sync.WaitGroup
406408
}
407409

410+
// A compile time check to ensure that routerClient implements the RouterClient
411+
// interface.
412+
var _ RouterClient = (*routerClient)(nil)
413+
408414
func newRouterClient(conn grpc.ClientConnInterface,
409415
routerKitMac serializedMacaroon, timeout time.Duration) *routerClient {
410416

@@ -426,6 +432,15 @@ func (r *routerClient) WaitForFinished() {
426432
r.wg.Wait()
427433
}
428434

435+
// RawClientWithMacAuth returns a context with the proper macaroon
436+
// authentication, the default RPC timeout, and the raw client.
437+
func (r *routerClient) RawClientWithMacAuth(
438+
parentCtx context.Context) (context.Context, time.Duration,
439+
routerrpc.RouterClient) {
440+
441+
return r.routerKitMac.WithMacaroonAuth(parentCtx), r.timeout, r.client
442+
}
443+
429444
// SendPayment attempts to route a payment to the final destination. The call
430445
// returns a payment update stream and an error stream.
431446
func (r *routerClient) SendPayment(ctx context.Context,

signer_client.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import (
1818

1919
// SignerClient exposes sign functionality.
2020
type SignerClient interface {
21+
ServiceClient[signrpc.SignerClient]
22+
2123
// SignOutputRaw is a method that can be used to generate a signature
2224
// for a set of inputs/outputs to a transaction. Each request specifies
2325
// details concerning how the outputs should be signed, which keys they
@@ -190,6 +192,10 @@ type signerClient struct {
190192
timeout time.Duration
191193
}
192194

195+
// A compile time check to ensure that signerClient implements the SignerClient
196+
// interface.
197+
var _ SignerClient = (*signerClient)(nil)
198+
193199
func newSignerClient(conn grpc.ClientConnInterface,
194200
signerMac serializedMacaroon, timeout time.Duration) *signerClient {
195201

@@ -200,6 +206,15 @@ func newSignerClient(conn grpc.ClientConnInterface,
200206
}
201207
}
202208

209+
// RawClientWithMacAuth returns a context with the proper macaroon
210+
// authentication, the default RPC timeout, and the raw client.
211+
func (s *signerClient) RawClientWithMacAuth(
212+
parentCtx context.Context) (context.Context, time.Duration,
213+
signrpc.SignerClient) {
214+
215+
return s.signerMac.WithMacaroonAuth(parentCtx), s.timeout, s.client
216+
}
217+
203218
func marshallSignDescriptors(
204219
signDescriptors []*SignDescriptor) []*signrpc.SignDescriptor {
205220

state_client.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import (
1212

1313
// StateClient exposes base lightning functionality.
1414
type StateClient interface {
15+
ServiceClient[lnrpc.StateClient]
16+
1517
// SubscribeState subscribes to the current state of the wallet.
1618
SubscribeState(ctx context.Context) (chan WalletState, chan error,
1719
error)
@@ -96,6 +98,10 @@ type stateClient struct {
9698
wg sync.WaitGroup
9799
}
98100

101+
// A compile time check to ensure that stateClient implements the StateClient
102+
// interface.
103+
var _ StateClient = (*stateClient)(nil)
104+
99105
// newStateClient returns a new stateClient.
100106
func newStateClient(conn grpc.ClientConnInterface,
101107
readonlyMac serializedMacaroon, timeout time.Duration) *stateClient {
@@ -112,6 +118,15 @@ func (s *stateClient) WaitForFinished() {
112118
s.wg.Wait()
113119
}
114120

121+
// RawClientWithMacAuth returns a context with the proper macaroon
122+
// authentication, the default RPC timeout, and the raw client.
123+
func (s *stateClient) RawClientWithMacAuth(
124+
parentCtx context.Context) (context.Context, time.Duration,
125+
lnrpc.StateClient) {
126+
127+
return s.readonlyMac.WithMacaroonAuth(parentCtx), s.timeout, s.client
128+
}
129+
115130
// SubscribeState subscribes to the current state of the wallet.
116131
func (s *stateClient) SubscribeState(ctx context.Context) (chan WalletState,
117132
chan error, error) {

0 commit comments

Comments
 (0)