-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
312 lines (287 loc) · 8.92 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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
const express = require("express");
const bodyParser = require("body-parser");
const https = require("https");
const mongoose = require('mongoose');
const cookieParser = require('cookie-parser');
const { ethers } = require("ethers");
const crypto = require('crypto');
const {API_URL} = require('./config.js');
const { MONGO_URL } = require("./config.js");
const { user } = require("./config.js");
const { PROVIDER } = require("./config.js");
const { KEY, iv } = require("./config.js");
const twelveHoursInMilliseconds = 12 * 60 * 60 * 1000; // 12 hours in milliseconds
const app = express();
app.use(express.static("public"));
app.use(cookieParser());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
const encryptionKey = Buffer.from(KEY,'utf-8');
const IV = Buffer.from(iv,'utf-8'); // Create an initialization vector (IV) for additional security (16 bytes for AES-256)
//connect to database
async function ConnectDB(){
try{
await mongoose.connect(MONGO_URL);
console.log("Database in connected...");
}catch(error){
console.log("Error while connecting database...");
}
}
ConnectDB();
app.get("/",function(req,res){
//sending connect file
Mew_ID = null;
res.sendFile(__dirname + "/connect.html");
})
app.get("/homepage",function(req,res){
//sending homepage file
const mew_id = req.cookies.mew_id;
const ip = req.ip;
if(Users[mew_id] && Users[mew_id].login && ip === Users[mew_id].ip)
res.sendFile(__dirname + "/homepage.html");
else res.send("Unauthorised access !");
})
let Mew_ID;
app.post('/user',async function(req,res){
const account = req.body.wallet;
console.log("User : " +account);
let present = await ValidateUser(account);
if(present == null){
const mew_id = CreateWallet();
const newUser = new user({
wallet : account,
mew_id : mew_id.address,
pvt_key : mew_id.privateKey
});
present = await newUser.save();
}
Mew_ID = {
add : present.mew_id
}
res.send("ok");
});
app.get('/data', function(req, res) {
// API request
const url = API_URL;
https.get(url, function(response){
response.on('data', function(data){
try{
const ETHdata = JSON.parse(data);
const P = ETHdata.data.ETH.quote.USDT.price;
const Price = P.toFixed(2); //Price
const PC1D = ETHdata.data.ETH.quote.USDT.percent_change_24h;
const PercentChange24H = PC1D.toFixed(2); //24H change
const PC7D = ETHdata.data.ETH.quote.USDT.percent_change_7d;
const PercentChange7D = PC7D.toFixed(2); //7D change
const MC = ETHdata.data.ETH.quote.USDT.market_cap / 10e9;
const MarketCap = MC.toFixed(2); //Market cap
const V24H = ETHdata.data.ETH.quote.USDT.volume_24h / 10e9;
const Volume24H = V24H.toFixed(2); //Volume
// Create a JSON object with the data
const dataToSend = {
price: Price,
percentchange24h: PercentChange24H,
percentchange7d: PercentChange7D,
marketcap: MarketCap,
volume: Volume24H
};
// Set the Content-Type header to "application/json"
res.setHeader('Content-Type', 'application/json');
// Send the JSON object as a response
res.send(dataToSend);
}catch(error){
console.log(error);
}
});
});
});
app.get('/balance',async function(req,res){
const mew_id = req.cookies.mew_id;
const provider = new ethers.providers.JsonRpcProvider(PROVIDER);
try{
const account = await user.findOne({mew_id : mew_id});
const balance = await provider.getBalance(account.wallet);
const EtherBalance = ethers.utils.formatEther(balance);
const Ether = {
balance : (EtherBalance*1).toFixed(8)
}
// const ETH_USD = EtherBalance * currPrice;
// const ETHinUSD = ETH_USD.toFixed(4);
res.send(Ether);
}catch(error){
console.log(error);
}
});
//send transaction
// bug : showing previous info
app.post('/send',async function(req,res){
const to = req.body.toAddress;
const amount = req.body.amount;
const mew_id = req.cookies.mew_id;
let txnUpdate;
try{
const txn = await sendTxn(mew_id, to, amount);
console.log("Txn done");
if(txn.hash == undefined){
// res.send("Transaction rejected due to " +txn.reason+".");
txnUpdate = {
type : 0,
update : txn.reason
}
}else{
// res.send("Transaction done with Txn hash: "+txn.hash);
txnUpdate = {
type : 1,
update : txn.hash
}
}
}catch(error){
// res.send("Transaction cannot be done without connecting wallet!");
txnUpdate = {
type : -1,
update : "Transaction cannot be done!"
}
}
res.send(txnUpdate);
});
//Transaction update
// app.get('/txn-update',function(req,res){
// res.setHeader('Content-Type', 'application/json');
// res.send(txnUpdate);
// });
//create wallet function
function CreateWallet(){
const wallet = ethers.Wallet.createRandom();
return wallet;
}
//check user exist or not ?
async function ValidateUser(address){
const present = await user.findOne({wallet : address});
return present;
}
//function to send transaction
async function sendTxn(from, to, amount){
const provider = new ethers.providers.JsonRpcProvider(PROVIDER);
const account = await user.findOne({mew_id : from});
const key = decryptData(account.pvt_key);
const signer = new ethers.Wallet(key,provider);
try{
const txn = {
to : to,
value : ethers.utils.parseEther(amount),
gasLimit : 21000
}
const txnStatus = await signer.sendTransaction(txn);
return txnStatus;
}catch(error){
console.log(error);
return error;
}
}
//function for encryption
function encryptData (data){
// Create a cipher using AES-256-CBC algorithm
const cipher = crypto.createCipheriv('aes-256-cbc', encryptionKey, IV);
// Encrypt the data
let encryptedData = cipher.update(data, 'utf-8', 'hex');
encryptedData += cipher.final('hex');
return encryptedData;
}
//function for decryption
function decryptData (data){
// To decrypt the data
const decipher = crypto.createDecipheriv('aes-256-cbc', encryptionKey, IV);
// Decrypt the data
let decryptedData = decipher.update(data, 'hex', 'utf-8');
decryptedData += decipher.final('utf-8');
return decryptedData;
}
const Users = {};
app.post('/verify', async function(req,res){
const mew_id = req.body.mew_id;
try{
const available = await user.findOne({mew_id : mew_id});
if(available){
res.send(false);
}else{
const Wallet = CreateWallet();
const wallet = Wallet.address;
const pvt_key = encryptData(Wallet.privateKey);
const mnemonic = encryptData(Wallet.mnemonic.phrase);
await user.create({mew_id,wallet,pvt_key,mnemonic});
res.cookie('mew_id', mew_id, { maxAge: twelveHoursInMilliseconds });
Users[mew_id] = {
login : true,
ip : req.ip
}
res.send(true);
}
}catch(error){
console.log(error);
}
})
app.post('/exist', async function(req,res){
const mew_id = req.body.mew_id;
try{
const available = await user.findOne({mew_id : mew_id});
if(available) res.send(true);
else res.send(false);
}catch(error){
console.log(error);
}
})
//rather perfect add hollow citizen please glide child cheese bread cousin journey
app.post('/verify-mnemonic', async function(req,res){
const mnemonic = req.body.mnemonic;
const mew_id = req.body.mew_id;
try{
const account = await user.findOne({mew_id : mew_id});
const decryptedMnemonic = decryptData(account.mnemonic);
if(decryptedMnemonic === mnemonic){
res.cookie('mew_id', mew_id, { maxAge: twelveHoursInMilliseconds });
Users[mew_id] = {
login : true,
ip : req.ip
}
res.send(true);
}
}catch(error){
console.log(error);
}
});
app.get('/address', async function(req,res){
const mew_id = req.cookies.mew_id;
try{
const account = await user.findOne({mew_id : mew_id});
const address = {
add : account.wallet
}
res.send(address);
}catch(error){
console.log(error);
}
});
app.get('/info', async function(req,res){
const mew_id = req.cookies.mew_id;
try{
const account = await user.findOne({mew_id : mew_id});
const userInfo = {
mew_id : mew_id,
address : account.wallet,
privateKey : decryptData(account.pvt_key),
mnemonic : decryptData(account.mnemonic)
}
res.send(userInfo);
}catch(error){
console.log(error);
}
});
app.get('/logout', async function(req,res){
const mew_id = req.cookies.mew_id;
delete Users[mew_id];
res.clearCookie('mew_id');
res.send(true);
});
app.listen(3000,function(){
console.log("server started !!");
});