2017-10-19 04:28:45 +03:00
|
|
|
'use strict';
|
|
|
|
|
2016-07-27 00:58:17 +03:00
|
|
|
var crypto = require('crypto');
|
2017-10-19 04:28:45 +03:00
|
|
|
var fs = require('fs');
|
|
|
|
var path = require('path');
|
|
|
|
var zlib = require('zlib');
|
2016-07-27 00:58:17 +03:00
|
|
|
|
2018-06-22 03:24:30 +03:00
|
|
|
var bigNumber = require('../utils/bignumber');
|
|
|
|
var convert = require('../utils/bytes');
|
|
|
|
var keccak256 = require('../utils/keccak256').keccak256;
|
|
|
|
var utf8 = require('../utils/utf8');
|
2016-07-27 00:58:17 +03:00
|
|
|
|
2017-10-19 04:28:45 +03:00
|
|
|
/*
|
2016-07-27 00:58:17 +03:00
|
|
|
function random(lowerRandomInterval, upperOpenInterval) {
|
|
|
|
return lowerRandomInterval + parseInt((upperOpenInterval - lowerRandomInterval) * Math.random());
|
|
|
|
}
|
|
|
|
|
|
|
|
function randomBuffer(length) {
|
|
|
|
var buffer = crypto.randomBytes(length);
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
function randomHexString(length) {
|
|
|
|
return '0x' + randomBuffer(length).toString('hex');
|
|
|
|
}
|
2017-10-19 04:28:45 +03:00
|
|
|
*/
|
|
|
|
function randomBytes(seed, lower, upper) {
|
|
|
|
if (!upper) { upper = lower; }
|
|
|
|
|
|
|
|
if (upper === 0 && upper === lower) { return new Uint8Array(0); }
|
|
|
|
|
|
|
|
seed = utf8.toUtf8Bytes(seed);
|
|
|
|
|
|
|
|
var result = convert.arrayify(keccak256(seed));
|
|
|
|
while (result.length < upper) {
|
|
|
|
result = convert.concat([result, keccak256(convert.concat([seed, result]))]);
|
|
|
|
}
|
|
|
|
|
|
|
|
var top = convert.arrayify(keccak256(result));
|
|
|
|
var percent = ((top[0] << 16) | (top[1] << 8) | top[2]) / 0x00ffffff;
|
|
|
|
|
|
|
|
return result.slice(0, lower + parseInt((upper - lower) * percent));
|
|
|
|
}
|
|
|
|
|
|
|
|
function randomHexString(seed, lower, upper) {
|
|
|
|
return convert.hexlify(randomBytes(seed, lower, upper));
|
|
|
|
}
|
|
|
|
|
|
|
|
function randomNumber(seed, lower, upper) {
|
|
|
|
var top = randomBytes(seed, 3);
|
|
|
|
var percent = ((top[0] << 16) | (top[1] << 8) | top[2]) / 0x00ffffff;
|
|
|
|
return lower + parseInt((upper - lower) * percent);
|
|
|
|
}
|
|
|
|
|
2016-07-27 00:58:17 +03:00
|
|
|
|
2017-02-24 22:41:24 +03:00
|
|
|
function equals(a, b) {
|
|
|
|
|
|
|
|
// Array (treat recursively)
|
|
|
|
if (Array.isArray(a)) {
|
|
|
|
if (!Array.isArray(b) || a.length !== b.length) { return false; }
|
|
|
|
for (var i = 0; i < a.length; i++) {
|
|
|
|
if (!equals(a[i], b[i])) { return false; }
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// BigNumber
|
|
|
|
if (a.eq) {
|
|
|
|
if (!b.eq || !a.eq(b)) { return false; }
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Uint8Array
|
|
|
|
if (a.buffer) {
|
|
|
|
if (!b.buffer || a.length !== b.length) { return false; }
|
|
|
|
for (var i = 0; i < a.length; i++) {
|
|
|
|
if (a[i] !== b[i]) { return false; }
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Something else
|
|
|
|
return a === b;
|
|
|
|
}
|
|
|
|
|
2017-10-19 04:28:45 +03:00
|
|
|
function saveTests(tag, data) {
|
|
|
|
var filename = path.resolve(__dirname, 'tests', tag + '.json.gz');
|
|
|
|
|
|
|
|
var data = JSON.stringify(data, undefined, ' ') + '\n';
|
|
|
|
fs.writeFileSync(filename, zlib.gzipSync(data));
|
|
|
|
|
|
|
|
console.log('Save testcase: ' + filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
function loadTests(tag) {
|
|
|
|
var filename = path.resolve(__dirname, 'tests', tag + '.json.gz');
|
|
|
|
return JSON.parse(zlib.gunzipSync(fs.readFileSync(filename)));
|
|
|
|
}
|
|
|
|
|
2018-06-21 03:29:54 +03:00
|
|
|
function loadJson(filename) {
|
|
|
|
var filename = path.resolve(__dirname, 'tests', filename);
|
|
|
|
return JSON.parse(fs.readFileSync(filename).toString());
|
|
|
|
}
|
|
|
|
|
2018-07-16 07:19:50 +03:00
|
|
|
function loadText(filename) {
|
|
|
|
var filename = path.resolve(__dirname, filename);
|
|
|
|
return fs.readFileSync(filename).toString();
|
|
|
|
}
|
|
|
|
|
2018-06-21 03:29:54 +03:00
|
|
|
|
2016-07-27 00:58:17 +03:00
|
|
|
module.exports = {
|
2017-10-19 04:28:45 +03:00
|
|
|
randomBytes: randomBytes,
|
2016-07-27 00:58:17 +03:00
|
|
|
randomHexString: randomHexString,
|
2017-10-19 04:28:45 +03:00
|
|
|
randomNumber:randomNumber,
|
|
|
|
|
|
|
|
bigNumberify: bigNumber.bigNumberify,
|
|
|
|
|
2017-02-24 22:41:24 +03:00
|
|
|
equals: equals,
|
2017-10-19 04:28:45 +03:00
|
|
|
isHexString: convert.isHexString,
|
|
|
|
hexlify: convert.hexlify,
|
|
|
|
arrayify: convert.arrayify,
|
2016-07-27 00:58:17 +03:00
|
|
|
|
2017-10-19 04:28:45 +03:00
|
|
|
loadTests: loadTests,
|
|
|
|
saveTests: saveTests,
|
2018-06-21 03:29:54 +03:00
|
|
|
|
|
|
|
loadJson: loadJson,
|
2018-07-16 07:19:50 +03:00
|
|
|
loadText: loadText,
|
2016-07-27 00:58:17 +03:00
|
|
|
}
|