2019-08-25 09:39:20 +03:00
|
|
|
"use strict";
|
|
|
|
import { arrayify } from "@ethersproject/bytes";
|
|
|
|
import { Logger } from "@ethersproject/logger";
|
|
|
|
import { version } from "./_version";
|
|
|
|
const logger = new Logger(version);
|
2020-04-18 12:14:55 +03:00
|
|
|
let 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 = {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let crypto = anyGlobal.crypto || anyGlobal.msCrypto;
|
2019-08-25 09:39:20 +03:00
|
|
|
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.errors.UNSUPPORTED_OPERATION, {
|
|
|
|
operation: "crypto.getRandomValues"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
export function randomBytes(length) {
|
2020-04-18 12:14:55 +03:00
|
|
|
if (length <= 0 || length > 1024 || (length % 1)) {
|
2019-08-25 09:39:20 +03:00
|
|
|
logger.throwArgumentError("invalid length", "length", length);
|
|
|
|
}
|
2019-11-20 12:57:38 +03:00
|
|
|
const result = new Uint8Array(length);
|
2019-08-25 09:39:20 +03:00
|
|
|
crypto.getRandomValues(result);
|
|
|
|
return arrayify(result);
|
|
|
|
}
|
|
|
|
;
|
2020-11-17 07:07:24 +03:00
|
|
|
//# sourceMappingURL=random.js.map
|