-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlnaddress.js
86 lines (67 loc) · 2.19 KB
/
lnaddress.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
const express = require('express')
const crypto = require('crypto');
const DEFAULT_DOMAIN = process.env.DEFAULT_DOMAIN
const app = express()
const addInvoice = async ({ amount, description_hash, description, lnd }) => {
try {
return new Promise((resolve, reject) => {
lnd.addInvoice({ value: amount / 1000, description_hash: description ? undefined : description_hash, memo: description }, (err, response) => {
if (err) reject(err);
else {
console.log('Invoice created successfully!');
console.log('Payment Request:', response.payment_request);
resolve(response.payment_request)
};
});
});
} catch (error) {
console.error('Error creating invoice:', error);
}
}
const sha256 = (input) => {
const hash = crypto.createHash('sha256');
hash.update(input);
return hash.digest('base64');
}
const startLnAddressService = ({ port, lnd, commentsMap }) => {
app.get(`/.well-known/lnurlp/:alias`, async (req, res) => {
console.log(`Request received:`)
console.log({ query: req.query, headers: req.headers, url: req.url })
const alias = req.params.alias;
if (!alias) {
return res.status(400).json({ error: "Alias is required" })
}
const metadata = JSON.stringify([["text/plain", `Sats for ${alias}`]])
// comment is meant to be made by user making a payment
// description is made by my services
const { amount, comment, description } = req.query
if (amount) {
const paymentRequest = await addInvoice({ amount, description_hash: sha256(metadata), description, lnd })
if (comment) {
commentsMap.set(paymentRequest, comment)
}
return res.json({
pr: paymentRequest,
routes: [],
successAction: {
tag: "message",
message: "Thank you so much!"
},
})
}
res.json({
callback: `https://${DEFAULT_DOMAIN}/.well-known/lnurlp/${alias}`,
tag: "payRequest",
maxSendable: 1000000000,
minSendable: 1000,
commentAllowed: 200,
metadata,
})
})
app.listen(port, () => {
console.log(`Node LND listening on port ${port}`)
})
}
module.exports = {
startLnAddressService
}