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 | var _ = require('underscore');
|
26 | var ethjsUnit = require('ethjs-unit');
|
27 | var utils = require('./utils.js');
|
28 | var soliditySha3 = require('./soliditySha3.js');
|
29 | var randomHex = require('randomhex');
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | var _fireError = function (error, emitter, reject, callback) {
|
44 |
|
45 |
|
46 |
|
47 | if(_.isObject(error) && !(error instanceof Error) && error.data) {
|
48 | if(_.isObject(error.data) || _.isArray(error.data)) {
|
49 | error.data = JSON.stringify(error.data, null, 2);
|
50 | }
|
51 |
|
52 | error = error.message +"\n"+ error.data;
|
53 | }
|
54 |
|
55 | if(_.isString(error)) {
|
56 | error = new Error(error);
|
57 | }
|
58 |
|
59 | if (_.isFunction(callback)) {
|
60 | callback(error);
|
61 | }
|
62 | if (_.isFunction(reject)) {
|
63 |
|
64 |
|
65 | if (emitter &&
|
66 | (_.isFunction(emitter.listeners) &&
|
67 | emitter.listeners('error').length) || _.isFunction(callback)) {
|
68 | emitter.catch(function(){});
|
69 | }
|
70 |
|
71 | setTimeout(function () {
|
72 | reject(error);
|
73 | }, 1);
|
74 | }
|
75 |
|
76 | if(emitter && _.isFunction(emitter.emit)) {
|
77 |
|
78 | setTimeout(function () {
|
79 | emitter.emit('error', error);
|
80 | emitter.removeAllListeners();
|
81 | }, 1);
|
82 | }
|
83 |
|
84 | return emitter;
|
85 | };
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 | var _jsonInterfaceMethodToString = function (json) {
|
95 | if (_.isObject(json) && json.name && json.name.indexOf('(') !== -1) {
|
96 | return json.name;
|
97 | }
|
98 |
|
99 | return json.name + '(' + _flattenTypes(false, json.inputs).join(',') + ')';
|
100 | };
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 | var _flattenTypes = function(includeTuple, puts)
|
112 | {
|
113 |
|
114 | var types = [];
|
115 |
|
116 | puts.forEach(function(param) {
|
117 | if (typeof param.components === 'object') {
|
118 | if (param.type.substring(0, 5) !== 'tuple') {
|
119 | throw new Error('components found but type is not tuple; report on GitHub');
|
120 | }
|
121 | var suffix = '';
|
122 | var arrayBracket = param.type.indexOf('[');
|
123 | if (arrayBracket >= 0) { suffix = param.type.substring(arrayBracket); }
|
124 | var result = _flattenTypes(includeTuple, param.components);
|
125 |
|
126 | if(_.isArray(result) && includeTuple) {
|
127 |
|
128 | types.push('tuple(' + result.join(',') + ')' + suffix);
|
129 | }
|
130 | else if(!includeTuple) {
|
131 |
|
132 | types.push('(' + result.join(',') + ')' + suffix);
|
133 | }
|
134 | else {
|
135 |
|
136 | types.push('(' + result + ')');
|
137 | }
|
138 | } else {
|
139 |
|
140 | types.push(param.type);
|
141 | }
|
142 | });
|
143 |
|
144 | return types;
|
145 | };
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 | var hexToAscii = function(hex) {
|
156 | if (!utils.isHexStrict(hex))
|
157 | throw new Error('The parameter must be a valid HEX string.');
|
158 |
|
159 | var str = "";
|
160 | var i = 0, l = hex.length;
|
161 | if (hex.substring(0, 2) === '0x') {
|
162 | i = 2;
|
163 | }
|
164 | for (; i < l; i+=2) {
|
165 | var code = parseInt(hex.substr(i, 2), 16);
|
166 | str += String.fromCharCode(code);
|
167 | }
|
168 |
|
169 | return str;
|
170 | };
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 | var asciiToHex = function(str) {
|
180 | if(!str)
|
181 | return "0x00";
|
182 | var hex = "";
|
183 | for(var i = 0; i < str.length; i++) {
|
184 | var code = str.charCodeAt(i);
|
185 | var n = code.toString(16);
|
186 | hex += n.length < 2 ? '0' + n : n;
|
187 | }
|
188 |
|
189 | return "0x" + hex;
|
190 | };
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 | var getUnitValue = function (unit) {
|
203 | unit = unit ? unit.toLowerCase() : 'ether';
|
204 | if (!ethjsUnit.unitMap[unit]) {
|
205 | throw new Error('This unit "'+ unit +'" doesn\'t exist, please use the one of the following units' + JSON.stringify(ethjsUnit.unitMap, null, 2));
|
206 | }
|
207 | return unit;
|
208 | };
|
209 |
|
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 |
|
216 |
|
217 |
|
218 |
|
219 |
|
220 |
|
221 |
|
222 |
|
223 |
|
224 |
|
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 |
|
231 | var fromWei = function(number, unit) {
|
232 | unit = getUnitValue(unit);
|
233 |
|
234 | if(!utils.isBN(number) && !_.isString(number)) {
|
235 | throw new Error('Please pass numbers as strings or BigNumber objects to avoid precision errors.');
|
236 | }
|
237 |
|
238 | return utils.isBN(number) ? ethjsUnit.fromWei(number, unit) : ethjsUnit.fromWei(number, unit).toString(10);
|
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 | var toWei = function(number, unit) {
|
264 | unit = getUnitValue(unit);
|
265 |
|
266 | if(!utils.isBN(number) && !_.isString(number)) {
|
267 | throw new Error('Please pass numbers as strings or BigNumber objects to avoid precision errors.');
|
268 | }
|
269 |
|
270 | return utils.isBN(number) ? ethjsUnit.toWei(number, unit) : ethjsUnit.toWei(number, unit).toString(10);
|
271 | };
|
272 |
|
273 |
|
274 |
|
275 |
|
276 |
|
277 |
|
278 |
|
279 |
|
280 |
|
281 |
|
282 |
|
283 | var toChecksumAddress = function (address) {
|
284 | if (typeof address === 'undefined') return '';
|
285 |
|
286 | if(!/^(0x)?[0-9a-f]{40}$/i.test(address))
|
287 | throw new Error('Given address "'+ address +'" is not a valid Ethereum address.');
|
288 |
|
289 |
|
290 |
|
291 | address = address.toLowerCase().replace(/^0x/i,'');
|
292 | var addressHash = utils.sha3(address).replace(/^0x/i,'');
|
293 | var checksumAddress = '0x';
|
294 |
|
295 | for (var i = 0; i < address.length; i++ ) {
|
296 |
|
297 | if (parseInt(addressHash[i], 16) > 7) {
|
298 | checksumAddress += address[i].toUpperCase();
|
299 | } else {
|
300 | checksumAddress += address[i];
|
301 | }
|
302 | }
|
303 | return checksumAddress;
|
304 | };
|
305 |
|
306 |
|
307 |
|
308 | module.exports = {
|
309 | _fireError: _fireError,
|
310 | _jsonInterfaceMethodToString: _jsonInterfaceMethodToString,
|
311 | _flattenTypes: _flattenTypes,
|
312 |
|
313 |
|
314 | randomHex: randomHex,
|
315 | _: _,
|
316 | BN: utils.BN,
|
317 | isBN: utils.isBN,
|
318 | isBigNumber: utils.isBigNumber,
|
319 | isHex: utils.isHex,
|
320 | isHexStrict: utils.isHexStrict,
|
321 | sha3: utils.sha3,
|
322 | keccak256: utils.sha3,
|
323 | soliditySha3: soliditySha3,
|
324 | isAddress: utils.isAddress,
|
325 | checkAddressChecksum: utils.checkAddressChecksum,
|
326 | toChecksumAddress: toChecksumAddress,
|
327 | toHex: utils.toHex,
|
328 | toBN: utils.toBN,
|
329 |
|
330 | bytesToHex: utils.bytesToHex,
|
331 | hexToBytes: utils.hexToBytes,
|
332 |
|
333 | hexToNumberString: utils.hexToNumberString,
|
334 |
|
335 | hexToNumber: utils.hexToNumber,
|
336 | toDecimal: utils.hexToNumber,
|
337 |
|
338 | numberToHex: utils.numberToHex,
|
339 | fromDecimal: utils.numberToHex,
|
340 |
|
341 | hexToUtf8: utils.hexToUtf8,
|
342 | hexToString: utils.hexToUtf8,
|
343 | toUtf8: utils.hexToUtf8,
|
344 |
|
345 | utf8ToHex: utils.utf8ToHex,
|
346 | stringToHex: utils.utf8ToHex,
|
347 | fromUtf8: utils.utf8ToHex,
|
348 |
|
349 | hexToAscii: hexToAscii,
|
350 | toAscii: hexToAscii,
|
351 | asciiToHex: asciiToHex,
|
352 | fromAscii: asciiToHex,
|
353 |
|
354 | unitMap: ethjsUnit.unitMap,
|
355 | toWei: toWei,
|
356 | fromWei: fromWei,
|
357 |
|
358 | padLeft: utils.leftPad,
|
359 | leftPad: utils.leftPad,
|
360 | padRight: utils.rightPad,
|
361 | rightPad: utils.rightPad,
|
362 | toTwosComplement: utils.toTwosComplement
|
363 | };
|
364 |
|