ethers.js/packages/random/lib/browser.js

48 lines
1.4 KiB
JavaScript
Raw Normal View History

2019-05-15 01:48:48 +03:00
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var bytes_1 = require("@ethersproject/bytes");
2019-08-02 09:10:58 +03:00
var logger_1 = require("@ethersproject/logger");
var _version_1 = require("./_version");
var logger = new logger_1.Logger(_version_1.version);
2019-05-15 01:48:48 +03:00
var shuffle_1 = require("./shuffle");
exports.shuffled = shuffle_1.shuffled;
2020-04-18 12:14:55 +03:00
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;
2019-05-15 01:48:48 +03:00
if (!crypto || !crypto.getRandomValues) {
2019-08-02 09:10:58 +03:00
logger.warn("WARNING: Missing strong random number source");
2019-05-15 01:48:48 +03:00
crypto = {
getRandomValues: function (buffer) {
2019-08-02 09:10:58 +03:00
return logger.throwError("no secure random source avaialble", logger_1.Logger.errors.UNSUPPORTED_OPERATION, {
2019-05-15 01:48:48 +03:00
operation: "crypto.getRandomValues"
});
}
};
}
function randomBytes(length) {
2020-04-18 12:14:55 +03:00
if (length <= 0 || length > 1024 || (length % 1)) {
2019-08-02 09:10:58 +03:00
logger.throwArgumentError("invalid length", "length", length);
2019-05-15 01:48:48 +03:00
}
var result = new Uint8Array(length);
crypto.getRandomValues(result);
return bytes_1.arrayify(result);
}
exports.randomBytes = randomBytes;
;
2020-07-13 15:03:56 +03:00
//# sourceMappingURL=browser.js.map