-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy path__init__.py
More file actions
584 lines (483 loc) · 19.9 KB
/
__init__.py
File metadata and controls
584 lines (483 loc) · 19.9 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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
from . import scheduler_pb2 as schedpb
from . import greenlight_pb2 as nodepb
from pyln import grpc as clnpb # type: ignore
from pyln.grpc import Amount, AmountOrAll, AmountOrAny # noqa: F401
from . import glclient as native
from .glclient import backup_decrypt_with_seed # noqa: F401
from .tls import TlsConfig
from google.protobuf.message import Message as PbMessage
from binascii import hexlify, unhexlify
from typing import Optional, List, Iterable, Any, Type, TypeVar
import logging
from glclient.glclient import Credentials
backup_decrypt_with_seed = native.backup_decrypt_with_seed
# Keep in sync with the libhsmd version, this is tested in unit tests.
__version__ = "v25.12"
E = TypeVar("E", bound=PbMessage)
def _convert(cls: Type[E], res: Iterable[Any]) -> E:
return cls.FromString(bytes(res))
class Signer(object):
def __init__(self, secret: bytes, network: str, creds: Credentials):
self.inner = native.Signer(secret, network, creds)
self.creds = creds
self.handle: Optional[native.SignerHandle] = None
def run_in_thread(self) -> "native.SignerHandle":
if self.handle is not None:
raise ValueError(
"This signer is already running, please shut it down before starting it again"
)
self.handle = self.inner.run_in_thread()
return self.handle
def run_in_foreground(self) -> None:
return self.inner.run_in_foreground()
def node_id(self) -> bytes:
return bytes(self.inner.node_id())
def version(self) -> str:
return self.inner.version()
def sign_challenge(self, message: bytes) -> bytes:
return bytes(self.inner.sign_challenge(message))
def shutdown(self) -> None:
if self.handle is None:
raise ValueError("Attempted to shut down a signer that is not running")
self.handle.shutdown()
self.handle = None
def create_rune(
self, restrictions: List[List[str]], rune: Optional[str] = None
) -> str:
return self.inner.create_rune(restrictions, rune)
def is_running(self) -> bool:
return self.handle is not None
class Scheduler(object):
def __init__(self, network: str, creds: Optional[Credentials] = None):
self.network = network
self.creds = creds if creds is not None else native.Credentials()
self.inner = native.Scheduler(network, self.creds)
def schedule(self) -> schedpb.NodeInfoResponse:
res = self.inner.schedule()
return schedpb.NodeInfoResponse.FromString(bytes(res))
def get_node_info(self, wait: bool = False):
res = self.inner.get_node_info(wait)
return schedpb.NodeInfoResponse.FromString(bytes(res))
def register(
self, signer: Signer, invite_code: Optional[str] = None
) -> schedpb.RegistrationResponse:
res = self.inner.register(signer.inner, invite_code)
return schedpb.RegistrationResponse.FromString(bytes(res))
def recover(self, signer: Signer) -> schedpb.RecoveryResponse:
res = self.inner.recover(signer.inner)
return schedpb.RecoveryResponse.FromString(bytes(res))
def authenticate(self, creds: Credentials):
self.creds = creds
self.inner = self.inner.authenticate(creds)
return self
def export_node(self) -> schedpb.ExportNodeResponse:
res = schedpb.ExportNodeResponse
return res.FromString(bytes(self.inner.export_node()))
def node(self) -> "Node":
res = self.inner.node()
info = schedpb.NodeInfoResponse.FromString(bytes(res))
return Node(
node_id=self.creds.node_id(),
grpc_uri=info.grpc_uri,
creds=self.creds,
)
def get_invite_codes(self) -> schedpb.ListInviteCodesResponse:
cls = schedpb.ListInviteCodesResponse
return cls.FromString(bytes(self.inner.get_invite_codes()))
def add_outgoing_webhook(self, uri: str) -> schedpb.AddOutgoingWebhookResponse:
res = self.inner.add_outgoing_webhook(uri)
return schedpb.AddOutgoingWebhookResponse.FromString(bytes(res))
def list_outgoing_webhooks(self) -> schedpb.ListOutgoingWebhooksResponse:
res = self.inner.list_outgoing_webhooks()
return schedpb.ListOutgoingWebhooksResponse.FromString(bytes(res))
def delete_outgoing_webhook(self, webhook_id: int) -> None:
res = self.inner.delete_outgoing_webhooks([webhook_id])
def delete_outgoing_webhooks(self, webhook_ids: List[int]) -> None:
res = self.inner.delete_outgoing_webhooks(webhook_ids)
def rotate_outgoing_webhook_secret(
self, webhook_id: int
) -> schedpb.WebhookSecretResponse:
res = self.inner.rotate_outgoing_webhook_secret(webhook_id)
return schedpb.WebhookSecretResponse.FromString(bytes(res))
class Node(object):
def __init__(self, node_id: bytes, grpc_uri: str, creds: Credentials) -> None:
self.creds = creds
self.grpc_uri = grpc_uri
self.inner = native.Node(node_id=node_id, grpc_uri=grpc_uri, creds=creds)
self.logger = logging.getLogger("glclient.Node")
def get_info(self) -> clnpb.GetinfoResponse:
uri = "/cln.Node/Getinfo"
req = clnpb.GetinfoRequest().SerializeToString()
res = clnpb.GetinfoResponse
return res.FromString(bytes(self.inner.call(uri, bytes(req))))
def stop(self) -> None:
uri = "/cln.Node/Stop"
req = clnpb.StopRequest().SerializeToString()
try:
# This fails, since we just get disconnected, but that's
# on purpose, so drop the error silently.
self.inner.call(uri, bytes(req))
except ValueError as e:
self.logger.debug(
f"Caught an expected exception: {e}. Don't worry it's expected."
)
def list_funds(
self,
spent: Optional[bool] = None,
) -> clnpb.ListfundsResponse:
uri = "/cln.Node/ListFunds"
res = clnpb.ListfundsResponse
req = clnpb.ListfundsRequest(
spent=spent,
).SerializeToString()
return res.FromString(bytes(self.inner.call(uri, bytes(req))))
def list_peers(self) -> clnpb.ListpeersResponse:
uri = "/cln.Node/ListPeers"
req = clnpb.ListpeersRequest().SerializeToString()
res = clnpb.ListpeersResponse
return res.FromString(bytes(self.inner.call(uri, bytes(req))))
def list_peer_channels(
self, node_id: Optional[bytes] = None
) -> clnpb.ListpeerchannelsResponse:
uri = "/cln.Node/ListPeerChannels"
req = clnpb.ListpeerchannelsRequest(
id=node_id,
).SerializeToString()
res = clnpb.ListpeerchannelsResponse
return res.FromString(bytes(self.inner.call(uri, bytes(req))))
def list_closed_channels(self) -> clnpb.ListclosedchannelsResponse:
uri = "/cln.Node/ListClosedChannels"
req = clnpb.ListclosedchannelsRequest().SerializeToString()
res = clnpb.ListclosedchannelsResponse
return res.FromString(bytes(self.inner.call(uri, bytes(req))))
def list_channels(
self,
short_channel_id: Optional[str] = None,
source: Optional[bytes] = None,
destination: Optional[bytes] = None,
) -> clnpb.ListchannelsResponse:
uri = "/cln.Node/ListChannels"
req = clnpb.ListchannelsRequest(
short_channel_id=short_channel_id, source=source, destination=destination
).SerializeToString()
res = clnpb.ListchannelsResponse
return res.FromString(bytes(self.inner.call(uri, bytes(req))))
def listpays(
self,
bolt11: Optional[str] = None,
payment_hash: Optional[bytes] = None,
status: Optional[clnpb.ListpaysRequest.ListpaysStatus.ValueType] = None,
) -> clnpb.ListpaysResponse:
uri = "/cln.Node/ListPays"
req = clnpb.ListpaysRequest(
bolt11=bolt11,
payment_hash=payment_hash,
status=status,
).SerializeToString()
res = clnpb.ListpaysResponse
return res.FromString(bytes(self.inner.call(uri, req)))
def list_invoices(
self,
label: Optional[str] = None,
invstring: Optional[str] = None,
payment_hash: Optional[bytes] = None,
offer_id: Optional[str] = None,
index: Optional[clnpb.ListinvoicesRequest.ListinvoicesIndex.ValueType] = None,
start: Optional[int] = None,
limit: Optional[int] = None,
) -> clnpb.ListinvoicesResponse:
uri = "/cln.Node/ListInvoices"
res = clnpb.ListinvoicesResponse
req = clnpb.ListinvoicesRequest(
label=label,
invstring=invstring,
payment_hash=payment_hash,
offer_id=offer_id,
index=index,
start=start,
limit=limit,
).SerializeToString()
return res.FromString(bytes(self.inner.call(uri, bytes(req))))
def connect_peer(
self, node_id, host: Optional[str] = None, port: Optional[int] = None
) -> clnpb.ConnectResponse:
if len(node_id) == 33:
node_id = hexlify(node_id)
if isinstance(node_id, bytes):
node_id = node_id.decode("ASCII")
uri = "/cln.Node/ConnectPeer"
res = clnpb.ConnectResponse
req = clnpb.ConnectRequest(
id=node_id,
host=host,
port=port,
).SerializeToString()
return res.FromString(bytes(self.inner.call(uri, bytes(req))))
def decode(self, string: str) -> clnpb.DecodeResponse:
uri = "/cln.Node/Decode"
res = clnpb.DecodeResponse
req = clnpb.DecodeRequest(
string=string,
).SerializeToString()
return res.FromString(bytes(self.inner.call(uri, bytes(req))))
def decodepay(
self, bolt11: str, description: Optional[str]
) -> clnpb.DecodepayResponse:
uri = "/cln.Node/DecodePay"
res = clnpb.DecodepayResponse
req = clnpb.DecodepayRequest(
bolt11=bolt11,
description=description,
).SerializeToString()
return res.FromString(bytes(self.inner.call(uri, bytes(req))))
def disconnect_peer(self, peer_id: str, force=False) -> clnpb.DisconnectResponse:
uri = "/cln.Node/Disconnect"
res = clnpb.DisconnectResponse
req = clnpb.DisconnectRequest(
id=bytes.fromhex(peer_id),
force=force,
).SerializeToString()
return res.FromString(bytes(self.inner.call(uri, bytes(req))))
def new_address(self) -> clnpb.NewaddrResponse:
uri = "/cln.Node/NewAddr"
req = clnpb.NewaddrRequest().SerializeToString()
res = clnpb.NewaddrResponse
return res.FromString(bytes(self.inner.call(uri, req)))
def withdraw(
self, destination, amount: AmountOrAll, minconf: int = 0
) -> clnpb.WithdrawResponse:
uri = "/cln.Node/Withdraw"
res = clnpb.WithdrawResponse
req = clnpb.WithdrawRequest(
destination=destination, satoshi=amount, minconf=minconf
).SerializeToString()
return res.FromString(bytes(self.inner.call(uri, bytes(req))))
def fund_channel(
self,
id: bytes,
amount,
announce: Optional[bool] = False,
minconf: Optional[int] = 1,
) -> clnpb.FundchannelResponse:
if len(id) != 33:
raise ValueError("id is not 33 bytes long")
uri = "/cln.Node/FundChannel"
res = clnpb.FundchannelResponse
req = clnpb.FundchannelRequest(
id=id,
amount=amount,
announce=announce,
minconf=minconf,
).SerializeToString()
return res.FromString(bytes(self.inner.call(uri, bytes(req))))
def close(
self, id: bytes, unilateraltimeout=None, destination=None
) -> clnpb.CloseResponse:
if len(id) != 33:
raise ValueError("node_id is not 33 bytes long")
uri = "/cln.Node/Close"
res = clnpb.CloseResponse
req = clnpb.CloseRequest(
id=id.hex(),
unilateraltimeout=unilateraltimeout,
destination=destination,
).SerializeToString()
return res.FromString(bytes(self.inner.call(uri, bytes(req))))
def invoice(
self,
amount_msat: clnpb.AmountOrAny,
label: str,
description: str,
expiry: Optional[int] = None,
fallbacks: Optional[List[str]] = None,
preimage: Optional[bytes] = None,
cltv: Optional[int] = None,
deschashonly: Optional[bool] = None,
) -> clnpb.InvoiceResponse:
if preimage and len(preimage) != 32:
raise ValueError("Preimage must be 32 bytes in length")
uri = "/cln.Node/Invoice"
res = clnpb.InvoiceResponse
req = clnpb.InvoiceRequest(
amount_msat=amount_msat,
label=label,
description=description,
preimage=preimage,
expiry=expiry,
fallbacks=fallbacks,
cltv=cltv,
deschashonly=deschashonly,
).SerializeToString()
return res.FromString(bytes(self.inner.call(uri, bytes(req))))
def pay(
self,
bolt11: str,
amount_msat: Optional[clnpb.Amount] = None,
retry_for: Optional[int] = None,
maxfee: Optional[clnpb.Amount] = None,
maxfeepercent: Optional[float] = None,
) -> clnpb.PayResponse:
uri = "/cln.Node/Pay"
res = clnpb.PayResponse
req = clnpb.PayRequest(
bolt11=bolt11,
amount_msat=amount_msat,
retry_for=retry_for,
maxfeepercent=maxfeepercent,
maxfee=maxfee,
).SerializeToString()
return res.FromString(bytes(self.inner.call(uri, bytes(req))))
def trampoline_pay(
self,
bolt11: str,
trampoline_node_id: bytes,
amount_msat: Optional[int] = None,
label: Optional[str] = None,
):
res = self.inner.trampoline_pay(
bolt11=bolt11,
trampoline_node_id=trampoline_node_id,
amount_msat=amount_msat,
label=label,
)
return nodepb.TrampolinePayResponse.FromString(bytes(res))
def keysend(
self,
destination: bytes,
amount: clnpb.Amount,
label: Optional[str] = None,
routehints: Optional[clnpb.RoutehintList] = None,
extratlvs: Optional[clnpb.TlvStream] = None,
) -> clnpb.KeysendResponse:
uri = "/cln.Node/KeySend"
res = clnpb.KeysendResponse
req = clnpb.KeysendRequest(
destination=normalize_node_id(destination, string=False),
amount_msat=amount,
label=label if label else "",
routehints=routehints,
extratlvs=extratlvs,
).SerializeToString()
return res.FromString(bytes(self.inner.call(uri, bytes(req))))
def stream_log(self):
"""Stream logs as they get generated on the server side."""
stream = self.inner.stream_log(b"")
while True:
n = stream.next()
if n is None:
break
yield nodepb.LogEntry.FromString(bytes(n))
def stream_custommsg(self):
stream = self.inner.stream_custommsg(b"")
while True:
n = stream.next()
if n is None:
break
yield nodepb.Custommsg.FromString(bytes(n))
def stream_node_events(self):
"""Stream node events (invoice payments, peer changes, etc.) as they occur."""
stream = self.inner.stream_node_events(b"")
while True:
n = stream.next()
if n is None:
break
yield nodepb.NodeEvent.FromString(bytes(n))
def send_custommsg(self, node_id: str, msg: bytes) -> clnpb.SendcustommsgResponse:
uri = "/cln.Node/SendCustomMsg"
res = clnpb.SendcustommsgResponse
req = clnpb.SendcustommsgRequest(
node_id=normalize_node_id(node_id),
msg=msg,
).SerializeToString()
return res.FromString(bytes(self.inner.call(uri, bytes(req))))
def datastore(self, key, string=None, hex=None, mode=None, generation=None):
uri = "/cln.Node/Datastore"
req = clnpb.DatastoreRequest(
key=key, string=string, hex=hex, mode=mode, generation=generation
).SerializeToString()
res = clnpb.DatastoreResponse
return res.FromString(bytes(self.inner.call(uri, bytes(req))))
def del_datastore(self, key, generation=None):
uri = "/cln.Node/DelDatastore"
req = clnpb.DeldatastoreRequest(
key=key, generation=generation
).SerializeToString()
res = clnpb.DeldatastoreResponse
return res.FromString(bytes(self.inner.call(uri, bytes(req))))
def list_datastore(self, key=None):
uri = "/cln.Node/ListDatastore"
req = clnpb.ListdatastoreRequest(key=key).SerializeToString()
res = clnpb.ListdatastoreResponse
return res.FromString(bytes(self.inner.call(uri, bytes(req))))
def configure(self, close_to_addr: str) -> None:
req = nodepb.GlConfig(close_to_addr=close_to_addr).SerializeToString()
return self.inner.configure(req)
def wait_blockheight(
self,
blockheight: int,
timeout: Optional[int] = None,
):
"""Wait until the blockchain has reached the specified blockheight."""
uri = "/cln.Node/WaitBlockheight"
req = clnpb.WaitblockheightRequest(
blockheight=blockheight, timeout=timeout
).SerializeToString()
res = clnpb.WaitblockheightResponse
return res.FromString(bytes(self.inner.call(uri, bytes(req))))
def fetch_invoice(
self,
offer: str,
amount_msat: Optional[Amount] = None,
quantity: Optional[int] = None,
recurrence_counter: Optional[int] = None,
recurrence_start: Optional[int] = None,
recurrence_label: Optional[str] = None,
timeout: Optional[int] = None,
payer_note: Optional[str] = None,
) -> clnpb.FetchinvoiceResponse:
"""Fetch an invoice based on an offer.
Contact the issuer of an offer to get an actual invoice that
can be paid. It highlights any changes between the offer and
the returned invoice.
"""
uri = "/cln.Node/FetchInvoice"
req = clnpb.FetchinvoiceRequest(
offer=offer,
amount_msat=amount_msat,
quantity=quantity,
recurrence_counter=recurrence_counter,
recurrence_start=recurrence_start,
recurrence_label=recurrence_label,
timeout=timeout,
payer_note=payer_note,
).SerializeToString()
res = clnpb.FetchinvoiceResponse
return res.FromString(bytes(self.inner.call(uri, bytes(req))))
def wait(self, subsystem, indexname, nextvalue: int) -> clnpb.WaitResponse:
"""Wait for the next event in the provided subsystem.
Returns once the index given by indexname in subsystem reaches
or exceeds nextvalue.
"""
uri = "/cln.Node/Wait"
req = clnpb.WaitRequest(
subsystem=subsystem,
indexname=indexname,
nextvalue=nextvalue,
).SerializeToString()
res = clnpb.WaitResponse
return res.FromString(bytes(self.inner.call(uri, bytes(req))))
def lsp_invoice(self, label: str, description: str, amount_msat: int | None = None):
uri = "/greenlight.Node/LspInvoice"
req = nodepb.LspInvoiceRequest(
label=label, description=description, amount_msat=amount_msat
).SerializeToString()
res = nodepb.LspInvoiceResponse
return res.FromString(bytes(self.inner.call(uri, bytes(req))))
def normalize_node_id(node_id, string=False):
if len(node_id) == 66:
node_id = unhexlify(node_id)
if len(node_id) != 33:
raise ValueError("node_id is not 33 (binary) or 66 (hex) bytes long")
if isinstance(node_id, str):
node_id = node_id.encode("ASCII")
return node_id if not string else hexlify(node_id).encode("ASCII")