Skip to main content

LNURL implementation for Python.

Project description

LNURL implementation for Python

travis-badge codecov-badge pypi-badge pypi-versions license-badge

A collection of helpers for building LNURL support into wallets and services.

Basic usage

>>> import lnurl
>>> lnurl.encode('https://service.io/?q=3fc3645b439ce8e7')
Lnurl('LNURL1DP68GURN8GHJ7UM9WFMXJCM99E5K7TELWY7NXENRXVMRGDTZXSENJCM98PJNWXQ96S9', url=Url('https://service.io/?q=3fc3645b439ce8e7', scheme='https', host='service.io', tld='io', host_type='domain', path='/', query='q=3fc3645b439ce8e7'))
>>> lnurl.decode('LNURL1DP68GURN8GHJ7UM9WFMXJCM99E5K7TELWY7NXENRXVMRGDTZXSENJCM98PJNWXQ96S9')
Url('https://service.io/?q=3fc3645b439ce8e7', scheme='https', host='service.io', tld='io', host_type='domain', path='/', query='q=3fc3645b439ce8e7')

The Lnurl object wraps a bech32 LNURL to provide some extra utilities.

from lnurl import Lnurl

lnurl = Lnurl('LNURL1DP68GURN8GHJ7UM9WFMXJCM99E5K7TELWY7NXENRXVMRGDTZXSENJCM98PJNWXQ96S9')
lnurl.bech32  # 'LNURL1DP68GURN8GHJ7UM9WFMXJCM99E5K7TELWY7NXENRXVMRGDTZXSENJCM98PJNWXQ96S9'
lnurl.bech32.hrp  # 'lnurl'
lnurl.url  # 'https://service.io/?q=3fc3645b439ce8e7'
lnurl.url.host  # 'service.io'
lnurl.url.base  # 'https://service.io/'
lnurl.url.query  # 'q=3fc3645b439ce8e7'
lnurl.url.query_params  # {'q': '3fc3645b439ce8e7'}

Parsing LNURL responses

You can use a LnurlResponse to wrap responses you get from a LNURL. The different types of responses defined in the LNURL spec have a different model with different properties (see models.py):

import requests

from lnurl import Lnurl, LnurlResponse

lnurl = Lnurl('LNURL1DP68GURN8GHJ7MRWW4EXCTNZD9NHXATW9EU8J730D3H82UNV94MKJARGV3EXZAELWDJHXUMFDAHR6WFHXQERSVPCA649RV')
r = requests.get(lnurl.url)

res = LnurlResponse.from_dict(r.json())  # LnurlPayResponse
res.ok  # bool
res.max_sendable  # int
res.max_sats  # int
res.callback.base  # str
res.callback.query_params # dict
res.metadata  # str
res.metadata.list()  # list
res.metadata.text  # str
res.metadata.images  # list

If you have already requests installed, you can also use the .handle() function directly. It will return the appropriate response for a LNURL.

>>> import lnurl
>>> lnurl.handle('lightning:LNURL1DP68GURN8GHJ7MRWW4EXCTNZD9NHXATW9EU8J730D3H82UNV94CXZ7FLWDJHXUMFDAHR6V33XCUNSVE38QV6UF')
LnurlPayResponse(tag='payRequest', callback=Url('https://lnurl.bigsun.xyz/lnurl-pay/callback/2169831', scheme='https', host='lnurl.bigsun.xyz', tld='xyz', host_type='domain', path='/lnurl-pay/callback/2169831'), min_sendable=3000, max_sendable=6000, metadata=LnurlPayMetadata('[["text/plain","rJcIdiekFE cTLynjPIqzum"]]'))

Building your own LNURL responses

For LNURL services, the lnurl package can be used to build valid responses.

from lnurl import LnurlWithdrawResponse

res = LnurlWithdrawResponse(
    callback='https://lnurl.bigsun.xyz/lnurl-withdraw/callback/9702808',
    k1='38d304051c1b76dcd8c5ee17ee15ff0ebc02090c0afbc6c98100adfa3f920874',
    min_withdrawable=551000,
    max_withdrawable=551000,
    default_description='sample withdraw',
)
res.json()  # str
res.dict()  # dict

All responses are pydantic models, so the information you provide will be validated and you have access to .json() and .dict() methods to export the data.

Data is exported using :camel: camelCase keys by default, as per spec. You can also use camelCases when you parse the data, and it will be converted to snake_case to make your Python code nicer.

If you want to export the data using :snake: snake_case (in your Python code, for example), you can change the by_alias parameter: res.dict(by_alias=False) (it is True by default).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lnurl-0.3.1.tar.gz (8.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

lnurl-0.3.1-py3-none-any.whl (9.6 kB view details)

Uploaded Python 3

File details

Details for the file lnurl-0.3.1.tar.gz.

File metadata

  • Download URL: lnurl-0.3.1.tar.gz
  • Upload date:
  • Size: 8.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.7.4

File hashes

Hashes for lnurl-0.3.1.tar.gz
Algorithm Hash digest
SHA256 62e63c25089299970b15a9785dc694fd6f1561b1af7782e335aec8febdc56ed4
MD5 aad4ecc9963cd35cb1681f923c3d9eae
BLAKE2b-256 8684de48b2ba8cd9bea3a2fbb07abb8404ee7b15e548c3a81445e17dbcf4c449

See more details on using hashes here.

File details

Details for the file lnurl-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: lnurl-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 9.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.7.4

File hashes

Hashes for lnurl-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7ee67c5f0ba4a38f4d469328eb239449a3c7667fa026683dc4b4f0dc62e8ed61
MD5 6165bea73a8be0743d73f877e19eceff
BLAKE2b-256 f96c3f5e6fce4d3f2238ed44697e06d60718e50d637c12c5bea64aac4d501a90

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page