forked from TrinityLabDAO/eth-enq-bridge-v1.0
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathService.js
262 lines (257 loc) · 9.09 KB
/
Service.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
252
253
254
255
256
257
258
259
260
261
262
const logger = require('./logger');
const DB = require('./db').DB;
const config = require("./config.json");
const Utils = require("./Utils.js");
let crypto = require('crypto');
const enecuum = require("./helpers/enecuum");
const Sender = require("./helpers/Sender").Sender;
const sender = new Sender(new DB({
host: config.dbhost,
user: config.dbuser,
database: config.dbname,
password: config.dbpass.toString()
}));
class Service{
constructor(){
this.db = new DB({
host: config.dbhost,
user: config.dbuser,
database: config.dbname,
password: config.dbpass.toString()
});
// let tx = Utils.randTx();
// logger.info(tx);
}
compressKey(key){
return Utils.compressKey(key);
}
uncompressKey(key){
return Utils.uncompressKey(key);
}
async getHistory(pubkey){
let data = await this.db.get_history(pubkey);
return data;
}
async swapERC_ENQ(body){
try {
//let info = await Utils.getTransactionExtInfo(body.hash);
/**
* Check:
* - contract check
* - techAddress check
* TODO: validate linkedAddr
*/
let db_data = {
pubkey : body.pubkey,
in_hash : body.hash,
in_addr : body.eth_addr,
out_addr : body.pubkey,
amount : body.amount,
status : 0,
date : Math.floor(new Date() / 1000),
type : Utils.swapTypes.erc_enq,
hold : 0
};
let res1 = await this.db.put_erc_tx([[body.hash, body.amount, 0]]);
let res2 = await this.db.put_history(db_data);
return true;
}
catch (err) {
logger.error(err.stack);
return false;
}
}
async swapERC_BEP(body){
try {
let info = await Utils.getTransactionExtInfo(body.hash);
/**
* Check:
* - contract check
* - techAddress check
* - TODO: check non-zero amount
*/
if (info.ext.to.toLowerCase() !== config.eth_techAddr.toLowerCase()
|| info.to.toLowerCase() !== config.eth.tokenAddr.toLowerCase()
|| info.ext.linkedAddr === '') {
return false;
}
// Convert BEP address from hex string to ascii
info.ext.linkedAddr = Utils.hexToString('0x' + info.ext.linkedAddr);
let db_data = {
pubkey : body.pubkey,
in_hash : info.hash,
out_addr : info.ext.linkedAddr,
in_addr : info.from,
amount : info.ext.amount,
status : 0,
date : Math.floor(new Date() / 1000),
type : Utils.swapTypes.erc_bep,
hold : 0
};
let res1 = await this.db.put_erc_tx([[info.hash, info.ext.amount, 0]]);
let res2 = await this.db.put_history(db_data);
return true;
}
catch (err) {
logger.error(err.stack);
return false;
}
}
async swapENQ_ERC(body){
try{
let tx = body.tx;
tx.to = config.enq_techAddr;
let verifiedHash = Utils.hashTx(tx);
if(!Utils.verify(tx.from, verifiedHash, tx.sign)){
logger.warn(`Verification failed for tx ${JSON.stringify(tx)}`);
return {result : false, msg: "Sign not verified"};
}
logger.debug(JSON.stringify(tx));
let limit = await Utils.getSwapLimit();
if(tx.amount < limit)
return {result : false, msg: "Less than minimal amount to swap"};
let txhash = Utils.hashSignedTx(tx);
let db_data = {
pubkey : body.pubkey,
in_hash : txhash,
out_addr : body.eth_addr,
in_addr : body.pubkey,
amount : body.tx.amount,
status : 0,
date : Math.floor(new Date() / 1000),
type : Utils.swapTypes.enq_erc,
hold : 0
};
let result = await this.db.put_history(db_data);
if(!result.affectedRows)
return {result : false};
let posResponse;
// Trying to send tx to node
// TODO: Add to Sender
try{
posResponse = await Utils.apiRequest.post(config.nodeURL + '/api/v1/tx', [tx]);
// In case of bad response we save tx in pending (unknown tx state)
if(!posResponse.result[0].hash)
throw new Error('Invalid response');
}
catch(err){
logger.warn(`no response, save ${txhash} in pending`);
let rec = await this.db.get_history_row('in_hash', txhash);
tx.hash = txhash;
tx.recid = rec[0].recid;
let senderRes = await sender.add(tx);
return {result : senderRes};
}
logger.info(`ENQ TX ${posResponse.result[0].hash} for pubkey ${body.pubkey} was sent to node`);
if(posResponse){
if(posResponse.result[0].hash !== txhash){
logger.error(`Hash mismatch, node: ${posResponse.result[0].hash} | ${txhash}`);
return {result : true};
}
await this.db.put_enq_tx([[txhash, body.tx.amount, 0]]);
return posResponse;
}
}
catch(err){
logger.error(err.stack);
return {result : false};
}
}
async swapENQ_BEP(body){
try{
let tx = body.tx;
tx.to = config.enq_techAddr;
let verifiedHash = Utils.sha256(
['amount','from','nonce','to'].map(v => Utils.sha256(tx[v].toString().toLowerCase())).join("")
);
if(!Utils.verify(tx.from, verifiedHash, tx.sign)){
logger.warn(`Verification failed for tx ${JSON.stringify(tx)}`);
return {result : false, msg: "Sign not verified"};
}
logger.debug(JSON.stringify(tx));
// TODO: check swap limit for BEP swap
// let limit = await Utils.getSwapLimit();
// if(tx.amount < limit)
// return {result : false, msg: "Less than minimal amount to swap"};
let posResponse = await Utils.apiRequest.post(config.nodeURL + '/api/v1/tx', [tx]);
logger.info(`ENQ TX ${posResponse.result[0].hash} for pubkey ${body.pubkey} was sent to node`);
let db_data = {
pubkey : body.pubkey,
in_hash : posResponse.result[0].hash,
out_addr : body.linked_addr,
in_addr : body.pubkey,
amount : body.tx.amount,
status : 0,
date : Math.floor(new Date() / 1000),
type : Utils.swapTypes.enq_bep,
hold : 0
};
await this.db.put_enq_tx([[posResponse.result[0].hash, body.tx.amount, 0]]);
let result = await this.db.put_history(db_data);
if(result.affectedRows)
return posResponse;
else
return {result : false};
}
catch(err){
logger.error(err.stack);
return {result : false};
}
}
async auth(token, sign) {
let dbres = await this.db.get_token(token);
if(dbres === undefined)
return false;
if((dbres.cram === null))
return false;
await this.db.put_message({token: token, cram: null});
let result = Utils.verify(dbres.pubkey, dbres.cram, sign);
logger.debug(`${token} auth: ${result}`);
return result;
}
async logout(token) {
try{
let dbres = await this.db.delete_token(token);
if(dbres.affectedRows)
return true;
return false;
}
catch (err) {
logger.error(err.stack);
return {result : false};
}
}
async login(pubkey, ua) {
try{
let token = crypto.randomBytes(16).toString('hex');
let dbData = {
token : token,
pubkey : pubkey,
user_agent : ua
};
let dbres = await this.db.put_token(dbData);
if(!dbres.affectedRows)
return false;
return token;
}
catch (err) {
logger.error(err.stack);
return false;
}
}
async challenge(token) {
try{
let msg = crypto.randomBytes(32).toString('hex');
// TODO: check db response
let dbres = await this.db.put_message({token: token, cram: msg});
if(!dbres)
return false;
return msg;
}
catch (err) {
logger.error(err.stack);
return false;
}
}
}
module.exports.Service = Service;