-
Notifications
You must be signed in to change notification settings - Fork 1
/
lnurlstep2.mjs
108 lines (94 loc) · 3.06 KB
/
lnurlstep2.mjs
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
import Database from 'better-sqlite3';
import crypto from "crypto";
import grpc from '@grpc/grpc-js';
import protoLoader from '@grpc/proto-loader';
import config from 'config';
const appDb = new Database(config.get("applicationDatabase"));
var serviceUrl = config.get("serviceUrl");
if(serviceUrl ==""){
serviceUrl = process.env.APP_HIDDEN_SERVICE;
}
function lnurlstep2(id,comment,res,lndCredentials){
try{
const dataDb = appDb.prepare("SELECT * FROM invoice WHERE id=?;").get(id);
if(dataDb!=undefined){
var amount = dataDb.value;
var expiry = dataDb.expiry;
var memo = dataDb.memo;
var currency = dataDb.currency;
const loaderOptions = {
keepCase: true,
longs: String,
enums: String,
defaults: true,
oneofs: true
};
const packageDefinition = protoLoader.loadSync('lightning.proto', loaderOptions);
const lnrpc = grpc.loadPackageDefinition(packageDefinition).lnrpc;
const macaroon = lndCredentials.macaroon;
process.env.GRPC_SSL_CIPHER_SUITES = 'HIGH+ECDSA';
const lndCert = lndCredentials.cert;
const sslCreds = grpc.credentials.createSsl(lndCert);
const macaroonCreds = grpc.credentials.createFromMetadataGenerator(function(args, callback) {
let metadata = new grpc.Metadata();
metadata.add('macaroon', macaroon);
callback(null, metadata);
});
let creds = grpc.credentials.combineChannelCredentials(sslCreds, macaroonCreds);
let lightning = new lnrpc.Lightning(lndCredentials.lndEndpoint+":"+lndCredentials.lndPort, creds);
var completeUrl = serviceUrl+ "/v1/invoices";
var metadatastring = '[["text/plain", "'+memo+'"]]';
var hashedMemo = crypto.createHash('sha256').update(metadatastring).digest('base64');
let request = {
memo: memo,
value: amount/1000,
description_hash: hashedMemo,
expiry: expiry
};
lightning.addInvoice(request, function(err, response) {
if(response != undefined && response.r_hash != undefined && response.payment_request != undefined ){
var bolt11 = response.payment_request;
let hexHash = Buffer.from(response.r_hash, "base64").toString("hex");
const dataUpdate = appDb.prepare("UPDATE invoice SET status='OPEN', dateissued=CURRENT_TIMESTAMP, r_hash=?, comment=? WHERE id=?;").run(hexHash,sanitize(comment),id);
let lnurl = {
pr: bolt11,
routes: []
}
res.setHeader('Content-Type', 'application/json');
res.send(lnurl);
}
else{
res.status(500).json({ error: 'LND error' });
}
});
}
else{
let lnurl = {
status: "ERROR",
reason: "unknown LNURL"
}
res.setHeader('Content-Type', 'application/json');
res.send(lnurl);
}
}
catch(err){
console.error(err);
}
}
function sanitize(strIn) {
var strOut = "";
if(strIn != undefined && strIn != ""){
const map = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": ''',
"/": '/',
};
const reg = /[&<>"'/]/ig;
strOut= strIn.replace(reg, (match)=>(map[match]));
}
return strOut;
}
export { lnurlstep2 };