Skip to content

Instantly share code, notes, and snippets.

@fiatjaf
Last active December 26, 2019 22:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fiatjaf/468d8ec581bc498664cafeef755c02ff to your computer and use it in GitHub Desktop.
Save fiatjaf/468d8ec581bc498664cafeef755c02ff to your computer and use it in GitHub Desktop.
lnurl snippets
import hashlib
import json
from flask import Flask, jsonify, request
app = Flask(__name__)
BASE_URL = 'https://myservice.com'
@app.route('/lnurl-one')
def lnurl_one():
return jsonify({
'tag': 'payRequest',
'callback': BASE_URL + '/lnurl-two',
'metadata': do_metadata(question),
'minSendable': 1000,
'maxSendable': 1000,
})
@app.route('/lnurl-two')
def lnurl_two(i):
description_hash = hashlib.sha256(do_metadata()).hexdigest()
return jsonify({
'pr': make_bolt11(description_hash),
'routes': [],
'successAction': None
})
def do_metadata():
return json.dumps([['text/plain', 'something']])
def make_bolt11(description_hash):
pass
+----------------------------+ +--------------------+
|server | |wallet |
+----------------------------+ +--------------------+
is listening at
https://x.com/.
user visits site,
has some balance to
withdraw.
generates a secret URL
specific to the user,
say, x.com/bob/withdraw?
secret=123abc456xyz
encodes that as bech32
with human-readable-prefix
`lnurl`.
displays it as a QR code.
scans the QR code,
decodes the bech32,
calls the URL.
when receiving a
+--------------------+ call at /bob/withdraw
|if something is | checks the given secret
|wrong, the server | query string.
|can just return |
| | if everything is ok and
|{ | this is really bob, checks
| "status": "ERROR",| bob's balance.
| "reason": "..." |
|} | bob's balance is 23 sat.
+--------------------+
returns an answer like
+--------------------+ {
|if the server wants | "defaultDescription": "withdraw from x.com",
|the user to withdraw| "minWithdrawable": 0,
|a fixed amount, | "maxWithdrawable": 23,
|make min and max | "tag": "withdrawRequest",
|equal. | "k1": "123abc456xyz",
+--------------------+ "callback": "https://x.com/bob/withdraw/fulfill"
}
using the received params,
the wallet shows a prefilled
invoice generation screen.
user selects the amount and
confirms.
the wallet generates the
invoice and calls
https://x.com/bob/withdraw/
fulfill?k1=123abc456xyz&
pr=lnbc...
the wallet is now ready to
receive the payment.
+--------------------+ upon receiving the bolt11
|if something is | invoice as the query string
|wrong, the server | on /bob/withdraw/fulfill,
|can just return | checks "k1" for the secret.
| |
|{ | if everything is ok, pays
| "status": "ERROR",| the invoice.
| "reason": "..." |
|} | returns {"status": "OK"}
+--------------------+
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment