forked from lightninglabs/lndclient
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwtclient_client.go
More file actions
238 lines (191 loc) · 8.01 KB
/
Copy pathwtclient_client.go
File metadata and controls
238 lines (191 loc) · 8.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package lndclient
import (
"context"
"time"
"github.com/lightningnetwork/lnd/lnrpc/wtclientrpc"
"google.golang.org/grpc"
)
// WatchtowerClientClient implements the watchtower client interface
// https://lightning.engineering/api-docs/category/watchtowerclient-service/
// NOTE: For the macaroon code to work correctly, this needs to be named
// WatchtowerClientClient since we use reflection to set macaroon permissions.
type WatchtowerClientClient interface {
ServiceClient[wtclientrpc.WatchtowerClientClient]
// AddTower adds a new watchtower reachable at the given address and
// considers it for new sessions. If the watchtower already exists, then
// any new addresses included will be considered when dialing it for
// session negotiations and backups.
AddTower(ctx context.Context, pubkey []byte, address string) error
// DeactivateTower sets the given tower's status to inactive so that it
// is not considered for session negotiation. Its sessions will also not
// be used while the tower is inactive.
DeactivateTower(ctx context.Context, pubkey []byte) (string, error)
// GetTowerInfo gets information about a watchtower that corresponds to
// the given pubkey. The `includeSessions` flag controls whether session
// information is included. The `excludeExhaustedSessions` controls
// whether exhausted sessions are included in the response.
GetTowerInfo(ctx context.Context, pubkey []byte, includeSessions,
excludeExhaustedSessions bool) (*wtclientrpc.Tower, error)
// ListTowers gets information about all registered watchtowers. The
// `includeSessions` and `excludeExhaustedSessions` flags serve the same
// function as in the `GetTowerInfo` method.
ListTowers(ctx context.Context, includeSessions,
excludeExhaustedSessions bool) ([]*wtclientrpc.Tower, error)
// Policy returns the active watchtower client policy configuration.
Policy(ctx context.Context, policyType wtclientrpc.PolicyType) (
*wtclientrpc.PolicyResponse, error)
// RemoveTower removes a watchtower from being considered for future
// session negotiations and from being used for any subsequent backups
// until it's added again. If an address is provided, then this RPC only
// serves as a way of removing the address from the watchtower instead.
RemoveTower(ctx context.Context, pubkey []byte, address string) error
// Stats returns the in-memory statistics of the client since startup.
Stats(ctx context.Context) (*wtclientrpc.StatsResponse, error)
// TerminateSession terminates the given session and marks it to not be
// used for backups anymore. Returns a human-readable status string.
TerminateSession(ctx context.Context, sessionId []byte) (string, error)
}
type wtClient struct {
client wtclientrpc.WatchtowerClientClient
wtClientMac serializedMacaroon
timeout time.Duration
}
// A compile time check to ensure that wtClientClient implements the
// WtclientClient interface.
var _ WatchtowerClientClient = (*wtClient)(nil)
// newClientClient creates a new watchtower client interface.
func newWtClient(conn grpc.ClientConnInterface, wtClientMac serializedMacaroon,
timeout time.Duration) *wtClient {
return &wtClient{
client: wtclientrpc.NewWatchtowerClientClient(conn),
wtClientMac: wtClientMac,
timeout: timeout,
}
}
// RawClientWithMacAuth returns a context with the proper macaroon
// authentication, the default RPC timeout, and the raw client.
func (m *wtClient) RawClientWithMacAuth(
parentCtx context.Context) (context.Context, time.Duration,
wtclientrpc.WatchtowerClientClient) {
return m.wtClientMac.WithMacaroonAuth(parentCtx), m.timeout, m.client
}
// AddTower adds a new watchtower reachable at the given address and
// considers it for new sessions. If the watchtower already exists, then
// any new addresses included will be considered when dialing it for
// session negotiations and backups.
func (m *wtClient) AddTower(ctx context.Context, pubkey []byte,
address string) error {
rpcCtx, cancel := context.WithTimeout(ctx, m.timeout)
defer cancel()
rpcReq := &wtclientrpc.AddTowerRequest{
Pubkey: pubkey,
Address: address,
}
rpcCtx = m.wtClientMac.WithMacaroonAuth(rpcCtx)
_, err := m.client.AddTower(rpcCtx, rpcReq)
return err
}
// DeactivateTower sets the given tower's status to inactive so that it
// is not considered for session negotiation. Its sessions will also not
// be used while the tower is inactive.
func (m *wtClient) DeactivateTower(ctx context.Context, pubkey []byte) (
string, error) {
rpcCtx, cancel := context.WithTimeout(ctx, m.timeout)
defer cancel()
rpcCtx = m.wtClientMac.WithMacaroonAuth(rpcCtx)
resp, err := m.client.DeactivateTower(rpcCtx,
&wtclientrpc.DeactivateTowerRequest{
Pubkey: pubkey,
},
)
if err != nil {
return "", err
}
return resp.Status, nil
}
// GetTowerInfo gets information about a watchtower that corresponds to
// the given pubkey. The `includeSessions` flag controls whether session
// information is included. The `excludeExhaustedSessions` controls
// whether exhausted sessions are included in the response.
func (m *wtClient) GetTowerInfo(ctx context.Context, pubkey []byte,
includeSessions, excludeExhaustedSessions bool) (*wtclientrpc.Tower,
error) {
rpcCtx, cancel := context.WithTimeout(ctx, m.timeout)
defer cancel()
rpcCtx = m.wtClientMac.WithMacaroonAuth(rpcCtx)
return m.client.GetTowerInfo(rpcCtx,
&wtclientrpc.GetTowerInfoRequest{
Pubkey: pubkey,
IncludeSessions: includeSessions,
ExcludeExhaustedSessions: excludeExhaustedSessions,
},
)
}
// ListTowers gets information about all registered watchtowers. The
// `includeSessions` and `excludeExhaustedSessions` flags serve the same
// function as in the `GetTowerInfo` method.
func (m *wtClient) ListTowers(ctx context.Context, includeSessions,
excludeExhaustedSessions bool) ([]*wtclientrpc.Tower, error) {
rpcCtx, cancel := context.WithTimeout(ctx, m.timeout)
defer cancel()
rpcCtx = m.wtClientMac.WithMacaroonAuth(rpcCtx)
resp, err := m.client.ListTowers(rpcCtx, &wtclientrpc.ListTowersRequest{
IncludeSessions: includeSessions,
ExcludeExhaustedSessions: excludeExhaustedSessions,
})
if err != nil {
return nil, err
}
return resp.Towers, nil
}
// Policy returns the active watchtower client policy configuration.
func (m *wtClient) Policy(ctx context.Context,
policyType wtclientrpc.PolicyType) (*wtclientrpc.PolicyResponse,
error) {
rpcCtx, cancel := context.WithTimeout(ctx, m.timeout)
defer cancel()
rpcCtx = m.wtClientMac.WithMacaroonAuth(rpcCtx)
return m.client.Policy(rpcCtx, &wtclientrpc.PolicyRequest{
PolicyType: policyType,
})
}
// RemoveTower removes a watchtower from being considered for future
// session negotiations and from being used for any subsequent backups
// until it's added again. If an address is provided, then this RPC only
// serves as a way of removing the address from the watchtower instead.
func (m *wtClient) RemoveTower(ctx context.Context, pubkey []byte,
address string) error {
rpcCtx, cancel := context.WithTimeout(ctx, m.timeout)
defer cancel()
rpcCtx = m.wtClientMac.WithMacaroonAuth(rpcCtx)
_, err := m.client.RemoveTower(rpcCtx, &wtclientrpc.RemoveTowerRequest{
Pubkey: pubkey,
Address: address,
})
return err
}
// Stats returns the in-memory statistics of the client since startup.
func (m *wtClient) Stats(ctx context.Context) (*wtclientrpc.StatsResponse,
error) {
rpcCtx, cancel := context.WithTimeout(ctx, m.timeout)
defer cancel()
rpcCtx = m.wtClientMac.WithMacaroonAuth(rpcCtx)
return m.client.Stats(rpcCtx, &wtclientrpc.StatsRequest{})
}
// TerminateSession terminates the given session and marks it to not be used
// for backups anymore. Returns a human-readable status string.
func (m *wtClient) TerminateSession(ctx context.Context,
sessionId []byte) (string, error) {
rpcCtx, cancel := context.WithTimeout(ctx, m.timeout)
defer cancel()
rpcCtx = m.wtClientMac.WithMacaroonAuth(rpcCtx)
resp, err := m.client.TerminateSession(rpcCtx,
&wtclientrpc.TerminateSessionRequest{
SessionId: sessionId,
},
)
if err != nil {
return "", err
}
return resp.Status, nil
}