|
| 1 | +import json |
| 2 | +import os |
| 3 | +from dataclasses import dataclass |
| 4 | +from typing import Any |
| 5 | +from uuid import UUID |
| 6 | + |
| 7 | +from flask import Flask, abort, request |
| 8 | +from flask.typing import ResponseReturnValue |
| 9 | + |
| 10 | +from lightspark.lightspark_client import LightsparkSyncClient |
| 11 | + |
| 12 | +app = Flask(__name__) |
| 13 | + |
| 14 | +################################################################# |
| 15 | +## MODIFY THOSE VARIABLES BEFORE RUNNING THE EXAMPLE |
| 16 | +################################################################# |
| 17 | +## |
| 18 | +## We defined those variables as environment variables, but if you are just |
| 19 | +## running the example locally, feel free to just set the values in Python. |
| 20 | +## |
| 21 | +## First, initialize your client ID and client secret. Those are available |
| 22 | +## in your account at https://app.lightspark.com/api_config |
| 23 | +## |
| 24 | +## export LIGHTSPARK_API_TOKEN_CLIENT_ID=<client_id> |
| 25 | +## export LIGHTSPARK_API_TOKEN_CLIENT_SECRET=<client_secret> |
| 26 | +API_TOKEN_CLIENT_ID = os.environ.get("LIGHTSPARK_API_TOKEN_CLIENT_ID") |
| 27 | +API_TOKEN_CLIENT_SECRET = os.environ.get("LIGHTSPARK_API_TOKEN_CLIENT_SECRET") |
| 28 | +## |
| 29 | +## This example also assumes you already know your node UUID. Generally, an LNURL API would serve |
| 30 | +## many different usernames while maintaining some internal mapping from username to node UUID. For |
| 31 | +## simplicity, this example works with a single username and node UUID. |
| 32 | +## |
| 33 | +## export LIGHTSPARK_LNURL_NODE_UUID=0187c4d6-704b-f96b-0000-a2e8145bc1f9 |
| 34 | +LNURL_NODE_UUID = os.environ.get("LIGHTSPARK_LNURL_NODE_UUID") |
| 35 | +LNURL_USERNAME = os.environ.get("LIGHTSPARK_LNURL_USERNAME", "ls_test") |
| 36 | +## |
| 37 | +## To run the webserver, run this command from the root of the SDK folder: |
| 38 | +## $ pipenv run flask --app examples.flask_lnurl_server run |
| 39 | +## |
| 40 | +## By default the server will run on port 5000. You can make a request to the API through |
| 41 | +## curl to make sure the server is working properly (replace ls_test with the username you have |
| 42 | +## configured): |
| 43 | +## |
| 44 | +## $ curl http://127.0.0.1:7120/.well-known/lnurlp/ls_test |
| 45 | +################################################################# |
| 46 | + |
| 47 | +assert API_TOKEN_CLIENT_ID |
| 48 | +assert API_TOKEN_CLIENT_SECRET |
| 49 | +assert LNURL_NODE_UUID |
| 50 | +assert LNURL_USERNAME |
| 51 | + |
| 52 | + |
| 53 | +@dataclass |
| 54 | +class User: |
| 55 | + uuid: UUID |
| 56 | + username: str |
| 57 | + node_uuid: str |
| 58 | + |
| 59 | + |
| 60 | +LS_TEST_USER = User( |
| 61 | + # Static UUID so that callback URLs are always the same. |
| 62 | + UUID("4b41ae03-01b8-4974-8d26-26a35d28851b"), |
| 63 | + LNURL_USERNAME, |
| 64 | + f"LightsparkNode:{LNURL_NODE_UUID}", |
| 65 | +) |
| 66 | + |
| 67 | + |
| 68 | +def _generate_callback_for_user(user: User) -> str: |
| 69 | + return f"{request.url_root}api/lnurl/payreq/{user.uuid}" |
| 70 | + |
| 71 | + |
| 72 | +def _generate_metadata_for_user(user: User) -> str: |
| 73 | + return json.dumps( |
| 74 | + [ |
| 75 | + ["text/plain", f"Pay to domain.org user {user.username}"], |
| 76 | + ["text/identifier", f"{user.username}@domain.org"], |
| 77 | + ] |
| 78 | + ) |
| 79 | + |
| 80 | + |
| 81 | +@app.route("/.well-known/lnurlp/<username>") |
| 82 | +def well_known_lnurlp_username(username: str) -> ResponseReturnValue: |
| 83 | + if username != LS_TEST_USER.username: |
| 84 | + abort(404, description=f"User not found: {username}") |
| 85 | + |
| 86 | + user = LS_TEST_USER |
| 87 | + callback = _generate_callback_for_user(user) |
| 88 | + metadata = _generate_metadata_for_user(user) |
| 89 | + |
| 90 | + return { |
| 91 | + "callback": callback, |
| 92 | + "maxSendable": 10_000_000, |
| 93 | + "minSendable": 1_000, |
| 94 | + "metadata": metadata, |
| 95 | + "tag": "payRequest", |
| 96 | + } |
| 97 | + |
| 98 | + |
| 99 | +@app.route("/api/lnurl/payreq/<uuid>") |
| 100 | +def lnurl_payreq(uuid: str) -> ResponseReturnValue: |
| 101 | + if uuid != str(LS_TEST_USER.uuid): |
| 102 | + abort(404, description=f"User not found: {uuid}") |
| 103 | + |
| 104 | + user = LS_TEST_USER |
| 105 | + amount_msats = int(request.args["amount"]) |
| 106 | + |
| 107 | + ls_client = LightsparkSyncClient( |
| 108 | + api_token_client_id=API_TOKEN_CLIENT_ID, |
| 109 | + api_token_client_secret=API_TOKEN_CLIENT_SECRET, |
| 110 | + ) |
| 111 | + |
| 112 | + invoice = ls_client.create_lnurl_invoice( |
| 113 | + user.node_uuid, |
| 114 | + amount_msats, |
| 115 | + _generate_metadata_for_user(user), |
| 116 | + ) |
| 117 | + |
| 118 | + return {"pr": invoice.data.encoded_payment_request, "routes": []} |
| 119 | + |
| 120 | + |
| 121 | +@app.errorhandler(404) |
| 122 | +def error_not_found(e: Any) -> ResponseReturnValue: |
| 123 | + return {"status": "ERROR", "reason": e.description}, 404 |
0 commit comments