Skip to content

Commit f3abe61

Browse files
committed
multi: add RawClientWithMacAuth to every client
1 parent 48610c4 commit f3abe61

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
@@ -63,6 +63,8 @@ type InvoiceHtlcModifyHandler func(context.Context,
6363

6464
// InvoicesClient exposes invoice functionality.
6565
type InvoicesClient interface {
66+
ServiceClient[invoicesrpc.InvoicesClient]
67+
6668
SubscribeSingleInvoice(ctx context.Context, hash lntypes.Hash) (
6769
<-chan InvoiceUpdate, <-chan error, error)
6870

@@ -97,6 +99,10 @@ type invoicesClient struct {
9799
wg sync.WaitGroup
98100
}
99101

102+
// A compile time check to ensure that invoicesClient implements the
103+
// InvoicesClient interface.
104+
var _ InvoicesClient = (*invoicesClient)(nil)
105+
100106
func newInvoicesClient(conn grpc.ClientConnInterface,
101107
invoiceMac serializedMacaroon, timeout time.Duration) *invoicesClient {
102108

@@ -116,6 +122,15 @@ func (s *invoicesClient) WaitForFinished() {
116122
s.wg.Wait()
117123
}
118124

125+
// RawClientWithMacAuth returns a context with the proper macaroon
126+
// authentication, the default RPC timeout, and the raw client.
127+
func (s *invoicesClient) RawClientWithMacAuth(
128+
parentCtx context.Context) (context.Context, time.Duration,
129+
invoicesrpc.InvoicesClient) {
130+
131+
return s.invoiceMac.WithMacaroonAuth(parentCtx), s.timeout, s.client
132+
}
133+
119134
func (s *invoicesClient) SettleInvoice(ctx context.Context,
120135
preimage lntypes.Preimage) error {
121136

lightning_client.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ func WithRemoteMaxHtlc(maxHtlc uint32) OpenChannelOption {
8181

8282
// LightningClient exposes base lightning functionality.
8383
type LightningClient interface {
84+
ServiceClient[lnrpc.LightningClient]
85+
8486
PayInvoice(ctx context.Context, invoice string,
8587
maxFee btcutil.Amount,
8688
outgoingChannel *uint64) chan PaymentResult
@@ -1330,6 +1332,10 @@ type lightningClient struct {
13301332
adminMac serializedMacaroon
13311333
}
13321334

1335+
// A compile time check to ensure that lightningClient implements the
1336+
// LightningClient interface.
1337+
var _ LightningClient = (*lightningClient)(nil)
1338+
13331339
func newLightningClient(conn grpc.ClientConnInterface, timeout time.Duration,
13341340
params *chaincfg.Params, adminMac serializedMacaroon) *lightningClient {
13351341

@@ -1353,6 +1359,15 @@ func (s *lightningClient) WaitForFinished() {
13531359
s.wg.Wait()
13541360
}
13551361

1362+
// RawClientWithMacAuth returns a context with the proper macaroon
1363+
// authentication, the default RPC timeout, and the raw client.
1364+
func (s *lightningClient) RawClientWithMacAuth(
1365+
parentCtx context.Context) (context.Context, time.Duration,
1366+
lnrpc.LightningClient) {
1367+
1368+
return s.adminMac.WithMacaroonAuth(parentCtx), s.timeout, s.client
1369+
}
1370+
13561371
// WalletBalance returns a summary of the node's wallet balance.
13571372
func (s *lightningClient) WalletBalance(ctx context.Context) (
13581373
*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)