Created
September 17, 2021 13:00
-
-
Save takinbo/db89fab5ed08b4a1631be4a234c5be54 to your computer and use it in GitHub Desktop.
sample implementation of lnurl-pay and lightning address
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const lnurl = require('lnurl') | |
const { v4: uuid4 } = require('uuid') | |
const express = require('express') | |
const { exec } = require('child_process') | |
const crypto = require('crypto') | |
const app = express() | |
const port = 7000 | |
const users = ['godwin', 'bernard'] | |
app.get('/', (req, res) => { | |
res.send('Hello World!') | |
}) | |
app.get('/user/:userid', (req, res) => { | |
const url = lnurl.encode(`https://${req.hostname}/lnurlp?q=${req.params.userid}`) | |
console.log(url) | |
res.send(`<img src="https://chart.googleapis.com/chart?chs=500x500&cht=qr&chl=${url}" alt="${url}">`) | |
}) | |
app.get('/lnurlp', (req, res) => { | |
let response = {} | |
if (users.includes(req.query.q) == true) { | |
response = { | |
callback: `https://${req.hostname}/pay?q=${req.query.q}`, | |
tag: 'payRequest', | |
minSendable: 10000, | |
maxSendable: 10000, | |
metadata: `[["text/plain", "send sats to ${req.query.q}"]]` | |
} | |
} else { | |
response = { | |
'status': 'ERROR', | |
'reason': 'user not found' | |
} | |
} | |
res.json(response) | |
}) | |
app.get('/.well-known/lnurlp/:userid', (req, res) => { | |
let response = {} | |
if (users.includes(req.params.userid) == true) { | |
response = { | |
callback: `https://${req.hostname}/pay?q=${req.params.userid}`, | |
tag: 'payRequest', | |
minSendable: 1000, | |
maxSendable: 5000000, | |
metadata: `[["text/plain", "send sats to ${req.params.userid}"]]` | |
} | |
} else { | |
response = { | |
'status': 'ERROR', | |
'reason': 'user not found' | |
} | |
} | |
res.json(response) | |
}) | |
app.get('/pay', (req, res) => { | |
let recipient = req.query.q | |
let amount_msats = parseInt(req.query.amount) | |
let amount_sats = amount_msats / 1000 | |
let hash = crypto.createHash('sha256') | |
hash.update(`[["text/plain", "send sats to ${recipient}"]]`) | |
let desc = hash.digest('hex') | |
console.log(`generating invoice of ${amount_msats}msats to ${recipient}`) | |
exec(`lncli addinvoice --amt=${amount_sats} --description_hash=${desc}`, (err, stdout, sterr) => { | |
let invoice = JSON.parse(stdout) | |
console.log(`hash: ${invoice.r_hash} bolt11: ${invoice.payment_request}`) | |
response = { | |
pr: invoice.payment_request, | |
routes: [] | |
} | |
res.json(response) | |
}) | |
}) | |
app.listen(port, () => { | |
console.log(`Server listening on locahost:${port}`) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment