-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.js
300 lines (262 loc) · 10.2 KB
/
utils.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
import crypto from 'crypto';
import bip39 from 'bip39';
import bip32 from 'bip32';
import TronWeb from 'tronweb';
import pbkdf2 from 'pbkdf2';
import aesjs from "aes-js";
import { isAddressValid,pkToAddress } from "@tronscan/client/src/utils/crypto";
import {utils} from 'ethers';
const encryptKey = (password, salt) => {
return pbkdf2.pbkdf2Sync(password, salt, 1, 256 / 8, 'sha512');
};
const AbiCoder = utils.AbiCoder;
const abiCoder = new AbiCoder();
const Utils = {
encryptionAlgorithm: 'aes-256-ctr',
hashAlgorithm: 'sha256',
stringToByte(str) {
var bytes = new Array();
var len, c;
len = str.length;
for(var i = 0; i < len; i++) {
c = str.charCodeAt(i);
if(c >= 0x010000 && c <= 0x10FFFF) {
bytes.push(((c >> 18) & 0x07) | 0xF0);
bytes.push(((c >> 12) & 0x3F) | 0x80);
bytes.push(((c >> 6) & 0x3F) | 0x80);
bytes.push((c & 0x3F) | 0x80);
} else if(c >= 0x000800 && c <= 0x00FFFF) {
bytes.push(((c >> 12) & 0x0F) | 0xE0);
bytes.push(((c >> 6) & 0x3F) | 0x80);
bytes.push((c & 0x3F) | 0x80);
} else if(c >= 0x000080 && c <= 0x0007FF) {
bytes.push(((c >> 6) & 0x1F) | 0xC0);
bytes.push((c & 0x3F) | 0x80);
} else {
bytes.push(c & 0xFF);
}
}
return bytes;
},
byteToString(arr) {
if(typeof arr === 'string') {
return arr;
}
var str = '',
_arr = arr;
for(var i = 0; i < _arr.length; i++) {
var one = _arr[i].toString(2),
v = one.match(/^1+?(?=0)/);
if(v && one.length == 8) {
var bytesLength = v[0].length;
var store = _arr[i].toString(2).slice(7 - bytesLength);
for(var st = 1; st < bytesLength; st++) {
store += _arr[st + i].toString(2).slice(2);
}
str += String.fromCharCode(parseInt(store, 2));
i += bytesLength - 1;
} else {
str += String.fromCharCode(_arr[i]);
}
}
return str;
},
hash(string) {
return crypto
.createHash(this.hashAlgorithm)
.update(string)
.digest('hex');
},
encrypt(data, key) {
const encoded = JSON.stringify(data);
const cipher = crypto.createCipher(this.encryptionAlgorithm, key);
let crypted = cipher.update(encoded, 'utf8', 'hex');
crypted += cipher.final('hex');
return crypted;
},
decrypt(data, key) {
const decipher = crypto.createDecipher(this.encryptionAlgorithm, key);
let decrypted = decipher.update(data, 'hex', 'utf8');
decrypted += decipher.final('utf8');
return JSON.parse(decrypted);
},
requestHandler(target) {
return new Proxy(target, {
get(target, prop) {
// First, check if the property exists on the target
// If it doesn't, throw an error
if(!Reflect.has(target, prop))
throw new Error(`Object does not have property '${ prop }'`);
// If the target is a variable or the internal 'on'
// method, simply return the standard function call
if(typeof target[ prop ] !== 'function' || prop === 'on')
return Reflect.get(target, prop);
// The 'req' object can be destructured into three components -
// { resolve, reject and data }. Call the function (and resolve it)
// so the result can then be passed back to the request.
return (...args) => {
if(!args.length)
args[ 0 ] = {};
const [ firstArg ] = args;
const {
resolve = () => {},
reject = ex => console.error(ex),
data
} = firstArg;
if(typeof firstArg !== 'object' || !('data' in firstArg))
return target[ prop ].call(target, ...args);
Promise.resolve(target[ prop ].call(target, data))
.then(resolve)
.catch(reject);
};
}
});
},
generateMnemonic() {
return bip39.generateMnemonic(128);
},
getAccountAtIndex(mnemonic, index = 0) {
const seed = bip39.mnemonicToSeed(mnemonic);
const node = bip32.fromSeed(seed);
const child = node.derivePath(`m/44'/195'/${ index }'/0/0`);
const privateKey = child.privateKey.toString('hex');
const address = TronWeb.address.fromPrivateKey(privateKey);
return {
privateKey,
address
};
},
validateMnemonic(mnemonic) {
return bip39.validateMnemonic(mnemonic);
},
injectPromise(func, ...args) {
return new Promise((resolve, reject) => {
func(...args, (err, res) => {
if(err)
reject(err);
else resolve(res);
});
});
},
isFunction(obj) {
return typeof obj === 'function';
},
dataLetterSort (data, field, field2, topArray) {
let needArray = [];
if(topArray){
topArray.forEach(v => {
needArray.push(v)
});
data = data.filter(({tokenId})=> !topArray.map(v=>v.tokenId).includes(tokenId))
}
let list = {};
let LetterArray = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','0','1','2','3','4','5','6','7','8','9'];
for (let i = 0; i < data.length; i++) {
const d = data[i][field] || data[i][field2] || data[i]['name'];
let letter = d.split('').filter(v=> v.match(/[a-zA-Z0-9]/)).join('').substr(0, 1).toUpperCase();
if(!list[letter]) {
list[letter] = [];
}
list[letter].push(data[i]);
}
LetterArray.forEach( v => {
if(list[v]) {
needArray = needArray.concat(list[v])
}
});
return needArray;
},
validatInteger(str) { // integer
const reg = /^\+?[1-9][0-9]*$/;
return reg.test(str);
},
requestUrl() { // request url
const curHost = location.hostname;
let curApiHost;
// const defaultUrl = 'http://52.14.133.221:8990'; //test
const defaultUrl = 'https://manger.tronlending.org'; //online
switch (curHost) {
case 'nnceancbokoldkjjbpopcffaoekebnnb':
curApiHost = defaultUrl;
break;
case 'ibnejdfjmmkpcnlpebklmnkoeoihofec':
curApiHost = defaultUrl;
break;
default:
curApiHost = defaultUrl;
break;
}
return curApiHost;
},
timetransTime(date) {
const newDate = new Date(date * 1000);
const timeY = newDate.getFullYear();
const timeM = (newDate.getMonth() + 1 < 10 ? `0${newDate.getMonth() + 1}` : newDate.getMonth() + 1);
const timeD = (newDate.getDate() < 10 ? `0${newDate.getDate()}` : newDate.getDate());
const timeh = (newDate.getHours() < 10 ? `0${newDate.getHours()}` : newDate.getHours());
const timem = (newDate.getMinutes() < 10 ? `0${newDate.getMinutes()}` : newDate.getMinutes());
return `${timeY}.${timeM}.${timeD} ${timeh}:${timem}`;
},
timeFormatTime(date) {
const newDate = new Date(date * 1000);
const timeY = newDate.getFullYear();
const timeM = (newDate.getMonth() + 1 < 10 ? `0${newDate.getMonth() + 1}` : newDate.getMonth() + 1);
const timeD = (newDate.getDate() < 10 ? `0${newDate.getDate()}` : newDate.getDate());
const timeh = (newDate.getHours() < 10 ? `0${newDate.getHours()}` : newDate.getHours());
const timem = (newDate.getMinutes() < 10 ? `0${newDate.getMinutes()}` : newDate.getMinutes());
return `${timeY}/${timeM}/${timeD} ${timeh}:${timem}`;
},
readFileContentsFromEvent(ev) {
return new Promise(resolve => {
const files = ev.target.files;
const reader = new FileReader();
reader.onload = (file) => {
const contents = file.target.result;
resolve(contents);
};
reader.readAsText(files[0]);
});
},
decryptString(password, salt, hexString) {
const key = encryptKey(password, salt);
const encryptedBytes = aesjs.utils.hex.toBytes(hexString);
const aesCtr = new aesjs.ModeOfOperation.ctr(key);
const decryptedBytes = aesCtr.decrypt(encryptedBytes);
return aesjs.utils.utf8.fromBytes(decryptedBytes);
},
validatePrivateKey(privateKey){
try {
let address = pkToAddress(privateKey);
return isAddressValid(address);
} catch (e) {
return false;
}
},
delay(timeout){
return new Promise(resolve => {
setTimeout(resolve, timeout);
});
},
decodeParams(message,abiCode,function_selector) {
const cutArr = function_selector.match(/(.+)\((.*)\)/);
if(cutArr[2]!==''){
const byteArray = TronWeb.utils.code.hexStr2byteArray(message);
const abi = abiCode.filter(({name})=> name === cutArr[1]);
return abi[0].inputs.map(({name,type},i)=>{
let value;
const array = byteArray.filter((v,index)=>index >=32 * i && index< 32 * (i + 1));
if(type === 'address') {
value = TronWeb.address.fromHex('41'+TronWeb.utils.code.byteArray2hexStr(array.filter((v,i) => i>11)));
} else if(type === 'trcToken') {
value = TronWeb.toDecimal('0x'+TronWeb.utils.code.byteArray2hexStr(array));
} else {
value = TronWeb.toDecimal('0x'+TronWeb.utils.code.byteArray2hexStr(array));
}
return {name,type,value};
});
}else{
return [];
}
},
};
export default Utils;