-
Notifications
You must be signed in to change notification settings - Fork 2
/
fourohtwo.js
204 lines (177 loc) · 5.16 KB
/
fourohtwo.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
const path = require("path");
const express = require("express");
const bodyParser = require("body-parser");
const cors = require("cors");
const { boltwall, TIME_CAVEAT_CONFIGS } = require("boltwall");
const nodeFetch = require("node-fetch");
// const passport = require('passport');
// const session = require('express-session');
// const LnurlAuth = require('passport-lnurl-auth');
require("dotenv").config();
class Lnd {
constructor(config) {
this.config = config;
}
getInfo() {
return this.request("GET", "/v1/getinfo", undefined, {});
}
sendPayment(args) {
return this.request("POST", "/v2/router/send", args, {}).then((res) => {
console.log(res);
return res.json();
});
}
makeInvoice(args) {
return this.request("POST", "/v1/invoices", {
memo: args.memo,
value: args.amount,
});
}
getAddress() {
return this.request("POST", "/v2/wallet/address/next", undefined, {});
}
getBlockchainBalance() {
return this.request("GET", "/v1/balance/blockchain", undefined, {});
}
async request(method, path, args, defaultValues) {
let body = null;
let query = "";
const headers = new nodeFetch.Headers();
headers.append("Accept", "application/json");
if (method === "POST") {
body = JSON.stringify(args);
headers.append("Content-Type", "application/json");
} else if (args !== undefined) {
query = `?`; //`?${stringify(args)}`;
}
if (this.config.macaroon) {
headers.append("Grpc-Metadata-macaroon", this.config.macaroon);
}
try {
const res = await nodeFetch(this.config.url + path + query, {
method,
headers,
body,
});
if (!res.ok) {
let errBody;
try {
errBody = await res.json();
if (!errBody.error) {
throw new Error();
}
} catch (err) {
throw {
statusText: res.statusText,
status: res.status,
};
}
console.log("errBody", errBody);
throw errBody;
}
let data = await res.json();
if (defaultValues) {
data = Object.assign(Object.assign({}, defaultValues), data);
}
return { data };
} catch (err) {
console.error(`API error calling ${method} ${path}`, err);
// Thrown errors must be JSON serializable, so include metadata if possible
if (err.code || err.status || !err.message) {
throw err;
}
throw err.message;
}
}
}
lnd = new Lnd({
url: process.env.LND_URL,
macaroon: process.env.LND_MACAROON_HEX,
});
lnd.getInfo().then(console.log);
const app = express();
const lsatRouter = express.Router();
const appRouter = express.Router();
appRouter.get("/", async function (req, res) {
const invoice = await lnd.makeInvoice({ amount: 100, memo: "a402" });
res.render("index", { invoice: invoice.data, headers: req.headers, user: req.user });
});
appRouter.post("/invoice", async function (req, res) {
const invoice = await lnd.makeInvoice({ amount: 100, memo: "a402" });
res.json({ payment_request: invoice.data.payment_request });
});
appRouter.get("/webamp", function (req, res) {
res.render("webamp", {});
});
// appRouter.get('/logout', function(req, res) {
// req.session.destroy();
// return res.redirect('/');
// });
// appRouter.get('/login',
// function(req, res, next) {
// console.log('request user', req.user);
// if (req.user) {
// // Already authenticated.
// return res.redirect('/');
// }
// next();
// },
// new LnurlAuth.Middleware({
// callbackUrl: 'https://regtest-alice.herokuapp.com/login',
// cancelUrl: 'https://regtest-alice.herokuapp.com/'
// })
// );
lsatRouter.get("/", function (req, res) {
res.json("yay, thanks");
});
lsatRouter.get("/files/:name", function (req, res) {
let options = {
root: path.join(__dirname, "public"),
dotfiles: "deny",
headers: {
"x-timestamp": Date.now(),
"x-sent": true,
},
};
const fileName = req.params.name;
res.sendFile(fileName, options);
});
// const map = {
// user: new Map(),
// };
// passport.serializeUser(function(user, done) {
// done(null, user.id);
// });
//
// passport.deserializeUser(function(id, done) {
// done(null, map.user.get(id) || null);
// });
app.set("views", path.join(__dirname, "views"));
app.set("view engine", "ejs");
app.use(express.static(path.join(__dirname, 'public')))
app.use(cors());
// app.use(session({
// secret: 'skjldsadiufhadiwewdkasdiuc2fdcui',
// resave: true,
// saveUninitialized: true,
// }));
// app.use(passport.initialize());
// app.use(passport.session());
// passport.use(new LnurlAuth.Strategy(function(linkingPublicKey, done) {
// let user = map.user.get(linkingPublicKey);
// if (!user) {
// user = { id: linkingPublicKey };
// map.user.set(linkingPublicKey, user);
// }
//
// console.log(user);
// done(null, user);
// }));
// app.use(passport.authenticate('lnurl-auth'));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use("/lsat", boltwall({ ...TIME_CAVEAT_CONFIGS, rate: 0.1 }), lsatRouter);
app.use("/", appRouter);
const port = process.env.PORT || 3030;
console.log(`Running on ${port}`);
app.listen(port);