-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
92 lines (72 loc) · 3.31 KB
/
app.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
/*
* Copyright (c) 2019 LNBIG.com
* All rights reserved.
*/
// Должен быть первым - загружает переменные
require('dotenv').config()
process.umask(0o77);
const nodeStorage = require('./global/nodeStorage')
const billingNode = require('./global/billingNode')
const chat = require('./global/chat')
const clientStorage = require('./global/clientStorage')
const Koa = require('koa');
const Router = require('koa-router');
const bech32 = require('bech32');
const websockify = require('koa-websocket');
const debug = require('debug')('lnbig:app')
const port = process.env.LISTEN_PORT || 3000
const app = websockify(new Koa())
const httpRouter = new Router()
const wsRouter = new Router()
if (!process.env.BASE_STORAGE_DIR)
throw Error('Not defined BASE_STORAGE_DIR env!')
// Websocket router
require('./routes/WebSocket')(wsRouter, '/ws')
// GET запрос callback для забирания средств, которые причитаются за что либо
require('./routes/LnurlWithdraw')(httpRouter, '/lnurl-withdraw');
// GET запрос для фомирования JSON структуры для открытия канала
require('./routes/GetLnurl')(httpRouter, '/lnurl');
// GET запрос callback для открытия канала
require('./routes/OpenChannel')(httpRouter, '/oc');
// шаг 2 - получаем 33-байтный nodeid, проверяем его по нодам на лимиты и после либо создаём, либо отказываем
// Пункт 6 спецификации, если ошибка: { status: "ERROR", reason: "Second level nonce not found" }
// reason может быть любой
if (process.env.CRYPT_PASSWORD) {
// The password for crypted macaroon files in env settings (.env file for example)
startServer(process.env.CRYPT_PASSWORD)
} else {
// Or prompt the password from terminal
var read = require("read");
read(
{
prompt: 'Password: ',
silent: true,
replace: '*',
terminal: true
},
(error, password) => {
if (error)
throw new Error(error);
startServer(password);
}
)
}
async function startServer(password) {
// To create object for node storage
// load node storage data included crypted macaroon files, and decrypt macaroon files by password. After the password to be cleaned from memory
await nodeStorage.init(require('./global/nodesInfo'), password);
await billingNode.init(password);
// To connect to nodes
await nodeStorage.connect()
await billingNode.connect()
await clientStorage.init()
await chat.load()
// It's test of bech32 decryption
debug('The test of bech32: %s', String.fromCharCode.apply(String, bech32.fromWords(bech32.decode('LNURL1DP68GURN8GHJ7UM9WFMXJCM99E3K7MF0V9CXJ0M385EKVCENXC6R2C35XVUKXEFCV5MKVV34X5EKZD3EV56NYD3HXQURZEPEXEJXXEPNXSCRVWFNV9NXZCN9XQ6XYEFHVGCXXCMYXYMNSERXFQ5FNS', 1023).words)))
// The registration of HTTP routes
app.use(httpRouter.routes()).use(httpRouter.allowedMethods());
// The registration of WebSocket routes
app.ws.use(wsRouter.routes()).use(wsRouter.allowedMethods());
// Start application...
app.listen(port, () => console.log(`Example app listening on port ${port}!`))
}