2021-08-12 03:19:27 +02:00
|
|
|
from bech32 import bech32_decode, bech32_encode, convertbits
|
2021-07-31 02:01:19 +02:00
|
|
|
|
|
|
|
|
|
|
|
def decode(lnurl: str) -> str:
|
|
|
|
hrp, data = bech32_decode(lnurl)
|
2023-01-21 13:07:19 +01:00
|
|
|
assert hrp
|
2021-08-12 03:19:27 +02:00
|
|
|
assert data
|
2021-07-31 02:01:19 +02:00
|
|
|
bech32_data = convertbits(data, 5, 8, False)
|
2021-08-12 03:19:27 +02:00
|
|
|
assert bech32_data
|
2022-12-29 16:50:05 +01:00
|
|
|
return bytes(bech32_data).decode()
|
2021-08-12 03:19:27 +02:00
|
|
|
|
|
|
|
|
|
|
|
def encode(url: str) -> str:
|
2022-12-29 16:50:05 +01:00
|
|
|
bech32_data = convertbits(url.encode(), 8, 5, True)
|
2021-08-12 03:19:27 +02:00
|
|
|
assert bech32_data
|
|
|
|
lnurl = bech32_encode("lnurl", bech32_data)
|
|
|
|
return lnurl.upper()
|