ethers.js/tests/utils.js

58 lines
1.3 KiB
JavaScript
Raw Normal View History

2016-07-27 00:58:17 +03:00
var crypto = require('crypto');
//var utils = require('../lib/utils.js');
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');
}
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;
}
2016-07-27 00:58:17 +03:00
module.exports = {
random: random,
randomBuffer: randomBuffer,
randomHexString: randomHexString,
equals: equals,
2016-07-27 00:58:17 +03:00
//isHexString: utils.isHexString,
//hexOrBuffer: utils.hexOrBuffer,
2016-07-27 00:58:17 +03:00
}