ethers.js/packages/random/lib/browser.js
2020-07-13 08:03:56 -04:00

48 lines
1.4 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var bytes_1 = require("@ethersproject/bytes");
var logger_1 = require("@ethersproject/logger");
var _version_1 = require("./_version");
var logger = new logger_1.Logger(_version_1.version);
var shuffle_1 = require("./shuffle");
exports.shuffled = shuffle_1.shuffled;
var anyGlobal = null;
try {
anyGlobal = window;
if (anyGlobal == null) {
throw new Error("try next");
}
}
catch (error) {
try {
anyGlobal = global;
if (anyGlobal == null) {
throw new Error("try next");
}
}
catch (error) {
anyGlobal = {};
}
}
var crypto = anyGlobal.crypto || anyGlobal.msCrypto;
if (!crypto || !crypto.getRandomValues) {
logger.warn("WARNING: Missing strong random number source");
crypto = {
getRandomValues: function (buffer) {
return logger.throwError("no secure random source avaialble", logger_1.Logger.errors.UNSUPPORTED_OPERATION, {
operation: "crypto.getRandomValues"
});
}
};
}
function randomBytes(length) {
if (length <= 0 || length > 1024 || (length % 1)) {
logger.throwArgumentError("invalid length", "length", length);
}
var result = new Uint8Array(length);
crypto.getRandomValues(result);
return bytes_1.arrayify(result);
}
exports.randomBytes = randomBytes;
;
//# sourceMappingURL=browser.js.map