forked from jb55/lnurl-commando
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
138 lines (114 loc) · 2.79 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
const LNSocket = require('lnsocket')
const querystring = require('querystring')
const url = require('url')
const http = require('http')
const crypto = require('crypto')
const fs = require('fs/promises')
const path = require('path')
async function create_server(opts={})
{
if (opts.thumbnail) {
const image = await fs.readFile(opts.thumbnail)
opts.thumbnailType = path.extname(opts.thumbnail).slice(1).toLowerCase()
opts.thumbnail = image.toString('base64')
}
const server = http.createServer(handle_request.bind(null, opts))
return server
}
async function make_invoice(opts, amount)
{
let bolt11 = null
const ln = await LNSocket()
try {
ln.genkey()
await ln.connect_and_init(opts.nodeid, opts.host)
const res = await ln.rpc({
rune: opts.rune,
method: "invoice",
params: {
msatoshi: amount || "any",
description: opts.description,
label: crypto.randomUUID().toString(),
}
});
if (res.error && res.error.message)
throw new Error(res.error.message)
if (res.error)
throw new Error(res.error)
if (!(res.result && res.result.bolt11))
throw new Error(JSON.stringify(res))
bolt11 = res.result.bolt11
} catch (err) {
ln.destroy()
console.error(err)
throw err
}
ln.destroy()
return bolt11
}
function handle_static_payreq(opts, req, res)
{
let metadata = []
if (opts.description) {
metadata.push(["text/plain", opts.description])
}
if (opts.longDescription) {
metadata.push([`text/long-desc`, opts.longDescription])
}
if (opts.identifier) {
metadata.push(["text/identifier", opts.identifier])
}
if (opts.thumbnail) {
metadata.push([`image/${opts.thumbnailType};base64`, opts.thumbnail])
}
const resp = {
status: "OK",
tag: "payRequest",
minSendable: 1,
maxSendable: 10000000000,
callback: opts.callback,
metadata: JSON.stringify(metadata),
}
//NOSTR Zap support
if (opts.npub) {
resp.commentAllowed = 255
resp.allowsNostr = true
resp.nostrPubkey = opts.npub
}
res.statusCode = 200
res.write(JSON.stringify(resp))
res.end()
}
async function handle_payreq(amount, opts, req, res)
{
try {
const pr = await make_invoice(opts, amount)
const routes = []
const resp = JSON.stringify({pr, routes})
res.statusCode = 200
res.write(resp)
} catch (e) {
res.statusCode = 500
res.write(JSON.stringify({status: "ERROR", "reason": JSON.stringify(e)}))
}
res.end()
return
}
function handle_request(opts, req, res)
{
console.log("%s - %s", req.method, req.url)
if (req.method == "GET") {
const parsed = url.parse(req.url)
const qs = querystring.parse(parsed.query)
if (qs && qs.amount) {
handle_payreq(qs.amount, opts, req, res)
return
}
handle_static_payreq(opts, req, res)
return
}
res.statusCode = 404
res.end()
}
module.exports = create_server
// bin stuff (move this?)