This repository has been archived by the owner on Sep 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
lnplace.js
251 lines (222 loc) · 6.02 KB
/
lnplace.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
require('dotenv').config();
const {
LND_HOMEDIR,
LISTEN_PORT,
DB_NAME,
NODE_ENV,
} = process.env;
const grpc = require('grpc');
const fs = require('fs');
const helmet = require('helmet');
const logger = require('winston');
const express = require('express');
const bodyParser = require('body-parser');
const LND_UNAVAILABLE = {
status: 503,
message: 'LND on server is down',
};
const logDir = 'log';
if (!fs.existsSync(logDir)) {
fs.mkdirSync(logDir);
}
const lndCert = fs.readFileSync(`${LND_HOMEDIR}tls.cert`);
const credentials = grpc.credentials.createSsl(lndCert);
const lnrpcDescriptor = grpc.load('rpc.proto');
const lightning = new lnrpcDescriptor.lnrpc.Lightning('127.0.0.1:10009', credentials);
const adminMacaroon = fs.readFileSync(`${LND_HOMEDIR}admin.macaroon`);
const meta = new grpc.Metadata();
meta.add('macaroon', adminMacaroon.toString('hex'));
const { MongoClient } = require('mongodb');
const dbUrl = 'mongodb://127.0.0.1:27017';
const app = express();
app.use(express.static('public'));
app.use(helmet());
let db;
/**
* The current RGB values
*/
const colors = Array(...Array(256)).map(() => '#FFFFFF');
/**
* The pending invoices
*/
const invoices = {};
/**
* An array of clients listening to server side events
*/
const listeners = [];
/**
* Creates an index on r_hash for invoices if it doesn't exist.
* Fetches the most recent colors from the database.
*/
async function init() {
db.collection('colors').find().sort({
_id: -1,
}).toArray()
.then(async (dbColors) => {
for (let n = 0; n < dbColors.length; n += 1) {
const { _id, hex } = dbColors[n];
colors[_id] = hex;
}
});
}
/**
* Adds an invoice to lnd.
* @returns - A promise that resolves when the invoice is added
*/
function addInvoice() {
return new Promise((resolve, reject) => lightning.addInvoice({
value: 1,
memo: 'lnplace',
}, meta, (err, response) => {
if (err) {
if (err.code === 14) {
reject(LND_UNAVAILABLE);
} else {
const error = new Error(`Server error: ${err.message}`);
error.status = 500;
reject(error);
}
} else {
resolve(response);
}
}));
}
app.get('/colors', (req, res) => {
res.status(200).json({
colors,
});
});
app.get('/listen', (req, res) => {
res.writeHead(200, {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
Connection: 'keep-alive',
});
listeners.push(res);
});
app.post('/invoice', bodyParser.json(), (req, res) => {
logger.info(`invoice request: ${JSON.stringify(req.body)}`);
if (!req.body.color || !req.body.index) {
res.status(400).end();
} else {
// valid so far, check for and validate payment request
let responseBody;
addInvoice().then((response) => {
responseBody = {
payment_request: response.payment_request,
};
const invoice = {
index: req.body.index,
color: req.body.color,
r_hash: response.r_hash.toString('hex'),
value: 1,
};
invoices[invoice.r_hash] = invoice;
logger.verbose(`invoice added: ${JSON.stringify(responseBody)}`);
res.status(200).json(responseBody);
}).catch((err) => {
if (err.status) {
logger.error(err.message);
res.status(err.status).send(err.message);
} else {
logger.error(err);
res.status(400).send(err.message);
}
});
}
});
/**
* Send an event
* @param event - The event type to send
*/
function sendEvent(message) {
for (let n = 0; n < listeners.length; n += 1) {
const listener = listeners[n];
if (listener && !listener.finished) {
try {
listener.write(`data: ${message}\n\n`);
} catch (err) {
listeners[n] = undefined;
logger.error(err);
}
}
}
}
/**
* Updates a pixel and notifies all listening clients
* @param invoice - The invoice for the purchase
*/
async function purchasePixel(invoice) {
try {
const newVals = { _id: invoice.index, hex: invoice.color };
await db.collection('colors').replaceOne(
{ _id: invoice.index },
{ $set: newVals },
{ upsert: true },
);
colors[invoice.index] = invoice.color;
sendEvent(`${invoice.index} ${invoice.color}`);
} catch (err) {
logger.error(err);
}
}
/**
* Handler for updates on lightning invoices
* @param data - The data from the lightning invoice subscription
* @returns - A promise that resolves once the invoice is handled
* and updated, or undefined if the invoice was not settled
*/
async function invoiceHandler(data) {
if (data.settled) {
const r_hash = data.r_hash.toString('hex');
try {
const invoice = invoices[r_hash];
if (invoice) {
logger.info(`invoice settled: ${JSON.stringify(invoice)}`);
await purchasePixel(invoice);
}
} catch (err) {
logger.error(err);
throw err;
}
}
return false;
}
lightning.subscribeInvoices({}, meta).on('data', invoiceHandler).on('end', () => {
logger.warn('subscribeInvoices ended');
}).on('status', (status) => {
logger.debug(`subscribeInvoices status: ${JSON.stringify(status)}`);
})
.on('error', (error) => {
logger.error(`subscribeInvoices error: ${error}`);
});
MongoClient.connect(dbUrl).then((connection) => {
db = connection.db(DB_NAME);
return init();
}).then(() => {
// listen and write logs only when started directly
if (!module.parent) {
const tsFormat = () => (new Date()).toLocaleString();
logger.configure({
transports: [
new (logger.transports.Console)({
timestamp: tsFormat,
colorize: true,
}),
new (logger.transports.File)({
filename: `${logDir}/lnplace.log`,
timestamp: tsFormat,
}),
],
});
logger.level = NODE_ENV === 'development' ? 'debug' : 'info';
app.listen(LISTEN_PORT, () => {
logger.info(`App listening on port ${LISTEN_PORT}`);
});
} else {
// don't log at all if not started directly
logger.clear();
}
}).catch((err) => {
logger.error(`Error on initialization: ${err}`);
});