-
Notifications
You must be signed in to change notification settings - Fork 5
/
lib.py
627 lines (478 loc) · 23.1 KB
/
lib.py
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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
try:
from typing import List
from pyln.client import Plugin, RpcError, Millisatoshi
import re
import os
import uuid
import json
import math
import time
import hashlib
except ModuleNotFoundError as err:
# OK, something is not installed?
import json
import sys
getmanifest = json.loads(sys.stdin.readline())
print(json.dumps({'jsonrpc': "2.0",
'id': getmanifest['id'],
'result': {'disable': str(err)}}))
sys.exit(1)
prism_db_version = "v2.1"
pubkeyRegex = re.compile(r'^0[2-3][0-9a-fA-F]{64}$')
bolt12Regex = re.compile(
r'^ln([a-zA-Z0-9]{1,90})[0-9]+[munp]?[a-zA-Z0-9]+[0-9]+[munp]?[a-zA-Z0-9]*$')
plugin_out = "/tmp/plugin_out"
if os.path.isfile(plugin_out):
os.remove(plugin_out)
class Member:
@staticmethod
def validate(plugin, member):
if not isinstance(member, dict):
raise ValueError("Each member in the list must be a dictionary.")
if not isinstance(member["description"], str):
raise ValueError("Member 'description' must be a string.")
if not isinstance(member["destination"], str):
raise ValueError("Member 'destination' must be a string")
destination = member["destination"]
if not bolt12Regex.match(destination) and not pubkeyRegex.match(destination) and destination != "":
raise Exception(
"Destination must be a valid bolt12 offer, node pubkey, or empty.")
if bolt12Regex.match(member["destination"]):
# if it is a bolt12, run it through rpc.decode
decode_result = plugin.rpc.decode(string=destination)
#plugin.log(f"decode_result: {decode_result}")
if decode_result["type"] != "bolt12 offer" or decode_result["valid"] != True:
raise Exception("The destination is not recognized as a valid BOLT12 offer.")
if not isinstance(member["split"], float):
try:
if 'split' in member and isinstance(member['split'], (int, float)):
member['split'] = float(member['split'])
except Exception as e:
raise ValueError(f"Member 'split' must be an float (e.g., 2.0) {e}")
# TODO we should make the "split" value positive, yes?
fees_incurred_by = member.get('fees_incurred_by', "remote")
allowed_values_icb = ["local", "remote"]
if fees_incurred_by not in allowed_values_icb:
raise Exception("'fees_incurred_by' can only be 'local' or 'remote'.")
member_payout = int(member.get('payout_threshold_msat', 0))
if member_payout < 0:
raise Exception("'payout_threshold_msat' must be greater than or equal to 0.")
member['payout_threshold_msat'] = member_payout
@staticmethod
def get(plugin: Plugin, member_id: str):
member_record = plugin.rpc.listdatastore(
key=["prism", prism_db_version, "member", member_id])["datastore"]
if not member_record:
return None
member_dict = json.loads(member_record[0]["string"])
return Member(plugin, member_dict)
@staticmethod
def find_many(plugin: Plugin, member_ids: List[str]):
members = []
for member_id in member_ids:
member = Member.get(plugin, member_id)
if member:
members.append(member)
else:
raise Exception(f"Could not find member: {member_id}")
return members
@property
def datastore_key(self):
return self._datastore_key
def __init__(self, plugin: Plugin, member_dict=None):
self._plugin = plugin
self.validate(plugin, member_dict)
self.id: str = member_dict.get("member_id") if member_dict.get("member_id") else hashlib.sha256(str(uuid.uuid4()).encode('utf-8')).hexdigest()
self.description: str = member_dict.get("description")
self.destination: str = member_dict.get("destination")
self.split: float = float(member_dict.get("split"))
if self.split <= 0:
raise Exception("The split MUST be a positive number (e.g., 2.0)")
self.fees_incurred_by: str = member_dict.get(
"fees_incurred_by") if member_dict.get("fees_incurred_by") else "remote"
self.payout_threshold_msat: int = int(member_dict.get(
"payout_threshold_msat")) if member_dict.get("payout_threshold_msat") else int(0)
self._datastore_key = ["prism", prism_db_version, "member", self.id]
def save(self):
self._plugin.log(f"Saving member: {self.id}")
self._plugin.rpc.datastore(
key=self._datastore_key, string=self.to_json(), mode="create-or-replace")
def delete(self):
self._plugin.log(f"Deleting member: {self.id}")
self._plugin.rpc.deldatastore(key=self._datastore_key)
def to_json(self):
return json.dumps({
"member_id": self.id,
"description": self.description,
"destination": self.destination,
"split": self.split,
"fees_incurred_by": self.fees_incurred_by,
"payout_threshold_msat": self.payout_threshold_msat
})
def to_dict(self):
return {
"member_id": self.id,
"description": self.description,
"destination": self.destination,
"split": self.split,
"fees_incurred_by": self.fees_incurred_by,
"payout_threshold_msat": self.payout_threshold_msat
}
class Prism:
@staticmethod
def datastore_key(id):
return ["prism", prism_db_version, "prism", id]
@staticmethod
def from_db_string(plugin: Plugin, prism_string: str):
prism_dict = json.loads(prism_string)
prism_id = prism_dict.get("prism_id")
description = prism_dict.get("description")
members = Member.find_many(plugin, prism_dict.get("prism_members"))
timestamp = prism_dict.get("timestamp")
outlay_factor = prism_dict.get("outlay_factor")
return Prism(plugin, outlay_factor=outlay_factor, description=description, timestamp=timestamp, members=members, prism_id=prism_id)
@staticmethod
def get(plugin: Plugin, prism_id: str):
prism_record = plugin.rpc.listdatastore(
key=Prism.datastore_key(id=prism_id))["datastore"]
if not prism_record:
return None
return Prism.from_db_string(plugin, prism_record[0]["string"])
@staticmethod
def find_all(plugin: Plugin):
key = ["prism", prism_db_version, "prism"]
prism_records = plugin.rpc.listdatastore(key=key).get("datastore", [])
prism_ids = []
for prism in prism_records:
prism_id = prism["key"][3]
prism_ids.append(prism_id)
return prism_ids
@staticmethod
def create(plugin: Plugin, outlay_factor, description: str = None, members: List[Member] = None):
timestamp = round(time.time())
prism = Prism(plugin, timestamp=timestamp, description=description, members=members, outlay_factor=outlay_factor)
prism.save()
return prism
@staticmethod
def validate(members):
if len(members) < 1:
raise ValueError("Prism must contain at least one member.")
if not isinstance(members, list):
raise ValueError("Members must be a list.")
@property
def total_splits(self) -> int:
"""sum each members split"""
return sum([m.split for m in self.members])
@property
def bindings(self):
all_bindings = PrismBinding.list_binding_offers(plugin=self._plugin)
our_bindings = [b for b in all_bindings if b.prism.id == self.id]
self._plugin.log(f"This prism's bindings: {our_bindings}")
return our_bindings
def __init__(self, plugin: Plugin, outlay_factor: float, timestamp: str, description: str = "", members: List[Member] = None, prism_id: str = ""):
self.validate(members)
self.members = members
self.description = description
self.timestamp = timestamp
self.outlay_factor = outlay_factor
self._plugin = plugin
self.id: str = prism_id if prism_id != "" else hashlib.sha256(str(uuid.uuid4()).encode('utf-8')).hexdigest()
def to_json(self, member_ids_only=False):
members = []
if member_ids_only:
members = [member.id for member in self.members]
else:
members = [member.to_dict() for member in self.members]
return json.dumps({
"prism_id": self.id,
"description": self.description,
"timestamp": self.timestamp,
"outlay_factor": self.outlay_factor,
"prism_members": members
})
def to_dict(self):
return {
"prism_id": self.id,
"description": self.description,
"timestamp": self.timestamp,
"outlay_factor": self.outlay_factor,
"prism_members": [member.to_dict() for member in self.members]
}
# save a Prism object and members to the database.
# these records are stored under prism,prism_version,prism,prism_id_a
def save(self):
self._plugin.log(f"Saving prism: {self.id}")
# TODO add a 'last_updated_timestamp' to prism data
# save each prism member
for member in self.members:
member.save()
# save the prism
self._plugin.rpc.datastore(key=self.datastore_key(id=self.id),
string=self.to_json(member_ids_only=True), mode="create-or-replace")
def update(self, members: List[Member]):
self._plugin.log(f"Updating prism: {self.id}")
self.members = members
self.save()
def delete(self):
self._plugin.log(f"Deleting prism: {self.id}", "debug")
# delete each prism member
for member in self.members:
self._plugin.log(f"About to call prism.member.delete() on member {member.id}", "debug")
member.delete()
# delete the prism
rtnVal = self._plugin.rpc.deldatastore(key=self.datastore_key(id=self.id))
return rtnVal
def pay(self, amount_msat: int, binding = None):
"""
Pay each member in the prism their respective share of `amount_msat`
"""
results = {}
for m in self.members:
member_msat = 0
if binding is None:
# when a binding is not provided (when we're using prism.pay, for example)
# the member_msat is set to the proportional share of defined in the split defintion
member_msat = int(math.floor(amount_msat * (m.split / self.total_splits)))
self._plugin.log(f"In Prism.pay, but no binding was provided, thus setting member_msat to a {member_msat}.")
else:
# but if the user provids a binding object, then we set the member_msat to the
# outlay for the respective prism member.
member_msat = int(binding.outlays[m.id])
self._plugin.log(f"In Prism.pay, and a binding was provided. Setting member_msat to the member's outlay: {member_msat}")
# we stop processing if the
if member_msat <= m.payout_threshold_msat:
self._plugin.log("Member outlay is less than the payout threshold. Skipping.")
continue
payment = None
if bolt12Regex.match(m.destination):
try:
self._plugin.log(f"in prism.pay_bolt12regex", 'debug')
bolt12_invoice = self._plugin.rpc.fetchinvoice(offer=m.destination, amount_msat=member_msat)
invoice = bolt12_invoice.get("invoice")
if invoice is not None:
payment = self._plugin.rpc.pay(invoice)
self._plugin.log(f"bolt12_payment: {payment}")
else:
self._plugin.log(f"Could not fetch an invoice from the remote peer.", "warn")
except RpcError as e:
self._plugin.log(f"Prism member bolt12 payment did not complete.: {e}", 'warn')
continue
except Exception as e:
self._plugin.log(f"Prism member bolt12 payment did not complete.: {e}", 'warn')
continue
elif pubkeyRegex.match(m.destination):
try:
self._plugin.log(f"Attempting keysend payment for {member_msat}msats to node with pubkey {m.destination}", 'debug')
payment = self._plugin.rpc.keysend(destination=m.destination, amount_msat=member_msat)
self._plugin.log(f"keysend_payment: {payment}")
except RpcError as e:
self._plugin.log(f"Prism member bolt12 payment did not complete.: {e}", 'warn')
continue
except Exception as e:
self._plugin.log(f"Prism member keysend payment did not complete: {e}", 'warn')
continue
elif m.destination == "":
# in this case, we don't have payment information, so we do nothing. The outlay will only
# clear when a valid destination can be found.
self._plugin.log(f"Prism member destination was empty (member_id={m.id}). No payouts will occur for this member.", 'info')
else:
raise Exception("ERROR: The destination was an invalid format. This should never happen!")
results[m.id] = None
if payment is not None:
results[m.id] = payment
# if there's a binding, we update the outlay.
if binding is not None:
status = payment["status"]
if status != "complete":
self._plugin.log(f"Failed to pay member {m.id}")
continue
# update the member outlay with the payment amount (respecting fee accounting)
total_amount_sent: Millisatoshi = payment["amount_sent_msat"]
total_amount_sent_minus_fees: Millisatoshi = payment["amount_msat"]
self._plugin.log(f"total_amount_sent: {total_amount_sent}", 'debug')
self._plugin.log(f"total_amount_sent_minus_fees: {total_amount_sent_minus_fees}", 'debug')
new_outlay = None
if m.fees_incurred_by == "remote":
new_outlay = member_msat - int(total_amount_sent)
self._plugin.log(f"fees_incurred_by is set to remote. New outlay: {member_msat}-{total_amount_sent}={new_outlay}")
elif m.fees_incurred_by == "local":
new_outlay = member_msat - int(total_amount_sent_minus_fees)
self._plugin.log(f"fees_incurred_by is set to local. New outlay is {member_msat}-{total_amount_sent_minus_fees}={new_outlay}")
else:
raise Exception("If this happens then we have some input validation issues.")
# now that we have the new outlay value, we need to persist it to the db
self._plugin.log(f"new_outlay: {new_outlay}", "debug")
binding.outlays[m.id] = new_outlay
# TODO this saves the entire prism bindings; we probably need something more
# precise that saves only the binding-member. But this works for now
binding.save()
self._plugin.log(
f"PRISM-PAY: ID={self.id}: {len(self.members)} members; {amount_msat} msat total", 'debug')
return results
class PrismBinding:
prism: Prism
@staticmethod
def delete(plugin: Plugin, offer_id: str):
bindings_key = ["prism", prism_db_version,
"bind", "bolt12", offer_id]
binding_records = {}
try:
binding_records = plugin.rpc.deldatastore(
key=bindings_key)
except RpcError as e:
plugin.log(f"ERROR DELETING: {e}", 'error')
if not binding_records:
raise Exception(
f"Could not find a prism binding for offer {offer_id}")
return binding_records
@staticmethod
def from_db_string(plugin: Plugin, string: str, offer_id: str):
parsed = json.loads(string)
prism_id = parsed.get('prism_id', None)
member_outlays = parsed.get('member_outlays', None)
timestamp = parsed.get('timestamp', 0)
if not prism_id:
raise Exception("Invalid binding. Missing prism_id")
if not member_outlays:
raise Exception("Invalid binding. Missing member_outlays")
return PrismBinding(plugin, timestamp=timestamp, outlays=member_outlays, offer_id=offer_id, prism_id=prism_id)
@staticmethod
def get(plugin: Plugin, offer_id: str):
bindings_key = ["prism", prism_db_version,
"bind", "bolt12", offer_id]
binding_records = plugin.rpc.listdatastore(
key=bindings_key).get("datastore", [])
if not binding_records:
raise Exception(
f"Could not find: {bindings_key}")
return PrismBinding.from_db_string(plugin, string=binding_records[0].get('string'), offer_id=offer_id)
@staticmethod
def set_member_outlay(binding: None, member_id: str, new_outlay_value=0):
if member_id in binding.outlays:
binding.outlays[member_id] = int(new_outlay_value)
else:
raise Exception(f"ERROR: Could not find a member with a member_id of {member_id}")
# TODO this saves the entire prism bindings; we probably need something more
# precise that saves only the binding-member. But this works for now
binding.save()
# is is the revers of the method above. It return all prism-bindings,
# but keyed on offer_id, as stored in the database.
@staticmethod
def add_binding(plugin: Plugin, prism_id: str, offer_id: str, ):
prism_binding_key = ["prism", prism_db_version,
"bind", "bolt12", offer_id]
# first we need to see if there are any existing binding records for this prism_id/invoice_type
# plugin.log(f"prism_binding_key: {prism_binding_key}")
binding_record = plugin.rpc.listdatastore(
key=prism_binding_key)["datastore"]
dbmode = "must-create"
# if the record already exists, we adjust the dbmode.
if len(binding_record) > 0:
# oh, the record already exists. switch to must-replace
dbmode = "must-replace"
prism = Prism.get(plugin=plugin, prism_id=prism_id)
if not prism:
raise Exception(f"Could not find prism: {prism_id}")
members = prism.members
if not prism:
raise Exception(f"Could not find prism: {prism_id}")
timestamp = round(time.time())
binding_value = {
"prism_id": prism_id,
"timestamp": timestamp,
"member_outlays": {member.id: 0 for member in members}
}
# save the record
plugin.rpc.datastore(key=prism_binding_key, string=json.dumps(
binding_value), mode=f"{dbmode}")
response = {
"status": dbmode,
"timestamp": timestamp,
"offer_id": offer_id,
"prism_id": prism_id,
"prism_binding_key": prism_binding_key,
"prism_members": [member.to_dict() for member in members]
}
return response
@staticmethod
def list_binding_offers(plugin):
bindings_key = ["prism", prism_db_version, "bind", "bolt12"]
binding_records = plugin.rpc.listdatastore(
key=bindings_key).get("datastore", [])
bindings = []
for binding_record in binding_records:
offer_id = binding_record["key"][4]
#plugin.log(f"offer_id: {offer_id}")
binding_record_str = binding_record['string']
binding = PrismBinding.from_db_string(plugin, string=binding_record_str, offer_id=offer_id)
bindings.append(binding)
return bindings
@property
def datastore_key(self):
return self._datastore_key
def __init__(self, plugin: Plugin, timestamp: int, outlays, offer_id, prism_id, binding_dict=None):
self._plugin = plugin
self.offer_id = offer_id
self.timestamp = timestamp
self.members = None
self.prism = Prism.get(plugin, prism_id)
self.outlays = outlays
if binding_dict:
self.offer_id = binding_dict.get("offer_id")
self.timestamp = binding_dict.get("timestamp")
self.members = binding_dict
self._datastore_key = ["prism", prism_db_version,
"bind", "bolt12", offer_id]
def to_dict(self):
return {
"offer_id": self.offer_id,
"prism_id": self.prism.id,
"timestamp": self.timestamp,
"member_outlays": [
{
"member_id": member_id,
"outlay_msat": outlay
}
for member_id, outlay in self.outlays.items()
]
}
def to_json(self):
return {
"offer_id": self.offer_id,
"prism_id": self.prism.id,
"timestamp": self.timestamp
}
def save(self):
string = json.dumps({
"prism_id": self.prism.id,
"timestamp": self.timestamp,
"member_outlays": self.outlays
})
self._plugin.rpc.datastore(
key=self._datastore_key, string=string, mode="must-replace")
def increment_outlays(self, amount_msat):
self._plugin.log(f"Incrementing outlays for binding '{self.offer_id}' with total income of {amount_msat}msats.")
new_outlays = {}
for member_id, outlay in self.outlays.items():
# find member in the Prism by the member id in the outlays
m = [m for m in self.prism.members if m.id == member_id][0]
if not m:
raise Exception(
f"Binding and prism in different states. Expected to find member {member_id} in prism {self.prism.id}")
member_msat = math.floor(
amount_msat * (m.split / self.prism.total_splits))
new_amount = int(outlay) + int(member_msat)
self._plugin.log(
f"Updating member {member_id} outlay to {new_amount}")
new_outlays[member_id] = new_amount
self.outlays = new_outlays
self.save()
def pay(self, amount_msat):
payment_results = None
self._plugin.log(f"Calculating total outlays...")
total_outlays = amount_msat * self.prism.outlay_factor
self._plugin.log(f"Total outlays will be {total_outlays} after applying an outlay factor of {self.prism.outlay_factor} to the income amount {amount_msat}.")
self.increment_outlays(amount_msat=total_outlays)
payment_results = self.prism.pay(amount_msat, binding=self)
self._plugin.log(f"PAYMENT RESULTS: {payment_results}", 'debug')
return True