forked from TrinityLabDAO/eth-enq-bridge-v1.0
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
249 lines (225 loc) · 7.65 KB
/
index.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
let express = require('express');
let app = express();
let cors = require('cors');
app.use(cors());
let bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.set('views', '../sitev2');
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'ejs');
//app.use(express.static('../sitev2'));
const logger = require('./logger');
const TxStatus = require('./TxStatus').TxStatus;
const config = require("./config.json");
const Service = require("./Service").Service;
let txStatus = new TxStatus(config);
let service = new Service();
let staticify = require('staticify')('../sitev2');
app.use(staticify.middleware);
app.locals = {
getVersionedPath: staticify.getVersionedPath
};
app.use(function (req, res, next) {
logger.debug(`Request ${req.headers['x-forwarded-for']} | ${req.connection.remoteAddress} ${req.method} \t ${req.url}`);
next();
});
app.use(function (err, req, res, next) {
return res.status(400).send({result : false, err : 1});
});
let auth = async function(req, res, next) {
logger.silly(`Auth attempt`);
let token = req.headers['x-session-token'];
if(!token)
return res.status(401).send({result: false});
let authRes = await service.auth(token, req.body.sign);
if(authRes)
next();
else return res.status(401).send({result: false});
};
let validate = async function(req, res, next) {
// let token = req.headers['x-session-token'];
// if(!token){
// return res.status(401).send({result: false});
// }
let eth_regexp = /^(0x)?[0-9a-fA-F]{40}$/i;
let hash_regexp = /^(0x)?[0-9a-fA-F]{64}$/i;
let enq_regexp = /^(02|03)[0-9a-fA-F]{64}$/i;
let data = req.body;
if(!enq_regexp.test(data.pubkey))
return res.status(400).send({result: false, msg : "pubkey format error"});
if(!data.hasOwnProperty('hash') && !data.hasOwnProperty('tx'))
return res.status(400).send({result: false, msg : "Data error"});
// TODO: eth_addr only with tx
// if(!(data.hasOwnProperty('eth_addr') && data.hasOwnProperty('tx')))
// return res.status(400).send({result: false, msg : "Data error"});
if(data.hasOwnProperty('eth_addr')){
if(!eth_regexp.test(data.eth_addr))
return res.status(400).send({result: false, msg : "Eth address error"});
return next();
}
if(data.hasOwnProperty('hash')){
if(!hash_regexp.test(data.hash))
return res.status(400).send({result: false, msg : "Hash error"});
return next();
}
else if(data.hasOwnProperty('tx')){
for (let prop of ['amount','from','nonce','sign','to']){
if(!data.tx.hasOwnProperty(prop))
return res.status(400).send({result: false, msg : "TX format error"});
}
return next();
}
};
app.get('/', function(req, res){
return res.render("index.ejs");
});
app.post('/login', async function (req, res) {
if(config.blacklist.includes(req.body.pubkey.toLowerCase())){
let msg = `compromised wallet login: ${req.body.pubkey}, ${req.headers['x-forwarded-for']} | ${req.connection.remoteAddress} ${ JSON.stringify(req.headers)}`;
logger.warn(msg);
}
let token = await service.login(req.body.pubkey, req.headers['user-agent']);
if(!token)
return res.send({result : false});
logger.debug(`Set token ${token} for pubkey ${req.body.pubkey}`);
return res.send({result : true, token : token});
});
app.get('/login', function(req, res) {
return res.render("index.ejs");
});
app.post('/logout', async function(req, res) {
let token = req.headers['x-session-token'];
if(!token)
return res.status(401).send({result: false});
logger.debug(`Logout ${token}`);
let result = await service.logout(token);
return res.status(200).send({result: result});
});
app.get('/logout', function(req, res) {
return res.render("index.ejs");
});
/** Token faucet
app.get('/dev/faucet', async function(req, res) {
try{
logger.debug(`Faucet request for addr ${req.query.addr}`);
let hash = await Utils.sendTokenTransaction(req.query.addr, 50 * 1e10);
logger.debug(`Hash: ${hash}`);
return res.send({result : true, hash : hash});
}
catch (err) {
logger.error(err);
return res.send({result : false, err: 1});
}
});
*/
app.get('/dev/compress', function(req, res) {
const key = service.compressKey(key);
return res.send(key);
});
app.get('/dev/uncompress', function(req, res) {
const key = service.uncompressKey(key);
return res.send(key);
});
/**
* Ask server for a message to CRAM
*/
app.post('/challenge', async function (req, res) {
let token = req.headers['x-session-token'];
if(!token)
return res.status(401).send({result: false});
let msg = await service.challenge(token);
if(msg)
return res.send({msg : msg});
else return res.status(401).send({result: false, msg: 'Not authorized'});
});
app.post('/getHistory', async function (req, res) {
let data = await service.getHistory(req.body.pubkey);
res.status(200).send(data);
});
app.get('/getConfig', async function (req, res) {
let cfg = {
nodeURL : config.nodeURL,
enq_techAddr : config.enq_techAddr,
eth_techAddr : config.eth_techAddr,
tokenAddr : config.eth.tokenAddr
};
res.status(200).send(cfg);
});
/** ERC -> ENQ swap */
app.post('/swap/erc/enq',
(config.authState ? auth : (req, res, next)=>{next()}),
validate,
async function (req, res) {
let body = req.body;
logger.info(`Swap ERC->ENQ for pubkey ${body.pubkey}, ETH hash is ${body.hash}`);
try{
let result = await service.swapERC_ENQ(body);
return res.status(200).send({result: result});
}
catch (err) {
logger.error(err);
return res.status(200).send({result: false});
}
});
/** ENQ -> ERC swap */
app.post('/swap/enq/erc',
(config.authState ? auth : (req, res, next)=>{next()}),
validate,
async function (req, res) {
let body = req.body;
logger.info(`Swap ENQ->ERC for pubkey ${req.body.pubkey}`);
let result = await service.swapENQ_ERC(body);
return res.status(200).send(result);
});
/** ERC -> BEP swap
app.post('/swap/erc/bep',
(config.authState ? auth : (req, res, next)=>{next()}),
validate,
async function (req, res) {
let body = req.body;
logger.info(`Swap ERC->BEP for pubkey ${body.eth_addr}`);
try{
let result = await service.swapERC_BEP(body);
return res.status(200).send({result: result});
}
catch (e) {
logger.error(e);
return res.status(200).send({result: false});
}
});
*/
/** BEP -> ENQ swap
app.post('/swap/bep/enq',
(config.authState ? auth : (req, res, next)=>{next()}),
validate,
async function (req, res) {
let body = req.body;
logger.info(`Swap BEP->ENQ for pubkey ${body.pubkey}, ETH hash is ${body.hash}`);
try{
let result = await service.swapBEP_ENQ(body);
return res.status(200).send({result: result});
}
catch (err) {
logger.error(err);
return res.status(200).send({result: false});
}
});
*/
/** ENQ -> BEP swap
app.post('/swap/enq/bep',
(config.authState ? auth : (req, res, next)=>{next()}),
validate,
async function (req, res) {
let body = req.body;
logger.info(`Swap ENQ->BEP for pubkey ${req.body.pubkey}`);
let result = await service.swapENQ_BEP(body);
return res.status(200).send(result);
});
*/
app.listen(config.port, function () {
logger.info(`Wallet is running on port ${config.port}!`);
});
app.use(function(req, res, next){
res.status(404).render('404.html', {title: "Sorry, page not found"});
});