SatSale

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

lightning_address.py (2635B)


      1 from flask import request
      2 from flask_restx import Resource
      3 import hashlib
      4 import logging
      5 
      6 min_sats = 10 ** 2
      7 max_sats = 10 ** 6
      8 
      9 # Following https://github.com/andrerfneves/lightning-address/blob/master/DIY.md
     10 
     11 
     12 def add_ln_address_decorators(app, api, node):
     13     description = node.config['lightning_address_comment']
     14     address = node.config['lightning_address']
     15     if description is None:
     16         description = "Thank you for your support <3"
     17 
     18     metadata = '[["text/plain", "{}"], ["text/identifier", "{}"]]'.format(
     19         description, address.split("@")[0]
     20     )
     21 
     22     class get_ln_address(Resource):
     23         def get(self):
     24             try:
     25                 logging.info(
     26                     "Someone requested our ln address: {}!".format(
     27                         address
     28                     )
     29                 )
     30                 resp = {
     31                     "callback": "https://{}/lnaddr".format(
     32                         address.split("@")[1]
     33                     ),
     34                     "maxSendable": max_sats * 10 ** 3,
     35                     "minSendable": min_sats * 10 ** 3,
     36                     "metadata": metadata,
     37                     "tag": "payRequest",
     38                 }
     39                 return resp
     40 
     41             except Exception as e:
     42                 logging.error(e)
     43                 return {"status": "ERROR", "reason": e}
     44 
     45     class init_ln_addr_payment(Resource):
     46         def get(self):
     47             if request.args.get("amount") is None:
     48                 return {
     49                     "status": "ERROR",
     50                     "reason": "You need to request an ?amount=MSATS",
     51                 }
     52 
     53             amount_msats = int(request.args.get("amount"))
     54             amount_btc = amount_msats / 10 ** (3 + 8)
     55 
     56             logging.info(
     57                 "Received payment request from ln address for {} msats...".format(
     58                     amount_msats
     59                 )
     60             )
     61 
     62             description_hash = hashlib.sha256(metadata.encode()).digest()
     63 
     64             try:
     65                 invoice, _ = node.create_lnd_invoice(
     66                     amount_btc,
     67                     memo="lightning address payment",
     68                     description_hash=description_hash,
     69                 )
     70                 logging.info("Responding with invoice {}".format(invoice))
     71                 return {"pr": invoice, "routes": []}
     72             except Exception as e:
     73                 logging.error(e)
     74                 return {"status": "ERROR", "reason": e}
     75 
     76     api.add_resource(
     77         get_ln_address,
     78         "/.well-known/lnurlp/{}".format(address.split("@")[0]),
     79     )
     80     api.add_resource(init_ln_addr_payment, "/lnaddr")
     81     return