-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
274 lines (250 loc) · 10.4 KB
/
index.js
File metadata and controls
274 lines (250 loc) · 10.4 KB
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
"use strict";
//Print publisher info
console.log('EUBITokenBridge v1.1 - the bridge between EUBI and bEUBI');
console.log('Made by Jessie Lesbian');
console.log('Email: jessielesbian@protonmail.com Reddit: https://www.reddit.com/u/jessielesbian');
console.log('');
//Protect against potentially insecure libraries by putting everything in this scope
{
/* We define our methods before we continue the initialization process */
//The final send have barely enough permissions!
const finalSend = async function(MintX2Signed, BSCTXSigned, MintME, BSC, res, end2){
MintME.sendSignedTransaction(MintX2Signed).then(async function(){
BSC.sendSignedTransaction(BSCTXSigned).then(async function(){
res.writeHead(200, {"Access-Control-Allow-Origin": "*"});
res.write('Thanks for using EUBITokenBridge v1.1!');
end2();
}, async function(){
res.writeHead(500, {"Access-Control-Allow-Origin": "*"});
res.write('Unable to send bEUBI tokens!');
end2();
});
BSC = undefined;
}, async function(){
res.writeHead(500, {"Access-Control-Allow-Origin": "*"});
res.write('Unable to pull deposit to core wallet!');
end2();
});
MintME = undefined;
};
//The finish method - this method have very little permissions!
const finishBridge = async function(MintX2, MintME, BSCTX, BSC, res, end2, MintySendy){
MintX2.then(async function(MintX2Signed){
BSCTX.then(async function(BSCTXSigned){
if(MintySendy === undefined){
finalSend(MintX2Signed.rawTransaction, BSCTXSigned.rawTransaction, MintME, BSC, res, end2);
} else{
MintySendy.then(async function(){
finalSend(MintX2Signed.rawTransaction, BSCTXSigned.rawTransaction, MintME, BSC, res, end2);
}, async function(){
res.writeHead(500, {"Access-Control-Allow-Origin": "*"});
res.write('Unable to send MintME to deposit address for gas!');
end2();
});
MintySendy = undefined;
}
MintME = undefined;
}, async function(){
res.writeHead(500, {"Access-Control-Allow-Origin": "*"});
res.write('Unable sign third transaction!');
end2();
});
BSCTX = undefined;
}, async function(){
res.writeHead(500, {"Access-Control-Allow-Origin": "*"});
res.write('Unable sign second transaction!');
end2();
});
MintX2 = undefined;
};
//The core bridge method, after we are done fetching information from the MintME blockchain.
//This method have too many permissions, maybe we should split it one day?
const bridge2 = async function(gasPrice1, balance, value1, deposit, address, res, address2, end2, MintyFundTX, gasFee, MintyPrivateKey, MintME, BSC, EUBI, fee, mul2){
//Calculate if EUBI sent is enough for bridge fee
let postFee = balance.sub(fee);
if(postFee.toString().startsWith('-')){
res.writeHead(200, {"Access-Control-Allow-Origin": "*"});
res.write('Minimum conversion: 10 EUBI!');
end2();
} else{
//Start asynchronous transaction signing as soo as freaking possible!
let BSCTX = BSC.accounts.privateKeyToAccount(MintyPrivateKey).signTransaction({chainId: "56", gas: "100000", to: '0x27fAAa5bD713DCd4258D5C49258FBef45314ae5D', data: EUBI.methods.transfer(address2, postFee.mul(mul2).toString()).encodeABI()});
MintyPrivateKey = undefined;
let MintX2 = deposit.signTransaction({chainId: "24734", gasPrice: gasPrice1, gas: "100000", to: '0x8AFA1b7a8534D519CB04F4075D3189DF8a6738C1', data: EUBI.methods.transfer('0x77c4529FC9D0446642EB29cE33b8B2afD43926d0', balance).encodeABI()});
//load up some gas - send MintME to the deposit address if the're not enough for 100,000 gas
if(value1.sub(gasFee).toString().startsWith('-')){
MintyFundTX.then(async function(MintyFundTXSigned){
finishBridge(MintX2, MintME, BSCTX, BSC, res, end2, MintME.sendSignedTransaction(MintyFundTXSigned.rawTransaction));
}, async function(){
res.writeHead(500, {"Access-Control-Allow-Origin": "*"});
res.write('Unable to sign first transaction!');
end2();
});
MintyFundTX = undefined;
} else{
finishBridge(MintX2, MintME, BSCTX, BSC, res, end2, undefined);
}
}
};
//Core scope enhances protections against
{
console.log('Securely loading modules...');
//Connect to blockchains
let Blockchain = require('web3-eth');
const MintME = new Blockchain('https://node1.mintme.com:443');
const BSC = new Blockchain('https://speedy-nodes-nyc.moralis.io/41590f438df3f8018a1e84b1/bsc/mainnet');
Blockchain = undefined;
//Load Web3 Ethereum Utilities
let ethutils = require('web3-utils');
const solsha3 = ethutils.soliditySha3;
const isAddress = ethutils.isAddress;
const BigInt = ethutils.BN;
ethutils = undefined;
//Initialize constants
const mul2 = new BigInt('100000');
const fee = new BigInt('10000000000000');
const egas = new BigInt('100000');
//Initialize ERC20 connector
const EUBI = new MintME.Contract([{"inputs": [{"internalType": "address","name": "account","type": "address"}],"name": "balanceOf","outputs": [{"internalType": "uint256","name": "","type": "uint256"}],"stateMutability": "view","type": "function"},{"inputs": [{"internalType": "address","name": "recipient","type": "address"},{"internalType": "uint256","name": "amount","type": "uint256"}],"name": "transfer","outputs": [{"internalType": "bool","name": "","type": "bool"}],"stateMutability": "nonpayable","type": "function"}], '0x8AFA1b7a8534D519CB04F4075D3189DF8a6738C1');
//Provision our asynchronous lock
console.log('Creating lock...');
const lock = new (require('async-lock'))();
//Load wallet
console.log('Loading wallet...');
let MintMEWallet_;
try {
MintMEWallet_ = MintME.accounts.privateKeyToAccount(require('fs').readFileSync('private_key.txt', 'utf8'));
} catch (err) {
console.log('Please put a valid private key into private_key.txt');
process.exit(1);
}
console.log('Our wallet address is: ' + MintMEWallet_.address);
const MintMEWallet = MintMEWallet_;
MintMEWallet_ = undefined;
//The deposit address is the hash of the private key and the user address
//This function must be protected since it's super sensitive!
const getDepositAccount = function(address){
return MintME.accounts.privateKeyToAccount(solsha3({t: 'bytes', v: MintMEWallet.privateKey + address.substr(2)}));
};
//Create HTTP Listener
console.log('Creating HTTP Listener...');
let hserver = require('http').createServer(async function (req, res) {
let write2 = true;
let msg = undefined;
let hcode = 500;
try{
if(req.url.startsWith('/getdepaddr/')){
let address = req.url.substr(12);
if(isAddress(address)){
msg = 'Your deposit address is ' + getDepositAccount(address).address;
hcode = 200;
} else{
hcode = 404;
msg = 'Invalid address!';
}
} else if(req.url.startsWith('/flushtobinance/')){
//Here is where the bridge logic is defined
let address2 = req.url.substr(16);
let deposit;
let address;
let MintyFundTX;
let gasFee;
if(isAddress(address2)){
deposit = getDepositAccount(address2);
address = deposit.address;
let counter = 0;
let collect = {};
let end4;
let temp2 = async function(value, name){
collect[name] = value;
if(name == 'gasPrice1'){
gasFee = new BigInt(value).mul(egas);
//start signing MintME transaction to send money to deposit address for gas
//as soon as we knew the gas price on MintME.
MintyFundTX = MintMEWallet.signTransaction({to: address, chainId: "24734", gasPrice: value, gas: "21000", value: gasFee.toString()});
}
if(counter++ == 2){
bridge2(collect.gasPrice1, new BigInt(collect.balance), new BigInt(collect.MintyBalance3), deposit, address, res, address2, end4, MintyFundTX, gasFee, MintMEWallet.privateKey, MintME, BSC, EUBI, fee, mul2);
}
};
//Use batch requests to save time
let batch = new MintME.BatchRequest();
write2 = false; //cause we are being asynchronous
hcode = 200;
//We can only process 1 migration at a time!
lock.acquire('GlobalBridgeLock', async function(end3){
let canExit = true;
end4 = function(){
if(canExit){
canExit = false;
end3();
}
};
//Get the MintME gas price cause we are sending 2 MintME transactions
batch.add(MintME.getGasPrice.request(async function(error, value){
if(error == null){
temp2(value, 'gasPrice1');
} else{
res.writeHead(500, {"Access-Control-Allow-Origin": "*"});
res.write('Unable to get MintME gas price!');
end4();
}
}));
//Get the EUBI balance of the deposit address
batch.add(EUBI.methods.balanceOf(address).call.request(async function(error, value){
if(error == null){
temp2(value, 'balance');
} else{
res.writeHead(500, {"Access-Control-Allow-Origin": "*"});
res.write('Unable to get deposit address EUBI balance!');
end4();
}
}));
//Get the MintME balance of the deposit address
batch.add(MintME.getBalance.request(address, async function(error, value){
if(error == null){
temp2(value, 'MintyBalance3');
} else{
res.writeHead(500, {"Access-Control-Allow-Origin": "*"});
res.write('Unable to fetch MintME balance for deposit address!');
end4();
}
}));
batch.execute();
}, async function(){
if(!res.writableEnded){
res.end();
}
});
} else{
hcode = 404;
msg = 'Invalid address!';
}
} else{
hcode = 404;
msg = 'Improper API call!';
}
} catch(e){
console.error('SERVER ERROR: ' + e);
hcode = 500;
}
if(write2 || hcode == 500){
res.writeHead(hcode, {"Access-Control-Allow-Origin": "*"});
if(msg != undefined){ //no internal server errors occoured
res.write(msg);
}
res.end(); //end the response
}
});
//Start HTTP Listener
console.log('Starting HTTP Listener...');
hserver.setTimeout(120000);
hserver.listen(process.env.PORT || 80);
process.on('SIGTERM', async function(){
if(hserver){
hserver.close();
hserver = null;
}
});
}
}