Skip to content

Commit

Permalink
Merge pull request #43 from FelixFrei/feature-36-new-function-to-pay-…
Browse files Browse the repository at this point in the history
…LNURLP

Feature #36 new function to get bolt11 from lnurlp
  • Loading branch information
bitkarrot authored Sep 11, 2023
2 parents 654efff + af25f1f commit 3f37141
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 21 deletions.
65 changes: 45 additions & 20 deletions pylnbits/user_wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging

from aiohttp.client import ClientSession
from lnurl import Lnurl

from pylnbits.utils import get_url, post_url

Expand Down Expand Up @@ -242,6 +243,47 @@ def get_payurl(self, email: str):
except Exception as e:
print("Exception, possibly malformed LN Address: " + str(e))

# generates bolt11 invoice from payurl and amount to pay
async def get_bolt11_from_payurl(self, purl: str, amount: int):
json_content = await get_url(self._session, path=purl, headers=self._invoice_headers)
lnurlpay = json_content["callback"]

millisats = amount * 1000
payquery = lnurlpay + "?amount=" + str(millisats)

# get bech32-serialized lightning invoice
pr_dict = await get_url(self._session, path=payquery, headers=self._invoice_headers)
# check keys returned for status
if "status" in pr_dict:
reason = pr_dict["reason"]
return reason
elif "pr" in pr_dict:
bolt11 = pr_dict["pr"]
return bolt11



# from LNURLPay link
async def get_bolt11_from_lnurlp(self, lnurlp_string: str, amount: int):
"""
fail state
{'reason': 'Amount not between min_sendable and max_sendable', 'status': 'ERROR'}
success state
{'pr': 'lnbc1......azgfe0',
'routes': [], 'successAction': {'description': 'Thanks love for the lightning!',
'tag': 'url', 'url': 'https:/.......'}}
"""
try:
lnurl = Lnurl(lnurlp_string)
purl = lnurl.url
return await self.get_bolt11_from_payurl(purl, amount)

except Exception as e:
print("Exception as: ", str(e))
return e


# from lnaddress
async def get_bolt11(self, email: str, amount: int):
"""
Expand All @@ -255,25 +297,8 @@ async def get_bolt11(self, email: str, amount: int):
"""
try:
purl = self.get_payurl(email)
json_content = await get_url(self._session, path=purl, headers=self._invoice_headers)
# res = requests.get(purl)
lnurlpay = json_content["callback"]

millisats = amount * 1000
payquery = lnurlpay + "?amount=" + str(millisats)

# get bech32-serialized lightning invoice
# pr_dict = requests.get(payquery)
pr_dict = await get_url(self._session, path=payquery, headers=self._invoice_headers)
# check keys returned for status
if "status" in pr_dict:
reason = pr_dict["reason"]
return reason
elif "pr" in pr_dict:
bolt11 = pr_dict["pr"]
return bolt11
return await self.get_bolt11_from_payurl(purl, amount)

except Exception as e:
print("Exception as: ", str(e))
return e

# get lnbits pay_id from lightning address if its a lnpayurl
return e
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ mkdocs==1.5.2
mkdocs-autorefs==0.5.0
mkdocs-material==9.2.8
mkdocs-material-extensions==1.1.1
lnurl==0.4.0
mkdocstrings==0.23.0
mkdocstrings-python==1.6.2
mkdocstrings-python==1.6.2

0 comments on commit 3f37141

Please sign in to comment.