-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutilities.js
437 lines (377 loc) · 15.5 KB
/
utilities.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/*********************************************************************************
* The MIT License (MIT) *
* *
* Copyright (c) 2020 KMi, The Open University UK *
* *
* Permission is hereby granted, free of charge, to any person obtaining *
* a copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the Software *
* is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, *
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN *
* THE SOFTWARE. *
* *
**********************************************************************************/
/** Author: Michelle Bachler, KMi, The Open University **/
/** Author: Kevin Quick, KMi, The Open University **/
var cfg = require('./config.js');
const fs = require( 'fs' );
// Create web3 instance
const Web3 = require('web3');
var web3 = new Web3(new Web3.providers.WebsocketProvider(cfg.parity_ipc_path));
/**
* Transfer funds between data.from and data.to accounts, to the value of data.amount (given in ether)
* Calls the return handler, with new data properties: data.frombalance (before transfer), data.tobalance (before transfer),
* data.tx (the funds transfer transaction number) and/or res.locals.errormsg is set to a message if somethig went wrong.
*/
exports.transferFunds = function(req, res, callback, data, returnhandler) {
data.wei = web3.utils.toWei(data.amount.toString());
//console.log(data.wei);
//console.log(data.from);
//console.log(data.to);
web3.eth.getBalance(data.from)
.then(function(balance){
var wei = balance;
data.frombalance = web3.utils.fromWei(wei, "ether");
console.log("From Balance = " + data.frombalance + " ether");
if (parseInt(data.frombalance) < parseInt(data.amount)) {
res.locals.errormsg = "insufficient funds in the from address";
} else {
web3.eth.getBalance(data.to)
.then(function(balance2){
var weito = balance2;
data.tobalance = web3.utils.fromWei(weito, "ether");
console.log("To Balance = " + data.tobalance + " ether");
web3.eth.sendTransaction({from: data.from, to: data.to, value: data.wei})
.on('receipt', function(receipt) {
console.log("Transfer result: ");
console.log(receipt);
if (receipt.status == "0x0") {
console.log("Funds transfer transaction failed");
res.locals.errormsg = "Funds transfer transaction failed";
} else {
data.tx = receipt.transactionHash;
returnhandler(req, res, callback, data);
}
})
.on('error', function(error3) {
console.log("Transfer error: " + error3);
res.locals.errormsg = "Transfer error: " + error3;
});
}).catch((error2) => {
console.log("get to account balance error: "+error2);
res.locals.errormsg = error2;
});
}
}).catch((error) => {
console.log("get from account balance error: "+error);
res.locals.errormsg = error;
});
}
/**
* Create a new blockchain account using the given data.accountname and data.accountpassword
* Calls the return handler, with new data properties: data.account, data.secretphrase or data.error array if things went wrong.
*/
exports.createAccount = function(req, res, callback, data, handler) {
var util = require('util');
var exec = require('child_process').exec;
data.error = [];
var command = 'curl --data \'{"method":"parity_generateSecretPhrase","params":[],"id":1,"jsonrpc":"2.0"}\' -H "Content-Type: application/json" -X POST '+cfg.rpcapi+':'+cfg.rpcport;
child = exec(command, function(e, stdout, stderr){
if(e !== null) {
//console.log('stderr: ' + stderr);
//console.log('exec error: ' + e);
data.error.push(e);
} else {
data.secretphrase = JSON.parse(stdout).result;
var command2 = 'curl --data \'{"method":"parity_newAccountFromPhrase","params":["'+data.secretphrase+'","'+data.accountpassword+'"],"id":1,"jsonrpc":"2.0"}\' -H "Content-Type: application/json" -X POST '+cfg.rpcapi+':'+cfg.rpcport;
child2 = exec(command2, function(e2, stdout2, stderr2){
if(e2 !== null) {
//console.log('stderr2: ' + stderr2);
//console.log('exec error2: ' + e2);
data.error.push(e2);
} else {
var newaccount = JSON.parse(stdout2).result;
data.account = web3.utils.toChecksumAddress(newaccount); // Parity account creation does not return checksummed accounts yet - 10/10/2019
//console.log(data.account);
// single speach marks upset parity or curl
var accountname = data.accountname.replace(/\'/g, ' ');
var command3 = 'curl --data \'{"method":"parity_setAccountName","params":["'+data.account+'","'+accountname+'"],"id":1,"jsonrpc":"2.0"}\' -H "Content-Type: application/json" -X POST '+cfg.rpcapi+':'+cfg.rpcport;
child3 = exec(command3, function(e3, stdout3, stderr3){
if(e3 !== null) {
//console.log('stderr3: ' + stderr3);
//console.log('exec error3: ' + e3);
data.error.push(e3);
} else {
data.message = JSON.parse(stdout3);
//console.log(stdout3);
handler(req, res, callback, data);
}
});
}
});
}
});
}
/**
* Check to see if account balance is below minimumAccountBalanceWei,
* if it is, top up the account to equal optimumAccountBalanceWei.
* Call return handler with data.error string set to a message if something went wrong.
*/
exports.topUpAccount = function(req, res, callback, data, handler, account){
var minimumAccountBalanceWei = parseInt(web3.utils.toWei(cfg.minimumAccountBalance.toString(), "ether"));
var optimumAccountBalanceWei = parseInt(web3.utils.toWei(cfg.optimumAccountBalance.toString(), "ether"));
data.error = "";
web3.eth.getBalance(account)
.then(function(balance){
var wei = parseInt(balance);
if (wei < minimumAccountBalanceWei) {
//console.log("topUpAccount");
//console.log(account);
//console.log(balance);
req.topup = optimumAccountBalanceWei - wei;
console.log("Topping up account with an extra " + req.topup + " wei");
web3.eth.sendTransaction({from: cfg.systemBankAccount, to: account, value: req.topup.toString()})
.on('receipt', function(receipt) {
console.log("TopUp Transfer result: ");
console.log(receipt);
if (receipt.status == "0x0") {
console.log("Funds transfer transaction failed");
data.error = "Funds transfer transaction failed";
handler(req, res, callback, data);
} else {
handler(req, res, callback, data);
}
})
.on('error', function(error) {
console.log("Transfer error: " + error);
data.error = "Transfer error: " + error;
handler(req, res, callback, data);
});
} else {
//console.log("Sufficient balance - no top up needed");
handler(req, res, callback, data);
}
}).catch((error) => {
console.log("topUpAccount error: "+error);
data.error = "getBalance error: " + error;
handler(req, res, callback, data);
});
}
/**
* Unlock the given account with the given password
* Call the return handler once account unlocked
*/
exports.unlockAccount = function(req, res, callback, data, returnhandler, account, accountpassword){
// check if account needs toppping up before unlocking it
var thishandler = function (req, res, callback, data) {
if (data.error === undefined || data.error == "") {
//if no password given assume nothing needs unlocking, just continue on.
if (!accountpassword || accountpassword == "") {
returnhandler(req, res, callback);
} else {
var innerhandler = function (e, result) {
if (!e) {
console.log("Unlocked " + account);
returnhandler(req, res, callback, data);
return;
} else {
console.error("Error unlocking " + account + " = "+e);
res.locals.errormsg = e;
}
};
web3.eth.personal.unlockAccount(account, accountpassword, innerhandler);
}
} else {
res.locals.errormsg = data.error;
}
}
if (account != cfg.systemBankAccount) {
exports.topUpAccount(req, res, callback, data, thishandler, account);
} else {
thishandler(req, res, callback, data);
}
}
/**
* Function to return the transaction object for a given transaction id.
*/
exports.getTransaction = function(transactionid, req, res) {
if (transactionid == "") {
return res.status(401).send("The transaction for that address cannot be found");
}
web3.eth.getTransaction(transactionid)
.then(function(transaction){
res.send({transaction: transaction});
}).catch((error) => {
console.log("getTransaction error: "+error);
res.status(404).send({error: error});
});
}
/**
* Function to return the transaction receipt object for a given transaction id.
*/
exports.getTransactionReceipt = function(transactionid, req, res) {
if (transactionid == "") {
return res.status(401).send("The transaction for that address cannot be found");
}
web3.eth.getTransactionReceipt(transactionid)
.then(function(transaction){
res.send({transactionreceipt: transaction});
}).catch((error) => {
console.log("getTransactionReceipt error: "+error);
res.status(404).send({error: error});
});
}
/**
* Get the json block object for the given block number
*
* @param blocknumber, the block number to get the object for.
* @return json block object for the given block number, or error.
*/
exports.getBlock = function(blocknumber, req, res, next) {
if (blocknumber == "") {
return res.status(400).send({error:"You must include the block number of the block you want to get"});
}
web3.eth.getBlock(blocknumber, true) // true = includes transactions
.then(function(block){
if (block == null) {
res.status(404).send({error: "invalid block number"});
} else {
res.send({block: block});
}
}).catch((error) => {
res.status(404).send({error: error});
});
}
/**
* Get the block number of the current block
*
* @return the block number of the current block, or error.
*/
exports.getCurrentBlockNumber = function(req, res, next) {
web3.eth.getBlockNumber()
.then(function(currentblock){
res.send({currentblock: currentblock});
}).catch((error) => {
console.log("currentblock error: "+error);
res.status(404).send({error: error});
});
};
/**
* Function to escape single speeach marks
* Returns the original passed string with any single speech marks escaped.
*/
exports.escape = function(thestring) {
return thestring.split("'").join("\\\'");
}
/**
* Function to check that a contract address exists.
* Call the handler and passes back true and an empty error string if it exists, false and any error message if it does not.
*/
exports.contractExists = function(address, returnhandler, req, res) {
var exists = false;
var error = "";
web3.eth.getCode(address)
.then(function(code){
console.log(code);
if (code != "0x") {
exists = true;
}
returnhandler(exists, error);
}).catch((error) => {
console.log("contractExists error: "+error);
returnhandler(exists, error);
});
}
/**
* Generate a digital random number/letter combination for registration keys and temporary passwords etc.. of the given length
* - Original code by By Damian Dadswell in PHP
* @param keylength the length of the key to generate
* return the random number.
*/
exports.createKey = function(keylength) {
var registration = "";
var newlength = 0;
while(newlength < keylength) {
var part = rand(1,3);
var a,b;
if(part==1){a=48;b=57;} // Numbers
if(part==2){a=65;b=90;} // UpperCase
if(part==3){a=97;b=122;} // LowerCase
var code_part=String.fromCharCode(rand(a,b));
newlength = newlength + 1;
registration = registration+code_part;
}
return registration;
}
/**
* Taken From - http://stackoverflow.com/questions/46155/validate-email-address-in-javascript
*/
exports.isValidEmail = function(email) {
var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(email);
}
function rand(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
/**
* Function to encode file data to base64 encoded string
*/
function base64_encode(file) {
// read binary data
var bitmap = fs.readFileSync(file);
// convert binary data to base64 encoded string
//return new Buffer(bitmap).toString('base64');
//console.log(new Buffer.from(bitmap).toString('base64'));
return new Buffer.from(bitmap).toString('base64');
}
// must match version in client side untilites for claiming to work
exports.demicrosoftize = function(str) {
str = str.replace("\x82","'");
str = str.replace("\x83","f");
str = str.replace("\x84","\"");
str = str.replace("\x85","...");
str = str.replace("\x86","+");
str = str.replace("\x87","#");
str = str.replace("\x89","^");
str = str.replace("\x8a","\xa6");
str = str.replace("\x8b","<");
str = str.replace("\x8c","\xbc");
str = str.replace("\x8e","\xb4");
str = str.replace("\x91","'");
str = str.replace("\x92","'");
str = str.replace("\x93","\"");
str = str.replace("\x94","\"");
str = str.replace("\x95","*");
str = str.replace("\x96","-");
str = str.replace("\x97","--");
str = str.replace("\x98","~");
str = str.replace("\x99","(TM)");
str = str.replace("\x9a","\xa8");
str = str.replace("\x9b",">");
str = str.replace("\x9c","\xbd");
str = str.replace("\x9e","\xb8");
str = str.replace("\x9f","\xbe");
str = str.replace(/[\u2018|\u2019|\u201A]/g, "\'");
str = str.replace(/[\u201C|\u201D|\u201E]/g, "\"");
str = str.replace(/\u2026/g, "...");
str = str.replace(/[\u2013|\u2014]/g, "-");
str = str.replace(/\u02C6/g, "^");
str = str.replace(/\u2039/g, "");
//str = str.replace(/\u2039/g, "<");
str = str.replace(/\u203A/g, "");
//str = str.replace(/\u203A/g, ">");
str = str.replace(/[\u02DC|\u00A0]/g, " ");
str = str.replace(/[\u2022|\u00B7|\u2024|\u2219|\u25D8|\u25E6]/g, "-");
return str;
}