-
Notifications
You must be signed in to change notification settings - Fork 39
/
invoice_check.py
52 lines (35 loc) · 1.18 KB
/
invoice_check.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
import time
from bolt11 import Bolt11, decode
from pyln.client import Plugin
from plugins.hold.encoder import get_network_prefix
class InvoiceNoSelfPaymentError(Exception):
pass
class InvoiceNetworkInvalidError(Exception):
pass
class InvoiceExpiredError(Exception):
pass
class InvoiceChecker:
_pl: Plugin
_node_id: str
_prefix: str
def __init__(self, pl: Plugin) -> None:
self._pl = pl
def init(self) -> None:
info = self._pl.rpc.getinfo()
self._node_id = info["id"]
self._prefix = get_network_prefix(info["network"])
def check(self, invoice: str) -> None:
dec = decode(invoice)
self._check_network(dec)
self._check_self_payment(dec)
self._check_expiry(dec)
def _check_network(self, dec: Bolt11) -> None:
if dec.currency != self._prefix:
raise InvoiceNetworkInvalidError
def _check_self_payment(self, dec: Bolt11) -> None:
if dec.payee == self._node_id:
raise InvoiceNoSelfPaymentError
@staticmethod
def _check_expiry(dec: Bolt11) -> None:
if dec.date + dec.expiry <= int(time.time()):
raise InvoiceExpiredError