2
2
import logging
3
3
4
4
from aiohttp .client import ClientSession
5
+ from lnurl import Lnurl
5
6
6
7
from pylnbits .utils import get_url , post_url
7
8
@@ -242,6 +243,47 @@ def get_payurl(self, email: str):
242
243
except Exception as e :
243
244
print ("Exception, possibly malformed LN Address: " + str (e ))
244
245
246
+ # generates bolt11 invoice from payurl and amount to pay
247
+ async def get_bolt11_from_payurl (self , purl : str , amount : int ):
248
+ json_content = await get_url (self ._session , path = purl , headers = self ._invoice_headers )
249
+ lnurlpay = json_content ["callback" ]
250
+
251
+ millisats = amount * 1000
252
+ payquery = lnurlpay + "?amount=" + str (millisats )
253
+
254
+ # get bech32-serialized lightning invoice
255
+ pr_dict = await get_url (self ._session , path = payquery , headers = self ._invoice_headers )
256
+ # check keys returned for status
257
+ if "status" in pr_dict :
258
+ reason = pr_dict ["reason" ]
259
+ return reason
260
+ elif "pr" in pr_dict :
261
+ bolt11 = pr_dict ["pr" ]
262
+ return bolt11
263
+
264
+
265
+
266
+ # from LNURLPay link
267
+ async def get_bolt11_from_lnurlp (self , lnurlp_string : str , amount : int ):
268
+ """
269
+ fail state
270
+ {'reason': 'Amount not between min_sendable and max_sendable', 'status': 'ERROR'}
271
+
272
+ success state
273
+ {'pr': 'lnbc1......azgfe0',
274
+ 'routes': [], 'successAction': {'description': 'Thanks love for the lightning!',
275
+ 'tag': 'url', 'url': 'https:/.......'}}
276
+ """
277
+ try :
278
+ lnurl = Lnurl (lnurlp_string )
279
+ purl = lnurl .url
280
+ return await self .get_bolt11_from_payurl (purl , amount )
281
+
282
+ except Exception as e :
283
+ print ("Exception as: " , str (e ))
284
+ return e
285
+
286
+
245
287
# from lnaddress
246
288
async def get_bolt11 (self , email : str , amount : int ):
247
289
"""
@@ -255,25 +297,8 @@ async def get_bolt11(self, email: str, amount: int):
255
297
"""
256
298
try :
257
299
purl = self .get_payurl (email )
258
- json_content = await get_url (self ._session , path = purl , headers = self ._invoice_headers )
259
- # res = requests.get(purl)
260
- lnurlpay = json_content ["callback" ]
261
-
262
- millisats = amount * 1000
263
- payquery = lnurlpay + "?amount=" + str (millisats )
264
-
265
- # get bech32-serialized lightning invoice
266
- # pr_dict = requests.get(payquery)
267
- pr_dict = await get_url (self ._session , path = payquery , headers = self ._invoice_headers )
268
- # check keys returned for status
269
- if "status" in pr_dict :
270
- reason = pr_dict ["reason" ]
271
- return reason
272
- elif "pr" in pr_dict :
273
- bolt11 = pr_dict ["pr" ]
274
- return bolt11
300
+ return await self .get_bolt11_from_payurl (purl , amount )
301
+
275
302
except Exception as e :
276
303
print ("Exception as: " , str (e ))
277
- return e
278
-
279
- # get lnbits pay_id from lightning address if its a lnpayurl
304
+ return e
0 commit comments