2020-07-13 08:03:56 -04:00
|
|
|
/* istanbul ignore file */
|
2019-05-14 18:48:48 -04:00
|
|
|
'use strict';
|
2019-08-25 02:39:20 -04:00
|
|
|
import { ethers } from "ethers";
|
2019-05-14 18:48:48 -04:00
|
|
|
function randomBytes(seed, lower, upper) {
|
|
|
|
if (!upper) {
|
|
|
|
upper = lower;
|
|
|
|
}
|
|
|
|
if (upper === 0 && upper === lower) {
|
|
|
|
return new Uint8Array(0);
|
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
let result = ethers.utils.arrayify(ethers.utils.keccak256(ethers.utils.toUtf8Bytes(seed)));
|
2019-05-14 18:48:48 -04:00
|
|
|
while (result.length < upper) {
|
2019-08-25 02:39:20 -04:00
|
|
|
result = ethers.utils.concat([result, ethers.utils.keccak256(ethers.utils.concat([seed, result]))]);
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
let top = ethers.utils.arrayify(ethers.utils.keccak256(result));
|
|
|
|
let percent = ((top[0] << 16) | (top[1] << 8) | top[2]) / 0x01000000;
|
2019-05-14 18:48:48 -04:00
|
|
|
return result.slice(0, lower + Math.floor((upper - lower) * percent));
|
|
|
|
}
|
|
|
|
function randomHexString(seed, lower, upper) {
|
2019-08-25 02:39:20 -04:00
|
|
|
return ethers.utils.hexlify(randomBytes(seed, lower, upper));
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
|
|
|
function randomNumber(seed, lower, upper) {
|
2019-08-25 02:39:20 -04:00
|
|
|
let top = randomBytes(seed, 3);
|
|
|
|
let percent = ((top[0] << 16) | (top[1] << 8) | top[2]) / 0x01000000;
|
2019-05-14 18:48:48 -04:00
|
|
|
return lower + Math.floor((upper - lower) * percent);
|
|
|
|
}
|
|
|
|
function equals(a, b) {
|
|
|
|
// Array (treat recursively)
|
|
|
|
if (Array.isArray(a)) {
|
|
|
|
if (!Array.isArray(b) || a.length !== b.length) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
for (let i = 0; i < a.length; i++) {
|
2019-05-14 18:48:48 -04:00
|
|
|
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;
|
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
for (let i = 0; i < a.length; i++) {
|
2019-05-14 18:48:48 -04:00
|
|
|
if (a[i] !== b[i]) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Something else
|
|
|
|
return a === b;
|
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
export { randomBytes, randomHexString, randomNumber, equals };
|
2020-07-13 08:03:56 -04:00
|
|
|
//# sourceMappingURL=utils.js.map
|