1
0

lnurl: add encode_lnurl() for console usage, fix tests

This commit is contained in:
SomberNight
2023-06-23 16:01:03 +00:00
parent 759eaf1cf5
commit 8075c0d02a
2 changed files with 27 additions and 3 deletions

View File

@@ -11,8 +11,9 @@ import urllib.parse
import aiohttp.client_exceptions
from aiohttp import ClientResponse
from electrum.segwit_addr import bech32_decode, Encoding, convertbits
from electrum.lnaddr import LnDecodeException
from electrum import segwit_addr
from electrum.segwit_addr import bech32_decode, Encoding, convertbits, bech32_encode
from electrum.lnaddr import LnDecodeException, LnEncodeException
from electrum.network import Network
from electrum.logging import get_logger
@@ -45,6 +46,19 @@ def decode_lnurl(lnurl: str) -> str:
return url
def encode_lnurl(url: str) -> str:
"""Encode url to bech32 lnurl string."""
try:
url = url.encode("utf-8")
except UnicodeError as e:
raise LnEncodeException("invalid url") from e
bech32_data = convertbits(url, 8, 5, True)
assert bech32_data
lnurl = bech32_encode(
encoding=segwit_addr.Encoding.BECH32, hrp="lnurl", data=bech32_data)
return lnurl.upper()
def _is_url_safe_enough_for_lnurl(url: str) -> bool:
u = urllib.parse.urlparse(url)
if u.scheme.lower() == "https":